Skip to content

Commit a462838

Browse files
committed
implement slack oauth exchange
This exposes a `/slack` 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 fe572b2 commit a462838

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
@@ -232,4 +232,34 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
232232
| Context.Context_error msg ->
233233
log#error "%s" msg;
234234
Lwt.return_unit
235+
236+
let process_slack_oauth (ctx : Context.t) args =
237+
try%lwt
238+
let secrets = Context.get_secrets_exn ctx in
239+
match secrets.slack_access_token with
240+
| Some _ -> Lwt.return "ok"
241+
| None ->
242+
match Slack.validate_state ?oauth_state:secrets.slack_oauth_state ~args with
243+
| Error e -> action_error e
244+
| Ok () ->
245+
match List.Assoc.find args "code" ~equal:String.equal with
246+
| None -> action_error "argument `code` not found in slack authorization request"
247+
| Some code ->
248+
( match%lwt Slack_api.update_access_token_of_context ~ctx ~code with
249+
| Error e -> action_error e
250+
| Ok () -> Lwt.return "ok"
251+
)
252+
with
253+
| Yojson.Json_error msg ->
254+
let e = Printf.sprintf "failed to parse file as valid JSON (%s)\naborting slack oauth exchange" msg in
255+
log#error "%s" e;
256+
Lwt.return e
257+
| Action_error msg ->
258+
let e = Printf.sprintf "%s\naborting slack oauth exchange" msg in
259+
log#error "%s" e;
260+
Lwt.return e
261+
| Context.Context_error msg ->
262+
let e = Printf.sprintf "%s\naborting slack oauth exchange" msg in
263+
log#error "%s" e;
264+
Lwt.return e
235265
end

src/request_handler.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ let setup_http ~ctx ~signature ~port ~ip =
3838
log#info "%s" request.body;
3939
let%lwt () = Action.process_github_notification ctx request.headers request.body in
4040
ret (Lwt.return "ok")
41+
| _, [ "slack"; "oauth" ] ->
42+
log#info "slack oauth authorization request received";
43+
ret @@ Action.process_slack_oauth ctx request.args
4144
| _, _ ->
4245
log#error "unknown path : %s" (Httpev.show_request request);
4346
ret_err `Not_found "not found"

0 commit comments

Comments
 (0)