175 lines
5.6 KiB
TypeScript
175 lines
5.6 KiB
TypeScript
/** @file
|
|
* @author Edouard DUPIN
|
|
* @copyright 2018, Edouard DUPIN, all right reserved
|
|
* @license PROPRIETARY (see license file)
|
|
*/
|
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { Location } from '@angular/common';
|
|
import { SessionService } from 'common/service';
|
|
import { createLoginState, createPasswordState, getLoginType, isNullOrUndefined } from 'common/utils';
|
|
import { AdminUserService, ApplicationService } from 'app/service';
|
|
import { SpecificTokenResponse } from 'app/service/application';
|
|
|
|
|
|
@Component({
|
|
selector: 'app-sign-in',
|
|
templateUrl: './sign-in.html',
|
|
styleUrls: ['./sign-in.less'],
|
|
})
|
|
export class SignInScene implements OnInit {
|
|
public loginState: boolean|string = false;
|
|
public login: string = '';
|
|
|
|
public passwordState: boolean|string = false;
|
|
public password: string = '';
|
|
|
|
public loginButtonDisabled: boolean = true;
|
|
|
|
public error: string = '';
|
|
public loginType: string = 'Username/E-mail';
|
|
|
|
public rememberMe: boolean = true;
|
|
public loginButtonIsClicked: boolean = false;
|
|
|
|
private ssoApplicationId: string | undefined;
|
|
private ssoReturnData: string | undefined;
|
|
private ssoApplicationReturnUrl: string | undefined;
|
|
public ssoReady: boolean = true;
|
|
|
|
constructor(
|
|
private router: Router,
|
|
private route: ActivatedRoute,
|
|
private locate: Location,
|
|
private sessionService: SessionService,
|
|
private applicationService: ApplicationService,
|
|
private adminUserService: AdminUserService
|
|
) {}
|
|
|
|
ngOnInit() {
|
|
const ssoApplicationId = this.route.snapshot.paramMap.get('applicationId');
|
|
if (isNullOrUndefined(ssoApplicationId)) {
|
|
this.ssoApplicationId = undefined;
|
|
} else {
|
|
this.ssoApplicationId = ssoApplicationId;
|
|
}
|
|
const ssoReturnData = this.route.snapshot.paramMap.get('dataReturn');
|
|
if (isNullOrUndefined(ssoReturnData)) {
|
|
this.ssoReturnData = undefined;
|
|
} else {
|
|
this.ssoReturnData = ssoReturnData;
|
|
}
|
|
// TODO: check auto-reconnection !!!
|
|
let self = this;
|
|
if (this.sessionService.islogged() == true && self.loginButtonIsClicked === false) {
|
|
// in theory it is the inly one case possible, the system loading page after retreiving session ....
|
|
if (!isNullOrUndefined(self.ssoApplicationId)) {
|
|
// detect an auto-relog...
|
|
self.transferToApplicationThatRequiredTheSSO();
|
|
}
|
|
} else {
|
|
this.sessionService.change.subscribe(isConnected => {
|
|
console.log(
|
|
`receive event from session ...${isConnected} , ssoApplicationI=${self.ssoApplicationId}, loginButtonIsClicked=${self.loginButtonIsClicked}`
|
|
);
|
|
if (isConnected === true && self.loginButtonIsClicked === false) {
|
|
if (!isNullOrUndefined(self.ssoApplicationId)) {
|
|
// detect an auto-relog...
|
|
self.transferToApplicationThatRequiredTheSSO();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
updateButtonVisibility(): void {
|
|
if (this.loginState === true && this.passwordState === true) {
|
|
this.loginButtonDisabled = false;
|
|
} else {
|
|
this.loginButtonDisabled = true;
|
|
}
|
|
this.error = '';
|
|
}
|
|
|
|
checkRememberMe(newValue: boolean): void {
|
|
this.rememberMe = newValue;
|
|
this.updateButtonVisibility();
|
|
}
|
|
/**
|
|
* Check the login writing rules
|
|
*/
|
|
checkLogin(newValue: string): void {
|
|
this.login = newValue;
|
|
this.loginState = createLoginState(this.login);
|
|
this.loginType = getLoginType(this.login);
|
|
this.updateButtonVisibility();
|
|
}
|
|
|
|
/**
|
|
* Check the password writing rules
|
|
*/
|
|
checkPassword(newValue: string): void {
|
|
this.password = newValue;
|
|
this.passwordState = createPasswordState(this.password);
|
|
this.updateButtonVisibility();
|
|
}
|
|
|
|
onLogin(): void {
|
|
this.loginButtonIsClicked = true;
|
|
let self = this;
|
|
this.adminUserService
|
|
.login(this.login, this.password, this.rememberMe)
|
|
.then(() => {
|
|
// go to the home page
|
|
//console.log(`Request Sign-in for other application ... ==> ${self.ssoApplicationId}`);
|
|
if (!isNullOrUndefined(self.ssoApplicationId)) {
|
|
self.transferToApplicationThatRequiredTheSSO();
|
|
} else {
|
|
self.router.navigate(['home']);
|
|
}
|
|
})
|
|
.catch((error: any) => {
|
|
self.error = `Wrong e-mail/login or password: ${error}`;
|
|
});
|
|
}
|
|
private transferToApplicationThatRequiredTheSSO(): void {
|
|
let self = this;
|
|
this.applicationService
|
|
.getApplicationSpecificToken(this.ssoApplicationId)
|
|
.then((result: SpecificTokenResponse) => {
|
|
self.transferToApplicationThatRequiredTheSSO2(result.url, result.jwt);
|
|
})
|
|
.catch((error: any) => {
|
|
self.error = `Can not retreive the application interface`;
|
|
});
|
|
}
|
|
|
|
private transferToApplicationThatRequiredTheSSO2(url: string, token: string): void {
|
|
if (url.slice(-1) === '/') {
|
|
url = url.slice(0, -1);
|
|
}
|
|
if (isNullOrUndefined(this.ssoReturnData)) {
|
|
this.ssoApplicationReturnUrl = `${url}/aG9tZQ/${this.rememberMe}/`;
|
|
} else {
|
|
this.ssoApplicationReturnUrl = `${url}/${this.ssoReturnData}/${this.rememberMe}/`;
|
|
}
|
|
//console.log(`generate in new URL: ${this.ssoApplicationReturnUrl + token}`);
|
|
//this.router.navigate([ this.ssoApplicationReturnUrl+"aBeautifullToken" ], { replaceUrl: true });
|
|
window.location.href = this.ssoApplicationReturnUrl + token;
|
|
}
|
|
|
|
onCancel(): void {
|
|
//console.log(`onCancel ... '${ this.login }':'${ this.password }'`);
|
|
// go to the home page
|
|
if (this.ssoApplicationId !== undefined) {
|
|
//this.ssoApplicationReturnUrl = this.generateBaseSSOReturn(this.ssoApplicationId, this.rememberMe);
|
|
//window.location.href = this.ssoApplicationReturnUrl + "__CANCEL__";
|
|
// we can not do it anymore ==> need to be logged to know the remotre adresses !!! Otherwise it is a "security fail"
|
|
this.router.navigate(['home']);
|
|
} else {
|
|
this.locate.back();
|
|
}
|
|
}
|
|
}
|