forked from internxt/drive-desktop
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: Improve token schedule to avoid session close #202
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a433680
fix: remove redundant credential updates on user login to avoid race …
egalvis27 1a93f96
fix: ensure credentials are updated correctly during user initialization
egalvis27 7923558
fix: standardize log message formatting for token refresh failure
egalvis27 316860a
feat(auth): improve-refresh-token-tests
egalvis27 803f763
fix: update eslintignore to include vitest tests and ensure proper fo…
egalvis27 90dc49e
feat(auth): code review changes applied
egalvis27 562deea
fix(auth): remove redundant line breaks in import statements
egalvis27 eb70e32
fix(auth): remove redundant create-token-schedule tests
egalvis27 9f41d56
fix(auth): refactor token scheduling and update import paths
egalvis27 60e0a6a
fix(auth): format code for consistency in createTokenScheduleWithRetr…
egalvis27 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
58 changes: 58 additions & 0 deletions
58
src/apps/main/auth/refresh-token/create-token-schedule-with-retry.test.ts
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,58 @@ | ||
| import { createTokenScheduleWithRetry } from './create-token-schedule-with-retry'; | ||
| import * as authServiceModule from '../service'; | ||
| import { call, calls, partialSpyOn } from 'tests/vitest/utils.helper'; | ||
| import { loggerMock } from 'tests/vitest/mocks.helper'; | ||
| import { TokenScheduler } from '../../token-scheduler/TokenScheduler'; | ||
| import { Job } from 'node-schedule'; | ||
|
|
||
| describe('createTokenScheduleWithRetry', () => { | ||
| const obtainTokensMock = partialSpyOn(authServiceModule, 'obtainTokens'); | ||
| const scheduleMock = partialSpyOn(TokenScheduler.prototype, 'schedule'); | ||
|
|
||
| const jobMock: Partial<Job> = { | ||
| cancel: vi.fn(), | ||
| }; | ||
| const validTokens = ['token-1', 'token-2']; | ||
|
|
||
| beforeEach(() => { | ||
| scheduleMock.mockReturnValue(jobMock as Job); | ||
| }); | ||
|
|
||
| it('should create token schedule with provided refreshedTokens parameter', async () => { | ||
| await createTokenScheduleWithRetry({ refreshedTokens: validTokens }); | ||
|
|
||
| calls(scheduleMock).toHaveLength(1); | ||
| calls(obtainTokensMock).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should create token schedule using obtainStoredTokens when no parameter provided', async () => { | ||
| obtainTokensMock.mockReturnValue(validTokens); | ||
|
|
||
| await createTokenScheduleWithRetry(); | ||
|
|
||
| calls(obtainTokensMock).toHaveLength(1); | ||
| calls(scheduleMock).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('should attempt to schedule only once when schedule() succeeds immediately', async () => { | ||
| scheduleMock.mockReturnValue(jobMock); | ||
|
|
||
| await createTokenScheduleWithRetry({ refreshedTokens: validTokens }); | ||
|
|
||
| calls(scheduleMock).toHaveLength(1); | ||
| calls(loggerMock.debug).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should retry when schedule() fails and succeed on second attempt', async () => { | ||
| scheduleMock.mockReturnValueOnce(undefined).mockReturnValueOnce(jobMock); | ||
|
|
||
| await createTokenScheduleWithRetry({ refreshedTokens: validTokens }); | ||
|
|
||
| calls(scheduleMock).toHaveLength(2); | ||
| calls(loggerMock.debug).toHaveLength(1); | ||
| call(loggerMock.debug).toMatchObject({ | ||
| msg: '[TOKEN] Failed to create token schedule, retrying...', | ||
| tag: 'AUTH', | ||
| }); | ||
| }); | ||
| }); | ||
28 changes: 28 additions & 0 deletions
28
src/apps/main/auth/refresh-token/create-token-schedule-with-retry.ts
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,28 @@ | ||
| import { TokenScheduler } from '../../token-scheduler/TokenScheduler'; | ||
| import { onUserUnauthorized } from '../handlers'; | ||
| import { obtainTokens as obtainStoredTokens } from '../service'; | ||
| import { logger } from '@internxt/drive-desktop-core/build/backend'; | ||
| import { refreshToken } from './refresh-token'; | ||
|
|
||
| const CREATE_SCHEDULE_RETRY_LIMIT = 3; | ||
|
|
||
| type Props = { | ||
| refreshedTokens?: Array<string | undefined>; | ||
| }; | ||
|
|
||
| export async function createTokenScheduleWithRetry({ refreshedTokens }: Props = {}) { | ||
| const tokens = refreshedTokens || obtainStoredTokens(); | ||
| const tokenScheduler = new TokenScheduler(5, tokens, onUserUnauthorized); | ||
|
|
||
| let attempt = 0; | ||
| while (attempt < CREATE_SCHEDULE_RETRY_LIMIT) { | ||
| const schedule = tokenScheduler.schedule(() => refreshToken()); | ||
| if (schedule) break; | ||
|
|
||
| attempt++; | ||
| logger.debug({ | ||
| tag: 'AUTH', | ||
| msg: '[TOKEN] Failed to create token schedule, retrying...', | ||
| }); | ||
| } | ||
| } |
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.