-
Notifications
You must be signed in to change notification settings - Fork 22
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
[ID-1099]getUser will get new token when existed token expired. #913
Merged
carmen0208
merged 7 commits into
main
from
feature/ID-1099-get-user-not-return-expired-user
Oct 3, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c2a3d3
get user call silentlogin
carmen0208 0e64165
valid if token expired with id token expired
carmen0208 be12376
correct unit test
carmen0208 76e78ef
address comments
carmen0208 1d30bf4
Merge branch 'main' into feature/ID-1099-get-user-not-return-expired-…
carmen0208 b82ad5d
address comments2
carmen0208 62437a4
Merge branch 'main' into feature/ID-1099-get-user-not-return-expired-…
carmen0208 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 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'; | ||
haydenfowler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll also need to add this
devDependency
topackages/passport/package.json
(seems as thoughjwt-decode
was somehow missed 🤔)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be because it already included in yarn.lock: https://github.com/immutable/ts-immutable-sdk/blob/main/yarn.lock#L21022
I will add the package json anyway. would be in dependency thought as the code need to be run within the build sdk.