mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
AAE-25816 Provide a separate storage instance for the AuthModule to allow setting a custom storage prefix for authentication data stored in local storage
This commit is contained in:
parent
59818226aa
commit
5b0c0affd5
@ -20,6 +20,7 @@ import { InjectionToken } from '@angular/core';
|
|||||||
export interface AuthModuleConfig {
|
export interface AuthModuleConfig {
|
||||||
readonly useHash: boolean;
|
readonly useHash: boolean;
|
||||||
preventClearHashAfterLogin?: boolean;
|
preventClearHashAfterLogin?: boolean;
|
||||||
|
overrideAuthStoragePrefix?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AUTH_MODULE_CONFIG = new InjectionToken<AuthModuleConfig>('AUTH_MODULE_CONFIG');
|
export const AUTH_MODULE_CONFIG = new InjectionToken<AuthModuleConfig>('AUTH_MODULE_CONFIG');
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { APP_INITIALIZER, ModuleWithProviders, NgModule } from '@angular/core';
|
import { APP_INITIALIZER, EnvironmentProviders, ModuleWithProviders, NgModule, Provider } from '@angular/core';
|
||||||
import { AUTH_CONFIG, OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
|
import { AUTH_CONFIG, OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
|
||||||
import { AuthenticationService } from '../services/authentication.service';
|
import { AuthenticationService } from '../services/authentication.service';
|
||||||
import { StorageService } from '../../common/services/storage.service';
|
import { StorageService } from '../../common/services/storage.service';
|
||||||
@ -25,6 +25,8 @@ import { AuthRoutingModule } from './auth-routing.module';
|
|||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { RedirectAuthService } from './redirect-auth.service';
|
import { RedirectAuthService } from './redirect-auth.service';
|
||||||
import { AuthenticationConfirmationComponent } from './view/authentication-confirmation/authentication-confirmation.component';
|
import { AuthenticationConfirmationComponent } from './view/authentication-confirmation/authentication-confirmation.component';
|
||||||
|
import { CustomAuthStorageService } from '../services/custom-auth-storage.service';
|
||||||
|
import { STORAGE_SERVICE } from '../../common/interface/storage-service.interface';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a Login Factory function
|
* Create a Login Factory function
|
||||||
@ -41,7 +43,7 @@ export function loginFactory(redirectService: RedirectAuthService): () => Promis
|
|||||||
imports: [AuthRoutingModule, OAuthModule.forRoot()],
|
imports: [AuthRoutingModule, OAuthModule.forRoot()],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: OAuthStorage, useExisting: StorageService },
|
{ provide: OAuthStorage, useExisting: StorageService },
|
||||||
{ provide: AuthenticationService},
|
{ provide: AuthenticationService },
|
||||||
{
|
{
|
||||||
provide: AUTH_CONFIG,
|
provide: AUTH_CONFIG,
|
||||||
useFactory: authConfigFactory,
|
useFactory: authConfigFactory,
|
||||||
@ -58,11 +60,18 @@ export function loginFactory(redirectService: RedirectAuthService): () => Promis
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class AuthModule {
|
export class AuthModule {
|
||||||
static forRoot(config: AuthModuleConfig = { useHash: false }): ModuleWithProviders<AuthModule> {
|
static forRoot(config: AuthModuleConfig = { useHash: false, overrideAuthStoragePrefix: false }): ModuleWithProviders<AuthModule> {
|
||||||
config.preventClearHashAfterLogin = config.preventClearHashAfterLogin ?? true;
|
config.preventClearHashAfterLogin = config.preventClearHashAfterLogin ?? true;
|
||||||
|
const providers: (Provider | EnvironmentProviders)[] = [
|
||||||
|
{ provide: AUTH_MODULE_CONFIG, useValue: config }
|
||||||
|
];
|
||||||
|
if(config.overrideAuthStoragePrefix){
|
||||||
|
providers.push({ provide: OAuthStorage, useClass: CustomAuthStorageService });
|
||||||
|
providers.push({ provide: STORAGE_SERVICE, useExisting: OAuthStorage });
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
ngModule: AuthModule,
|
ngModule: AuthModule,
|
||||||
providers: [{ provide: AUTH_MODULE_CONFIG, useValue: config }]
|
providers
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ export * from './services/identity-group.service';
|
|||||||
export * from './services/jwt-helper.service';
|
export * from './services/jwt-helper.service';
|
||||||
export * from './services/oauth2.service';
|
export * from './services/oauth2.service';
|
||||||
export * from './services/user-access.service';
|
export * from './services/user-access.service';
|
||||||
|
export * from './services/custom-auth-storage.service';
|
||||||
|
|
||||||
export * from './basic-auth/basic-alfresco-auth.service';
|
export * from './basic-auth/basic-alfresco-auth.service';
|
||||||
export * from './basic-auth/process-auth';
|
export * from './basic-auth/process-auth';
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { of } from 'rxjs';
|
||||||
|
import { CUSTOM_AUTH_STORAGE_PREFIX, CustomAuthStorageService } from './custom-auth-storage.service';
|
||||||
|
|
||||||
|
describe('CustomAuthStorageService', () => {
|
||||||
|
let service: CustomAuthStorageService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
CustomAuthStorageService,
|
||||||
|
{ provide: CUSTOM_AUTH_STORAGE_PREFIX, useValue: of('custom-prefix') }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
service = TestBed.inject(CustomAuthStorageService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set the prefix from the observable', () => {
|
||||||
|
const expectedPrefix = 'custom-prefix_';
|
||||||
|
expect(service.prefix).toBe(expectedPrefix);
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,33 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Inject, Injectable, InjectionToken } from '@angular/core';
|
||||||
|
import { StorageService } from '../../common/services/storage.service';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
export const CUSTOM_AUTH_STORAGE_PREFIX = new InjectionToken<any>('CUSTOM_AUTH_STORAGE_PREFIX');
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CustomAuthStorageService extends StorageService {
|
||||||
|
|
||||||
|
constructor(@Inject(CUSTOM_AUTH_STORAGE_PREFIX) customAuthStoragePrefix$: Observable<string>) {
|
||||||
|
super();
|
||||||
|
customAuthStoragePrefix$.subscribe(prefix => {
|
||||||
|
this.prefix = prefix;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,7 @@
|
|||||||
import { JwtHelperService } from './jwt-helper.service';
|
import { JwtHelperService } from './jwt-helper.service';
|
||||||
import { mockToken } from '../mock/jwt-helper.service.spec';
|
import { mockToken } from '../mock/jwt-helper.service.spec';
|
||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { STORAGE_SERVICE } from '../../common';
|
||||||
|
|
||||||
describe('JwtHelperService', () => {
|
describe('JwtHelperService', () => {
|
||||||
|
|
||||||
@ -25,7 +26,10 @@ describe('JwtHelperService', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
providers: [JwtHelperService]
|
providers: [
|
||||||
|
JwtHelperService,
|
||||||
|
{ provide: STORAGE_SERVICE, useValue: {} }
|
||||||
|
]
|
||||||
});
|
});
|
||||||
jwtHelperService = TestBed.inject(JwtHelperService);
|
jwtHelperService = TestBed.inject(JwtHelperService);
|
||||||
});
|
});
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Inject, Injectable } from '@angular/core';
|
||||||
import { StorageService } from '../../common/services/storage.service';
|
import { STORAGE_SERVICE, StorageServiceInterface } from '../../common/interface/storage-service.interface';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@ -34,8 +34,7 @@ export class JwtHelperService {
|
|||||||
static USER_PREFERRED_USERNAME = 'preferred_username';
|
static USER_PREFERRED_USERNAME = 'preferred_username';
|
||||||
static HXP_AUTHORIZATION = 'hxp_authorization';
|
static HXP_AUTHORIZATION = 'hxp_authorization';
|
||||||
|
|
||||||
constructor(private storageService: StorageService) {
|
constructor(@Inject(STORAGE_SERVICE) private storageService: StorageServiceInterface) {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decodes a JSON web token into a JS object.
|
* Decodes a JSON web token into a JS object.
|
||||||
|
@ -32,6 +32,7 @@ export * from './models/log-levels.model';
|
|||||||
export * from './models/user-info-mode.enum';
|
export * from './models/user-info-mode.enum';
|
||||||
|
|
||||||
export * from './interface/search-component.interface';
|
export * from './interface/search-component.interface';
|
||||||
|
export * from './interface/storage-service.interface';
|
||||||
|
|
||||||
export * from './mock/app-config.service.mock';
|
export * from './mock/app-config.service.mock';
|
||||||
|
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { InjectionToken } from '@angular/core';
|
||||||
|
|
||||||
|
export const STORAGE_SERVICE = new InjectionToken<StorageServiceInterface>('STORAGE_SERVICE');
|
||||||
|
|
||||||
|
export interface StorageServiceInterface {
|
||||||
|
prefix: string;
|
||||||
|
getItem(key: string): string | null;
|
||||||
|
setItem(key: string, data: string): void;
|
||||||
|
clear(): void;
|
||||||
|
removeItem(key: string): void;
|
||||||
|
hasItem(key: string): boolean;
|
||||||
|
}
|
@ -16,11 +16,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { StorageServiceInterface } from '../interface/storage-service.interface';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class StorageService {
|
export class StorageService implements StorageServiceInterface {
|
||||||
private memoryStore: { [key: string]: any } = {};
|
private memoryStore: { [key: string]: any } = {};
|
||||||
private readonly useLocalStorage: boolean = false;
|
private readonly useLocalStorage: boolean = false;
|
||||||
private _prefix: string = '';
|
private _prefix: string = '';
|
||||||
|
@ -50,6 +50,7 @@ import { MAT_SNACK_BAR_DEFAULT_OPTIONS } from '@angular/material/snack-bar';
|
|||||||
import { loadAppConfig } from './app-config/app-config.loader';
|
import { loadAppConfig } from './app-config/app-config.loader';
|
||||||
import { AppConfigService } from './app-config/app-config.service';
|
import { AppConfigService } from './app-config/app-config.service';
|
||||||
import { StorageService } from './common/services/storage.service';
|
import { StorageService } from './common/services/storage.service';
|
||||||
|
import { STORAGE_SERVICE } from './common/interface/storage-service.interface';
|
||||||
import { MomentDateAdapter } from './common/utils/moment-date-adapter';
|
import { MomentDateAdapter } from './common/utils/moment-date-adapter';
|
||||||
import { AppConfigPipe, StoragePrefixFactory } from './app-config';
|
import { AppConfigPipe, StoragePrefixFactory } from './app-config';
|
||||||
import { IconComponent } from './icon';
|
import { IconComponent } from './icon';
|
||||||
@ -138,6 +139,7 @@ export class CoreModule {
|
|||||||
TranslateService,
|
TranslateService,
|
||||||
{ provide: TranslateLoader, useClass: TranslateLoaderService },
|
{ provide: TranslateLoader, useClass: TranslateLoaderService },
|
||||||
MomentDateAdapter,
|
MomentDateAdapter,
|
||||||
|
{ provide: STORAGE_SERVICE, useExisting: StorageService },
|
||||||
StoragePrefixFactory,
|
StoragePrefixFactory,
|
||||||
{
|
{
|
||||||
provide: APP_INITIALIZER,
|
provide: APP_INITIALIZER,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user