Skip to content

Commit ad4b090

Browse files
Added AbstractSecurityStorage
1 parent 42659ab commit ad4b090

File tree

9 files changed

+72
-53
lines changed

9 files changed

+72
-53
lines changed

projects/angular-auth-oidc-client/src/lib/auth.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { StandardLoginService } from './login/standard/standard-login.service';
3939
import { LogoffRevocationService } from './logoff-revoke/logoff-revocation.service';
4040
import { OidcSecurityService } from './oidc.security.service';
4141
import { PublicEventsService } from './public-events/public-events.service';
42+
import { AbstractSecurityStorage } from './storage/abstract-security-storage';
4243
import { BrowserStorageService } from './storage/browser-storage.service';
4344
import { DefaultSessionStorageService } from './storage/default-sessionstorage.service';
4445
import { StoragePersistenceService } from './storage/storage-persistence.service';
@@ -132,6 +133,8 @@ export class AuthModule {
132133
DefaultSessionStorageService,
133134
BrowserStorageService,
134135
CryptoService,
136+
137+
{ provide: AbstractSecurityStorage, useClass: DefaultSessionStorageService },
135138
],
136139
};
137140
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ export class ConfigurationService {
136136
private prepareConfig(configuration: OpenIdConfiguration): OpenIdConfiguration {
137137
const openIdConfigurationInternal = { ...DEFAULT_CONFIG, ...configuration };
138138
this.setSpecialCases(openIdConfigurationInternal);
139-
this.setStorage(openIdConfigurationInternal);
140139

141140
return openIdConfigurationInternal;
142141
}
@@ -150,18 +149,6 @@ export class ConfigurationService {
150149
}
151150
}
152151

153-
private setStorage(currentConfig: OpenIdConfiguration): void {
154-
if (currentConfig.storage) {
155-
return;
156-
}
157-
158-
if (this.hasBrowserStorage()) {
159-
currentConfig.storage = this.defaultSessionStorageService;
160-
} else {
161-
currentConfig.storage = null;
162-
}
163-
}
164-
165152
private hasBrowserStorage(): boolean {
166153
return typeof navigator !== 'undefined' && navigator.cookieEnabled && typeof Storage !== 'undefined';
167154
}

projects/angular-auth-oidc-client/src/lib/config/default-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const DEFAULT_CONFIG: OpenIdConfiguration = {
3030
historyCleanupOff: false,
3131
maxIdTokenIatOffsetAllowedInSeconds: 120,
3232
disableIatOffsetValidation: false,
33-
storage: null,
3433
customParamsAuthRequest: {},
3534
customParamsRefreshTokenRequest: {},
3635
customParamsEndSessionRequest: {},

projects/angular-auth-oidc-client/src/lib/config/openid-configuration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ export interface OpenIdConfiguration {
131131
* The acceptable range is client specific.
132132
*/
133133
disableIatOffsetValidation?: boolean;
134-
/** The storage mechanism to use */
135-
storage?: any;
134+
136135
/** Extra parameters to add to the authorization URL request */
137136
customParamsAuthRequest?: { [key: string]: string | number | boolean };
138137

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Injectable } from '@angular/core';
2+
import { OpenIdConfiguration } from '../config/openid-configuration';
3+
4+
/**
5+
* Implement this class-interface to create a custom logger service.
6+
*/
7+
@Injectable()
8+
export abstract class AbstractLoggerService {
9+
abstract logError(configuration: OpenIdConfiguration, message: any, ...args: any[]): void;
10+
11+
abstract logWarning(configuration: OpenIdConfiguration, message: any, ...args: any[]): void;
12+
13+
abstract logDebug(configuration: OpenIdConfiguration, message: any, ...args: any[]): void;
14+
}

projects/angular-auth-oidc-client/src/lib/logging/logger.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Injectable } from '@angular/core';
22
import { OpenIdConfiguration } from '../config/openid-configuration';
3+
import { AbstractLoggerService } from './abstract-logger.service';
34
import { LogLevel } from './log-level';
45

