From a42de41587a62749d699af04ea8d2102234d690b Mon Sep 17 00:00:00 2001 From: rr-bw <102181210+rr-bw@users.noreply.github.com> Date: Wed, 8 May 2024 11:34:47 -0700 Subject: [PATCH] [PM-5363] PinService State Providers (#8244) * move pinKeyEncryptedUserKey * move pinKeyEncryptedUserKeyEphemeral * remove comments, move docs * cleanup * use UserKeyDefinition * refactor methods * add migration * fix browser dependency * add tests for migration * rename to pinService * move state to PinService * add PinService dep to CryptoService * move protectedPin to state provider * update service deps * renaming * move decryptUserKeyWithPin to pinService * update service injection * move more methods our of crypto service * remove CryptoService dep from PinService and update service injection * remove cryptoService reference * add method to FakeMasterPasswordService * fix circular dependency * fix desktop service injection * update browser dependencies * add protectedPin to migrations * move storePinKey to pinService * update and clarify documentation * more jsdoc updates * update import paths * refactor isPinLockSet method * update state definitions * initialize service before injecting into other services * initialize service before injecting into other services (bw.ts) * update clearOn and do additional cleanup * clarify docs and naming * assign abstract & private methods, add clarity to decryptAndMigrateOldPinKeyEncryptedMasterKey() method * derived state (attempt) * fix typos * use accountService to get active user email * use constant userId * add derived state * add get and clear for oldPinKeyEncryptedMasterKey * require userId * move pinProtected * add clear methods * remove pinProtected from account.ts and replace methods * add methods to create and store pinKeyEncryptedUserKey * add pinProtected/oldPinKeyEncrypterMasterKey to migration * update migration tests * update migration rollback tests * update to systemService and decryptAndMigrate... method * remove old test * increase length of state definition name to meet test requirements * rename 'TRANSIENT' to 'EPHEMERAL' for consistency * fix tests for login strategies, vault-export, and fake MP service * more updates to login-strategy tests * write new tests for core pinKeyEncrypterUserKey methods and isPinSet * write new tests for pinProtected and oldPinKeyEncryptedMasterKey methods * minor test reformatting * update test for decryptUserKeyWithPin() * fix bug with oldPinKeyEncryptedMasterKey * fix tests for vault-timeout-settings.service * fix bitwarden-password-protected-importer test * fix login strategy tests and auth-request.service test * update pinService tests * fix crypto service tests * add jsdoc * fix test file import * update jsdocs for decryptAndMigrateOldPinKeyEncryptedMasterKey() * update error messages and jsdocs * add null checks, move userId retrievals * update migration tests * update stateService calls to require userId * update test for decryptUserKeyWithPin() * update oldPinKeyEncryptedMasterKey migration tests * more test updates * fix factory import * update tests for isPinSet() and createProtectedPin() * add test for makePinKey() * add test for createPinKeyEncryptedUserKey() * add tests for getPinLockType() * consolidate userId verification tests * add tests for storePinKeyEncryptedUserKey() * fix service dep * get email based on userId * use MasterPasswordService instead of internal * rename protectedPin to userKeyEncryptedPin * rename to pinKeyEncryptedUserKeyPersistent * update method params * fix CryptoService tests * jsdoc update * use EncString for userKeyEncryptedPin * remove comment * use cryptoFunctionService.compareFast() * update tests * cleanup, remove comments * resolve merge conflict * fix DI of MasterPasswordService * more DI fixes --- .../master-password-service.factory.ts | 25 +- .../pin-crypto-service.factory.ts | 53 -- .../service-factories/pin-service.factory.ts | 74 +++ .../user-verification-service.factory.ts | 6 +- .../popup/components/set-pin.component.html | 12 +- apps/browser/src/auth/popup/lock.component.ts | 6 +- .../settings/account-security.component.ts | 7 +- .../browser/src/background/main.background.ts | 42 +- .../background/nativeMessaging.background.ts | 2 +- .../vault-timeout-settings-service.factory.ts | 12 + .../crypto-service.factory.ts | 6 + .../services/browser-crypto.service.ts | 3 + .../src/popup/services/services.module.ts | 5 +- .../import-service.factory.ts | 8 +- apps/cli/src/auth/commands/unlock.command.ts | 2 +- apps/cli/src/bw.ts | 41 +- .../src/app/accounts/settings.component.ts | 14 +- .../src/app/services/services.module.ts | 3 + .../auth/components/set-pin.component.html | 12 +- apps/desktop/src/auth/lock.component.spec.ts | 6 +- apps/desktop/src/auth/lock.component.ts | 6 +- .../services/electron-crypto.service.spec.ts | 3 + .../services/electron-crypto.service.ts | 8 +- .../settings/change-password.component.ts | 5 +- .../emergency-access-takeover.component.ts | 3 + .../src/app/auth/update-password.component.ts | 3 + .../components/change-password.component.ts | 2 + .../src/auth/components/lock.component.ts | 21 +- .../auth/components/set-password.component.ts | 3 +- .../src/auth/components/set-pin.component.ts | 51 +- .../components/update-password.component.ts | 3 + .../update-temp-password.component.ts | 3 +- .../src/services/jslib-services.module.ts | 30 +- libs/auth/src/common/abstractions/index.ts | 2 +- .../pin-crypto.service.abstraction.ts | 5 - .../abstractions/pin.service.abstraction.ts | 129 ++++ .../auth-request-login.strategy.spec.ts | 2 +- .../auth-request-login.strategy.ts | 2 +- .../login-strategies/login.strategy.spec.ts | 4 +- .../password-login.strategy.spec.ts | 2 +- .../password-login.strategy.ts | 2 +- .../sso-login.strategy.spec.ts | 16 +- .../login-strategies/sso-login.strategy.ts | 2 +- .../user-api-login.strategy.spec.ts | 8 +- .../user-api-login.strategy.ts | 2 +- .../auth-request/auth-request.service.spec.ts | 8 +- .../auth-request/auth-request.service.ts | 2 +- libs/auth/src/common/services/index.ts | 2 +- .../pin-crypto.service.implementation.ts | 104 --- .../pin-crypto/pin-crypto.service.spec.ts | 192 ------ .../pin/pin.service.implementation.ts | 501 +++++++++++++++ .../common/services/pin/pin.service.spec.ts | 597 ++++++++++++++++++ .../vault-timeout-settings.service.ts | 8 - .../master-password.service.abstraction.ts | 16 +- .../fake-master-password.service.ts | 10 +- .../master-password.service.ts | 57 +- .../user-verification.service.ts | 27 +- .../webauthn-login-prf-crypto.service.ts | 1 + .../platform/abstractions/crypto.service.ts | 64 +- .../abstractions/key-generation.service.ts | 7 + .../platform/abstractions/state.service.ts | 45 -- .../models/domain/account-settings.spec.ts | 17 +- .../src/platform/models/domain/account.ts | 14 +- .../platform/services/crypto.service.spec.ts | 81 ++- .../src/platform/services/crypto.service.ts | 202 ++---- .../services/key-generation.service.ts | 11 + .../src/platform/services/state.service.ts | 97 --- .../src/platform/services/system.service.ts | 13 +- .../src/platform/state/state-definitions.ts | 28 +- .../vault-timeout-settings.service.spec.ts | 35 +- .../vault-timeout-settings.service.ts | 35 +- libs/common/src/state-migrations/migrate.ts | 6 +- .../61-move-pin-state-to-providers.spec.ts | 176 ++++++ .../61-move-pin-state-to-providers.ts | 129 ++++ ...warden-password-protected-importer.spec.ts | 4 + .../bitwarden/bitwarden-json-importer.ts | 2 + .../bitwarden-password-protected-importer.ts | 6 +- .../src/services/import.service.spec.ts | 4 + libs/importer/src/services/import.service.ts | 3 + .../src/services/base-vault-export.service.ts | 4 +- .../individual-vault-export.service.spec.ts | 4 + .../individual-vault-export.service.ts | 4 +- .../src/services/org-vault-export.service.ts | 4 +- .../src/services/vault-export.service.spec.ts | 4 + 84 files changed, 2182 insertions(+), 998 deletions(-) delete mode 100644 apps/browser/src/auth/background/service-factories/pin-crypto-service.factory.ts create mode 100644 apps/browser/src/auth/background/service-factories/pin-service.factory.ts delete mode 100644 libs/auth/src/common/abstractions/pin-crypto.service.abstraction.ts create mode 100644 libs/auth/src/common/abstractions/pin.service.abstraction.ts delete mode 100644 libs/auth/src/common/services/pin-crypto/pin-crypto.service.implementation.ts delete mode 100644 libs/auth/src/common/services/pin-crypto/pin-crypto.service.spec.ts create mode 100644 libs/auth/src/common/services/pin/pin.service.implementation.ts create mode 100644 libs/auth/src/common/services/pin/pin.service.spec.ts create mode 100644 libs/common/src/state-migrations/migrations/61-move-pin-state-to-providers.spec.ts create mode 100644 libs/common/src/state-migrations/migrations/61-move-pin-state-to-providers.ts diff --git a/apps/browser/src/auth/background/service-factories/master-password-service.factory.ts b/apps/browser/src/auth/background/service-factories/master-password-service.factory.ts index a2f9052a3f..37fcf789ff 100644 --- a/apps/browser/src/auth/background/service-factories/master-password-service.factory.ts +++ b/apps/browser/src/auth/background/service-factories/master-password-service.factory.ts @@ -4,20 +4,35 @@ import { } from "@bitwarden/common/auth/abstractions/master-password.service.abstraction"; import { MasterPasswordService } from "@bitwarden/common/auth/services/master-password/master-password.service"; +import { + encryptServiceFactory, + EncryptServiceInitOptions, +} from "../../../platform/background/service-factories/encrypt-service.factory"; import { CachedServices, factory, FactoryOptions, } from "../../../platform/background/service-factories/factory-options"; +import { + keyGenerationServiceFactory, + KeyGenerationServiceInitOptions, +} from "../../../platform/background/service-factories/key-generation-service.factory"; import { stateProviderFactory, StateProviderInitOptions, } from "../../../platform/background/service-factories/state-provider.factory"; +import { + stateServiceFactory, + StateServiceInitOptions, +} from "../../../platform/background/service-factories/state-service.factory"; type MasterPasswordServiceFactoryOptions = FactoryOptions; export type MasterPasswordServiceInitOptions = MasterPasswordServiceFactoryOptions & - StateProviderInitOptions; + StateProviderInitOptions & + StateServiceInitOptions & + KeyGenerationServiceInitOptions & + EncryptServiceInitOptions; export function internalMasterPasswordServiceFactory( cache: { masterPasswordService?: InternalMasterPasswordServiceAbstraction } & CachedServices, @@ -27,7 +42,13 @@ export function internalMasterPasswordServiceFactory( cache, "masterPasswordService", opts, - async () => new MasterPasswordService(await stateProviderFactory(cache, opts)), + async () => + new MasterPasswordService( + await stateProviderFactory(cache, opts), + await stateServiceFactory(cache, opts), + await keyGenerationServiceFactory(cache, opts), + await encryptServiceFactory(cache, opts), + ), ); } diff --git a/apps/browser/src/auth/background/service-factories/pin-crypto-service.factory.ts b/apps/browser/src/auth/background/service-factories/pin-crypto-service.factory.ts deleted file mode 100644 index db16245f67..0000000000 --- a/apps/browser/src/auth/background/service-factories/pin-crypto-service.factory.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { PinCryptoServiceAbstraction, PinCryptoService } from "@bitwarden/auth/common"; - -import { - VaultTimeoutSettingsServiceInitOptions, - vaultTimeoutSettingsServiceFactory, -} from "../../../background/service-factories/vault-timeout-settings-service.factory"; -import { - CryptoServiceInitOptions, - cryptoServiceFactory, -} from "../../../platform/background/service-factories/crypto-service.factory"; -import { - FactoryOptions, - CachedServices, - factory, -} from "../../../platform/background/service-factories/factory-options"; -import { - LogServiceInitOptions, - logServiceFactory, -} from "../../../platform/background/service-factories/log-service.factory"; -import { - StateServiceInitOptions, - stateServiceFactory, -} from "../../../platform/background/service-factories/state-service.factory"; - -import { KdfConfigServiceInitOptions, kdfConfigServiceFactory } from "./kdf-config-service.factory"; - -type PinCryptoServiceFactoryOptions = FactoryOptions; - -export type PinCryptoServiceInitOptions = PinCryptoServiceFactoryOptions & - StateServiceInitOptions & - CryptoServiceInitOptions & - VaultTimeoutSettingsServiceInitOptions & - LogServiceInitOptions & - KdfConfigServiceInitOptions; - -export function pinCryptoServiceFactory( - cache: { pinCryptoService?: PinCryptoServiceAbstraction } & CachedServices, - opts: PinCryptoServiceInitOptions, -): Promise { - return factory( - cache, - "pinCryptoService", - opts, - async () => - new PinCryptoService( - await stateServiceFactory(cache, opts), - await cryptoServiceFactory(cache, opts), - await vaultTimeoutSettingsServiceFactory(cache, opts), - await logServiceFactory(cache, opts), - await kdfConfigServiceFactory(cache, opts), - ), - ); -} diff --git a/apps/browser/src/auth/background/service-factories/pin-service.factory.ts b/apps/browser/src/auth/background/service-factories/pin-service.factory.ts new file mode 100644 index 0000000000..f15e7fe762 --- /dev/null +++ b/apps/browser/src/auth/background/service-factories/pin-service.factory.ts @@ -0,0 +1,74 @@ +import { PinServiceAbstraction, PinService } from "@bitwarden/auth/common"; + +import { + CryptoFunctionServiceInitOptions, + cryptoFunctionServiceFactory, +} from "../../../platform/background/service-factories/crypto-function-service.factory"; +import { + EncryptServiceInitOptions, + encryptServiceFactory, +} from "../../../platform/background/service-factories/encrypt-service.factory"; +import { + FactoryOptions, + CachedServices, + factory, +} from "../../../platform/background/service-factories/factory-options"; +import { + KeyGenerationServiceInitOptions, + keyGenerationServiceFactory, +} from "../../../platform/background/service-factories/key-generation-service.factory"; +import { + LogServiceInitOptions, + logServiceFactory, +} from "../../../platform/background/service-factories/log-service.factory"; +import { + StateProviderInitOptions, + stateProviderFactory, +} from "../../../platform/background/service-factories/state-provider.factory"; +import { + StateServiceInitOptions, + stateServiceFactory, +} from "../../../platform/background/service-factories/state-service.factory"; + +import { AccountServiceInitOptions, accountServiceFactory } from "./account-service.factory"; +import { KdfConfigServiceInitOptions, kdfConfigServiceFactory } from "./kdf-config-service.factory"; +import { + MasterPasswordServiceInitOptions, + masterPasswordServiceFactory, +} from "./master-password-service.factory"; + +type PinServiceFactoryOptions = FactoryOptions; + +export type PinServiceInitOptions = PinServiceFactoryOptions & + AccountServiceInitOptions & + CryptoFunctionServiceInitOptions & + EncryptServiceInitOptions & + KdfConfigServiceInitOptions & + KeyGenerationServiceInitOptions & + LogServiceInitOptions & + MasterPasswordServiceInitOptions & + StateProviderInitOptions & + StateServiceInitOptions; + +export function pinServiceFactory( + cache: { pinService?: PinServiceAbstraction } & CachedServices, + opts: PinServiceInitOptions, +): Promise { + return factory( + cache, + "pinService", + opts, + async () => + new PinService( + await accountServiceFactory(cache, opts), + await cryptoFunctionServiceFactory(cache, opts), + await encryptServiceFactory(cache, opts), + await kdfConfigServiceFactory(cache, opts), + await keyGenerationServiceFactory(cache, opts), + await logServiceFactory(cache, opts), + await masterPasswordServiceFactory(cache, opts), + await stateProviderFactory(cache, opts), + await stateServiceFactory(cache, opts), + ), + ); +} diff --git a/apps/browser/src/auth/background/service-factories/user-verification-service.factory.ts b/apps/browser/src/auth/background/service-factories/user-verification-service.factory.ts index d6f9ce7624..5b5a425045 100644 --- a/apps/browser/src/auth/background/service-factories/user-verification-service.factory.ts +++ b/apps/browser/src/auth/background/service-factories/user-verification-service.factory.ts @@ -37,7 +37,7 @@ import { internalMasterPasswordServiceFactory, MasterPasswordServiceInitOptions, } from "./master-password-service.factory"; -import { PinCryptoServiceInitOptions, pinCryptoServiceFactory } from "./pin-crypto-service.factory"; +import { PinServiceInitOptions, pinServiceFactory } from "./pin-service.factory"; import { userDecryptionOptionsServiceFactory, UserDecryptionOptionsServiceInitOptions, @@ -57,7 +57,7 @@ export type UserVerificationServiceInitOptions = UserVerificationServiceFactoryO I18nServiceInitOptions & UserVerificationApiServiceInitOptions & UserDecryptionOptionsServiceInitOptions & - PinCryptoServiceInitOptions & + PinServiceInitOptions & LogServiceInitOptions & VaultTimeoutSettingsServiceInitOptions & PlatformUtilsServiceInitOptions & @@ -80,7 +80,7 @@ export function userVerificationServiceFactory( await i18nServiceFactory(cache, opts), await userVerificationApiServiceFactory(cache, opts), await userDecryptionOptionsServiceFactory(cache, opts), - await pinCryptoServiceFactory(cache, opts), + await pinServiceFactory(cache, opts), await logServiceFactory(cache, opts), await vaultTimeoutSettingsServiceFactory(cache, opts), await platformUtilsServiceFactory(cache, opts), diff --git a/apps/browser/src/auth/popup/components/set-pin.component.html b/apps/browser/src/auth/popup/components/set-pin.component.html index aff87b5371..50e7aca75f 100644 --- a/apps/browser/src/auth/popup/components/set-pin.component.html +++ b/apps/browser/src/auth/popup/components/set-pin.component.html @@ -12,8 +12,16 @@ -