From 8814b0b95c2d4a1746392cb8e57fb9acd4b96cef Mon Sep 17 00:00:00 2001 From: eromano Date: Wed, 26 Jul 2023 13:06:25 +0200 Subject: [PATCH] grant type password login --- .../auth/services/authentication.service.ts | 8 +++++++ .../services/oidc-authentication.service.ts | 23 +++++++++++++++++-- .../lib/login/components/login.component.ts | 11 +++++---- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/core/src/lib/auth/services/authentication.service.ts b/lib/core/src/lib/auth/services/authentication.service.ts index 0b0a3b76bb..5f0ccf6b38 100644 --- a/lib/core/src/lib/auth/services/authentication.service.ts +++ b/lib/core/src/lib/auth/services/authentication.service.ts @@ -134,6 +134,14 @@ export class AuthenticationService implements AuthenticationServiceInterface, ee } } + login(username: string, password: string, rememberMe?: boolean): Observable<{ type: string; ticket: any }> { + if (this.isOauth()) { + return this.oidcAuthenticationService.loginWithPassword(username, password); + } else { + return this.basicAlfrescoAuthService.login(username, password, rememberMe); + } + } + getEcmUsername(): string { if (this.isOauth()) { return this.oidcAuthenticationService.getEcmUsername(); diff --git a/lib/core/src/lib/auth/services/oidc-authentication.service.ts b/lib/core/src/lib/auth/services/oidc-authentication.service.ts index 99436533ca..366895bd08 100644 --- a/lib/core/src/lib/auth/services/oidc-authentication.service.ts +++ b/lib/core/src/lib/auth/services/oidc-authentication.service.ts @@ -17,7 +17,7 @@ import { Injectable } from '@angular/core'; import { OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; -import { EMPTY, Observable } from 'rxjs'; +import { EMPTY, Observable, defer } from 'rxjs'; import { catchError, map } from 'rxjs/operators'; import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service'; import { OauthConfigModel } from '../models/oauth-config.model'; @@ -72,7 +72,7 @@ export class OidcAuthenticationService extends BaseAuthenticationService { } hasValidIdToken(): boolean { - return this.oauthService.hasValidAccessToken(); + return this.oauthService.hasValidIdToken(); } isImplicitFlow() { @@ -98,6 +98,25 @@ export class OidcAuthenticationService extends BaseAuthenticationService { ); } + loginWithPassword(username: string, password: string): Observable<{ type: string; ticket: any }> { + // @ts-ignore + return defer(async () => { + try { + await this.oauthService.fetchTokenUsingPasswordFlowAndLoadUserProfile(username, password); + await this.oauthService.refreshToken(); + const accessToken = this.oauthService.getAccessToken(); + this.onLogin.next(accessToken); + + return { + type: this.appConfig.get(AppConfigValues.PROVIDERS), + ticket: accessToken + }; + } catch (err) { + throw this.handleError(err); + } + }); + } + getEcmUsername(): string { return this.jwtHelperService.getValueFromLocalToken(JwtHelperService.USER_PREFERRED_USERNAME); } diff --git a/lib/core/src/lib/login/components/login.component.ts b/lib/core/src/lib/login/components/login.component.ts index 4e2aeb266c..e913f30857 100644 --- a/lib/core/src/lib/login/components/login.component.ts +++ b/lib/core/src/lib/login/components/login.component.ts @@ -55,7 +55,7 @@ interface ValidationMessage { templateUrl: './login.component.html', styleUrls: ['./login.component.scss'], encapsulation: ViewEncapsulation.None, - host: { class: 'adf-login' } + host: {class: 'adf-login'} }) export class LoginComponent implements OnInit, OnDestroy { isPasswordShow: boolean = false; @@ -167,7 +167,7 @@ export class LoginComponent implements OnInit, OnDestroy { const url = params['redirectUrl']; const provider = this.appConfig.get(AppConfigValues.PROVIDERS); - this.basicAlfrescoAuthService.setRedirect({ provider, url }); + this.basicAlfrescoAuthService.setRedirect({provider, url}); }); } @@ -198,16 +198,17 @@ export class LoginComponent implements OnInit, OnDestroy { * * @param values */ - onSubmit(values: any): void { + async onSubmit(values: any): Promise { this.disableError(); const args = new LoginSubmitEvent({ - controls: { username: this.form.controls.username } + controls: {username: this.form.controls.username} }); this.executeSubmit.emit(args); if (!args.defaultPrevented) { this.actualLoginStep = LoginSteps.Checking; + this.performLogin(values); } } @@ -249,7 +250,7 @@ export class LoginComponent implements OnInit, OnDestroy { } performLogin(values: { username: string; password: string }) { - this.basicAlfrescoAuthService.login(values.username, values.password, this.rememberMe) + this.authService.login(values.username, values.password, this.rememberMe) .subscribe( async (token: any) => { const redirectUrl = this.basicAlfrescoAuthService.getRedirect();