-
Notifications
You must be signed in to change notification settings - Fork 402
cache token_type to enable DPoP #1367
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
1
commit into
main
Choose a base branch
from
feature/cache-token-type
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { verify } from '../../src/jwt'; | ||
| import { MessageChannel } from 'worker_threads'; | ||
| import * as utils from '../../src/utils'; | ||
| import { expect } from '@jest/globals'; | ||
|
|
||
| import { setupFn, fetchResponse, loginWithRedirectFn } from './helpers'; | ||
| import { | ||
| TEST_ACCESS_TOKEN, | ||
| TEST_CODE_CHALLENGE, | ||
| TEST_ID_TOKEN, | ||
| TEST_REFRESH_TOKEN | ||
| } from '../constants'; | ||
|
|
||
| jest.mock('es-cookie'); | ||
| jest.mock('../../src/jwt'); | ||
| jest.mock('../../src/worker/token.worker'); | ||
|
|
||
| const mockWindow = <any>global; | ||
| const mockFetch = <jest.Mock>mockWindow.fetch; | ||
| const mockVerify = <jest.Mock>verify; | ||
|
|
||
| jest | ||
| .spyOn(utils, 'bufferToBase64UrlEncoded') | ||
| .mockReturnValue(TEST_CODE_CHALLENGE); | ||
|
|
||
| const setup = setupFn(mockVerify); | ||
| const loginWithRedirect = loginWithRedirectFn(mockWindow, mockFetch); | ||
|
|
||
| describe('Auth0Client - Token Type Preservation', () => { | ||
| const oldWindowLocation = window.location; | ||
|
|
||
| beforeEach(() => { | ||
| delete (window as any).location; | ||
| window.location = Object.defineProperties( | ||
| {}, | ||
| { | ||
| ...Object.getOwnPropertyDescriptors(oldWindowLocation), | ||
| assign: { | ||
| configurable: true, | ||
| value: jest.fn() | ||
| } | ||
| } | ||
| ) as Location; | ||
|
|
||
| mockWindow.open = jest.fn(); | ||
| mockWindow.addEventListener = jest.fn(); | ||
| mockWindow.removeEventListener = jest.fn(); | ||
|
|
||
| mockWindow.crypto = { | ||
| subtle: { digest: () => 'foo' }, | ||
| getRandomValues() { | ||
| return '123'; | ||
| } | ||
| }; | ||
| mockWindow.MessageChannel = MessageChannel; | ||
| mockWindow.Worker = {}; | ||
| sessionStorage.clear(); | ||
| localStorage.clear(); | ||
| mockFetch.mockReset(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| window.location = oldWindowLocation; | ||
| }); | ||
|
|
||
| describe('Token Type Preservation - HTTP Level', () => { | ||
| it('should preserve token_type when refreshing tokens via HTTP', async () => { | ||
| const auth0 = setup({ | ||
| useRefreshTokens: true, | ||
| cacheLocation: 'localstorage' | ||
| }); | ||
|
|
||
| // Perform login to set up authentication state with refresh token | ||
| await loginWithRedirect(auth0); | ||
| mockFetch.mockReset(); | ||
|
|
||
| // Mock HTTP refresh token response that includes token_type | ||
| mockFetch.mockResolvedValueOnce( | ||
| fetchResponse(true, { | ||
| id_token: TEST_ID_TOKEN, | ||
| access_token: 'new_access_token_with_type', | ||
| refresh_token: 'new_refresh_token', | ||
| expires_in: 3600, | ||
| scope: 'openid profile email offline_access', | ||
| token_type: 'Bearer' // This should be preserved | ||
| }) | ||
| ); | ||
|
|
||
| const result = await auth0.getTokenSilently({ | ||
| cacheMode: 'off', | ||
| detailedResponse: true | ||
| }); | ||
|
|
||
| // Verify token_type flows through the HTTP refresh flow | ||
| expect(result).toMatchObject({ | ||
| access_token: 'new_access_token_with_type', | ||
| token_type: 'Bearer' | ||
| }); | ||
|
|
||
| // Verify actual HTTP call was made with refresh token grant | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| const requestBodyString = mockFetch.mock.calls[0][1].body; | ||
| expect(requestBodyString).toContain('grant_type=refresh_token'); | ||
| }); | ||
|
|
||
| it('should preserve token_type through cache storage after HTTP response', async () => { | ||
| const auth0 = setup({ | ||
| useRefreshTokens: true, | ||
| cacheLocation: 'localstorage' | ||
| }); | ||
|
|
||
| // Perform login to set up authentication state | ||
| await loginWithRedirect(auth0); | ||
| mockFetch.mockReset(); | ||
|
|
||
| // Mock HTTP response with token_type | ||
| mockFetch.mockResolvedValueOnce( | ||
| fetchResponse(true, { | ||
| id_token: TEST_ID_TOKEN, | ||
| access_token: 'cached_token', | ||
| refresh_token: 'new_refresh_token', | ||
| expires_in: 3600, | ||
| scope: 'openid profile email offline_access', | ||
| token_type: 'Bearer' | ||
| }) | ||
| ); | ||
|
|
||
| // First call triggers HTTP request and caching | ||
| await auth0.getTokenSilently({ cacheMode: 'off' }); | ||
|
|
||
| // Second call should use cache without HTTP - this tests cache preservation | ||
| const cachedResult = await auth0.getTokenSilently({ | ||
| detailedResponse: true | ||
| }); | ||
|
|
||
| // Verify token_type is preserved in cached response | ||
| expect(cachedResult).toMatchObject({ | ||
| access_token: 'cached_token', | ||
| token_type: 'Bearer' | ||
| }); | ||
|
|
||
| // Only one HTTP call should have been made | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should handle HTTP response without token_type gracefully', async () => { | ||
| const auth0 = setup({ | ||
| useRefreshTokens: true, | ||
| cacheLocation: 'localstorage' | ||
| }); | ||
|
|
||
| // Perform login to set up authentication state | ||
| await loginWithRedirect(auth0); | ||
| mockFetch.mockReset(); | ||
|
|
||
| // Mock HTTP response WITHOUT token_type (backward compatibility) | ||
| mockFetch.mockResolvedValueOnce( | ||
| fetchResponse(true, { | ||
| id_token: TEST_ID_TOKEN, | ||
| access_token: 'token_without_type', | ||
| refresh_token: TEST_REFRESH_TOKEN, | ||
| expires_in: 3600, | ||
| scope: 'openid profile email offline_access' | ||
| // Note: no token_type field | ||
| }) | ||
| ); | ||
|
|
||
| const result = await auth0.getTokenSilently({ | ||
| cacheMode: 'off', | ||
| detailedResponse: true | ||
| }); | ||
|
|
||
| // Should work without token_type (graceful degradation) | ||
| expect(result).toMatchObject({ | ||
| access_token: 'token_without_type' | ||
| }); | ||
| expect(result.token_type).toBeUndefined(); | ||
|
|
||
| // Verify HTTP call was made | ||
| expect(mockFetch).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
| }); | ||
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
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.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note test
Copilot Autofix
AI 5 months ago
To fix the issue, we should remove the unused
TEST_ACCESS_TOKENimport from the file. This will clean up the code and eliminate the unnecessary import. No additional changes are required since the removal does not affect the functionality of the code.