This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
/
auth.clj
47 lines (42 loc) · 1.88 KB
/
auth.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns oc.lib.auth
"Uses a magic token to perform requests to the auth service like
getting a valid JWT or the complete user data."
(:require [cheshire.core :as json]
[org.httpkit.client :as http]
[clojure.walk :refer (keywordize-keys)]
[oc.lib.jwt :as jwt]))
(defn- user-data-map [user-map]
(let [initial-map {:user user-map}
has-user-id (contains? user-map :user-id)
with-user-id (merge initial-map
(when has-user-id
{:user-id (:user-id user-map)}))
has-slack-user-id (contains? user-map :slack-user-id)
with-slack-user (merge with-user-id
(when has-slack-user-id
{:slack-user-id (:slack-user-id user-map)
:slack-team-id (:slack-team-id user-map)}))]
with-slack-user))
(defn- magic-token
[user-map passphrase service-name]
(jwt/generate (merge (user-data-map user-map)
{:super-user true
:name service-name
:auth-source :services})
passphrase))
(defn get-options
[token]
{:headers {"Content-Type" "application/vnd.open-company.auth.v1+json"
"Authorization" (str "Bearer " token)}})
(defn user-token [user auth-server-url passphrase service-name]
(let [token-request
@(http/get (str auth-server-url "/users/refresh/")
(get-options (magic-token user passphrase service-name)))]
(when (= 201 (:status token-request))
(:body token-request))))
(defn user-data [user auth-server-url passphrase service-name]
(let [user-request
@(http/get (str auth-server-url "/users/" (:user-id user))
(get-options (magic-token user passphrase service-name)))]
(when (= 200 (:status user-request))
(dissoc (keywordize-keys (json/parse-string (:body user-request))) :links))))