Skip to content

Commit

Permalink
feat: angular custom auth (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
anninowak authored Jun 5, 2024
1 parent a5be70c commit e270909
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
46 changes: 27 additions & 19 deletions libs/angular-auth/src/lib/auth-service-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -41,32 +42,39 @@ export class AuthServiceWrapper {

async initializeAuthService(): Promise<void> {
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<AuthServiceFactory> {
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
}
}
1 change: 1 addition & 0 deletions libs/angular-auth/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e270909

Please sign in to comment.