-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): proactive token refresh (#14076)
* update isTokenExpired to include tolerance * add unit tests * update bundle sizes * tmp enable e2e * revert tmp changes * tmp enable e2e * correct spec name * revert tmp changes * use mock Date.now * add additional test for expiration time exactly equal to tolerance * move isTokenExpired to utils * update bundle size
- Loading branch information
Showing
7 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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,56 @@ | ||
import { isTokenExpired } from '../../src/libraryUtils'; | ||
|
||
describe('isTokenExpired', () => { | ||
const mockDate = Date.now(); | ||
jest.spyOn(Date, 'now').mockImplementation(() => mockDate); | ||
|
||
it('should return true when token is expired', () => { | ||
const result = isTokenExpired({ | ||
expiresAt: Date.now() - 1, | ||
clockDrift: 0, | ||
tolerance: 0, | ||
}); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when token is not expired', () => { | ||
const result = isTokenExpired({ | ||
expiresAt: Date.now() + 1, | ||
clockDrift: 0, | ||
tolerance: 0, | ||
}); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when expiration time is within tolerance', () => { | ||
const result = isTokenExpired({ | ||
expiresAt: Date.now() + 5001, // more than 5 seconds remaining until expiration | ||
clockDrift: 0, | ||
tolerance: 5000, | ||
}); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true when expiration time is outside tolerance', () => { | ||
const result = isTokenExpired({ | ||
expiresAt: Date.now() + 4999, // less than 5 seconds remaining until expiration | ||
clockDrift: 0, | ||
tolerance: 5000, | ||
}); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when expiration time is equal to tolerance', () => { | ||
const result = isTokenExpired({ | ||
expiresAt: Date.now() + 5000, // exactly 5 seconds remaining until expiration | ||
clockDrift: 0, | ||
tolerance: 5000, | ||
}); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains 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 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 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 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,16 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export function isTokenExpired({ | ||
expiresAt, | ||
clockDrift, | ||
tolerance = 5000, | ||
}: { | ||
expiresAt: number; | ||
clockDrift: number; | ||
tolerance?: number; | ||
}): boolean { | ||
const currentTime = Date.now(); | ||
|
||
return currentTime + clockDrift + tolerance > expiresAt; | ||
} |