Skip to content

Commit b9bd9d4

Browse files
committed
implement slack oauth exchange
This exposes a `/slack/oauth` route that Slack can query to trigger an OAuth exchange, culminating in the generation of an access token authorizing the bot to post messages to a workspace. When just working with one bot app per workspace, the token can be generated from the app dashboard and copy-pasted into the secrets file. But if the app is distributed, and a user who isn’t a bot server admin wants to install the bot into their workspace, the server needs to handle OAuth.
1 parent a2cbaa7 commit b9bd9d4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/action.ml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,34 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
280280
| Ok () ->
281281
match notification.event with
282282
| Link_shared event -> process_link_shared_event ctx event
283+
284+
let process_slack_oauth (ctx : Context.t) args =
285+
try%lwt
286+
let secrets = Context.get_secrets_exn ctx in
287+
match secrets.slack_access_token with
288+
| Some _ -> Lwt.return "ok"
289+
| None ->
290+
match Slack.validate_state ?oauth_state:secrets.slack_oauth_state ~args with
291+
| Error e -> action_error e
292+
| Ok () ->
293+
match List.Assoc.find args "code" ~equal:String.equal with
294+
| None -> action_error "argument `code` not found in slack authorization request"
295+
| Some code ->
296+
( match%lwt Slack_api.update_access_token_of_context ~ctx ~code with
297+
| Error e -> action_error e
298+
| Ok () -> Lwt.return "ok"
299+
)
300+
with
301+
| Yojson.Json_error msg ->
302+
let e = Printf.sprintf "failed to parse file as valid JSON (%s)\naborting slack oauth exchange" msg in
303+
log#error "%s" e;
304+
Lwt.return e
305+
| Action_error msg ->
306+
let e = Printf.sprintf "%s\naborting slack oauth exchange" msg in
307+
log#error "%s" e;
308+
Lwt.return e
309+
| Context.Context_error msg ->
310+
let e = Printf.sprintf "%s\naborting slack oauth exchange" msg in
311+
log#error "%s" e;
312+
Lwt.return e
283313
end

src/request_handler.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ let setup_http ~ctx ~signature ~port ~ip =
4141
| _, [ "slack"; "events" ] ->
4242
log#info "%s" request.body;
4343
ret @@ Action.process_slack_event ctx request.headers request.body
44+
| _, [ "slack"; "oauth" ] ->
45+
log#info "slack oauth authorization request received";
46+
ret @@ Action.process_slack_oauth ctx request.args
4447
| _, _ ->
4548
log#error "unknown path : %s" (Httpev.show_request request);
4649
ret_err `Not_found "not found"

0 commit comments

Comments
 (0)