From 21a4c97a016cf52bb613bafe026c157d75fe9a8a Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Tue, 10 Dec 2024 10:39:19 -0700 Subject: [PATCH] Allow session ID and login time params, and insert them into the database --- src/apps/persistence/users.clj | 13 ++++++++----- src/apps/routes/schemas/user.clj | 6 ++++-- src/apps/service/users.clj | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/apps/persistence/users.clj b/src/apps/persistence/users.clj index 70e9466a..a572f973 100644 --- a/src/apps/persistence/users.clj +++ b/src/apps/persistence/users.clj @@ -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 @@ -45,15 +46,17 @@ (defn- insert-login-record "Records when a user logs into the DE." - [user-id ip-address] + [user-id ip-address session-id login-time] (insert :logins (values (remove-nil-values {:user_id user-id - :ip_address ip-address})))) + :ip_address ip-address + :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] - (-> (insert-login-record (get-user-id username) ip-address) + [username ip-address session-id login-time] + (-> (insert-login-record (get-user-id username) ip-address session-id login-time) (:login_time) (.getTime))) diff --git a/src/apps/routes/schemas/user.clj b/src/apps/routes/schemas/user.clj index dbde61c9..059843a7 100644 --- a/src/apps/routes/schemas/user.clj +++ b/src/apps/routes/schemas/user.clj @@ -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])) @@ -17,4 +17,6 @@ (defschema LoginParams (merge SecuredQueryParams - sessions-schema/IPAddrParam)) + sessions-schema/IPAddrParam + {(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.")})) diff --git a/src/apps/service/users.clj b/src/apps/service/users.clj index 7ce685a2..891042e7 100644 --- a/src/apps/service/users.clj +++ b/src/apps/service/users.clj @@ -12,6 +12,6 @@ (remove-nil-vals (up/for-username username))) (defn login - [{:keys [username] :as current-user} {:keys [ip-address]}] - {:login_time (up/record-login username ip-address) + [{: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)})