Skip to content

Commit

Permalink
Merge pull request #277 from ianmcorvidae/keycloak-logins
Browse files Browse the repository at this point in the history
Keycloak-based login tracking
  • Loading branch information
ianmcorvidae authored Dec 12, 2024
2 parents 7ca9315 + 21a4c97 commit a409e56
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 34 deletions.
21 changes: 7 additions & 14 deletions src/apps/persistence/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:use [clojure-commons.core :only [remove-nil-values]]
[kameleon.uuids :only [uuidify]]
[korma.core :exclude [update]])
(:require [korma.core :as sql])
(:require [korma.core :as sql]
[apps.util.conversions :refer [long->timestamp]])
(:import [java.sql Timestamp]))

(defn- user-base-query
Expand Down Expand Up @@ -45,25 +46,17 @@

(defn- insert-login-record
"Records when a user logs into the DE."
[user-id ip-address user-agent]
[user-id ip-address session-id login-time]
(insert :logins
(values (remove-nil-values
{:user_id user-id
:ip_address ip-address
:user_agent user-agent}))))
:session_id session-id
:login_time (long->timestamp login-time)}))))

(defn record-login
"Records when a user logs into the DE. Returns the recorded login time."
[username ip-address user-agent]
(-> (insert-login-record (get-user-id username) ip-address user-agent)
[username ip-address session-id login-time]
(-> (insert-login-record (get-user-id username) ip-address session-id login-time)
(:login_time)
(.getTime)))

(defn record-logout
"Records when a user logs out of the DE."
[username ip-address login-time]
(sql/update :logins
(set-fields {:logout_time (sqlfn :now)})
(where {:user_id (get-user-id username)
:ip_address ip-address})
(where {(sqlfn :date_trunc "milliseconds" :login_time) (Timestamp. login-time)})))
9 changes: 3 additions & 6 deletions src/apps/routes/schemas/user.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns apps.routes.schemas.user
(:use [common-swagger-api.schema :only [describe]]
[apps.routes.params :only [SecuredQueryParams]]
[schema.core :only [defschema]])
[schema.core :only [defschema optional-key]])
(:require [common-swagger-api.schema.sessions :as sessions-schema])
(:import [java.util UUID]))

Expand All @@ -18,8 +18,5 @@
(defschema LoginParams
(merge SecuredQueryParams
sessions-schema/IPAddrParam
{:user-agent (describe String "The user agent obtained from the original request.")}))

(defschema LogoutParams
(merge SecuredQueryParams
sessions-schema/LogoutParams))
{(optional-key :session-id) (describe String "The session ID provided by the auth provider.")
(optional-key :login-time) (describe Long "Login time as milliseconds since the epoch, provided by auth provider.")}))
8 changes: 1 addition & 7 deletions src/apps/routes/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,4 @@
:summary "User Login Service"
:description "Terrain calls this service to record when a user logs in
and to fetch user session info."
(ok (users/login current-user params)))

(POST "/logout" []
:query [params LogoutParams]
:summary sessions-schema/LogoutSummary
:description sessions-schema/LogoutDocs
(ok (users/logout current-user params))))
(ok (users/login current-user params))))
9 changes: 2 additions & 7 deletions src/apps/service/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
(remove-nil-vals (up/for-username username)))

(defn login
[{:keys [username] :as current-user} {:keys [ip-address user-agent]}]
{:login_time (up/record-login username ip-address user-agent)
[{:keys [username] :as current-user} {:keys [ip-address login-time session-id]}]
{:login_time (up/record-login username ip-address session-id login-time)
:auth_redirect (oauth/get-redirect-uris current-user)})

(defn logout
[{:keys [username]} {:keys [ip-address login-time]}]
(up/record-logout username ip-address login-time)
nil)

0 comments on commit a409e56

Please sign in to comment.