From decfb95fbdee71046b2b40b3a41b28bdef10c458 Mon Sep 17 00:00:00 2001 From: Lukas Spirig Date: Tue, 17 Sep 2024 15:08:47 +0000 Subject: [PATCH 1/2] feat: add guard `autoLoginPartialRoutesGuardWithConfig` for specific configurations --- .../auto-login-partial-routes.guard.spec.ts | 54 +++++++++++++++++++ .../auto-login-partial-routes.guard.ts | 18 +++++-- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.spec.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.spec.ts index 206d925b..672e12b9 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.spec.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.spec.ts @@ -15,6 +15,7 @@ import { StoragePersistenceService } from '../storage/storage-persistence.servic import { AutoLoginPartialRoutesGuard, autoLoginPartialRoutesGuard, + autoLoginPartialRoutesGuardWithConfig, } from './auto-login-partial-routes.guard'; import { AutoLoginService } from './auto-login.service'; @@ -498,5 +499,58 @@ describe(`AutoLoginPartialRoutesGuard`, () => { }); })); }); + + describe('autoLoginPartialRoutesGuardWithConfig', () => { + let loginService: LoginService; + let authStateService: AuthStateService; + let storagePersistenceService: StoragePersistenceService; + let configurationService: ConfigurationService; + let autoLoginService: AutoLoginService; + + beforeEach(() => { + authStateService = TestBed.inject(AuthStateService); + loginService = TestBed.inject(LoginService); + storagePersistenceService = TestBed.inject(StoragePersistenceService); + configurationService = TestBed.inject(ConfigurationService); + + spyOn(configurationService, 'getOpenIDConfiguration').and.callFake( + (configId) => of({ configId }) + ); + + autoLoginService = TestBed.inject(AutoLoginService); + }); + + afterEach(() => { + storagePersistenceService.clear({}); + }); + + it('should save current route (empty) and call `login` if not authenticated already', waitForAsync(() => { + spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue( + false + ); + const checkSavedRedirectRouteAndNavigateSpy = spyOn( + autoLoginService, + 'checkSavedRedirectRouteAndNavigate' + ); + const saveRedirectRouteSpy = spyOn( + autoLoginService, + 'saveRedirectRoute' + ); + const loginSpy = spyOn(loginService, 'login'); + + const guard$ = TestBed.runInInjectionContext( + autoLoginPartialRoutesGuardWithConfig('configId1') + ); + + guard$.subscribe(() => { + expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith( + { configId: 'configId1' }, + '' + ); + expect(loginSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' }); + expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled(); + }); + })); + }); }); }); diff --git a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts index 3626c587..b88527ae 100644 --- a/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts +++ b/projects/angular-auth-oidc-client/src/lib/auto-login/auto-login-partial-routes.guard.ts @@ -78,7 +78,8 @@ export class AutoLoginPartialRoutesGuard { } export function autoLoginPartialRoutesGuard( - route?: ActivatedRouteSnapshot + route?: ActivatedRouteSnapshot, + configId?: string ): Observable { const configurationService = inject(ConfigurationService); const authStateService = inject(AuthStateService); @@ -98,19 +99,28 @@ export function autoLoginPartialRoutesGuard( authStateService, autoLoginService, loginService, - authOptions + authOptions, + configId ); } +export function autoLoginPartialRoutesGuardWithConfig( + configId: string +): (route?: ActivatedRouteSnapshot) => Observable { + return (route?: ActivatedRouteSnapshot) => + autoLoginPartialRoutesGuard(route, configId); +} + function checkAuth( url: string, configurationService: ConfigurationService, authStateService: AuthStateService, autoLoginService: AutoLoginService, loginService: LoginService, - authOptions?: AuthOptions + authOptions?: AuthOptions, + configId?: string ): Observable { - return configurationService.getOpenIDConfiguration().pipe( + return configurationService.getOpenIDConfiguration(configId).pipe( map((configuration) => { const isAuthenticated = authStateService.areAuthStorageTokensValid(configuration); From 51d4fb706645f865e36e4559443dd350c2cd88fb Mon Sep 17 00:00:00 2001 From: Lukas Spirig Date: Wed, 18 Sep 2024 12:21:46 +0200 Subject: [PATCH 2/2] build: update GitHub actions --- .github/workflows/build.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9b38ec32..3d08c6cd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -55,7 +55,7 @@ jobs: run: ls - name: Upload Artefact - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: angular_auth_oidc_client_artefact path: dist/angular-auth-oidc-client @@ -71,7 +71,7 @@ jobs: node-version: 20 - name: Download Artefact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: angular_auth_oidc_client_artefact path: angular-auth-oidc-client-artefact @@ -111,7 +111,7 @@ jobs: node-version: 20 - name: Download Artefact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: angular_auth_oidc_client_artefact path: angular-auth-oidc-client-artefact @@ -151,7 +151,7 @@ jobs: node-version: 20 - name: Download Artefact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: angular_auth_oidc_client_artefact path: angular-auth-oidc-client-artefact @@ -191,7 +191,7 @@ jobs: node-version: 20 - name: Download Artefact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: angular_auth_oidc_client_artefact path: angular-auth-oidc-client-artefact @@ -235,7 +235,7 @@ jobs: node-version: 18 - name: Download Artefact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v3 with: name: angular_auth_oidc_client_artefact path: angular-auth-oidc-client-artefact