-
Notifications
You must be signed in to change notification settings - Fork 425
Force Refresh Access Token Enhancements #2164
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
Draft
tusharpandey13
wants to merge
2
commits into
main
Choose a base branch
from
feature/force-refresh-at-enhancements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−19
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,13 +56,33 @@ test("getAccessToken()", async ({ page }) => { | |
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); | ||
await page.getByText("Continue", { exact: true }).click(); | ||
|
||
// fetch a token | ||
// request a token | ||
const requestPromise = page.waitForRequest("/auth/access-token"); | ||
await page.getByText("Get token").click(); | ||
await page.getByText("Get access token").click(); | ||
const request = await requestPromise; | ||
const tokenRequest = await (await request?.response())?.json(); | ||
expect(tokenRequest).toHaveProperty("token"); | ||
expect(tokenRequest).toHaveProperty("expires_at"); | ||
}); | ||
|
||
test("getAccessToken() with force refresh", async ({ page }) => { | ||
await page.goto("/auth/login?returnTo=/app-router/client"); | ||
|
||
// fill out Auth0 form | ||
await page.fill('input[id="username"]', "[email protected]"); | ||
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); | ||
await page.getByText("Continue", { exact: true }).click(); | ||
|
||
// force refresh a token | ||
const requestPromise = page.waitForRequest("/auth/access-token?refresh=true"); | ||
await page.getByText("Force refresh token").click(); | ||
const request = await requestPromise; | ||
const tokenRequest = await (await request.response())?.json(); | ||
expect(tokenRequest).toHaveProperty("token"); | ||
expect(tokenRequest).toHaveProperty("expires_at"); | ||
|
||
// Verify that the request URL contains the refresh parameter | ||
expect(request.url()).toContain("refresh=true"); | ||
}); | ||
|
||
test("protected server route", async ({ page, context }) => { | ||
|
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 |
---|---|---|
|
@@ -56,13 +56,33 @@ test("getAccessToken()", async ({ page }) => { | |
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); | ||
await page.getByText("Continue", { exact: true }).click(); | ||
|
||
// fetch a token | ||
// request a token | ||
const requestPromise = page.waitForRequest("/auth/access-token"); | ||
await page.getByText("Get token").click(); | ||
await page.getByText("Get access token").click(); | ||
const request = await requestPromise; | ||
const tokenRequest = await (await request?.response())?.json(); | ||
expect(tokenRequest).toHaveProperty("token"); | ||
expect(tokenRequest).toHaveProperty("expires_at"); | ||
}); | ||
|
||
test("getAccessToken() with force refresh", async ({ page }) => { | ||
await page.goto("/auth/login?returnTo=/pages-router/client"); | ||
|
||
// fill out Auth0 form | ||
await page.fill('input[id="username"]', "[email protected]"); | ||
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!); | ||
await page.getByText("Continue", { exact: true }).click(); | ||
|
||
// force refresh a token | ||
const requestPromise = page.waitForRequest("/auth/access-token?refresh=true"); | ||
await page.getByText("Force refresh token").click(); | ||
const request = await requestPromise; | ||
const tokenRequest = await (await request.response())?.json(); | ||
expect(tokenRequest).toHaveProperty("token"); | ||
expect(tokenRequest).toHaveProperty("expires_at"); | ||
|
||
// Verify that the request URL contains the refresh parameter | ||
expect(request.url()).toContain("refresh=true"); | ||
}); | ||
|
||
test("protected API route", async ({ page, request, context }) => { | ||
|
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
export { useUser } from "./hooks/use-user"; | ||
export { getAccessToken } from "./helpers/get-access-token"; | ||
export { | ||
getAccessToken, | ||
type GetAccessTokenOptions | ||
} from "./helpers/get-access-token"; | ||
export { Auth0Provider } from "./providers/auth0-provider"; |
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 |
---|---|---|
|
@@ -3624,6 +3624,88 @@ ca/T0LLtgmbMmxSv/MmzIg== | |
// validate that the session cookie has not been set | ||
expect(response.cookies.get("__session")).toBeUndefined(); | ||
}); | ||
|
||
it("should force refresh the access token when refresh=true query parameter is provided", async () => { | ||
const currentAccessToken = DEFAULT.accessToken; | ||
const newAccessToken = "at_refreshed_456"; | ||
|
||
const secret = await generateSecret(32); | ||
const transactionStore = new TransactionStore({ | ||
secret | ||
}); | ||
const sessionStore = new StatelessSessionStore({ | ||
secret | ||
}); | ||
const authClient = new AuthClient({ | ||
transactionStore, | ||
sessionStore, | ||
|
||
domain: DEFAULT.domain, | ||
clientId: DEFAULT.clientId, | ||
clientSecret: DEFAULT.clientSecret, | ||
|
||
secret, | ||
appBaseUrl: DEFAULT.appBaseUrl, | ||
|
||
fetch: getMockAuthorizationServer({ | ||
tokenEndpointResponse: { | ||
token_type: "Bearer", | ||
access_token: newAccessToken, | ||
scope: "openid profile email", | ||
expires_in: 86400 // expires in 1 day | ||
} as oauth.TokenEndpointResponse | ||
}) | ||
}); | ||
|
||
// we use a non-expired token to ensure force refresh works even with valid tokens | ||
const expiresAt = Math.floor(Date.now() / 1000) + 10 * 24 * 60 * 60; // expires in 10 days | ||
const session: SessionData = { | ||
user: { | ||
sub: DEFAULT.sub, | ||
name: "John Doe", | ||
email: "[email protected]", | ||
picture: "https://example.com/john.jpg" | ||
}, | ||
tokenSet: { | ||
accessToken: currentAccessToken, | ||
scope: "openid profile email", | ||
refreshToken: DEFAULT.refreshToken, | ||
expiresAt | ||
}, | ||
internal: { | ||
sid: DEFAULT.sid, | ||
createdAt: Math.floor(Date.now() / 1000) | ||
} | ||
}; | ||
const maxAge = 60 * 60; // 1 hour | ||
const expiration = Math.floor(Date.now() / 1000 + maxAge); | ||
const sessionCookie = await encrypt(session, secret, expiration); | ||
const headers = new Headers(); | ||
headers.append("cookie", `__session=${sessionCookie}`); | ||
const request = new NextRequest( | ||
new URL("/auth/access-token?refresh=true", DEFAULT.appBaseUrl), | ||
{ | ||
method: "GET", | ||
headers | ||
} | ||
); | ||
|
||
const response = await authClient.handleAccessToken(request); | ||
expect(response.status).toEqual(200); | ||
expect(await response.json()).toEqual({ | ||
token: newAccessToken, | ||
scope: "openid profile email", | ||
expires_at: expect.any(Number) | ||
}); | ||
|
||
// validate that the session cookie has been updated with the new token | ||
const updatedSessionCookie = response.cookies.get("__session"); | ||
const { payload: updatedSession } = await decrypt<SessionData>( | ||
updatedSessionCookie!.value, | ||
secret | ||
); | ||
expect(updatedSession.tokenSet.accessToken).toEqual(newAccessToken); | ||
}); | ||
}); | ||
|
||
describe("handleBackChannelLogout", async () => { | ||
|
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.
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.
Why do we need these overloads?
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.
To adhere to a no-contract change (adding new overloads)
Uh oh!
There was an error while loading. Please reload this page.
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.
We do not need those. We can just keep the one we have where options is marked optional, there is no point to the overloads here.