-
Notifications
You must be signed in to change notification settings - Fork 424
update id_token when a new Access Token is fetched #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…tered) in the case of a token update using the refresh token
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2189 +/- ##
==========================================
+ Coverage 82.91% 83.07% +0.16%
==========================================
Files 21 21
Lines 2095 2127 +32
Branches 372 373 +1
==========================================
+ Hits 1737 1767 +30
- Misses 351 353 +2
Partials 7 7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/server/auth-client.test.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authClient.getTokenSet now returns {tokenset, user}
hence updated test
src/server/auth-client.ts
Outdated
let finalSession = session; | ||
if (user) { | ||
finalSession.user = user!; | ||
} | ||
if (this.beforeSessionSaved) { | ||
const updatedSession = await this.beforeSessionSaved( | ||
finalSession, | ||
updatedTokenSet.idToken ?? null | ||
); | ||
finalSession = { | ||
...updatedSession, | ||
internal: finalSession.internal | ||
}; | ||
} else { | ||
finalSession.user = filterDefaultIdTokenClaims(finalSession.user); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- If idToken has updated (RT flow), update the new claims in the session.
- Call
beforeSessionSaved
hook if available, else filter claims using default rules.
@@ -771,6 +794,7 @@ export class AuthClient { | |||
]; | |||
} | |||
|
|||
const idTokenClaims = oauth.getValidatedIdTokenClaims(oauthRes)!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since oauthRes: oauth.TokenEndpointResponse
is available here, parse the id_token claims here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTokenSet
now returnstokenSet
and parseduser
object from id_token claims on success.handleAccessToken
updated to set session ifuser
is avaialble fromgetTokenSet
- A new method,
finalizeSession
was added that callsbeforeSessionSaved
hook if supplied else fitlers the ID token claims insession.user
using the default filtering rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAccessToken
changed similar to handleAccessToken
above.
src/server/get-access-token.test.ts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Generate id token for intial and updated scenarios
- Check that updated id_token claim information is being saved to session in
session.user
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The overall approach is in the right direction — left a few small comments mostly around organization.
…uth0/nextjs-auth0 into feature/handle-idtoken-refresh
async finalizeSession( | ||
session: SessionData, | ||
idToken?: string | ||
): Promise<SessionData> { | ||
if (this.beforeSessionSaved) { | ||
const updatedSession = await this.beforeSessionSaved( | ||
session, | ||
idToken ?? null | ||
); | ||
session = { | ||
...updatedSession, | ||
internal: session.internal | ||
}; | ||
} else { | ||
session.user = filterDefaultIdTokenClaims(session.user); | ||
} | ||
return session; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
finalizeSession
was added that calls beforeSessionSaved
hook if supplied else fitlers the ID token claims in session.user
using the default filtering rules.
This logic was duplicated at 3 places so moved this to a seperate method. Also, moving to a method was necessary to remove this logic from client.ts
…uth0/nextjs-auth0 into feature/handle-idtoken-refresh
Fixes: #1884
This change makes the access token methods,
getAccessToken
andhandleAccessToken
also update the ID token claims present insession.user
when a new Access Token is retrieved.beofreSessionSaved
hook is called before session data is updated.Changes
getTokenSet
now also returns updated id_token claims information in the case of a token update using the refresh token.getAccessToken
andhandleAccessToken
now handle the updateduser
object.finalizeSession
method that callsbeforeSessionSaved
hook if present (for custom filtering of id_token claims) else filters the id_token claims using default filtering rules.Tests
Unit tests are PASS.
Manual testing was done on a sample app and existing issue was not reproducible.