[Reset Password] Update Crypto and Policy services (#387)

This commit is contained in:
Vincent Salucci 2021-05-24 13:29:50 -05:00 committed by GitHub
parent 5f1ad85dd1
commit 395ded02aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 9 deletions

View File

@ -43,7 +43,7 @@ export abstract class CryptoService {
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<EncString>;
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<EncArrayBuffer>;
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer) => Promise<EncString>;
rsaDecrypt: (encValue: string) => Promise<ArrayBuffer>;
rsaDecrypt: (encValue: string, privateKeyValue?: ArrayBuffer) => Promise<ArrayBuffer>;
decryptToBytes: (encString: EncString, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
decryptToUtf8: (encString: EncString, key?: SymmetricCryptoKey) => Promise<string>;
decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise<ArrayBuffer>;

View File

@ -6,6 +6,9 @@ import { ResetPasswordPolicyOptions } from '../models/domain/resetPasswordPolicy
import { PolicyType } from '../enums/policyType';
import { ListResponse } from '../models/response/listResponse';
import { PolicyResponse } from '../models/response/policyResponse';
export abstract class PolicyService {
policyCache: Policy[];
@ -16,5 +19,6 @@ export abstract class PolicyService {
getMasterPasswordPolicyOptions: (policies?: Policy[]) => Promise<MasterPasswordPolicyOptions>;
evaluateMasterPassword: (passwordStrength: number, newPassword: string,
enforcedPolicyOptions?: MasterPasswordPolicyOptions) => boolean;
getResetPasswordPolicyOptions: (policy: Policy) => ResetPasswordPolicyOptions;
getResetPasswordPolicyOptions: (policies: Policy[], orgId: string) => [ResetPasswordPolicyOptions, boolean];
mapPoliciesFromToken: (policiesResponse: ListResponse<PolicyResponse>) => Policy[];
}

View File

@ -446,7 +446,7 @@ export class CryptoService implements CryptoServiceAbstraction {
return new EncString(EncryptionType.Rsa2048_OaepSha1_B64, Utils.fromBufferToB64(encBytes));
}
async rsaDecrypt(encValue: string): Promise<ArrayBuffer> {
async rsaDecrypt(encValue: string, privateKeyValue?: ArrayBuffer): Promise<ArrayBuffer> {
const headerPieces = encValue.split('.');
let encType: EncryptionType = null;
let encPieces: string[];
@ -477,7 +477,7 @@ export class CryptoService implements CryptoServiceAbstraction {
}
const data = Utils.fromB64ToArray(encPieces[0]).buffer;
const privateKey = await this.getPrivateKey();
const privateKey = privateKeyValue ?? await this.getPrivateKey();
if (privateKey == null) {
throw new Error('No private key.');
}

View File

@ -6,9 +6,12 @@ import { PolicyData } from '../models/data/policyData';
import { MasterPasswordPolicyOptions } from '../models/domain/masterPasswordPolicyOptions';
import { Policy } from '../models/domain/policy';
import { ResetPasswordPolicyOptions } from '../models/domain/resetPasswordPolicyOptions';
import { PolicyType } from '../enums/policyType';
import { ResetPasswordPolicyOptions } from '../models/domain/resetPasswordPolicyOptions';
import { ListResponse } from '../models/response/listResponse';
import { PolicyResponse } from '../models/response/policyResponse';
const Keys = {
policiesPrefix: 'policies_',
@ -140,13 +143,25 @@ export class PolicyService implements PolicyServiceAbstraction {
return true;
}
getResetPasswordPolicyOptions(policy: Policy): ResetPasswordPolicyOptions {
getResetPasswordPolicyOptions(policies: Policy[], orgId: string): [ResetPasswordPolicyOptions, boolean] {
const resetPasswordPolicyOptions = new ResetPasswordPolicyOptions();
if (policy != null && policy.enabled && policy.data != null) {
resetPasswordPolicyOptions.autoEnrollEnabled = policy.data.autoEnrollEnabled;
if (policies == null || orgId == null) {
return [resetPasswordPolicyOptions, false];
}
return resetPasswordPolicyOptions;
const policy = policies.find(p => p.organizationId === orgId && p.type === PolicyType.ResetPassword && p.enabled);
resetPasswordPolicyOptions.autoEnrollEnabled = policy?.data?.autoEnrollEnabled ?? false;
return [resetPasswordPolicyOptions, policy?.enabled ?? false];
}
mapPoliciesFromToken(policiesResponse: ListResponse<PolicyResponse>): Policy[] {
if (policiesResponse == null || policiesResponse.data == null) {
return null;
}
const policiesData = policiesResponse.data.map(p => new PolicyData(p));
return policiesData.map(p => new Policy(p));
}
}