From e270909988ba1e85c5d91c4f5ce56a908ab562e0 Mon Sep 17 00:00:00 2001 From: Annika Nowak <139357202+anninowak@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:44:52 +0200 Subject: [PATCH] feat: angular custom auth (#269) --- .../src/lib/auth-service-wrapper.ts | 46 +++++++++++-------- libs/angular-auth/src/lib/auth.service.ts | 1 + .../src/lib/model/config-key.model.ts | 1 + package-lock.json | 4 +- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/libs/angular-auth/src/lib/auth-service-wrapper.ts b/libs/angular-auth/src/lib/auth-service-wrapper.ts index c64670c9..3a3c4966 100644 --- a/libs/angular-auth/src/lib/auth-service-wrapper.ts +++ b/libs/angular-auth/src/lib/auth-service-wrapper.ts @@ -5,6 +5,7 @@ import { AppStateService, CONFIG_KEY, ConfigurationService } from '@onecx/angula import { Injectable, Injector } from '@angular/core' import { KeycloakAuthService } from './auth_services/keycloak-auth.service' import { loadRemoteModule } from '@angular-architects/module-federation' +import { Config } from '@onecx/integration-interface' @Injectable() export class AuthServiceWrapper { @@ -41,32 +42,39 @@ export class AuthServiceWrapper { async initializeAuthService(): Promise { const serviceTypeConfig = this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE) ?? 'keycloak' - let factory - let module - const customUrl = this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE_CUSTOM_URL) + switch (serviceTypeConfig) { case 'keycloak': this.authService = this.injector.get(KeycloakAuthService) break - case 'custom': - if (!customUrl) { - throw new Error('URL of the custom auth service is not defined') - } - module = await loadRemoteModule({ - type: 'module', - remoteEntry: customUrl, - exposedModule: './CustomAuth', - }) - factory = module.default as AuthServiceFactory - this.authService = factory((injectable: Injectables) => { - if (injectable === Injectables.KEYCLOAK_AUTH_SERVICE) { - return this.injector.get(KeycloakAuthService) - } - throw new Error('unknown injectable type') - }) + case 'custom': { + const factory = await this.getAuthServiceFactory() + this.authService = factory((injectable: Injectables) => this.retrieveInjectables(injectable)) break + } default: throw new Error('Configured AuthService not found') } } + + retrieveInjectables(injectable: Injectables): KeycloakAuthService | Config | undefined { + if (injectable === Injectables.KEYCLOAK_AUTH_SERVICE) { + return this.injector.get(KeycloakAuthService) + } else if (injectable === Injectables.CONFIG) { + return this.configService.getConfig() + } + throw new Error('unknown injectable type') + } + + async getAuthServiceFactory(): Promise { + if (!this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE_CUSTOM_URL)) { + throw new Error('URL of the custom auth service is not defined') + } + const module = await loadRemoteModule({ + type: 'module', + remoteEntry: this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE_CUSTOM_URL) ?? '', + exposedModule: this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE_CUSTOM_MODULE_NAME) ?? './CustomAuth', + }) + return module.default as AuthServiceFactory + } } diff --git a/libs/angular-auth/src/lib/auth.service.ts b/libs/angular-auth/src/lib/auth.service.ts index 3fb1b8ce..27413c09 100644 --- a/libs/angular-auth/src/lib/auth.service.ts +++ b/libs/angular-auth/src/lib/auth.service.ts @@ -8,6 +8,7 @@ export interface AuthService { export enum Injectables { KEYCLOAK_AUTH_SERVICE = 'KEYCLOAK_AUTH_SERVICE', + CONFIG = 'CONFIG', } export type AuthServiceFactory = (injectorFunction: (injectable: Injectables) => unknown) => AuthService diff --git a/libs/angular-integration-interface/src/lib/model/config-key.model.ts b/libs/angular-integration-interface/src/lib/model/config-key.model.ts index 0146be2c..4c14ebaf 100644 --- a/libs/angular-integration-interface/src/lib/model/config-key.model.ts +++ b/libs/angular-integration-interface/src/lib/model/config-key.model.ts @@ -24,4 +24,5 @@ export enum CONFIG_KEY { IS_SHELL = 'IS_SHELL', AUTH_SERVICE = 'AUTH_SERVICE', AUTH_SERVICE_CUSTOM_URL = 'AUTH_SERVICE_CUSTOM_URL', + AUTH_SERVICE_CUSTOM_MODULE_NAME = 'AUTH_SERVICE_CUSTOM_MODULE_NAME', } diff --git a/package-lock.json b/package-lock.json index 6bf61240..76e24ca0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@onecx/onecx-portal-ui-libs", - "version": "4.24.0", + "version": "4.29.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@onecx/onecx-portal-ui-libs", - "version": "4.24.0", + "version": "4.29.0", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": {