2021-04-22 21:17:14 +02:00
|
|
|
import { Directive } from '@angular/core';
|
2020-10-13 22:21:03 +02:00
|
|
|
import {
|
|
|
|
ActivatedRoute,
|
|
|
|
Router
|
|
|
|
} from '@angular/router';
|
2020-08-19 16:57:35 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
|
|
|
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
|
|
|
|
import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
|
|
|
import { PolicyService } from 'jslib-common/abstractions/policy.service';
|
|
|
|
import { SyncService } from 'jslib-common/abstractions/sync.service';
|
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2020-08-19 16:57:35 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { EncString } from 'jslib-common/models/domain/encString';
|
|
|
|
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
|
2020-08-19 16:57:35 +02:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { KeysRequest } from 'jslib-common/models/request/keysRequest';
|
2021-09-03 21:49:03 +02:00
|
|
|
import { OrganizationUserResetPasswordEnrollmentRequest } from 'jslib-common/models/request/organizationUserResetPasswordEnrollmentRequest';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { SetPasswordRequest } from 'jslib-common/models/request/setPasswordRequest';
|
2020-08-19 16:57:35 +02:00
|
|
|
|
|
|
|
import { ChangePasswordComponent as BaseChangePasswordComponent } from './change-password.component';
|
|
|
|
|
2021-06-09 23:24:31 +02:00
|
|
|
import { HashPurpose } from 'jslib-common/enums/hashPurpose';
|
2021-06-03 18:58:57 +02:00
|
|
|
import { KdfType } from 'jslib-common/enums/kdfType';
|
2021-09-03 21:49:03 +02:00
|
|
|
import { PolicyType } from 'jslib-common/enums/policyType';
|
|
|
|
|
|
|
|
import { Utils } from 'jslib-common/misc/utils';
|
2020-08-19 16:57:35 +02:00
|
|
|
|
2021-04-22 21:17:14 +02:00
|
|
|
@Directive()
|
2020-08-19 16:57:35 +02:00
|
|
|
export class SetPasswordComponent extends BaseChangePasswordComponent {
|
2020-08-27 20:24:38 +02:00
|
|
|
syncLoading: boolean = true;
|
2020-08-19 16:57:35 +02:00
|
|
|
showPassword: boolean = false;
|
|
|
|
hint: string = '';
|
2020-10-13 22:21:03 +02:00
|
|
|
identifier: string = null;
|
2021-09-03 21:49:03 +02:00
|
|
|
orgId: string;
|
|
|
|
resetPasswordAutoEnroll = false;
|
2020-08-19 16:57:35 +02:00
|
|
|
|
|
|
|
onSuccessfulChangePassword: () => Promise<any>;
|
|
|
|
successRoute = 'vault';
|
|
|
|
|
2020-08-21 19:25:33 +02:00
|
|
|
constructor(i18nService: I18nService, cryptoService: CryptoService, messagingService: MessagingService,
|
2020-08-19 16:57:35 +02:00
|
|
|
userService: UserService, passwordGenerationService: PasswordGenerationService,
|
2021-08-10 14:02:53 +02:00
|
|
|
platformUtilsService: PlatformUtilsService, policyService: PolicyService, protected router: Router,
|
2020-10-13 22:21:03 +02:00
|
|
|
private apiService: ApiService, private syncService: SyncService, private route: ActivatedRoute) {
|
2020-08-21 19:25:33 +02:00
|
|
|
super(i18nService, cryptoService, messagingService, userService, passwordGenerationService,
|
|
|
|
platformUtilsService, policyService);
|
2020-08-19 16:57:35 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 20:24:38 +02:00
|
|
|
async ngOnInit() {
|
|
|
|
await this.syncService.fullSync(true);
|
|
|
|
this.syncLoading = false;
|
2020-10-13 22:21:03 +02:00
|
|
|
|
2021-02-04 16:49:23 +01:00
|
|
|
const queryParamsSub = this.route.queryParams.subscribe(async qParams => {
|
2020-10-13 22:21:03 +02:00
|
|
|
if (qParams.identifier != null) {
|
|
|
|
this.identifier = qParams.identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queryParamsSub != null) {
|
|
|
|
queryParamsSub.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-03 21:49:03 +02:00
|
|
|
// Automatic Enrollment Detection
|
|
|
|
if (this.identifier != null) {
|
|
|
|
const org = await this.userService.getOrganizationByIdentifier(this.identifier);
|
|
|
|
this.orgId = org?.id;
|
|
|
|
const policyList = await this.policyService.getAll(PolicyType.ResetPassword);
|
|
|
|
const policyResult = this.policyService.getResetPasswordPolicyOptions(policyList, this.orgId);
|
|
|
|
this.resetPasswordAutoEnroll = policyResult[1] && policyResult[0].autoEnrollEnabled;
|
|
|
|
}
|
|
|
|
|
2020-08-27 20:24:38 +02:00
|
|
|
super.ngOnInit();
|
|
|
|
}
|
|
|
|
|
2020-08-19 16:57:35 +02:00
|
|
|
async setupSubmitActions() {
|
|
|
|
this.kdf = KdfType.PBKDF2_SHA256;
|
2020-09-15 16:23:21 +02:00
|
|
|
const useLowerKdf = this.platformUtilsService.isIE();
|
2020-08-19 16:57:35 +02:00
|
|
|
this.kdfIterations = useLowerKdf ? 10000 : 100000;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async performSubmitActions(masterPasswordHash: string, key: SymmetricCryptoKey,
|
2021-04-21 02:16:19 +02:00
|
|
|
encKey: [SymmetricCryptoKey, EncString]) {
|
2020-08-19 16:57:35 +02:00
|
|
|
const keys = await this.cryptoService.makeKeyPair(encKey[0]);
|
2021-09-03 21:49:03 +02:00
|
|
|
const request = new SetPasswordRequest(
|
|
|
|
masterPasswordHash,
|
|
|
|
encKey[1].encryptedString,
|
|
|
|
this.hint,
|
|
|
|
this.kdf,
|
|
|
|
this.kdfIterations,
|
|
|
|
this.identifier,
|
|
|
|
new KeysRequest(keys[0], keys[1].encryptedString)
|
|
|
|
);
|
2020-08-19 16:57:35 +02:00
|
|
|
try {
|
2021-09-03 21:49:03 +02:00
|
|
|
if (this.resetPasswordAutoEnroll) {
|
|
|
|
this.formPromise = this.apiService.setPassword(request).then(async () => {
|
|
|
|
await this.onSetPasswordSuccess(key, encKey, keys);
|
|
|
|
return this.apiService.getOrganizationKeys(this.orgId);
|
|
|
|
}).then(async response => {
|
|
|
|
if (response == null) {
|
|
|
|
throw new Error(this.i18nService.t('resetPasswordOrgKeysError'));
|
|
|
|
}
|
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
const publicKey = Utils.fromB64ToArray(response.publicKey);
|
|
|
|
|
|
|
|
// RSA Encrypt user's encKey.key with organization public key
|
|
|
|
const userEncKey = await this.cryptoService.getEncKey();
|
|
|
|
const encryptedKey = await this.cryptoService.rsaEncrypt(userEncKey.key, publicKey.buffer);
|
|
|
|
|
|
|
|
const resetRequest = new OrganizationUserResetPasswordEnrollmentRequest();
|
|
|
|
resetRequest.resetPasswordKey = encryptedKey.encryptedString;
|
|
|
|
|
|
|
|
return this.apiService.putOrganizationUserResetPasswordEnrollment(this.orgId, userId, resetRequest);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.formPromise = this.apiService.setPassword(request).then(async () => {
|
|
|
|
await this.onSetPasswordSuccess(key, encKey, keys);
|
|
|
|
});
|
|
|
|
}
|
2020-08-19 16:57:35 +02:00
|
|
|
|
2021-09-03 21:49:03 +02:00
|
|
|
await this.formPromise;
|
2021-06-09 23:24:31 +02:00
|
|
|
|
2020-08-19 16:57:35 +02:00
|
|
|
if (this.onSuccessfulChangePassword != null) {
|
|
|
|
this.onSuccessfulChangePassword();
|
|
|
|
} else {
|
|
|
|
this.router.navigate([this.successRoute]);
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
this.platformUtilsService.showToast('error', null, this.i18nService.t('errorOccurred'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePassword(confirmField: boolean) {
|
|
|
|
this.showPassword = !this.showPassword;
|
|
|
|
document.getElementById(confirmField ? 'masterPasswordRetype' : 'masterPassword').focus();
|
|
|
|
}
|
2021-09-03 21:49:03 +02:00
|
|
|
|
|
|
|
private async onSetPasswordSuccess(key: SymmetricCryptoKey, encKey: [SymmetricCryptoKey, EncString], keys: [string, EncString]) {
|
|
|
|
await this.userService.setInformation(await this.userService.getUserId(), await this.userService.getEmail(),
|
|
|
|
this.kdf, this.kdfIterations);
|
|
|
|
await this.cryptoService.setKey(key);
|
|
|
|
await this.cryptoService.setEncKey(encKey[1].encryptedString);
|
|
|
|
await this.cryptoService.setEncPrivateKey(keys[1].encryptedString);
|
|
|
|
|
|
|
|
const localKeyHash = await this.cryptoService.hashPassword(this.masterPassword, key,
|
|
|
|
HashPurpose.LocalAuthorization);
|
|
|
|
await this.cryptoService.setKeyHash(localKeyHash);
|
|
|
|
}
|
2020-08-19 16:57:35 +02:00
|
|
|
}
|