Skip to content

Commit

Permalink
fix: use a prefix (icm_) for internal token oauth storage (#1601)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Silke <[email protected]>
  • Loading branch information
2 people authored and shauke committed Mar 15, 2024
1 parent b230a93 commit f3e940f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions e2e/cypress/e2e/specs/account/login-user.b2c.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Returning User', () => {
});

cy.getAllLocalStorage().then(
localStorage => expect(localStorage[Cypress.config('baseUrl')].access_token).to.not.be.empty
localStorage => expect(localStorage[Cypress.config('baseUrl')].icm_access_token).to.not.be.empty
);
});

Expand All @@ -61,7 +61,7 @@ describe('Returning User', () => {
});

cy.getAllLocalStorage().then(
localStorage => expect(localStorage[Cypress.config('baseUrl')].access_token).to.be.undefined
localStorage => expect(localStorage[Cypress.config('baseUrl')].icm_access_token).to.be.undefined
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/token/token.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Token Service', () => {
when(oAuthService.configure(anything())).thenResolve();
when(oAuthService.events).thenReturn(of(undefined));

when(instanceCreators.getOAuthServiceInstance(anything())).thenReturn(instance(oAuthService));
when(instanceCreators.getOAuthServiceInstance(anything(), anything())).thenReturn(instance(oAuthService));

beforeEach(() => {
TestBed.configureTestingModule({
Expand Down
28 changes: 26 additions & 2 deletions src/app/core/services/token/token.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { HttpHeaders } from '@angular/common/http';
import { Injectable, Injector } from '@angular/core';
import { AuthConfig, OAuthInfoEvent, OAuthService, OAuthSuccessEvent, TokenResponse } from 'angular-oauth2-oidc';
import {
AuthConfig,
OAuthInfoEvent,
OAuthService,
OAuthStorage,
OAuthSuccessEvent,
TokenResponse,
} from 'angular-oauth2-oidc';
import { BehaviorSubject, Observable, filter, first, from, map, noop, switchMap, take } from 'rxjs';

import { FetchTokenOptions, GrantType } from 'ish-core/models/token/token.interface';
Expand All @@ -9,13 +16,30 @@ import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { InstanceCreators } from 'ish-core/utils/instance-creators';
import { whenTruthy } from 'ish-core/utils/operators';

function storageFactory(): OAuthStorage {
const prefix = 'icm_' as const;
if (!SSR) {
return {
getItem(key: string): string {
return localStorage.getItem(`${prefix}${key}`);
},
removeItem(key: string): void {
return localStorage.removeItem(`${prefix}${key}`);
},
setItem(key: string, data: string): void {
return localStorage.setItem(`${prefix}${key}`, data);
},
};
}
}

@Injectable({ providedIn: 'root' })
export class TokenService {
private oAuthService: OAuthService;
private serviceConfigured$ = new BehaviorSubject<boolean>(false);

constructor(private apiService: ApiService, private apiTokenService: ApiTokenService, parent: Injector) {
this.oAuthService = InstanceCreators.getOAuthServiceInstance(parent);
this.oAuthService = InstanceCreators.getOAuthServiceInstance(parent, storageFactory);

this.apiService
.constructUrlForPath('token', {
Expand Down
12 changes: 9 additions & 3 deletions src/app/core/utils/instance-creators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Injector } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { OAuthService, OAuthStorage } from 'angular-oauth2-oidc';

export class InstanceCreators {
static getOAuthServiceInstance(parent: Injector): OAuthService {
const injector = Injector.create({ providers: [{ provide: OAuthService }], parent });
static getOAuthServiceInstance(parent: Injector, storageFactory?: () => OAuthStorage): OAuthService {
const injector = Injector.create({
providers: [
...(storageFactory ? [{ provide: OAuthStorage, useFactory: storageFactory }] : []),
{ provide: OAuthService },
],
parent,
});
return injector.get(OAuthService);
}
}

0 comments on commit f3e940f

Please sign in to comment.