@@ -26,6 +26,7 @@ import {
26
26
catchError
27
27
} from 'rxjs/operators' ;
28
28
import { DOCUMENT } from '@angular/common' ;
29
+ import { DateTimeProvider } from './date-time-provider' ;
29
30
30
31
import {
31
32
ValidationHandler ,
@@ -120,7 +121,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
120
121
protected urlHelper : UrlHelperService ,
121
122
protected logger : OAuthLogger ,
122
123
@Optional ( ) protected crypto : HashHandler ,
123
- @Inject ( DOCUMENT ) document : any
124
+ @Inject ( DOCUMENT ) document : any ,
125
+ protected dateTimeService : DateTimeProvider ,
124
126
) {
125
127
super ( ) ;
126
128
@@ -463,7 +465,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
463
465
}
464
466
465
467
protected calcTimeout ( storedAt : number , expiration : number ) : number {
466
- const now = Date . now ( ) ;
468
+ const now = this . dateTimeService . now ( ) ;
467
469
const delta =
468
470
( expiration - storedAt ) * this . timeoutFactor - ( now - storedAt ) ;
469
471
return Math . max ( 0 , delta ) ;
@@ -1572,10 +1574,10 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1572
1574
this . _storage . setItem ( 'granted_scopes' , JSON . stringify ( grantedScopes ) ) ;
1573
1575
}
1574
1576
1575
- this . _storage . setItem ( 'access_token_stored_at' , '' + Date . now ( ) ) ;
1577
+ this . _storage . setItem ( 'access_token_stored_at' , '' + this . dateTimeService . now ( ) ) ;
1576
1578
if ( expiresIn ) {
1577
1579
const expiresInMilliSeconds = expiresIn * 1000 ;
1578
- const now = new Date ( ) ;
1580
+ const now = this . dateTimeService . new ( ) ;
1579
1581
const expiresAt = now . getTime ( ) + expiresInMilliSeconds ;
1580
1582
this . _storage . setItem ( 'expires_at' , '' + expiresAt ) ;
1581
1583
}
@@ -1973,7 +1975,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
1973
1975
this . _storage . setItem ( 'id_token' , idToken . idToken ) ;
1974
1976
this . _storage . setItem ( 'id_token_claims_obj' , idToken . idTokenClaimsJson ) ;
1975
1977
this . _storage . setItem ( 'id_token_expires_at' , '' + idToken . idTokenExpiresAt ) ;
1976
- this . _storage . setItem ( 'id_token_stored_at' , '' + Date . now ( ) ) ;
1978
+ this . _storage . setItem ( 'id_token_stored_at' , '' + this . dateTimeService . now ( ) ) ;
1977
1979
}
1978
1980
1979
1981
protected storeSessionState ( sessionState : string ) : void {
@@ -2094,7 +2096,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
2094
2096
return Promise . reject ( err ) ;
2095
2097
}
2096
2098
2097
- const now = Date . now ( ) ;
2099
+ const now = this . dateTimeService . now ( ) ;
2098
2100
const issuedAtMSec = claims . iat * 1000 ;
2099
2101
const expiresAtMSec = claims . exp * 1000 ;
2100
2102
const clockSkewInMSec = ( this . clockSkewInSec || 600 ) * 1000 ;
@@ -2254,7 +2256,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
2254
2256
public hasValidAccessToken ( ) : boolean {
2255
2257
if ( this . getAccessToken ( ) ) {
2256
2258
const expiresAt = this . _storage . getItem ( 'expires_at' ) ;
2257
- const now = new Date ( ) ;
2259
+ const now = this . dateTimeService . new ( ) ;
2258
2260
if ( expiresAt && parseInt ( expiresAt , 10 ) < now . getTime ( ) ) {
2259
2261
return false ;
2260
2262
}
@@ -2271,7 +2273,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
2271
2273
public hasValidIdToken ( ) : boolean {
2272
2274
if ( this . getIdToken ( ) ) {
2273
2275
const expiresAt = this . _storage . getItem ( 'id_token_expires_at' ) ;
2274
- const now = new Date ( ) ;
2276
+ const now = this . dateTimeService . new ( ) ;
2275
2277
if ( expiresAt && parseInt ( expiresAt , 10 ) < now . getTime ( ) ) {
2276
2278
return false ;
2277
2279
}
0 commit comments