@@ -80,4 +80,36 @@ module Slack : Api.Slack = struct
80
80
)
81
81
| Error e -> Lwt. return @@ build_query_error url e
82
82
)
83
+
84
+ let access_token_of_code ~(ctx : Context.t ) ~code =
85
+ let secrets = Context. get_secrets_exn ctx in
86
+ let body = `Form [ " code" , code ] in
87
+ match secrets.slack_client_id with
88
+ | None -> Lwt. return @@ Error " slack_client_id is undefined"
89
+ | Some client_id ->
90
+ match secrets.slack_client_secret with
91
+ | None -> Lwt. return @@ Error " slack_client_secret is undefined"
92
+ | Some client_secret ->
93
+ let auth_header = Base64. encode_string @@ sprintf " %s:%s" client_id client_secret in
94
+ let headers = [ Printf. sprintf " Authorization: Basic %s" auth_header ] in
95
+ ( match % lwt Common. http_request ~body ~headers `POST " https://slack.com/api/oauth.v2.access" with
96
+ | Error e -> Lwt. return @@ Error e
97
+ | Ok data ->
98
+ let response = Slack_j. oauth_access_res_of_string data in
99
+ ( match response.access_token, response.error with
100
+ | Some access_token , _ -> Lwt. return @@ Ok access_token
101
+ | None , Some e -> Lwt. return @@ Error e
102
+ | None , None -> Lwt. return @@ Error " an unknown error occurred while getting access token"
103
+ )
104
+ )
105
+
106
+ let update_access_token_of_context ~ctx ~code =
107
+ let secrets = Context. get_secrets_exn ctx in
108
+ match % lwt access_token_of_code ~ctx ~code with
109
+ | Error e -> Lwt. return @@ Error e
110
+ | Ok access_token ->
111
+ let secrets = { secrets with slack_access_token = Some access_token } in
112
+ ctx.secrets < - Some secrets;
113
+ let data = Config_j. string_of_secrets secrets |> Yojson.Basic. from_string |> Yojson.Basic. pretty_to_string in
114
+ Lwt. return @@ write_to_local_file ~data ctx.secrets_filepath
83
115
end
0 commit comments