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

NAS-132648 / 25.04 / query param handling for auth tokens #11080

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/app/core/testing/utils/empty-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export class EmptyAuthService {
logout = getMissingInjectionErrorFactory(AuthService.name);
refreshUser = getMissingInjectionErrorFactory(AuthService.name);
loginWithToken = getMissingInjectionErrorFactory(AuthService.name);
setQueryTokenIfExists = getMissingInjectionErrorFactory(AuthService.name);
}
6 changes: 4 additions & 2 deletions src/app/pages/signin/store/signin.store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';
import { mockProvider } from '@ngneat/spectator/jest';
import { BehaviorSubject, firstValueFrom, of } from 'rxjs';
import { MockApiService } from 'app/core/testing/classes/mock-api.service';
import { getTestScheduler } from 'app/core/testing/utils/get-test-scheduler.utils';
import { mockCall, mockApi } from 'app/core/testing/utils/mock-api.utils';
import { mockApi, mockCall } from 'app/core/testing/utils/mock-api.utils';
import { FailoverDisabledReason } from 'app/enums/failover-disabled-reason.enum';
import { FailoverStatus } from 'app/enums/failover-status.enum';
import { LoginResult } from 'app/enums/login-result.enum';
Expand Down Expand Up @@ -66,6 +66,7 @@
},
},
},
mockProvider(ActivatedRoute, { snapshot: { queryParamMap: { get: jest.fn() } } }),
],
});

Expand All @@ -82,6 +83,7 @@
});
jest.spyOn(authService, 'loginWithToken').mockReturnValue(of(LoginResult.Success));
jest.spyOn(authService, 'clearAuthToken').mockReturnValue(null);
jest.spyOn(authService, 'setQueryTokenIfExists').mockReturnValue(undefined)

Check failure on line 86 in src/app/pages/signin/store/signin.store.spec.ts

View workflow job for this annotation

GitHub Actions / Validate code style

Missing semicolon
});

describe('selectors', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/app/pages/signin/store/signin.store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Router } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { ComponentStore } from '@ngrx/component-store';
import { Actions, ofType } from '@ngrx/effects';
Expand Down Expand Up @@ -81,6 +81,7 @@ export class SigninStore extends ComponentStore<SigninState> {
private authService: AuthService,
private updateService: UpdateService,
private actions$: Actions,
private activatedRoute: ActivatedRoute,
@Inject(WINDOW) private window: Window,
) {
super(initialState);
Expand Down Expand Up @@ -240,6 +241,9 @@ export class SigninStore extends ComponentStore<SigninState> {
}

private handleLoginWithToken(): Observable<LoginResult> {
aervin marked this conversation as resolved.
Show resolved Hide resolved
this.authService.setQueryTokenIfExists(
this.activatedRoute.snapshot.queryParamMap.get('token'),
);
return this.tokenLastUsedService.isTokenWithinTimeline$.pipe(take(1)).pipe(
filter((isTokenWithinTimeline) => {
if (!isTokenWithinTimeline) {
Expand Down
8 changes: 4 additions & 4 deletions src/app/services/auth/auth-guard.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { WINDOW } from 'app/helpers/window.helper';
import { AuthService } from 'app/services/auth/auth.service';
Expand All @@ -20,13 +20,13 @@ export class AuthGuardService {
});
}

canActivate(_: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
canActivate({ queryParams }: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.isAuthenticated) {
return true;
}

this.window.sessionStorage.setItem('redirectUrl', state.url);
this.router.navigate(['/signin']);
this.window.sessionStorage.setItem('redirectUrl', state.url.split('?')[0]);
this.router.navigate(['/signin'], { queryParams });

return false;
}
Expand Down
10 changes: 10 additions & 0 deletions src/app/services/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export class AuthService {
);
}

setQueryTokenIfExists(token: string | null): void {
if (!token || this.window.location.protocol !== 'https:') {
return;
}

this.token = token;
this.tokenLastUsedService.updateTokenLastUsed();
aervin marked this conversation as resolved.
Show resolved Hide resolved
this.latestTokenGenerated$.next(token);
}

loginWithToken(): Observable<LoginResult> {
if (!this.token) {
return of(LoginResult.NoToken);
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
provideRouter,
PreloadAllModules,
withComponentInputBinding,
withDebugTracing,
} from '@angular/router';
import { provideEffects } from '@ngrx/effects';
import { provideRouterStore } from '@ngrx/router-store';
Expand Down Expand Up @@ -121,6 +122,6 @@ bootstrapApplication(AppComponent, {
provideCharts(withDefaultRegisterables()),
provideHttpClient(withInterceptorsFromDi()),
provideAnimations(),
provideRouter(rootRoutes, withPreloading(PreloadAllModules), withComponentInputBinding()),
provideRouter(rootRoutes, withPreloading(PreloadAllModules), withComponentInputBinding(), withDebugTracing()),
],
});
Loading