grant type password login

This commit is contained in:
eromano
2023-08-31 10:27:07 +02:00
parent 3e22233002
commit 8814b0b95c
3 changed files with 35 additions and 7 deletions
@@ -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 { getEcmUsername(): string {
if (this.isOauth()) { if (this.isOauth()) {
return this.oidcAuthenticationService.getEcmUsername(); return this.oidcAuthenticationService.getEcmUsername();
@@ -17,7 +17,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { OAuthService, OAuthStorage } from 'angular-oauth2-oidc'; 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 { catchError, map } from 'rxjs/operators';
import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service'; import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service';
import { OauthConfigModel } from '../models/oauth-config.model'; import { OauthConfigModel } from '../models/oauth-config.model';
@@ -72,7 +72,7 @@ export class OidcAuthenticationService extends BaseAuthenticationService {
} }
hasValidIdToken(): boolean { hasValidIdToken(): boolean {
return this.oauthService.hasValidAccessToken(); return this.oauthService.hasValidIdToken();
} }
isImplicitFlow() { 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 { getEcmUsername(): string {
return this.jwtHelperService.getValueFromLocalToken<string>(JwtHelperService.USER_PREFERRED_USERNAME); return this.jwtHelperService.getValueFromLocalToken<string>(JwtHelperService.USER_PREFERRED_USERNAME);
} }
@@ -198,7 +198,7 @@ export class LoginComponent implements OnInit, OnDestroy {
* *
* @param values * @param values
*/ */
onSubmit(values: any): void { async onSubmit(values: any): Promise<void> {
this.disableError(); this.disableError();
const args = new LoginSubmitEvent({ const args = new LoginSubmitEvent({
@@ -208,6 +208,7 @@ export class LoginComponent implements OnInit, OnDestroy {
if (!args.defaultPrevented) { if (!args.defaultPrevented) {
this.actualLoginStep = LoginSteps.Checking; this.actualLoginStep = LoginSteps.Checking;
this.performLogin(values); this.performLogin(values);
} }
} }
@@ -249,7 +250,7 @@ export class LoginComponent implements OnInit, OnDestroy {
} }
performLogin(values: { username: string; password: string }) { 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( .subscribe(
async (token: any) => { async (token: any) => {
const redirectUrl = this.basicAlfrescoAuthService.getRedirect(); const redirectUrl = this.basicAlfrescoAuthService.getRedirect();