Skip to content

Commit ce842be

Browse files
authored
Merge pull request #280 from ianmcorvidae/logins-endpoint
Logins endpoint
2 parents 85f4b3d + 74282de commit ce842be

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/apps/persistence/users.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@
7676
(-> (upsert-login-record (get-user-id username) ip-address session-id login-time)
7777
(:login_time)
7878
(.getTime)))
79+
80+
(defn list-logins
81+
"Lists a number of the most recent logins for a user"
82+
[username query-limit]
83+
(->> (select :logins
84+
(where {:user_id (get-user-id username)})
85+
(limit (or query-limit 5)))
86+
(mapv (fn [login] (select-keys login [:ip_address :login_time])))))

src/apps/routes/schemas/user.clj

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns apps.routes.schemas.user
22
(:use [common-swagger-api.schema :only [describe]]
33
[apps.routes.params :only [SecuredQueryParams]]
4-
[schema.core :only [defschema optional-key]])
4+
[schema.core :only [defschema optional-key both pred]])
55
(:require [common-swagger-api.schema.sessions :as sessions-schema])
66
(:import [java.util UUID]))
77

@@ -20,3 +20,18 @@
2020
sessions-schema/IPAddrParam
2121
{(optional-key :session-id) (describe String "The session ID provided by the auth provider.")
2222
(optional-key :login-time) (describe Long "Login time as milliseconds since the epoch, provided by auth provider.")}))
23+
24+
(defschema ListLoginsParams
25+
(merge SecuredQueryParams
26+
{(optional-key :limit)
27+
(describe (both Long (pred pos? 'positive-integer?))
28+
"Limits the response to X number of results.")}))
29+
30+
; NOTE that the IP Address key uses an underscore here. Other schemas are inconsistent about this.
31+
(defschema ListLoginsResponse
32+
{:logins
33+
[{(optional-key :ip_address)
34+
(describe String "The IP address associated with this login session.")
35+
36+
:login_time
37+
(describe Long "Login time as milliseconds since the epoch.")}]})

src/apps/routes/users.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@
2929
:summary "User Login Service"
3030
:description "Terrain calls this service to record when a user logs in
3131
and to fetch user session info."
32-
(ok (users/login current-user params))))
32+
(ok (users/login current-user params)))
33+
34+
(GET "/logins" []
35+
:query [params ListLoginsParams]
36+
:return ListLoginsResponse
37+
:summary "Login listing"
38+
:description "Fetch a listing of recent logins"
39+
(ok (users/list-logins current-user params))))

src/apps/service/users.clj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@
1515
[{:keys [username] :as current-user} {:keys [ip-address login-time session-id]}]
1616
{:login_time (up/record-login username ip-address session-id login-time)
1717
:auth_redirect (oauth/get-redirect-uris current-user)})
18+
19+
(defn list-logins
20+
[{:keys [username] :as current-user} {:keys [limit] :or {limit 5}}]
21+
(up/list-logins username limit))

0 commit comments

Comments
 (0)