-
Notifications
You must be signed in to change notification settings - Fork 60
Implement token refresh and session management enhancements #467
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
Merged
DonOmalVindula
merged 12 commits into
asgardeo:main
from
kavindadimuthu:fix-refresh-issue
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d6b5cba
Implement token refresh and session management enhancements
kavindadimuthu 9f4f036
Fix lint errors
kavindadimuthu e26fb51
Refactor cookie handling to use new RequestCookies type across sessio…
kavindadimuthu ce75e4a
Enhance session management by implementing verifySessionTokenForRefre…
kavindadimuthu 8533a69
Fix middleware exports in package.json for edge-light and types
kavindadimuthu 399cc5d
Update refresh token handling to use RefreshResult type and improve s…
kavindadimuthu 4aed363
Add session cookie expiry time configuration and handle refresh token…
kavindadimuthu 8e38b45
Refactor configuration retrieval to remove unnecessary type casting i…
kavindadimuthu 0f75633
Fix lint errors
kavindadimuthu 0fbf449
Fix expiresIn value handling in signInAction to ensure valid numeric …
kavindadimuthu 93e9748
Validate expiresIn value in refreshToken to ensure proper numeric con…
kavindadimuthu 455674e
Fix lint errors
kavindadimuthu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Number of seconds before access token expiry at which the SDK proactively | ||
| * refreshes the token. A 25-second buffer prevents races where the token is | ||
| * valid when a request starts but expires mid-flight. | ||
| */ | ||
| export const REFRESH_BUFFER_SECONDS: number = 25; | ||
|
|
||
| /** | ||
| * Default session cookie lifetime in seconds (24 hours). | ||
| * | ||
| * Used when no explicit session cookie expiry is configured. The session cookie | ||
| * lifetime can be overridden in two ways (evaluated in this order): | ||
| * | ||
| * 1. `sessionCookieExpiryTime` in `AsgardeoNodeConfig` — set programmatically | ||
| * when initialising the SDK. | ||
| * 2. `ASGARDEO_SESSION_COOKIE_EXPIRY_TIME` environment variable — set in `.env` | ||
| * (e.g. `ASGARDEO_SESSION_COOKIE_EXPIRY_TIME=86400`). | ||
| * 3. This constant — applied when neither of the above is present. | ||
| * | ||
| * Two independent expiry bounds apply to the session and they are generally | ||
| * NOT the same value: | ||
| * | ||
| * - JWT `exp` claim — set by `SessionManager.createSessionToken(...)` from | ||
| * the `accessTokenTtlSeconds` argument (i.e. the access token's `expires_in` | ||
| * returned by the auth server, typically ~1 hour). This controls when | ||
| * `verifySessionToken` rejects the token and is the trigger for a refresh. | ||
| * - Browser cookie `maxAge` — set by the caller (sign-in / refresh / org-switch | ||
| * actions) from `SessionManager.resolveSessionCookieExpiry(...)`, which returns | ||
| * this constant by default (24 hours). This controls how long the browser | ||
| * holds the cookie before discarding it. | ||
| */ | ||
| export const DEFAULT_SESSION_COOKIE_EXPIRY_TIME: number = 86400; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Edge Runtime entry point — safe for use in Next.js middleware.ts. | ||
| * | ||
| * This file must only import modules whose full transitive dependency graph | ||
| * contains zero Node.js-only APIs (process.versions, fs, crypto, etc.). | ||
| * Permitted dependencies: jose, fetch, next/server, and local utilities | ||
| * that themselves satisfy the same constraint. | ||
| * | ||
| * Do NOT import from: | ||
| * - AsgardeoNextClient (depends on @asgardeo/node → @asgardeo/javascript) | ||
| * - server/AsgardeoProvider (depends on @asgardeo/node) | ||
| * - server/actions/* (depend on @asgardeo/node) | ||
| * - client/* (depend on @asgardeo/javascript via @asgardeo/react) | ||
| */ | ||
|
|
||
| export {default as asgardeoMiddleware} from './server/middleware/asgardeoMiddleware'; | ||
| export * from './server/middleware/asgardeoMiddleware'; | ||
|
|
||
| export {default as createRouteMatcher} from './server/middleware/createRouteMatcher'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| 'use server'; | ||
|
|
||
| import {cookies} from 'next/headers'; | ||
| import logger from '../../utils/logger'; | ||
| import SessionManager from '../../utils/SessionManager'; | ||
|
|
||
| type RequestCookies = Awaited<ReturnType<typeof cookies>>; | ||
|
|
||
| /** | ||
| * Deletes all Asgardeo session cookies from the browser without contacting the | ||
| * identity server. | ||
| * | ||
| * Use this for error-recovery scenarios where the local session must be wiped | ||
| * immediately: refresh token failures, corrupt sessions, or forced local sign-out | ||
| * when the identity server is unreachable. | ||
| * | ||
| * For a complete sign-out that also revokes the server-side session and obtains the | ||
| * after-sign-out redirect URL, use `signOutAction` instead. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { clearSession } from '@asgardeo/nextjs/server'; | ||
| * | ||
| * // Inside a Server Action or Route Handler: | ||
| * await clearSession(); | ||
| * redirect('/sign-in'); | ||
| * ``` | ||
| */ | ||
| const clearSession = async (): Promise<void> => { | ||
| const cookieStore: RequestCookies = await cookies(); | ||
| cookieStore.delete(SessionManager.getSessionCookieName()); | ||
| cookieStore.delete(SessionManager.getTempSessionCookieName()); | ||
| logger.debug('[clearSession] Session cookies cleared.'); | ||
| }; | ||
|
|
||
| export default clearSession; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.