Skip to content

Commit

Permalink
feat: introduce DateTimeProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Rauber committed Jul 3, 2020
1 parent 8d152c2 commit 0c0a4a7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion projects/lib/src/angular-oauth-oidc.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DateTimeProvider, SystemDateTimeProvider } from './date-time-provider';
import { OAuthStorage, OAuthLogger } from './types';
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
Expand Down Expand Up @@ -48,7 +49,8 @@ export class OAuthModule {
provide: HTTP_INTERCEPTORS,
useClass: DefaultOAuthInterceptor,
multi: true
}
},
{ provide: DateTimeProvider, useClass: SystemDateTimeProvider }
]
};
}
Expand Down
17 changes: 17 additions & 0 deletions projects/lib/src/date-time-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';

export abstract class DateTimeProvider {
abstract now(): number;
abstract new(): Date;
}

@Injectable()
export class SystemDateTimeProvider extends DateTimeProvider {
now(): number {
return Date.now();
}

new(): Date {
return new Date();
}
}
18 changes: 10 additions & 8 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
catchError
} from 'rxjs/operators';
import { DOCUMENT } from '@angular/common';
import { DateTimeProvider } from './date-time-provider';

import {
ValidationHandler,
Expand Down Expand Up @@ -120,7 +121,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
protected urlHelper: UrlHelperService,
protected logger: OAuthLogger,
@Optional() protected crypto: HashHandler,
@Inject(DOCUMENT) document: any
@Inject(DOCUMENT) document: any,
protected dateTimeService: DateTimeProvider,
) {
super();

Expand Down Expand Up @@ -463,7 +465,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
}

protected calcTimeout(storedAt: number, expiration: number): number {
const now = Date.now();
const now = this.dateTimeService.now();
const delta =
(expiration - storedAt) * this.timeoutFactor - (now - storedAt);
return Math.max(0, delta);
Expand Down Expand Up @@ -1572,10 +1574,10 @@ export class OAuthService extends AuthConfig implements OnDestroy {
this._storage.setItem('granted_scopes', JSON.stringify(grantedScopes));
}

this._storage.setItem('access_token_stored_at', '' + Date.now());
this._storage.setItem('access_token_stored_at', '' + this.dateTimeService.now());
if (expiresIn) {
const expiresInMilliSeconds = expiresIn * 1000;
const now = new Date();
const now = this.dateTimeService.new();
const expiresAt = now.getTime() + expiresInMilliSeconds;
this._storage.setItem('expires_at', '' + expiresAt);
}
Expand Down Expand Up @@ -1973,7 +1975,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
this._storage.setItem('id_token', idToken.idToken);
this._storage.setItem('id_token_claims_obj', idToken.idTokenClaimsJson);
this._storage.setItem('id_token_expires_at', '' + idToken.idTokenExpiresAt);
this._storage.setItem('id_token_stored_at', '' + Date.now());
this._storage.setItem('id_token_stored_at', '' + this.dateTimeService.now());
}

protected storeSessionState(sessionState: string): void {
Expand Down Expand Up @@ -2094,7 +2096,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
return Promise.reject(err);
}

const now = Date.now();
const now = this.dateTimeService.now();
const issuedAtMSec = claims.iat * 1000;
const expiresAtMSec = claims.exp * 1000;
const clockSkewInMSec = (this.clockSkewInSec || 600) * 1000;
Expand Down Expand Up @@ -2254,7 +2256,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
public hasValidAccessToken(): boolean {
if (this.getAccessToken()) {
const expiresAt = this._storage.getItem('expires_at');
const now = new Date();
const now = this.dateTimeService.new();
if (expiresAt && parseInt(expiresAt, 10) < now.getTime()) {
return false;
}
Expand All @@ -2271,7 +2273,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
public hasValidIdToken(): boolean {
if (this.getIdToken()) {
const expiresAt = this._storage.getItem('id_token_expires_at');
const now = new Date();
const now = this.dateTimeService.new();
if (expiresAt && parseInt(expiresAt, 10) < now.getTime()) {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions projects/lib/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './events';
export * from './interceptors/default-oauth.interceptor';
export * from './interceptors/resource-server-error-handler';
export * from './oauth-module.config';
export * from './date-time-provider';

0 comments on commit 0c0a4a7

Please sign in to comment.