Skip to content

Commit 34b27e4

Browse files
authored
Merge pull request #1477 from damienbod/fabiangosebrink/improving-offset-validation
improving validation
2 parents 8f48d2c + 3735b44 commit 34b27e4

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

projects/angular-auth-oidc-client/src/lib/validation/token-validation.service.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,25 @@ export class TokenValidationService {
6868

6969
// id_token C7: The current time MUST be before the time represented by the exp Claim
7070
// (possibly allowing for some small leeway to account for clock skew).
71-
hasIdTokenExpired(token: string, configuration: OpenIdConfiguration, offsetSeconds?: number, disableIdTokenValidation?: boolean): boolean {
71+
hasIdTokenExpired(
72+
token: string,
73+
configuration: OpenIdConfiguration,
74+
offsetSeconds?: number,
75+
disableIdTokenValidation?: boolean
76+
): boolean {
7277
const decoded = this.tokenHelperService.getPayloadFromToken(token, false, configuration);
7378

7479
return !this.validateIdTokenExpNotExpired(decoded, configuration, offsetSeconds, disableIdTokenValidation);
7580
}
7681

7782
// id_token C7: The current time MUST be before the time represented by the exp Claim
7883
// (possibly allowing for some small leeway to account for clock skew).
79-
validateIdTokenExpNotExpired(decodedIdToken: string, configuration: OpenIdConfiguration, offsetSeconds?: number, disableIdTokenValidation?: boolean): boolean {
84+
validateIdTokenExpNotExpired(
85+
decodedIdToken: string,
86+
configuration: OpenIdConfiguration,
87+
offsetSeconds?: number,
88+
disableIdTokenValidation?: boolean
89+
): boolean {
8090
if (disableIdTokenValidation) return true;
8191

8292
const tokenExpirationDate = this.tokenHelperService.getTokenExpirationDate(decodedIdToken);
@@ -87,7 +97,7 @@ export class TokenValidationService {
8797
}
8898

8999
const tokenExpirationValue = tokenExpirationDate.valueOf();
90-
const nowWithOffset = new Date(new Date().toUTCString()).valueOf() + offsetSeconds * 1000;
100+
const nowWithOffset = this.calculateNowWithOffset(offsetSeconds);
91101
const tokenNotExpired = tokenExpirationValue > nowWithOffset;
92102

93103
this.loggerService.logDebug(
@@ -97,7 +107,6 @@ export class TokenValidationService {
97107
)} , ${new Date(tokenExpirationValue).toLocaleTimeString()} > ${new Date(nowWithOffset).toLocaleTimeString()}`
98108
);
99109

100-
// Token not expired?
101110
return tokenNotExpired;
102111
}
103112

@@ -109,7 +118,7 @@ export class TokenValidationService {
109118

110119
offsetSeconds = offsetSeconds || 0;
111120
const accessTokenExpirationValue = accessTokenExpiresAt.valueOf();
112-
const nowWithOffset = new Date(new Date().toUTCString()).valueOf() + offsetSeconds * 1000;
121+
const nowWithOffset = this.calculateNowWithOffset(offsetSeconds);
113122
const tokenNotExpired = accessTokenExpirationValue > nowWithOffset;
114123

115124
this.loggerService.logDebug(
@@ -119,7 +128,6 @@ export class TokenValidationService {
119128
)} , ${new Date(accessTokenExpirationValue).toLocaleTimeString()} > ${new Date(nowWithOffset).toLocaleTimeString()}`
120129
);
121130

122-
// access token not expired?
123131
return tokenNotExpired;
124132
}
125133

@@ -523,4 +531,8 @@ export class TokenValidationService {
523531

524532
return minutes + ':' + (+seconds < 10 ? '0' : '') + seconds;
525533
}
534+
535+
private calculateNowWithOffset(offsetSeconds: number): number {
536+
return new Date(new Date().toUTCString()).valueOf() + offsetSeconds * 1000;
537+
}
526538
}

0 commit comments

Comments
 (0)