Fix missing onLogin event

This commit is contained in:
Andras Popovics
2023-08-31 10:27:07 +02:00
committed by eromano
parent 4232d04c2e
commit 0e40dad295
6 changed files with 73 additions and 22 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"name": "e2e", "name": "e2e",
"program": "${workspaceFolder}/node_modules/protractor/bin/protractor", "program": "${workspaceFolder}/node_modules/protractor/bin/protractor",
"args": [ "args": [
"`${workspaceFolder}/.vscode/closest-config-finder.sh ${file} e2e/protractor.conf.js`", "./e2e/protractor.conf.js",
"--specs=${file}" "--specs=${file}"
], ],
"envFile": "${workspaceFolder}/.env", "envFile": "${workspaceFolder}/.env",
@@ -17,6 +17,7 @@
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import ee from 'event-emitter'; import ee from 'event-emitter';
import { Observable } from 'rxjs';
export interface AuthenticationServiceInterface { export interface AuthenticationServiceInterface {
@@ -53,5 +54,7 @@ export interface AuthenticationServiceInterface {
getAuthHeaders(requestUrl: string, header: HttpHeaders): HttpHeaders; getAuthHeaders(requestUrl: string, header: HttpHeaders): HttpHeaders;
addTokenToHeader(requestUrl: string, headersArg?: HttpHeaders): Observable<HttpHeaders>;
reset(): void; reset(): void;
} }
@@ -22,6 +22,8 @@ import { Observable } from 'rxjs';
* Provide authentication/authorization through OAuth2/OIDC protocol. * Provide authentication/authorization through OAuth2/OIDC protocol.
*/ */
export abstract class AuthService { export abstract class AuthService {
abstract onLogin: Observable<any>;
/** Subscribe to whether the user has valid Id/Access tokens. */ /** Subscribe to whether the user has valid Id/Access tokens. */
abstract authenticated$: Observable<boolean>; abstract authenticated$: Observable<boolean>;
@@ -19,13 +19,16 @@ 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 } 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, startWith } from 'rxjs/operators'; import { distinctUntilChanged, filter, map, shareReplay } from 'rxjs/operators';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
const isPromise = <T>(value: T | Promise<T>): value is Promise<T> => value && typeof (value as Promise<T>).then === 'function'; const isPromise = <T>(value: T | Promise<T>): value is Promise<T> => value && typeof (value as Promise<T>).then === 'function';
@Injectable() @Injectable()
export class RedirectAuthService extends AuthService { export class RedirectAuthService extends AuthService {
onLogin: Observable<any>;
private _loadDiscoveryDocumentPromise = Promise.resolve(false); private _loadDiscoveryDocumentPromise = Promise.resolve(false);
/** Subscribe to whether the user has valid Id/Access tokens. */ /** Subscribe to whether the user has valid Id/Access tokens. */
@@ -48,29 +51,32 @@ export class RedirectAuthService extends AuthService {
) { ) {
super(); super();
this.authConfig = authConfig; this.authConfig = authConfig;
}
init() {
this.oauthService.clearHashAfterLogin = true; this.oauthService.clearHashAfterLogin = true;
this.authenticated$ = this.oauthService.events.pipe( this.authenticated$ = this.oauthService.events.pipe(
startWith(undefined),
map(() => this.authenticated), map(() => this.authenticated),
distinctUntilChanged(), distinctUntilChanged(),
shareReplay(1) shareReplay(1)
); );
this.onLogin = this.authenticated$.pipe(
filter((authenticated) => authenticated),
map(() => undefined)
);
this.idpUnreachable$ = this.oauthService.events.pipe( this.idpUnreachable$ = this.oauthService.events.pipe(
filter((event): event is OAuthErrorEvent => event.type === 'discovery_document_load_error'), filter((event): event is OAuthErrorEvent => event.type === 'discovery_document_load_error'),
map((event) => event.reason as Error) map((event) => event.reason as Error)
); );
}
init() {
if (isPromise(this.authConfig)) { if (isPromise(this.authConfig)) {
return this.authConfig.then((config) => this.configureAuth(config)); return this.authConfig.then((config) => this.configureAuth(config));
} }
return this.configureAuth(this.authConfig); return this.configureAuth(this.authConfig);
} }
logout() { logout() {
@@ -18,28 +18,64 @@
import { Injectable, Injector } from '@angular/core'; import { Injectable, Injector } from '@angular/core';
import { OidcAuthenticationService } from './oidc-authentication.service'; import { OidcAuthenticationService } from './oidc-authentication.service';
import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service'; import { BasicAlfrescoAuthService } from '../basic-auth/basic-alfresco-auth.service';
import { Observable, from } from 'rxjs'; import { Observable, from, merge } from 'rxjs';
import { BaseAuthenticationService } from './base-authentication.service';
import { AppConfigService } from '../../app-config';
import { CookieService, LogService } from '../../common';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { tap } from 'rxjs/operators'; import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
import ee from 'event-emitter';
import { RedirectAuthService } from '../oidc/redirect-auth.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthenticationService extends BaseAuthenticationService { export class AuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
constructor(appConfig: AppConfigService, onLogin: Observable<any>;
cookie: CookieService,
logService: LogService,
private injector: Injector) {
super(appConfig, cookie, logService);
(this.isOauth() ? this.oidcAuthenticationService.onLogin : this.basicAlfrescoAuthService.onLogin) constructor(
.pipe( private injector: Injector,
tap(() => this.onLogin.next()) private redirectAuthService: RedirectAuthService
).subscribe(); ) {
this.onLogin = merge(this.redirectAuthService.onLogin, this.basicAlfrescoAuthService.onLogin);
}
get on(): ee.EmitterMethod {
return this.isOauth() ? this.oidcAuthenticationService.on : this.basicAlfrescoAuthService.on;
}
get off(): ee.EmitterMethod {
return this.isOauth() ? this.oidcAuthenticationService.off : this.basicAlfrescoAuthService.off;
}
get once(): ee.EmitterMethod {
return this.isOauth() ? this.oidcAuthenticationService.once : this.basicAlfrescoAuthService.once;
}
get emit(): (type: string, ...args: any[]) => void {
return this.isOauth() ? this.oidcAuthenticationService.emit : this.basicAlfrescoAuthService.emit;
}
get onLogout(): Observable<any> {
return this.isOauth() ? this.oidcAuthenticationService.onLogout : this.basicAlfrescoAuthService.onLogout;
}
get onError(): Observable<any> {
return this.isOauth() ? this.oidcAuthenticationService.onError : this.basicAlfrescoAuthService.onError;
}
addTokenToHeader(requestUrl: string, headersArg?: HttpHeaders): Observable<HttpHeaders> {
return this.isOauth() ? this.oidcAuthenticationService.addTokenToHeader(requestUrl, headersArg) : from([headersArg]);
}
isECMProvider(): boolean {
return this.isOauth() ? this.oidcAuthenticationService.isECMProvider() : this.basicAlfrescoAuthService.isECMProvider();
}
isBPMProvider(): boolean {
return this.isOauth() ? this.oidcAuthenticationService.isBPMProvider() : this.basicAlfrescoAuthService.isBPMProvider();
}
isALLProvider(): boolean {
return this.isOauth() ? this.oidcAuthenticationService.isALLProvider() : this.basicAlfrescoAuthService.isALLProvider();
} }
private get oidcAuthenticationService(): OidcAuthenticationService { private get oidcAuthenticationService(): OidcAuthenticationService {
@@ -121,4 +157,8 @@ export class AuthenticationService extends BaseAuthenticationService {
return this.basicAlfrescoAuthService.getAuthHeaders(requestUrl, headers); return this.basicAlfrescoAuthService.getAuthHeaders(requestUrl, headers);
} }
} }
isOauth(): boolean {
return this.basicAlfrescoAuthService.isOauth();
}
} }
@@ -24,7 +24,7 @@ import { LogService } from '../../common/services/log.service';
import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface'; import { AuthenticationServiceInterface } from '../interfaces/authentication-service.interface';
import ee from 'event-emitter'; import ee from 'event-emitter';
export abstract class BaseAuthenticationService implements AuthenticationServiceInterface, ee.Emitter { export abstract class BaseAuthenticationService implements AuthenticationServiceInterface, ee.Emitter {
on: ee.EmitterMethod; on: ee.EmitterMethod;
off: ee.EmitterMethod; off: ee.EmitterMethod;