Skip to content

Commit

Permalink
fixing token expiration time (can be number or string), make sure we …
Browse files Browse the repository at this point in the history
…parse it correctly.
  • Loading branch information
AnalogJ committed Aug 25, 2023
1 parent b351919 commit c9b482d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

});
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

Expand Down

0 comments on commit c9b482d

Please sign in to comment.