-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycloak.clj
171 lines (146 loc) · 6.91 KB
/
keycloak.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(ns andrewslai.clj.http-api.auth.keycloak
(:require [andrewslai.clj.http-api.auth.jwt :as jwt]
[clj-http.client :as http]
[taoensso.timbre :as log])
(:import org.keycloak.adapters.KeycloakDeploymentBuilder
org.keycloak.adapters.rotation.AdapterTokenVerifier
(org.keycloak.admin.client Keycloak)
org.keycloak.jose.jws.JWSInput
org.keycloak.representations.adapters.config.AdapterConfig))
(defn- keycloak-adapter
[{:keys [realm ssl-required auth-server-url
client-id client-secret confidential-port]}]
(let [adapter-config (new AdapterConfig)]
(.setRealm adapter-config realm)
(.setSslRequired adapter-config ssl-required)
(.setAuthServerUrl adapter-config auth-server-url)
(.setResource adapter-config client-id)
(.setCredentials adapter-config {"secret" client-secret})
(.setConfidentialPort adapter-config confidential-port)
adapter-config))
(defn- make-adapter
[config]
(->> config
(keycloak-adapter)
(KeycloakDeploymentBuilder/build)))
(defn- validate-token
[keycloak token]
(AdapterTokenVerifier/verifyToken token keycloak))
(defn make-keycloak-authenticator
[config]
(let [keycloak (make-adapter config)]
(fn authfn
[{:keys [request-id] :as request} token]
(try
(log/infof "Validating jwt token:\n %s" (-> {:header (jwt/header token)
:body (jwt/body token)
:request-id request-id}
clojure.pprint/pprint
with-out-str))
(when (validate-token keycloak token)
(jwt/body token))
(catch Exception e
(log/error "Authentication exception" (pr-str e)))))))
;; Set up a Keycloak serice user
(comment
;; Setting up a service user in Keycloak.
;;
;; First, create a new client. This will be the Java client that will be able
;; to talk to Keycloak.
;;
;; After creating the client, download the client credentials/secret
;; information. This will be used to authenticate. Use the
;; `Keycloak/getInstance` builder to create a new Keycloak client.
;; Setting up roles in Keycloak
;;
;; Then, on the `Role Mapping` screen, assign roles to that user. That user
;; will need roles from the `relam-management` client to be able to view and
;; manipulate users.
(def kc-service-user
(Keycloak/getInstance "http://localhost:8080"
"test"
"local-svc-user" ;; username
"local-svc-user" ;; pw
"test-login-java"
"DES2AG0AjGggQKe1L25VPduVVHF5oaP0"))
(defn get-token
[kc-instance]
(:token (bean (.getAccessToken (.tokenManager kc-instance)))))
(defn get-users
[kc-instance]
(:body (http/request {:method :get
:url "http://localhost:8080/admin/realms/test/users"
:headers {"Authorization" (format "Bearer %s" (get-token kc-instance))}
:as :json})))
(defn create-user!
[kc-instance user]
(:body (http/request {:method :post
:url "http://localhost:8080/admin/realms/test/users"
:headers {"Authorization" (format "Bearer %s" (get-token kc-instance))}
:body {:email "my"}
:as :json})))
(def x
(get-users kc-service-user))
x;; => [{:requiredActions [],
;; :email "something@a.com",
;; :username "local-svc-user",
;; :disableableCredentialTypes [],
;; :firstName "",
;; :emailVerified false,
;; :id "aed3e21b-484d-40a7-8459-2e6fd6e60653",
;; :lastName "",
;; :notBefore 0,
;; :totp false,
;; :access {:manageGroupMembership true,
;; :view true,
;; :mapRoles true,
;; :impersonate false,
;; :manage true},
;; :enabled true,
;; :createdTimestamp 1677806861965} ]
(defn verified-user?
[user]
(:emailVerified user))
(defn email
[user]
(:email user))
(defn id
[user]
(:id user))
(defn username
[user]
(:username user))
(defn first-name
[user]
(:firstName user))
(defn last-name
[user]
(:lastName user))
(map (juxt id email verified-user?) x)
)
(comment
(def token
(str "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia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35g3RcTYIfG6LOdbuF1sYC_sO8F-AECHk-iK0b3NUTRgR8RlnL-p6hvyMWebqxeE63RprGCm6k1Mgg8hh3zXL6HjlyPVnLguxwSIUPb2QTVoPlExPCFA84lrSSwmGLDK7HH7eQKaIC8huu68qplH8yX1-6aKsbavgaL8IpNT5Y74PYPjnaqZkTmVBB-c11ukyzzV7lLwrTHzUcIPzUagWcghjRtk-KYpTB7yvGLJqogvsDCmJaSByW1-vDSLOpconildvmozUfWpfHPfp8BnM6mcE15gb2P3Dy3peB1ahfoJYpiVK10klSVzNyGb_j1kA"))
((make-keycloak-authenticator {:realm "test"
:ssl-required "external"
:auth-server-url "http://172.17.0.1:8080"
:client-id "test-login-java"
:client-secret "18c28e7a-3eb6-4726-b8c7-9c5d02f6bc88"
:confidential-port 0})
{}
token)
(.getRoles (get (.getResourceAccess verified-token) "account"))
(.getRealmAccess verified-token)
(.isActive verified-token)
(.isExpired verified-token)
(.isPublicClient adapter-config) ;; => false
(.getCredentials adapter-config) ;; => {:secret "18c28e7a-3eb6-4726-b8c7-9c5d02f6bc88"}
(.getRealm adapter-config) ;; => "test"
(.getSslRequired adapter-config) ;; => "external"
(.getAuthServerUrl adapter-config) ;; => "http://172.17.0.1:8080/auth/"
(.getResource adapter-config) ;; => "test-login-java"
(.getConfidentialPort adapter-config) ;; => 0
(type adapter-config)
)