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

fix: introduce AuthProxyService to reduce token calls #577

Merged
merged 1 commit into from
Nov 4, 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
34 changes: 27 additions & 7 deletions libs/angular-auth/src/lib/angular-auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TokenInterceptor } from './token.interceptor'
import { AuthService } from './auth.service'
import { AuthServiceWrapper } from './auth-service-wrapper'
import { KeycloakAuthService } from './auth_services/keycloak-auth.service'
import { AuthProxyService } from './auth-proxy.service'
import { KeycloakService } from 'keycloak-angular'

function appInitializer(configService: ConfigurationService, authService: AuthService) {
Expand All @@ -15,19 +16,38 @@ function appInitializer(configService: ConfigurationService, authService: AuthSe
}
}

@NgModule({
imports: [CommonModule],
providers: [
AuthServiceWrapper,
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true },
function provideAuthServices() {
return [AuthServiceWrapper, KeycloakAuthService, KeycloakService]
}

export function provideAuthService() {
return [
provideAuthServices(),
{
provide: APP_INITIALIZER,
useFactory: appInitializer,
deps: [ConfigurationService, AuthServiceWrapper],
multi: true,
},
KeycloakAuthService,
KeycloakService,
]
}

export function provideTokenInterceptor() {
return [
AuthProxyService,
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true,
},
]
}

@NgModule({
imports: [CommonModule],
providers: [
provideTokenInterceptor(),
provideAuthServices(), // Only needed as fallback if shell uses lib version without new auth mechanism
],
})
export class AngularAuthModule {}
29 changes: 29 additions & 0 deletions libs/angular-auth/src/lib/auth-proxy.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable, inject } from '@angular/core'
import './declarations'
import { AuthServiceWrapper } from './auth-service-wrapper'

@Injectable()
export class AuthProxyService {
authServiceWrapper?: AuthServiceWrapper | null

getHeaderValues(): Record<string, string> {
return (
window.onecxAngularAuth?.authServiceProxy?.v1?.getHeaderValues() ??
this.authServiceWrapper?.getHeaderValues() ??
{}
)
}

async updateTokenIfNeeded(): Promise<boolean> {
if (!window.onecxAngularAuth?.authServiceProxy?.v1?.updateTokenIfNeeded) {
console.info('AuthProxyService uses injected fallback.')
this.authServiceWrapper = inject(AuthServiceWrapper, { optional: true })
await this.authServiceWrapper?.init()
}
return (
window.onecxAngularAuth?.authServiceProxy?.v1?.updateTokenIfNeeded() ??
this.authServiceWrapper?.updateTokenIfNeeded() ??
Promise.reject('No authServiceWrapper provided.')
)
}
}
11 changes: 11 additions & 0 deletions libs/angular-auth/src/lib/auth-service-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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'
import './declarations'

@Injectable()
export class AuthServiceWrapper {
Expand All @@ -20,6 +21,16 @@ export class AuthServiceWrapper {
this.eventsTopic$
.pipe(filter((e) => e.type === 'authentication#logoutButtonClicked'))
.subscribe(() => this.authService?.logout())
window.onecxAngularAuth ??= {}
window.onecxAngularAuth.authServiceProxy ??= {}
window.onecxAngularAuth.authServiceProxy.v1 ??= {
updateTokenIfNeeded: (): Promise<boolean> => {
return this.updateTokenIfNeeded()
},
getHeaderValues: (): Record<string, string> => {
return this.getHeaderValues()
},
}
}
async init(): Promise<boolean | undefined> {
await this.configService.isInitialized
Expand Down
14 changes: 14 additions & 0 deletions libs/angular-auth/src/lib/declarations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare global {
interface Window {
onecxAngularAuth?: {
authServiceProxy?: {
v1?: {
getHeaderValues: () => Record<string, string>,
updateTokenIfNeeded: () => Promise<boolean>
}
}
}
}
}

export default globalThis
4 changes: 2 additions & 2 deletions libs/angular-auth/src/lib/token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Observable, from, mergeMap } from 'rxjs'
import { AuthServiceWrapper } from './auth-service-wrapper'
import { AuthProxyService } from './auth-proxy.service'

const WHITELIST = ['assets']

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private authService: AuthServiceWrapper) {}
constructor(private authService: AuthProxyService) {}

intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
const skip = WHITELIST.some((str) => request.url.includes(str))
Expand Down
Loading