56
@Injectable()
6-
export class LoggerService {
7+
export class LoggerService implements AbstractLoggerService {
78
logError(configuration: OpenIdConfiguration, message: any, ...args: any[]): void {
89
if (this.loggingIsTurnedOff(configuration)) {
910
return;

projects/angular-auth-oidc-client/src/lib/storage/abstract-security-storage.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ export abstract class AbstractSecurityStorage {
1010
*
1111
* @return The value of the given key
1212
*/
13-
public abstract read(key: string): any;
13+
abstract read(key: string): any;
1414

1515
/**
1616
* This method must contain the logic to write the storage.
1717
*
1818
* @param key The key to write a value for
1919
* @param value The value for the given key
2020
*/
21-
public abstract write(key: string, value: any): void;
21+
abstract write(key: string, value: any): void;
2222

2323
/**
2424
* This method must contain the logic to remove an item from the storage.
2525
*
2626
* @param key The value for the key to be removed
2727
*/
28-
public abstract remove(key: string): void;
28+
abstract remove(key: string): void;
2929

3030
/**
3131
* This method must contain the logic to remove all items from the storage.
3232
*/
33-
public abstract clear(): void;
33+
abstract clear(): void;
3434
}

projects/angular-auth-oidc-client/src/lib/storage/browser-storage.service.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AbstractSecurityStorage } from './abstract-security-storage';
55

66
@Injectable()
77
export class BrowserStorageService {
8-
constructor(private loggerService: LoggerService) {}
8+
constructor(private loggerService: LoggerService, private abstractSecurityStorage: AbstractSecurityStorage) {}
99

1010
read(key: string, configuration: OpenIdConfiguration): any {
1111
const { configId } = configuration;
@@ -16,15 +16,15 @@ export class BrowserStorageService {
1616
return null;
1717
}
1818

19-
const storage = this.getStorage(configuration);
19+
// const storage = this.getStorage(configuration);
2020

21-
if (!storage) {
22-
this.loggerService.logDebug(configuration, `Wanted to read config for '${configId}' but Storage was falsy`);
21+
// if (!storage) {
22+
// this.loggerService.logDebug(configuration, `Wanted to read config for '${configId}' but Storage was falsy`);
2323

24-
return null;
25-
}
24+
// return null;
25+
// }
2626

27-
const storedConfig = storage.read(configId);
27+
const storedConfig = this.abstractSecurityStorage.read(configId);
2828

2929
if (!storedConfig) {
3030
return null;
@@ -42,16 +42,16 @@ export class BrowserStorageService {
4242
return false;
4343
}
4444

45-
const storage = this.getStorage(configuration);
46-
if (!storage) {
47-
this.loggerService.logDebug(configuration, `Wanted to write '${value}' but Storage was falsy`);
45+
// const storage = this.getStorage(configuration);
46+
// if (!storage) {
47+
// this.loggerService.logDebug(configuration, `Wanted to write '${value}' but Storage was falsy`);
4848

49-
return false;
50-
}
49+
// return false;
50+
// }
5151

5252
value = value || null;
5353

54-
storage.write(configId, JSON.stringify(value));
54+
this.abstractSecurityStorage.write(configId, JSON.stringify(value));
5555

5656
return true;
5757
}
@@ -63,14 +63,14 @@ export class BrowserStorageService {
6363
return false;
6464
}
6565

66-
const storage = this.getStorage(configuration);
67-
if (!storage) {
68-
this.loggerService.logDebug(configuration, `Wanted to write '${key}' but Storage was falsy`);
66+
// const storage = this.getStorage(configuration);
67+
// if (!storage) {
68+
// this.loggerService.logDebug(configuration, `Wanted to write '${key}' but Storage was falsy`);
6969

70-
return false;
71-
}
70+
// return false;
71+
// }
7272

73-
storage.remove(key);
73+
this.abstractSecurityStorage.remove(key);
7474

7575
return true;
7676
}
@@ -83,24 +83,18 @@ export class BrowserStorageService {
8383
return false;
8484
}
8585

86-
const storage = this.getStorage(configuration);
87-
if (!storage) {
88-
this.loggerService.logDebug(configuration, `Wanted to clear storage but Storage was falsy`);
86+
// const storage = this.getStorage(configuration);
87+
// if (!storage) {
88+
// this.loggerService.logDebug(configuration, `Wanted to clear storage but Storage was falsy`);
8989

90-
return false;
91-
}
90+
// return false;
91+
// }
9292

93-
storage.clear();
93+
this.abstractSecurityStorage.clear();
9494

9595
return true;
9696
}
9797

98-
private getStorage(configuration: OpenIdConfiguration): AbstractSecurityStorage {
99-
const { storage } = configuration || {};
100-
101-
return storage;
102-
}
103-
10498
private hasStorage(): boolean {
10599
return typeof Storage !== 'undefined';
106100
}

projects/sample-code-flow-auth0/src/app/auth-config.module.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import { NgModule } from '@angular/core';
2-
import { AuthModule, LogLevel } from 'angular-auth-oidc-client';
2+
import { AbstractSecurityStorage, AuthModule, LogLevel } from 'angular-auth-oidc-client';
3+
4+
export class MyStorageService extends AbstractSecurityStorage {
5+
read(key: string) {
6+
throw new Error('Method not implemented.');
7+
}
8+
write(key: string, value: any): void {
9+
throw new Error('Method not implemented.');
10+
}
11+
remove(key: string): void {
12+
throw new Error('Method not implemented.');
13+
}
14+
clear(): void {
15+
throw new Error('Method not implemented.');
16+
}
17+
constructor() {
18+
super();
19+
}
20+
}
321

422
@NgModule({
523
imports: [
@@ -23,6 +41,10 @@ import { AuthModule, LogLevel } from 'angular-auth-oidc-client';
2341
},
2442
}),
2543
],
44+
providers: [
45+
// { provide: AbstractSecurityStorage, useClass: MyStorageService },
46+
//{ provide: LoggerService, useClass: MyLoggerService },
47+
],
2648
exports: [AuthModule],
2749
})
2850
export class AuthConfigModule {}

0 commit comments

Comments
 (0)