-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
3836508
commit 9b9b05a
Showing
8 changed files
with
189 additions
and
21 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
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,58 @@ | ||
import encode from 'jwt-encode'; | ||
import { | ||
User as OidcUser, | ||
} from 'oidc-client-ts'; | ||
import { isIdTokenExpired, isTokenExpired } from './token'; | ||
|
||
const now = Math.floor(Date.now() / 1000); | ||
const oneHourLater = now + 3600; | ||
const oneHourBefore = now - 3600; | ||
|
||
const mockExpiredIdToken = encode({ | ||
iat: oneHourBefore, | ||
exp: oneHourBefore, | ||
}, 'secret'); | ||
export const mockValidIdToken = encode({ | ||
iat: now, | ||
exp: oneHourLater, | ||
}, 'secret'); | ||
|
||
describe('isIdTokenExpired', () => { | ||
it('should return false if idToken is undefined', () => { | ||
expect(isIdTokenExpired(undefined)).toBe(false); | ||
}); | ||
|
||
it('should return true if idToken is expired', () => { | ||
expect(isIdTokenExpired(mockExpiredIdToken)).toBe(true); | ||
}); | ||
|
||
it('should return false if idToken is not expired', () => { | ||
expect(isIdTokenExpired(mockValidIdToken)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isTokenExpired', () => { | ||
it('should return true if expired is true', () => { | ||
const user = { | ||
id_token: mockValidIdToken, | ||
expired: true, | ||
} as unknown as OidcUser; | ||
expect(isTokenExpired(user)).toBe(true); | ||
}); | ||
|
||
it('should return false if idToken is valid', () => { | ||
const user = { | ||
id_token: mockValidIdToken, | ||
expired: false, | ||
} as unknown as OidcUser; | ||
expect(isTokenExpired(user)).toBe(false); | ||
}); | ||
|
||
it('should return true idToken is expired', () => { | ||
const user = { | ||
id_token: mockExpiredIdToken, | ||
expired: false, | ||
} as unknown as OidcUser; | ||
expect(isTokenExpired(user)).toBe(true); | ||
}); | ||
}); |
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,22 @@ | ||
import { IdTokenPayload } from 'types'; | ||
import jwt_decode from 'jwt-decode'; | ||
import { | ||
User as OidcUser, | ||
} from 'oidc-client-ts'; | ||
|
||
export function isIdTokenExpired(idToken: string | undefined): boolean { | ||
if (!idToken) { | ||
return false; | ||
} | ||
const decodedToken: IdTokenPayload = jwt_decode(idToken); | ||
const now = Math.floor(Date.now() / 1000); | ||
return decodedToken.exp < now; | ||
} | ||
|
||
export function isTokenExpired(oidcUser: OidcUser): boolean { | ||
const { id_token: idToken, expired } = oidcUser; | ||
if (expired) { | ||
return true; | ||
} | ||
return isIdTokenExpired(idToken); | ||
} |
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