Files
clients/angular/src/components/login.component.ts

177 lines
7.1 KiB
TypeScript
Raw Normal View History

2018-07-13 09:13:10 -04:00
import {
Directive,
2018-07-13 09:13:10 -04:00
Input,
NgZone,
2018-07-13 09:13:10 -04:00
OnInit,
} from '@angular/core';
2018-04-04 09:19:21 -04:00
import { Router } from '@angular/router';
import { take } from 'rxjs/operators';
import { AuthResult } from 'jslib-common/models/domain/authResult';
2018-04-04 09:19:21 -04:00
import { AuthService } from 'jslib-common/abstractions/auth.service';
import { CryptoFunctionService } from 'jslib-common/abstractions/cryptoFunction.service';
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { StateService } from 'jslib-common/abstractions/state.service';
import { StorageService } from 'jslib-common/abstractions/storage.service';
2018-04-04 09:19:21 -04:00
import { ConstantsService } from 'jslib-common/services/constants.service';
import { Utils } from 'jslib-common/misc/utils';
import { CaptchaProtectedComponent } from './captchaProtected.component';
2018-07-13 09:13:10 -04:00
const Keys = {
rememberedEmail: 'rememberedEmail',
rememberEmail: 'rememberEmail',
};
@Directive()
export class LoginComponent extends CaptchaProtectedComponent implements OnInit {
2018-04-04 09:19:21 -04:00
@Input() email: string = '';
2018-07-13 09:13:10 -04:00
@Input() rememberEmail = true;
2018-04-04 09:19:21 -04:00
masterPassword: string = '';
showPassword: boolean = false;
formPromise: Promise<AuthResult>;
2018-07-13 10:44:47 -04:00
onSuccessfulLogin: () => Promise<any>;
onSuccessfulLoginNavigate: () => Promise<any>;
2018-10-13 00:11:06 -04:00
onSuccessfulLoginTwoFactorNavigate: () => Promise<any>;
onSuccessfulLoginForceResetNavigate: () => Promise<any>;
2018-04-04 09:19:21 -04:00
protected twoFactorRoute = '2fa';
protected successRoute = 'vault';
protected forcePasswordResetRoute = 'update-temp-password';
2018-04-04 09:19:21 -04:00
constructor(protected authService: AuthService, protected router: Router,
platformUtilsService: PlatformUtilsService, i18nService: I18nService,
protected stateService: StateService, environmentService: EnvironmentService,
protected passwordGenerationService: PasswordGenerationService,
protected cryptoFunctionService: CryptoFunctionService, private storageService: StorageService,
protected logService: LogService, protected ngZone: NgZone) {
super(environmentService, i18nService, platformUtilsService);
}
2018-07-13 09:13:10 -04:00
async ngOnInit() {
if (this.email == null || this.email === '') {
this.email = await this.storageService.get<string>(Keys.rememberedEmail);
if (this.email == null) {
this.email = '';
}
}
this.rememberEmail = await this.storageService.get<boolean>(Keys.rememberEmail);
if (this.rememberEmail == null) {
this.rememberEmail = true;
}
if (Utils.isBrowser && !Utils.isNode) {
this.focusInput();
}
2018-07-13 09:13:10 -04:00
}
2018-04-04 09:19:21 -04:00
async submit() {
await this.setupCaptcha();
2018-04-04 09:19:21 -04:00
if (this.email == null || this.email === '') {
2018-10-02 23:09:19 -04:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-04 09:19:21 -04:00
this.i18nService.t('emailRequired'));
return;
}
if (this.email.indexOf('@') === -1) {
2018-10-02 23:09:19 -04:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-04 09:19:21 -04:00
this.i18nService.t('invalidEmail'));
return;
}
if (this.masterPassword == null || this.masterPassword === '') {
2018-10-02 23:09:19 -04:00
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
2018-04-04 09:19:21 -04:00
this.i18nService.t('masterPassRequired'));
return;
}
try {
this.formPromise = this.authService.logIn(this.email, this.masterPassword, this.captchaToken);
2018-04-04 09:19:21 -04:00
const response = await this.formPromise;
2018-07-13 09:13:10 -04:00
await this.storageService.save(Keys.rememberEmail, this.rememberEmail);
if (this.rememberEmail) {
await this.storageService.save(Keys.rememberedEmail, this.email);
} else {
await this.storageService.remove(Keys.rememberedEmail);
}
if (this.handleCaptchaRequired(response)) {
return;
} else if (response.twoFactor) {
2018-10-13 00:11:06 -04:00
if (this.onSuccessfulLoginTwoFactorNavigate != null) {
this.onSuccessfulLoginTwoFactorNavigate();
} else {
this.router.navigate([this.twoFactorRoute]);
}
} else if (response.forcePasswordReset) {
if (this.onSuccessfulLoginForceResetNavigate != null) {
this.onSuccessfulLoginForceResetNavigate();
} else {
this.router.navigate([this.forcePasswordResetRoute]);
}
2018-04-04 09:19:21 -04:00
} else {
const disableFavicon = await this.storageService.get<boolean>(ConstantsService.disableFaviconKey);
await this.stateService.save(ConstantsService.disableFaviconKey, !!disableFavicon);
2018-07-13 10:44:47 -04:00
if (this.onSuccessfulLogin != null) {
this.onSuccessfulLogin();
2018-04-25 12:08:18 -04:00
}
2018-07-13 10:44:47 -04:00
if (this.onSuccessfulLoginNavigate != null) {
this.onSuccessfulLoginNavigate();
} else {
this.router.navigate([this.successRoute]);
}
2018-04-04 09:19:21 -04:00
}
} catch (e) {
this.logService.error(e);
}
2018-04-04 09:19:21 -04:00
}
togglePassword() {
this.showPassword = !this.showPassword;
if (this.ngZone.isStable) {
document.getElementById('masterPassword').focus();
} else {
this.ngZone.onStable.pipe(take(1)).subscribe(() => document.getElementById('masterPassword').focus());
}
2018-04-04 09:19:21 -04:00
}
async launchSsoBrowser(clientId: string, ssoRedirectUri: string) {
// Generate necessary sso params
const passwordOptions: any = {
type: 'password',
length: 64,
uppercase: true,
lowercase: true,
numbers: true,
special: false,
};
const state = await this.passwordGenerationService.generatePassword(passwordOptions);
const ssoCodeVerifier = await this.passwordGenerationService.generatePassword(passwordOptions);
const codeVerifierHash = await this.cryptoFunctionService.hash(ssoCodeVerifier, 'sha256');
const codeChallenge = Utils.fromBufferToUrlB64(codeVerifierHash);
// Save sso params
await this.storageService.save(ConstantsService.ssoStateKey, state);
await this.storageService.save(ConstantsService.ssoCodeVerifierKey, ssoCodeVerifier);
// Build URI
const webUrl = this.environmentService.getWebVaultUrl();
// Launch browser
this.platformUtilsService.launchUri(webUrl + '/#/sso?clientId=' + clientId +
'&redirectUri=' + encodeURIComponent(ssoRedirectUri) +
'&state=' + state + '&codeChallenge=' + codeChallenge);
}
protected focusInput() {
document.getElementById(this.email == null || this.email === '' ? 'email' : 'masterPassword').focus();
}
2018-04-04 09:19:21 -04:00
}