Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: angular custom auth #269

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading