-
Notifications
You must be signed in to change notification settings - Fork 38
fix(auth): retry the OAuth token exchange on transient failures #2931
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
+275
−18
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,170 @@ | ||
| /** | ||
| * Tests for the token-exchange retry in exchangeCodeForTokens, exercised through | ||
| * handleLoginCallback (the shared path used by both popup and redirect flows). | ||
| */ | ||
|
|
||
| const trackMock = jest.fn(); | ||
|
|
||
| jest.mock('@imtbl/metrics', () => ({ | ||
| track: (...args: unknown[]) => trackMock(...args), | ||
| getDetail: jest.fn(), | ||
| Detail: { RUNTIME_ID: 'runtime_id' }, | ||
| })); | ||
|
|
||
| jest.mock('../utils/jwt', () => ({ | ||
| decodeJwtPayload: jest.fn(() => ({})), | ||
| })); | ||
|
|
||
| // eslint-disable-next-line import/first | ||
| import { handleLoginCallback, type LoginConfig } from './standalone'; | ||
|
|
||
| const CONFIG: LoginConfig = { | ||
| clientId: 'client-123', | ||
| redirectUri: 'https://app.example.com/callback', | ||
| authenticationDomain: 'https://auth.example.com', | ||
| }; | ||
|
|
||
| const PKCE = { | ||
| state: 'state-xyz', | ||
| verifier: 'verifier-abc', | ||
| redirectUri: 'https://app.example.com/callback', | ||
| }; | ||
|
|
||
| const TOKENS = { access_token: 'access-tok', refresh_token: 'refresh-tok' }; | ||
|
|
||
| function mockResponse(status: number, body: unknown): Response { | ||
| return { | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| json: async () => body, | ||
| text: async () => JSON.stringify(body), | ||
| } as unknown as Response; | ||
| } | ||
|
|
||
| describe('exchangeCodeForTokens retry (via handleLoginCallback)', () => { | ||
| let fetchMock: jest.Mock; | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| window.sessionStorage.setItem('imtbl_pkce_data', JSON.stringify(PKCE)); | ||
| window.history.replaceState(null, '', '/?code=auth-code&state=state-xyz'); | ||
| fetchMock = jest.fn(); | ||
| global.fetch = fetchMock as unknown as typeof fetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.runOnlyPendingTimers(); | ||
| jest.useRealTimers(); | ||
| window.sessionStorage.clear(); | ||
| }); | ||
|
|
||
| it('retries a 5xx and resolves on the next success', async () => { | ||
| fetchMock | ||
| .mockResolvedValueOnce(mockResponse(503, { error: 'unavailable' })) | ||
| .mockResolvedValueOnce(mockResponse(200, TOKENS)); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| await jest.advanceTimersByTimeAsync(2000); | ||
| const result = await promise; | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result?.accessToken).toBe('access-tok'); | ||
| }); | ||
|
|
||
| it('emits telemetry for the retry and the recovery', async () => { | ||
| fetchMock | ||
| .mockResolvedValueOnce(mockResponse(503, { error: 'unavailable' })) | ||
| .mockResolvedValueOnce(mockResponse(200, TOKENS)); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| await jest.advanceTimersByTimeAsync(2000); | ||
| await promise; | ||
|
|
||
| expect(trackMock).toHaveBeenCalledWith( | ||
| 'passport', | ||
| 'standaloneTokenExchangeFailed', | ||
| expect.objectContaining({ attempt: 1, reason: '503', willRetry: true }), | ||
| ); | ||
| expect(trackMock).toHaveBeenCalledWith( | ||
| 'passport', | ||
| 'standaloneTokenExchangeRecovered', | ||
| expect.objectContaining({ attempt: 2 }), | ||
| ); | ||
| }); | ||
|
|
||
| it('aborts a stalled response body and retries', async () => { | ||
| // Headers arrive but the body never completes — the abort must still be armed, or the | ||
| // read hangs forever (fetch resolves on headers, not on the full body). | ||
| fetchMock.mockImplementationOnce((_url: string, init: RequestInit) => Promise.resolve({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => new Promise((_resolve, reject) => { | ||
| init.signal?.addEventListener('abort', () => { | ||
| reject(Object.assign(new Error('The operation was aborted'), { name: 'AbortError' })); | ||
| }); | ||
| }), | ||
| text: async () => '', | ||
| } as unknown as Response)); | ||
| fetchMock.mockResolvedValueOnce(mockResponse(200, TOKENS)); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| await jest.advanceTimersByTimeAsync(10000); // body-read timeout fires | ||
| await jest.advanceTimersByTimeAsync(2000); // backoff before the retry | ||
| const result = await promise; | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result?.accessToken).toBe('access-tok'); | ||
| }); | ||
|
|
||
| it('retries a network error and resolves on the next success', async () => { | ||
| fetchMock | ||
| .mockRejectedValueOnce(new Error('network down')) | ||
| .mockResolvedValueOnce(mockResponse(200, TOKENS)); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| await jest.advanceTimersByTimeAsync(2000); | ||
| const result = await promise; | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result?.accessToken).toBe('access-tok'); | ||
| }); | ||
|
|
||
| it('aborts a hung attempt at the timeout and retries', async () => { | ||
| // First attempt never responds; the per-attempt AbortController should fire at | ||
| // TOKEN_EXCHANGE_TIMEOUT_MS and reject it, which is treated as transient. | ||
| fetchMock.mockImplementationOnce( | ||
| (_url: string, init: RequestInit) => new Promise<Response>((_resolve, reject) => { | ||
| init.signal?.addEventListener('abort', () => { | ||
| reject(Object.assign(new Error('The operation was aborted'), { name: 'AbortError' })); | ||
| }); | ||
| }), | ||
| ); | ||
| fetchMock.mockResolvedValueOnce(mockResponse(200, TOKENS)); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| await jest.advanceTimersByTimeAsync(10000); // fire the attempt timeout -> abort | ||
| await jest.advanceTimersByTimeAsync(2000); // backoff before the retry | ||
| const result = await promise; | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result?.accessToken).toBe('access-tok'); | ||
| }); | ||
|
|
||
| it('does not retry a 4xx', async () => { | ||
| fetchMock.mockResolvedValueOnce(mockResponse(400, { error: 'invalid_grant' })); | ||
|
|
||
| await expect(handleLoginCallback(CONFIG)).rejects.toThrow('invalid_grant'); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('gives up after exhausting retries', async () => { | ||
| fetchMock.mockResolvedValue(mockResponse(500, { error: 'boom' })); | ||
|
|
||
| const promise = handleLoginCallback(CONFIG); | ||
| promise.catch(() => {}); // avoid an unhandled rejection while advancing timers | ||
| await jest.advanceTimersByTimeAsync(6000); // both backoffs, generous for jitter | ||
|
|
||
| await expect(promise).rejects.toThrow('boom'); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); // initial + 2 retries | ||
| }); | ||
| }); |
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.
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.