Skip to content

Commit

Permalink
Merge pull request #280 from ianmcorvidae/logins-endpoint
Browse files Browse the repository at this point in the history
Logins endpoint
  • Loading branch information
ianmcorvidae authored Jan 10, 2025
2 parents 85f4b3d + 74282de commit ce842be
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/apps/persistence/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@
(-> (upsert-login-record (get-user-id username) ip-address session-id login-time)
(:login_time)
(.getTime)))

(defn list-logins
"Lists a number of the most recent logins for a user"
[username query-limit]
(->> (select :logins
(where {:user_id (get-user-id username)})
(limit (or query-limit 5)))
(mapv (fn [login] (select-keys login [:ip_address :login_time])))))
17 changes: 16 additions & 1 deletion 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 optional-key]])
[schema.core :only [defschema optional-key both pred]])
(:require [common-swagger-api.schema.sessions :as sessions-schema])
(:import [java.util UUID]))

Expand All @@ -20,3 +20,18 @@
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.")}))

(defschema ListLoginsParams
(merge SecuredQueryParams
{(optional-key :limit)
(describe (both Long (pred pos? 'positive-integer?))
"Limits the response to X number of results.")}))

; NOTE that the IP Address key uses an underscore here. Other schemas are inconsistent about this.
(defschema ListLoginsResponse
{:logins
[{(optional-key :ip_address)
(describe String "The IP address associated with this login session.")

:login_time
(describe Long "Login time as milliseconds since the epoch.")}]})
9 changes: 8 additions & 1 deletion src/apps/routes/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@
: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))))
(ok (users/login current-user params)))

(GET "/logins" []
:query [params ListLoginsParams]
:return ListLoginsResponse
:summary "Login listing"
:description "Fetch a listing of recent logins"
(ok (users/list-logins current-user params))))
4 changes: 4 additions & 0 deletions src/apps/service/users.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
[{: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 list-logins
[{:keys [username] :as current-user} {:keys [limit] :or {limit 5}}]
(up/list-logins username limit))

0 comments on commit ce842be

Please sign in to comment.