[AAE-12531] Fix window.location.search is empty when loginCallback is called

This commit is contained in:
Amedeo Lepore
2023-12-21 20:09:41 +01:00
parent 8ea24e5e6d
commit 51de25d2b7
5 changed files with 15 additions and 15 deletions

View File

@@ -18,9 +18,10 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AuthenticationConfirmationComponent } from './view/authentication-confirmation/authentication-confirmation.component'; import { AuthenticationConfirmationComponent } from './view/authentication-confirmation/authentication-confirmation.component';
import { OidcAuthGuard } from './oidc-auth.guard';
const routes: Routes = [ const routes: Routes = [
{ path: 'view/authentication-confirmation', component: AuthenticationConfirmationComponent } { path: 'view/authentication-confirmation', component: AuthenticationConfirmationComponent, canActivate: [OidcAuthGuard]}
]; ];
@NgModule({ @NgModule({

View File

@@ -61,7 +61,8 @@ export function loginFactory(oAuthService: OAuthService, storage: OAuthStorage,
useFactory: loginFactory, useFactory: loginFactory,
deps: [OAuthService, OAuthStorage, AUTH_CONFIG], deps: [OAuthService, OAuthStorage, AUTH_CONFIG],
multi: true multi: true
} },
OidcAuthGuard
] ]
}) })
export class AuthModule { export class AuthModule {

View File

@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { TokenResponse } from 'angular-oauth2-oidc'; import { LoginOptions, TokenResponse } from 'angular-oauth2-oidc';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
/** /**
@@ -54,6 +54,6 @@ export abstract class AuthService {
* *
* @returns Promise, resolve with stored state, reject if unable to reach IdP * @returns Promise, resolve with stored state, reject if unable to reach IdP
*/ */
abstract loginCallback(): Promise<string | undefined>; abstract loginCallback(loginOptions?: LoginOptions): Promise<string | undefined>;
abstract updateIDPConfiguration(...args: any[]): void; abstract updateIDPConfiguration(...args: any[]): void;
} }

View File

@@ -16,7 +16,7 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router'; import { CanActivate, UrlTree } from '@angular/router';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
@@ -25,22 +25,20 @@ export class OidcAuthGuard implements CanActivate {
constructor(private auth: AuthService) {} constructor(private auth: AuthService) {}
canActivate( canActivate(
_route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this._isAuthenticated(state); return this._isAuthenticated();
} }
canActivateChild(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { canActivateChild() {
return this._isAuthenticated(state); return this._isAuthenticated();
} }
private _isAuthenticated(state: RouterStateSnapshot) { private _isAuthenticated() {
if (this.auth.authenticated) { if (this.auth.authenticated) {
return true; return true;
} }
const loginResult = this.auth.login(state.url); const loginResult = this.auth.loginCallback({customHashFragment: window.location.search});
if (loginResult instanceof Promise) { if (loginResult instanceof Promise) {
return loginResult.then(() => true).catch(() => false); return loginResult.then(() => true).catch(() => false);

View File

@@ -16,7 +16,7 @@
*/ */
import { Inject, Injectable } from '@angular/core'; import { Inject, Injectable } from '@angular/core';
import { AuthConfig, AUTH_CONFIG, OAuthErrorEvent, OAuthService, OAuthStorage, TokenResponse } from 'angular-oauth2-oidc'; import { AuthConfig, AUTH_CONFIG, OAuthErrorEvent, OAuthService, OAuthStorage, TokenResponse, LoginOptions } from 'angular-oauth2-oidc';
import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks'; import { JwksValidationHandler } from 'angular-oauth2-oidc-jwks';
import { from, Observable } from 'rxjs'; import { from, Observable } from 'rxjs';
import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators';
@@ -127,9 +127,9 @@ export class RedirectAuthService extends AuthService {
); );
} }
async loginCallback(): Promise<string | undefined> { async loginCallback(loginOptions?: LoginOptions): Promise<string | undefined> {
return this.ensureDiscoveryDocument() return this.ensureDiscoveryDocument()
.then(() => this.oauthService.tryLogin({ preventClearHashAfterLogin: true })) .then(() => this.oauthService.tryLogin({ ...loginOptions, preventClearHashAfterLogin: true }))
.then(() => this._getRedirectUrl()); .then(() => this._getRedirectUrl());
} }