restore connect workspace to slack

This commit is contained in:
Ruben Fiszel
2022-11-01 22:03:28 +01:00
parent 7d3b8011be
commit ceb7d36dee
3 changed files with 28 additions and 51 deletions
+5 -25
View File
@@ -1026,12 +1026,14 @@ paths:
schema:
type: string
/oauth/connect_slack_callback:
/w/{workspace}/oauth/connect_slack_callback:
post:
summary: connect slack callback
operationId: connectSlackCallback
tags:
- oauth
parameters:
- $ref: "#/components/parameters/WorkspaceId"
requestBody:
description: code endpoint
required: true
@@ -1051,9 +1053,9 @@ paths:
"200":
description: slack token
content:
application/json:
text/plain:
schema:
$ref: "#/components/schemas/SlackToken"
type: string
/oauth/connect_callback/{client_name}:
post:
@@ -1185,28 +1187,6 @@ paths:
schema:
type: string
/w/{workspace}/oauth/set_workspace_slack:
post:
summary: set workspace's slack
operationId: setWorkspaceSlack
tags:
- oauth
parameters:
- $ref: "#/components/parameters/WorkspaceId"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/SlackToken"
responses:
"200":
description: workspace slack is set
content:
text/plain:
schema:
type: string
/oauth/list_logins:
get:
summary: list oauth logins
+19 -20
View File
@@ -39,7 +39,7 @@ use crate::{
workspaces::WorkspaceSettings,
BaseUrl,
};
use windmill_common::error::{self, to_anyhow, Error, Result};
use windmill_common::error::{self, to_anyhow, Error};
use windmill_common::oauth2::*;
use windmill_queue::JobPayload;
@@ -53,7 +53,6 @@ pub fn global_service() -> Router {
.route("/connect/:client", get(connect))
.route("/connect_callback/:client", post(connect_callback))
.route("/connect_slack", get(connect_slack))
.route("/connect_slack_callback", post(connect_slack_callback))
.route(
"/slack_command",
post(slack_command).route_layer(axum::middleware::from_extractor::<SlackSig>()),
@@ -66,10 +65,10 @@ pub fn workspaced_service() -> Router {
Router::new()
.route("/disconnect/:id", post(disconnect))
.route("/disconnect_slack", post(disconnect_slack))
.route("/set_workspace_slack", post(set_workspace_slack))
.route("/create_account", post(create_account))
.route("/delete_account/:id", post(delete_account))
.route("/refresh_token/:id", post(refresh_token))
.route("/connect_slack_callback", post(connect_slack_callback))
}
pub struct ClientWithScopes {
@@ -141,6 +140,7 @@ pub async fn build_oauth_clients(base_url: &str) -> anyhow::Result<AllClients> {
oauths.get(&k).unwrap().clone(),
true,
base_url,
None,
);
(
named_client.0,
@@ -166,6 +166,7 @@ pub async fn build_oauth_clients(base_url: &str) -> anyhow::Result<AllClients> {
oauths.get(&k).unwrap().clone(),
false,
base_url,
None,
);
(
named_client.0,
@@ -190,6 +191,7 @@ pub async fn build_oauth_clients(base_url: &str) -> anyhow::Result<AllClients> {
v.clone(),
false,
base_url,
Some(format!("{base_url}/oauth/callback_slack")),
)
.1
});
@@ -203,12 +205,15 @@ pub fn build_basic_client(
client_params: OAuthClient,
login: bool,
base_url: &str,
override_callback: Option<String>,
) -> (String, OClient) {
let auth_url = Url::parse(&config.auth_url).expect("Invalid authorization endpoint URL");
let token_url = Url::parse(&config.token_url).expect("Invalid token endpoint URL");
let redirect_url = if login {
format!("{base_url}/user/login_callback/{name}")
} else if let Some(callback) = override_callback {
callback
} else {
format!("{base_url}/oauth/callback/{name}")
};
@@ -555,11 +560,14 @@ async fn connect_callback(
}
async fn connect_slack_callback(
Path(w_id): Path<String>,
authed: Authed,
cookies: Cookies,
Json(callback): Json<OAuthCallback>,
Extension(user_db): Extension<UserDB>,
Extension(clients): Extension<Arc<AllClients>>,
Extension(http_client): Extension<Client>,
) -> error::JsonResult<SlackTokenResponse> {
) -> error::Result<String> {
let client = clients
.slack
.as_ref()
@@ -568,22 +576,13 @@ async fn connect_slack_callback(
let token =
exchange_code::<SlackTokenResponse>(callback, &cookies, client, &http_client).await?;
Ok(Json(token))
}
async fn set_workspace_slack(
Path(w_id): Path<String>,
Json(token): Json<SlackTokenResponse>,
Extension(user_db): Extension<UserDB>,
authed: Authed,
) -> Result<String> {
let mut tx = user_db.begin(&authed).await?;
sqlx::query!(
"INSERT INTO workspace_settings
(workspace_id, slack_team_id, slack_name)
VALUES ($1, $2, $3) ON CONFLICT (workspace_id) DO UPDATE SET slack_team_id = $2, \
slack_name = $3",
(workspace_id, slack_team_id, slack_name)
VALUES ($1, $2, $3) ON CONFLICT (workspace_id) DO UPDATE SET slack_team_id = $2, \
slack_name = $3",
&w_id,
token.team_id,
token.team_name
@@ -592,13 +591,13 @@ async fn set_workspace_slack(
.await?;
sqlx::query!(
"INSERT INTO group_
(workspace_id, name, summary)
VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
(workspace_id, name, summary)
VALUES ($1, $2, $3) ON CONFLICT DO NOTHING",
&w_id,
"slack",
"The group that runs the script triggered by the slack /windmill command.
Share scripts to this group to make them executable from slack and add
members to this group to let them manage the slack related owner space."
Share scripts to this group to make them executable from slack and add
members to this group to let them manage the slack related owner space."
)
.execute(&mut tx)
.await?;
@@ -18,12 +18,10 @@
if (error) {
sendUserToast(`Error trying to add slack connection: ${error}`, true)
} else if (code && state) {
const res = await OauthService.connectSlackCallback({ requestBody: { code, state } })
if (!res.bot.bot_access_token) {
throw Error('access token missing')
}
$oauthStore = { access_token: res.bot.bot_access_token }
await OauthService.setWorkspaceSlack({ workspace: $workspaceStore!, requestBody: res })
await OauthService.connectSlackCallback({
workspace: $workspaceStore!,
requestBody: { code, state }
})
sendUserToast('Slack workspace connected to your Windmill workspace')
} else {
sendUserToast('Missing code or state as query params', true)