2018-04-04 15:47:43 +02:00
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
|
|
|
import { ToasterService } from 'angular2-toaster';
|
|
|
|
import { Angulartics2 } from 'angulartics2';
|
|
|
|
|
2018-07-03 18:06:01 +02:00
|
|
|
import { KeysRequest } from '../../models/request/keysRequest';
|
|
|
|
import { RegisterRequest } from '../../models/request/registerRequest';
|
2018-04-04 15:47:43 +02:00
|
|
|
|
|
|
|
import { ApiService } from '../../abstractions/api.service';
|
|
|
|
import { AuthService } from '../../abstractions/auth.service';
|
|
|
|
import { CryptoService } from '../../abstractions/crypto.service';
|
|
|
|
import { I18nService } from '../../abstractions/i18n.service';
|
2018-10-02 15:20:32 +02:00
|
|
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
2018-07-13 17:06:44 +02:00
|
|
|
import { StateService } from '../../abstractions/state.service';
|
2018-04-04 15:47:43 +02:00
|
|
|
|
2018-08-14 21:12:10 +02:00
|
|
|
import { KdfType } from '../../enums/kdfType';
|
|
|
|
|
2018-04-04 15:47:43 +02:00
|
|
|
export class RegisterComponent {
|
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>;
|
|
|
|
|
|
|
|
protected successRoute = 'login';
|
|
|
|
|
|
|
|
constructor(protected authService: AuthService, protected router: Router,
|
|
|
|
protected analytics: Angulartics2, protected toasterService: ToasterService,
|
|
|
|
protected i18nService: I18nService, protected cryptoService: CryptoService,
|
2018-10-02 15:20:32 +02:00
|
|
|
protected apiService: ApiService, protected stateService: StateService,
|
|
|
|
protected platformUtilsService: PlatformUtilsService) { }
|
2018-04-04 15:47:43 +02:00
|
|
|
|
|
|
|
async submit() {
|
|
|
|
if (this.email == null || this.email === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('emailRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.email.indexOf('@') === -1) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('invalidEmail'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword == null || this.masterPassword === '') {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassRequired'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword.length < 8) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassLength'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.masterPassword !== this.confirmMasterPassword) {
|
|
|
|
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
|
|
|
|
this.i18nService.t('masterPassDoesntMatch'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 17:41:55 +02:00
|
|
|
this.name = this.name === '' ? null : this.name;
|
2018-09-08 14:13:33 +02:00
|
|
|
this.email = this.email.trim().toLowerCase();
|
2018-08-28 01:58:49 +02:00
|
|
|
const kdf = KdfType.PBKDF2_SHA256;
|
2018-10-02 15:20:32 +02:00
|
|
|
const useLowerKdf = this.platformUtilsService.isEdge() || this.platformUtilsService.isIE();
|
|
|
|
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,
|
2018-08-14 21:12:10 +02:00
|
|
|
this.hint, encKey[1].encryptedString, kdf, kdfIterations);
|
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);
|
2018-04-04 15:47:43 +02:00
|
|
|
await this.formPromise;
|
|
|
|
this.analytics.eventTrack.next({ action: 'Registered' });
|
|
|
|
this.toasterService.popAsync('success', null, this.i18nService.t('newAccountCreated'));
|
2018-07-13 16:44:47 +02:00
|
|
|
this.router.navigate([this.successRoute], { queryParams: { email: this.email } });
|
2018-04-04 15:47:43 +02:00
|
|
|
} catch { }
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePassword(confirmField: boolean) {
|
|
|
|
this.analytics.eventTrack.next({ action: 'Toggled Master Password on Register' });
|
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus();
|
|
|
|
}
|
|
|
|
}
|