fixing token expiration time (can be number or string), make sure we parse it correctly.

This commit is contained in:
Jason Kulatunga 2023-08-25 15:48:52 -07:00
parent b351919096
commit c9b482d668
No known key found for this signature in database
2 changed files with 17 additions and 4 deletions

View File

@ -31,4 +31,18 @@ describe('MedicalSourcesConnectedComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should handle nanosecond and microsecond token expirations', () => {
var tokenResponse = {
token_type: "Bearer",
expires_in: "3600",
access_token: "OXgK8mrfvMrxIMK38T6CAjKiLMDV",
refresh_token: "5Oq5ZgcTgi9-xxxxxxx",
patient: "a-80000.xxxx"
}
var expiresAt = component.getAccessTokenExpiration(tokenResponse)
expect(expiresAt.toString().length).toEqual(10)
})
});

View File

@ -243,19 +243,18 @@ export class MedicalSourcesConnectedComponent implements OnInit {
public getAccessTokenExpiration(tokenResponse: any): number
{
const now = Math.floor(Date.now() / 1000);
let expires_at = 0;
// Option 1 - using the expires_in property of the token response
if (tokenResponse.expires_in) {
return now + tokenResponse.expires_in;
return now + parseInt(tokenResponse.expires_in);
}
// Option 2 - using the exp property of JWT tokens (must not assume JWT!)
if (tokenResponse.access_token) {
let tokenBody = this.jwtDecode(tokenResponse.access_token);
if (tokenBody && tokenBody['exp']) {
return tokenBody['exp'];
return parseInt(tokenBody['exp']);
}
}