mirror of
https://github.com/bitwarden/clients.git
synced 2026-02-13 04:15:48 +08:00
* feat(change-password-component): Change Password Update [18720] - Very close to complete. * fix(policy-enforcement): [PM-21085] Fix Bug with Policy Enforcement - Removed temp code to force the state I need to verify correctness. * fix(policy-enforcement): [PM-21085] Fix Bug with Policy Enforcement - Recover account working with change password component. * fix(policy-enforcement): [PM-21085] Fix Bug with Policy Enforcement - Made code more dry. * fix(change-password-component): Change Password Update [18720] - Updates to routing and the extension. Extension is still a wip. * fix(change-password-component): Change Password Update [18720] - Extension routing changes. * feat(change-password-component): Change Password Update [18720] - More extension work * feat(change-password-component): Change Password Update [18720] - Pausing work for now while we wait for product to hear back. * feat(change-password-component): Change Password Update [18720] - Removed duplicated anon layouts. * feat(change-password-component): Change Password Update [18720] - Tidied up code. * feat(change-password-component): Change Password Update [18720] - Small fixes to the styling * feat(change-password-component): Change Password Update [18720] - Adding more content for the routing. * feat(change-password-component): Change Password Update [18720] - Removed circular loop for now. * feat(change-password-component): Change Password Update [18720] - Made comments regarding the change password routing complexities with change-password and auth guard. * feat(change-password-component): Change Password Update [18720] - Undid some changes because they will be conflicts later on. * feat(change-password-component): Change Password Update [18720] - Small directive change. * feat(change-password-component): Change Password Update [18720] - Small changes and added some clarification on where I'm blocked * feat(change-password-component): Change Password Update [18720] - Org invite is seemingly working, found one bug to iron out. * refactor(change-password-component): Change Password Update [18720] - Fixed up policy service to be made more clear. * docs(change-password-component): Change Password Update [18720] - Updated documentation. * refactor(change-password-component): Change Password Update [18720] - Routing changes and policy service changes. * fix(change-password-component): Change Password Update [18720] - Wrapping up changes. * feat(change-password-component): Change Password Update [18720] - Should be working fully * feat(change-password-component): Change Password Update [18720] - Found a bug, working on password policy being present on login. * feat(change-password-component): Change Password Update [18720] - Turned on auth guard on other clients for change-password route. * feat(change-password-component): Change Password Update [18720] - Committing intermediate changes. * feat(change-password-component): Change Password Update [18720] - The master password policy endpoint has been added! Should be working. Testing now. * feat(change-password-component): Change Password Update [18720] - Minor fixes. * feat(change-password-component): Change Password Update [18720] - Undid naming change. * feat(change-password-component): Change Password Update [18720] - Removed comment. * feat(change-password-component): Change Password Update [18720] - Removed unneeded code. * fix(change-password-component): Change Password Update [18720] - Took org invite state out of service and made it accessible. * fix(change-password-component): Change Password Update [18720] - Small changes. * fix(change-password-component): Change Password Update [18720] - Split up org invite service into client specific implementations and have them injected into clients properly * feat(change-password-component): Change Password Update [18720] - Stopping work and going to switch to a new branch to pare down some of the solutions that were made to get this over the finish line * feat(change-password-component): Change Password Update [18720] - Started to remove functionality in the login.component and the password login strategy. * feat(change-password-component): Change Password Update [18720] - Removed more unneded changes. * feat(change-password-component): Change Password Update [18720] - Change password clearing state working properly. * fix(change-password-component): Change Password Update [18720] - Added docs and moved web implementation. * comments(change-password-component): Change Password Update [18720] - Added more notes. * test(change-password-component): Change Password Update [18720] - Added in tests for policy service. * comment(change-password-component): Change Password Update [18720] - Updated doc with correct ticket number. * comment(change-password-component): Change Password Update [18720] - Fixed doc. * test(change-password-component): Change Password Update [18720] - Fixed tests. * test(change-password-component): Change Password Update [18720] - Fixed linting errors. Have more tests to fix. * test(change-password-component): Change Password Update [18720] - Added back in ignore for typesafety. * fix(change-password-component): Change Password Update [18720] - Fixed other type issues. * test(change-password-component): Change Password Update [18720] - Fixed tests. * test(change-password-component): Change Password Update [18720] - Fixed more tests. * test(change-password-component): Change Password Update [18720] - Fixed tiny duplicate code. * fix(change-password-component): Change Password Update [18720] - Fixed desktop component. * fix(change-password-component): Change Password Update [18720] - Removed unused code * fix(change-password-component): Change Password Update [18720] - Fixed locales. * fix(change-password-component): Change Password Update [18720] - Removed tracing. * fix(change-password-component): Change Password Update [18720] - Removed duplicative services module entry. * fix(change-password-component): Change Password Update [18720] - Added comment. * fix(change-password-component): Change Password Update [18720] - Fixed unneeded call in two factor to get user id. * fix(change-password-component): Change Password Update [18720] - Fixed a couple of tiny things. * fix(change-password-component): Change Password Update [18720] - Added comment for later fix. * fix(change-password-component): Change Password Update [18720] - Fixed linting error. * PM-18720 - AuthGuard - move call to get isChangePasswordFlagOn down after other conditions for efficiency. * PM-18720 - PasswordLoginStrategy tests - test new feature flagged combine org invite policies logic for weak password evaluation. * PM-18720 - CLI - fix dep issue * PM-18720 - ChangePasswordComp - extract change password warning up out of input password component * PM-18720 - InputPassword - remove unused dependency. * PM-18720 - ChangePasswordComp - add callout dep * PM-18720 - Revert all anon-layout changes * PM-18720 - Anon Layout - finish reverting changes. * PM-18720 - WIP move of change password out of libs/auth * PM-18720 - Clean up remaining imports from moving change password out of libs/auth * PM-18720 - Add change-password barrel file for better import grouping * PM-18720 - Change Password comp - restore maxWidth * PM-18720 - After merge, fix errors * PM-18720 - Desktop - fix api service import * PM-18720 - NDV - fix routing. * PM-18720 - Change Password Comp - add logout service todo * PM-18720 - PasswordSettings - per feedback, component is already feature flagged behind PM16117_ChangeExistingPasswordRefactor so we can just delete the replaced callout (new text is in change-password comp) * PM-18720 - Routing Modules - properly flag new component behind feature flag. * PM-18720 - SSO Login Strategy - fix config service import since it is now in shared deps from main merge. * PM-18720 - Fix SSO login strategy tests * PM-18720 - Default Policy Service - address AC PR feedback --------- Co-authored-by: Jared Snider <jsnider@bitwarden.com> Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
242 lines
9.7 KiB
TypeScript
242 lines
9.7 KiB
TypeScript
import { MockProxy, mock } from "jest-mock-extended";
|
|
import { BehaviorSubject, of } from "rxjs";
|
|
|
|
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
|
|
// eslint-disable-next-line no-restricted-imports
|
|
import { OrganizationUserApiService } from "@bitwarden/admin-console/common";
|
|
import {
|
|
FakeUserDecryptionOptions as UserDecryptionOptions,
|
|
InternalUserDecryptionOptionsServiceAbstraction,
|
|
} from "@bitwarden/auth/common";
|
|
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
|
|
import { OrganizationKeysResponse } from "@bitwarden/common/admin-console/models/response/organization-keys.response";
|
|
import { MasterPasswordApiService } from "@bitwarden/common/auth/abstractions/master-password-api.service.abstraction";
|
|
import { SetPasswordRequest } from "@bitwarden/common/auth/models/request/set-password.request";
|
|
import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service";
|
|
import { InternalMasterPasswordServiceAbstraction } from "@bitwarden/common/key-management/master-password/abstractions/master-password.service.abstraction";
|
|
import { KeysRequest } from "@bitwarden/common/models/request/keys.request";
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
|
import { EncString } from "@bitwarden/common/platform/models/domain/enc-string";
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/platform/models/domain/symmetric-crypto-key";
|
|
import { CsprngArray } from "@bitwarden/common/types/csprng";
|
|
import { UserId } from "@bitwarden/common/types/guid";
|
|
import { MasterKey, UserKey } from "@bitwarden/common/types/key";
|
|
import { DEFAULT_KDF_CONFIG, KdfConfigService, KeyService } from "@bitwarden/key-management";
|
|
|
|
import { PasswordInputResult } from "../input-password/password-input-result";
|
|
|
|
import { DefaultSetPasswordJitService } from "./default-set-password-jit.service";
|
|
import { SetPasswordCredentials } from "./set-password-jit.service.abstraction";
|
|
|
|
describe("DefaultSetPasswordJitService", () => {
|
|
let sut: DefaultSetPasswordJitService;
|
|
|
|
let masterPasswordApiService: MockProxy<MasterPasswordApiService>;
|
|
let keyService: MockProxy<KeyService>;
|
|
let encryptService: MockProxy<EncryptService>;
|
|
let i18nService: MockProxy<I18nService>;
|
|
let kdfConfigService: MockProxy<KdfConfigService>;
|
|
let masterPasswordService: MockProxy<InternalMasterPasswordServiceAbstraction>;
|
|
let organizationApiService: MockProxy<OrganizationApiServiceAbstraction>;
|
|
let organizationUserApiService: MockProxy<OrganizationUserApiService>;
|
|
let userDecryptionOptionsService: MockProxy<InternalUserDecryptionOptionsServiceAbstraction>;
|
|
|
|
beforeEach(() => {
|
|
masterPasswordApiService = mock<MasterPasswordApiService>();
|
|
keyService = mock<KeyService>();
|
|
encryptService = mock<EncryptService>();
|
|
i18nService = mock<I18nService>();
|
|
kdfConfigService = mock<KdfConfigService>();
|
|
masterPasswordService = mock<InternalMasterPasswordServiceAbstraction>();
|
|
organizationApiService = mock<OrganizationApiServiceAbstraction>();
|
|
organizationUserApiService = mock<OrganizationUserApiService>();
|
|
userDecryptionOptionsService = mock<InternalUserDecryptionOptionsServiceAbstraction>();
|
|
|
|
sut = new DefaultSetPasswordJitService(
|
|
encryptService,
|
|
i18nService,
|
|
kdfConfigService,
|
|
keyService,
|
|
masterPasswordApiService,
|
|
masterPasswordService,
|
|
organizationApiService,
|
|
organizationUserApiService,
|
|
userDecryptionOptionsService,
|
|
);
|
|
});
|
|
|
|
it("should instantiate the DefaultSetPasswordJitService", () => {
|
|
expect(sut).not.toBeFalsy();
|
|
});
|
|
|
|
describe("setPassword", () => {
|
|
let masterKey: MasterKey;
|
|
let userKey: UserKey;
|
|
let userKeyEncString: EncString;
|
|
let protectedUserKey: [UserKey, EncString];
|
|
let keyPair: [string, EncString];
|
|
let keysRequest: KeysRequest;
|
|
let organizationKeys: OrganizationKeysResponse;
|
|
let orgPublicKey: Uint8Array;
|
|
|
|
let orgSsoIdentifier: string;
|
|
let orgId: string;
|
|
let resetPasswordAutoEnroll: boolean;
|
|
let userId: UserId;
|
|
let passwordInputResult: PasswordInputResult;
|
|
let credentials: SetPasswordCredentials;
|
|
|
|
let userDecryptionOptionsSubject: BehaviorSubject<UserDecryptionOptions>;
|
|
let setPasswordRequest: SetPasswordRequest;
|
|
|
|
beforeEach(() => {
|
|
masterKey = new SymmetricCryptoKey(new Uint8Array(64).buffer as CsprngArray) as MasterKey;
|
|
userKey = new SymmetricCryptoKey(new Uint8Array(64).buffer as CsprngArray) as UserKey;
|
|
userKeyEncString = new EncString("userKeyEncrypted");
|
|
protectedUserKey = [userKey, userKeyEncString];
|
|
keyPair = ["publicKey", new EncString("privateKey")];
|
|
keysRequest = new KeysRequest(keyPair[0], keyPair[1].encryptedString);
|
|
organizationKeys = {
|
|
privateKey: "orgPrivateKey",
|
|
publicKey: "orgPublicKey",
|
|
} as OrganizationKeysResponse;
|
|
orgPublicKey = Utils.fromB64ToArray(organizationKeys.publicKey);
|
|
|
|
orgSsoIdentifier = "orgSsoIdentifier";
|
|
orgId = "orgId";
|
|
resetPasswordAutoEnroll = false;
|
|
userId = "userId" as UserId;
|
|
|
|
passwordInputResult = {
|
|
newMasterKey: masterKey,
|
|
newServerMasterKeyHash: "newServerMasterKeyHash",
|
|
newLocalMasterKeyHash: "newLocalMasterKeyHash",
|
|
newPasswordHint: "newPasswordHint",
|
|
kdfConfig: DEFAULT_KDF_CONFIG,
|
|
newPassword: "newPassword",
|
|
};
|
|
|
|
credentials = {
|
|
newMasterKey: passwordInputResult.newMasterKey,
|
|
newServerMasterKeyHash: passwordInputResult.newServerMasterKeyHash,
|
|
newLocalMasterKeyHash: passwordInputResult.newLocalMasterKeyHash,
|
|
newPasswordHint: passwordInputResult.newPasswordHint,
|
|
kdfConfig: passwordInputResult.kdfConfig,
|
|
orgSsoIdentifier,
|
|
orgId,
|
|
resetPasswordAutoEnroll,
|
|
userId,
|
|
};
|
|
|
|
userDecryptionOptionsSubject = new BehaviorSubject(null);
|
|
userDecryptionOptionsService.userDecryptionOptions$ = userDecryptionOptionsSubject;
|
|
|
|
setPasswordRequest = new SetPasswordRequest(
|
|
passwordInputResult.newServerMasterKeyHash,
|
|
protectedUserKey[1].encryptedString,
|
|
passwordInputResult.newPasswordHint,
|
|
orgSsoIdentifier,
|
|
keysRequest,
|
|
passwordInputResult.kdfConfig.kdfType,
|
|
passwordInputResult.kdfConfig.iterations,
|
|
);
|
|
});
|
|
|
|
function setupSetPasswordMocks(hasUserKey = true) {
|
|
if (!hasUserKey) {
|
|
keyService.userKey$.mockReturnValue(of(null));
|
|
keyService.makeUserKey.mockResolvedValue(protectedUserKey);
|
|
} else {
|
|
keyService.userKey$.mockReturnValue(of(userKey));
|
|
keyService.encryptUserKeyWithMasterKey.mockResolvedValue(protectedUserKey);
|
|
}
|
|
|
|
keyService.makeKeyPair.mockResolvedValue(keyPair);
|
|
|
|
masterPasswordApiService.setPassword.mockResolvedValue(undefined);
|
|
masterPasswordService.setForceSetPasswordReason.mockResolvedValue(undefined);
|
|
|
|
userDecryptionOptionsSubject.next(new UserDecryptionOptions({ hasMasterPassword: true }));
|
|
userDecryptionOptionsService.setUserDecryptionOptions.mockResolvedValue(undefined);
|
|
kdfConfigService.setKdfConfig.mockResolvedValue(undefined);
|
|
keyService.setUserKey.mockResolvedValue(undefined);
|
|
|
|
keyService.setPrivateKey.mockResolvedValue(undefined);
|
|
|
|
masterPasswordService.setMasterKeyHash.mockResolvedValue(undefined);
|
|
}
|
|
|
|
function setupResetPasswordAutoEnrollMocks(organizationKeysExist = true) {
|
|
if (organizationKeysExist) {
|
|
organizationApiService.getKeys.mockResolvedValue(organizationKeys);
|
|
} else {
|
|
organizationApiService.getKeys.mockResolvedValue(null);
|
|
return;
|
|
}
|
|
|
|
keyService.userKey$.mockReturnValue(of(userKey));
|
|
encryptService.encapsulateKeyUnsigned.mockResolvedValue(userKeyEncString);
|
|
|
|
organizationUserApiService.putOrganizationUserResetPasswordEnrollment.mockResolvedValue(
|
|
undefined,
|
|
);
|
|
}
|
|
|
|
it("should set password successfully (given a user key)", async () => {
|
|
// Arrange
|
|
setupSetPasswordMocks();
|
|
|
|
// Act
|
|
await sut.setPassword(credentials);
|
|
|
|
// Assert
|
|
expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest);
|
|
});
|
|
|
|
it("should set password successfully (given no user key)", async () => {
|
|
// Arrange
|
|
setupSetPasswordMocks(false);
|
|
|
|
// Act
|
|
await sut.setPassword(credentials);
|
|
|
|
// Assert
|
|
expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest);
|
|
});
|
|
|
|
it("should handle reset password auto enroll", async () => {
|
|
// Arrange
|
|
credentials.resetPasswordAutoEnroll = true;
|
|
|
|
setupSetPasswordMocks();
|
|
setupResetPasswordAutoEnrollMocks();
|
|
|
|
// Act
|
|
await sut.setPassword(credentials);
|
|
|
|
// Assert
|
|
expect(masterPasswordApiService.setPassword).toHaveBeenCalledWith(setPasswordRequest);
|
|
expect(organizationApiService.getKeys).toHaveBeenCalledWith(orgId);
|
|
expect(encryptService.encapsulateKeyUnsigned).toHaveBeenCalledWith(userKey, orgPublicKey);
|
|
expect(
|
|
organizationUserApiService.putOrganizationUserResetPasswordEnrollment,
|
|
).toHaveBeenCalled();
|
|
});
|
|
|
|
it("when handling reset password auto enroll, it should throw an error if organization keys are not found", async () => {
|
|
// Arrange
|
|
credentials.resetPasswordAutoEnroll = true;
|
|
|
|
setupSetPasswordMocks();
|
|
setupResetPasswordAutoEnrollMocks(false);
|
|
|
|
// Act and Assert
|
|
await expect(sut.setPassword(credentials)).rejects.toThrow();
|
|
expect(
|
|
organizationUserApiService.putOrganizationUserResetPasswordEnrollment,
|
|
).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|