2021-07-22 19:28:45 +02:00
|
|
|
import { Directive, OnInit } from '@angular/core';
|
2018-04-04 15:47:43 +02:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { KeysRequest } from 'jslib-common/models/request/keysRequest';
|
|
|
|
import { ReferenceEventRequest } from 'jslib-common/models/request/referenceEventRequest';
|
|
|
|
import { RegisterRequest } from 'jslib-common/models/request/registerRequest';
|
2018-04-04 15:47:43 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { AuthService } from 'jslib-common/abstractions/auth.service';
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
2021-07-22 19:28:45 +02:00
|
|
|
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-19 10:32:14 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { StateService } from 'jslib-common/abstractions/state.service';
|
2018-04-04 15:47:43 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { KdfType } from 'jslib-common/enums/kdfType';
|
2018-08-14 21:12:10 +02:00
|
|
|
|
2021-07-22 19:28:45 +02:00
|
|
|
import { CaptchaProtectedComponent } from './captchaProtected.component';
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
export class RegisterComponent extends CaptchaProtectedComponent implements OnInit {
|
2018-07-03 17:41:55 +02:00
|
|
|
name: string = '';
|
2018-04-04 15:47:43 +02:00
|
|
|
email: string = '';
|
|
|
|
masterPassword: string = '';
|
|
|
|
confirmMasterPassword: string = '';
|
|
|
|
hint: string = '';
|
|
|
|
showPassword: boolean = false;
|
|
|
|
formPromise: Promise<any>;
|
2018-11-13 04:54:18 +01:00
|
|
|
masterPasswordScore: number;
|
2020-07-20 21:21:01 +02:00
|
|
|
referenceData: ReferenceEventRequest;
|
2020-11-02 19:50:58 +01:00
|
|
|
showTerms = true;
|
|
|
|
acceptPolicies: boolean = false;
|
2018-04-04 15:47:43 +02:00
|
|
|
|
|
|
|
protected successRoute = 'login';
|
2018-11-13 04:54:18 +01:00
|
|
|
private masterPasswordStrengthTimeout: any;
|
2018-04-04 15:47:43 +02:00
|
|
|
|
|
|
|
constructor(protected authService: AuthService, protected router: Router,
|
2021-07-22 19:28:45 +02:00
|
|
|
i18nService: I18nService, protected cryptoService: CryptoService,
|
2018-10-02 15:20:32 +02:00
|
|
|
protected apiService: ApiService, protected stateService: StateService,
|
2021-07-22 19:28:45 +02:00
|
|
|
platformUtilsService: PlatformUtilsService,
|
2021-10-19 10:32:14 +02:00
|
|
|
protected passwordGenerationService: PasswordGenerationService, environmentService: EnvironmentService,
|
|
|
|
protected logService: LogService) {
|
2021-07-22 19:28:45 +02:00
|
|
|
super(environmentService, i18nService, platformUtilsService);
|
2020-11-02 19:50:58 +01:00
|
|
|
this.showTerms = !platformUtilsService.isSelfHost();
|
|
|
|
}
|
2018-04-04 15:47:43 +02:00
|
|
|
|
2021-07-22 19:28:45 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
this.setupCaptcha();
|
|
|
|
}
|
|
|
|
|
2018-11-15 21:27:04 +01:00
|
|
|
get masterPasswordScoreWidth() {
|
|
|
|
return this.masterPasswordScore == null ? 0 : (this.masterPasswordScore + 1) * 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
get masterPasswordScoreColor() {
|
|
|
|
switch (this.masterPasswordScore) {
|
|
|
|
case 4:
|
|
|
|
return 'success';
|
|
|
|
case 3:
|
|
|
|
return 'primary';
|
|
|
|
case 2:
|
|
|
|
return 'warning';
|
|
|
|
default:
|
|
|
|
return 'danger';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get masterPasswordScoreText() {
|
|
|
|
switch (this.masterPasswordScore) {
|
|
|
|
case 4:
|
|
|
|
return this.i18nService.t('strong');
|
|
|
|
case 3:
|
|
|
|
return this.i18nService.t('good');
|
|
|
|
case 2:
|
|
|
|
return this.i18nService.t('weak');
|
|
|
|
default:
|
|
|
|
return this.masterPasswordScore != null ? this.i18nService.t('weak') : null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:47:43 +02:00
|
|
|
async submit() {
|
2020-11-02 19:50:58 +01:00
|
|
|
if (!this.acceptPolicies && this.showTerms) {
|
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('acceptPoliciesError'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:47:43 +02:00
|
|
|
if (this.email == null || this.email === '') {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:47:43 +02:00
|
|
|
this.i18nService.t('emailRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.email.indexOf('@') === -1) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:47:43 +02:00
|
|
|
this.i18nService.t('invalidEmail'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:47:43 +02:00
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword.length < 8) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:47:43 +02:00
|
|
|
this.i18nService.t('masterPassLength'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword !== this.confirmMasterPassword) {
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'),
|
2018-04-04 15:47:43 +02:00
|
|
|
this.i18nService.t('masterPassDoesntMatch'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-13 05:22:37 +01:00
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
|
|
|
|
this.getPasswordStrengthUserInput());
|
2018-11-13 04:54:18 +01:00
|
|
|
if (strengthResult != null && strengthResult.score < 3) {
|
|
|
|
const result = await this.platformUtilsService.showDialog(this.i18nService.t('weakMasterPasswordDesc'),
|
|
|
|
this.i18nService.t('weakMasterPassword'), this.i18nService.t('yes'), this.i18nService.t('no'),
|
|
|
|
'warning');
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 22:04:48 +01:00
|
|
|
if (this.hint === this.masterPassword) {
|
|
|
|
this.platformUtilsService.showToast('error', this.i18nService.t('errorOccurred'), this.i18nService.t('hintEqualsPassword'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-28 14:54:15 +01:00
|
|
|
this.name = this.name === '' ? null : this.name;
|
|
|
|
this.email = this.email.trim().toLowerCase();
|
2018-08-28 01:58:49 +02:00
|
|
|
const kdf = KdfType.PBKDF2_SHA256;
|
2020-09-15 16:23:21 +02:00
|
|
|
const useLowerKdf = this.platformUtilsService.isIE();
|
2018-10-02 15:20:32 +02:00
|
|
|
const kdfIterations = useLowerKdf ? 10000 : 100000;
|
2018-08-14 21:12:10 +02:00
|
|
|
const key = await this.cryptoService.makeKey(this.masterPassword, this.email, kdf, kdfIterations);
|
2018-07-03 17:41:55 +02:00
|
|
|
const encKey = await this.cryptoService.makeEncKey(key);
|
|
|
|
const hashedPassword = await this.cryptoService.hashPassword(this.masterPassword, key);
|
|
|
|
const keys = await this.cryptoService.makeKeyPair(encKey[0]);
|
|
|
|
const request = new RegisterRequest(this.email, this.name, hashedPassword,
|
2021-07-22 19:28:45 +02:00
|
|
|
this.hint, encKey[1].encryptedString, kdf, kdfIterations, this.referenceData, this.captchaToken);
|
2018-07-03 18:06:01 +02:00
|
|
|
request.keys = new KeysRequest(keys[0], keys[1].encryptedString);
|
2018-07-13 17:06:44 +02:00
|
|
|
const orgInvite = await this.stateService.get<any>('orgInvitation');
|
|
|
|
if (orgInvite != null && orgInvite.token != null && orgInvite.organizationUserId != null) {
|
|
|
|
request.token = orgInvite.token;
|
|
|
|
request.organizationUserId = orgInvite.organizationUserId;
|
|
|
|
}
|
2018-07-03 17:41:55 +02:00
|
|
|
|
2018-04-04 15:47:43 +02:00
|
|
|
try {
|
2018-07-03 17:41:55 +02:00
|
|
|
this.formPromise = this.apiService.postRegister(request);
|
2021-07-22 19:28:45 +02:00
|
|
|
try {
|
|
|
|
await this.formPromise;
|
|
|
|
} catch (e) {
|
|
|
|
if (this.handleCaptchaRequired(e)) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 05:09:19 +02:00
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('newAccountCreated'));
|
2018-07-13 16:44:47 +02:00
|
|
|
this.router.navigate([this.successRoute], { queryParams: { email: this.email } });
|
2021-10-19 10:32:14 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-04-04 15:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
togglePassword(confirmField: boolean) {
|
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus();
|
|
|
|
}
|
2018-11-13 04:54:18 +01:00
|
|
|
|
|
|
|
updatePasswordStrength() {
|
|
|
|
if (this.masterPasswordStrengthTimeout != null) {
|
|
|
|
clearTimeout(this.masterPasswordStrengthTimeout);
|
|
|
|
}
|
|
|
|
this.masterPasswordStrengthTimeout = setTimeout(() => {
|
2018-11-13 05:22:37 +01:00
|
|
|
const strengthResult = this.passwordGenerationService.passwordStrength(this.masterPassword,
|
|
|
|
this.getPasswordStrengthUserInput());
|
2018-11-13 04:54:18 +01:00
|
|
|
this.masterPasswordScore = strengthResult == null ? null : strengthResult.score;
|
|
|
|
}, 300);
|
|
|
|
}
|
2018-11-13 05:22:37 +01:00
|
|
|
|
|
|
|
private getPasswordStrengthUserInput() {
|
|
|
|
let userInput: string[] = [];
|
|
|
|
const atPosition = this.email.indexOf('@');
|
|
|
|
if (atPosition > -1) {
|
|
|
|
userInput = userInput.concat(this.email.substr(0, atPosition).trim().toLowerCase().split(/[^A-Za-z0-9]/));
|
|
|
|
}
|
|
|
|
if (this.name != null && this.name !== '') {
|
|
|
|
userInput = userInput.concat(this.name.trim().toLowerCase().split(' '));
|
|
|
|
}
|
|
|
|
return userInput;
|
|
|
|
}
|
2018-04-04 15:47:43 +02:00
|
|
|
}
|