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 {
if (this.isOauth()) {
return this.oidcAuthenticationService.getEcmUsername();
@@ -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<string>(JwtHelperService.USER_PREFERRED_USERNAME);
}
@@ -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<string>(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<void> {
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();