Skip to content

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

tusharpandey13
Copy link
Contributor

@tusharpandey13 tusharpandey13 commented Jun 23, 2025

Fixes: #1884

This change makes the access token methods, getAccessToken and handleAccessToken also update the ID token claims present in session.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.
  • both getAccessToken and handleAccessToken now handle the updated user object.
  • Added finalizeSession method that calls beforeSessionSaved hook if present (for custom filtering of id_token claims) else filters the id_token claims using default filtering rules.
  • Added flow tests for the same, mocking http layer with msw.
  • Added relevant docstrings.

Tests

Unit tests are PASS.
Manual testing was done on a sample app and existing issue was not reproducible.

…tered) in the case of a token update using the refresh token
@tusharpandey13 tusharpandey13 requested a review from a team as a code owner June 23, 2025 10:18
@tusharpandey13 tusharpandey13 changed the title getTokenSet now also returns updated id_token claims information (filtered) in the case of a token update using the refresh token update id_token when a new Access Token is fetched Jun 23, 2025
@codecov-commenter
Copy link

codecov-commenter commented Jun 23, 2025

Codecov Report

Attention: Patch coverage is 92.45283% with 4 lines in your changes missing coverage. Please review.

Project coverage is 83.07%. Comparing base (061a7a0) to head (e8ac4a8).

Files with missing lines Patch % Lines
src/server/auth-client.ts 90.69% 3 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor Author

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

Comment on lines 668 to 684
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);
}

Copy link
Contributor Author

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)!;
Copy link
Contributor Author

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.

Copy link
Contributor Author

@tusharpandey13 tusharpandey13 Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • getTokenSet now returns tokenSet and parsed user object from id_token claims on success.
  • handleAccessToken updated to set session if user is avaialble from getTokenSet
  • A new method, finalizeSession was added that calls beforeSessionSaved hook if supplied else fitlers the ID token claims in session.user using the default filtering rules.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@guabu guabu left a 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.

Comment on lines +1210 to +1227
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;
}
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v4: Bring back the ability to pass refresh: true to getAccessToken
4 participants