-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from ianmcorvidae/login-tracking
Fetch login info (IP, session ID) from Keycloak for recording in the DE database
- Loading branch information
Showing
4 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(ns terrain.clients.keycloak.admin | ||
(:require [cemerick.url :as curl] | ||
[clj-http.client :as http] | ||
[terrain.util.config :as config])) | ||
|
||
(defn- keycloak-admin-url | ||
"Builds a Keycloak admin API URL with the given path components." | ||
[& components] | ||
(str (apply curl/url (config/keycloak-admin-base-uri) "realms" (config/keycloak-realm) components))) | ||
|
||
(defn- keycloak-admin-token-url | ||
"Like keycloak-admin-url but for the 'master' realm to get a token to use with the API" | ||
[& components] | ||
(str (apply curl/url (config/keycloak-admin-base-uri) "realms" "master" components))) | ||
|
||
(defn get-token | ||
"Obtains authorization token data for the admin service account. You'll probably want the access_token field in the return value." | ||
[] | ||
(:body (http/post (keycloak-admin-token-url "protocol" "openid-connect" "token") | ||
{:form-params {:grant_type "client_credentials" | ||
:client_id (config/keycloak-client-id) | ||
:client_secret (config/keycloak-client-secret)} | ||
:as :json}))) | ||
|
||
; https://www.keycloak.org/docs-api/26.0.5/rest-api/#_get_adminrealmsrealmusers | ||
(defn get-user | ||
"Obtains user information from keycloak | ||
This will be a map including keys at least :username and :id, which should be | ||
what we need to make further requests" | ||
([username] | ||
(get-user username (:access_token (get-token)))) | ||
([username token] | ||
(let [user-data (http/get (keycloak-admin-url "users") | ||
{:query-params {:username username | ||
:exact true} | ||
:headers {:authorization (str "Bearer " token)} | ||
:as :json})] | ||
; the 'exact' query parameter doesn't seem to work on all keycloak versions, so we filter it | ||
(->> user-data | ||
(filter (fn [user] (= (:username user) username))) | ||
first)))) | ||
|
||
; https://www.keycloak.org/docs-api/26.0.5/rest-api/#_get_adminrealmsrealmusersuser_idsessions | ||
(defn get-user-session | ||
"Obtains information about the user's current session from keycloak. | ||
This will be a list of maps, which will include user ID, ip address, session ID, and clients at least." | ||
([user-id] | ||
(get-user-session user-id (:access_token (get-token)))) | ||
([user-id token] | ||
(:body (http/get (keycloak-admin-url "users" user-id "sessions") | ||
{:headers {:authorization (str "Bearer " token)} | ||
:as :json})))) | ||
|
||
(defn get-user-session-by-username | ||
"Same as `get-user-session`, but by username by way of a request to `get-user` first." | ||
([username] | ||
(get-user-session-by-username username (:access_token (get-token)))) | ||
([username token] | ||
(get-user-session (:id (get-user username token)) token))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters