Skip to content

Commit

Permalink
auth: Improve JWT claims format
Browse files Browse the repository at this point in the history
  • Loading branch information
goshatch committed Sep 6, 2024
1 parent 6fdba16 commit 8c81882
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions dev/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ POST http://localhost:3000/api/auth/login
Content-Type: application/json

{
"email": "test9@example.com",
"password": "password9"
"email": "test2@example.com",
"password": "password2"
}

# Failing login
Expand All @@ -23,5 +23,5 @@ Content-Type: application/json

# Get account info
GET http://localhost:3000/api/account
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyLWlkIjoiY2M1YTVkMTYtYjgzYS00OTZkLTk5M2EtOTM2ZjQxOGUxZDNlIiwiZXhwIjoxNzI1NjE4NzIwfQ.NpBrduSxGICyTryLbuDQyOBK9VR4Wg-V23F_kRKrPMI
Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjMwMDAvYXBpIiwic3ViIjoiY2M1YTVkMTYtYjgzYS00OTZkLTk5M2EtOTM2ZjQxOGUxZDNlIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDozMDAwIiwiaWF0IjoxNzI1NjMzODgzLCJleHAiOjE3MjU2Mzc0ODN9.hDjmDNvy2MNwGP6Th0Jl8THSq9bbg6APv4WndbvOBUk
Content-Type: application/json
2 changes: 1 addition & 1 deletion src/apossiblespace/parts/account.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"Retrieve own account info"
[request]
(mulog/log ::get-account :request request)
(let [user-id (get-in request [:identity :user-id])
(let [user-id (get-in request [:identity :sub])
user-record (fetch-user user-id)]
(if user-record
(-> (response/response user-record)
Expand Down
5 changes: 4 additions & 1 deletion src/apossiblespace/parts/auth.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
[user-id]
(let [now (Instant/now)
exp (.plusSeconds now 3600)
claims {:user-id user-id
claims {:iss "http://localhost:3000/api" ;; TODO: Set this from configuration?
:sub user-id
:aud "http://localhost:3000"
:iat (.getEpochSecond now)
:exp (.getEpochSecond exp)}]
(jwt/sign claims secret {:alg :hs256})))

Expand Down
2 changes: 1 addition & 1 deletion test/apossiblespace/account_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(deftest test-get-account
(testing "returns currently signed in user's information"
(let [user (register-test-user)
mock-request {:identity {:user-id (:id user)}}
mock-request {:identity {:sub (:id user)}}
response (account/get-account mock-request)]
(is (= 200 (:status response)))
(is (= {:email (:email user)
Expand Down
2 changes: 1 addition & 1 deletion test/apossiblespace/auth_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
secret auth/secret
decoded (jwt/unsign token secret)
now-seconds (.getEpochSecond (Instant/now))]
(is (= user-id (:user-id decoded)))
(is (= user-id (:sub decoded)))
(is (> (:exp decoded) now-seconds))
(is (< (:exp decoded) (+ now-seconds 3601))))))

Expand Down

0 comments on commit 8c81882

Please sign in to comment.