Commit Graph

84 Commits

Author SHA1 Message Date
Matt Gibson 95d6b6b0f0
Merge remote-tracking branch 'origin/main' into auth/pm-6688/account-service-as-account-source 2024-04-25 15:17:36 -04:00
Ike 1e4158fd87
[PM-5735] Create kdf Service (#8715)
* key connector migration initial

* migrator complete

* fix dependencies

* finalized tests

* fix deps and sync main

* clean up definition file

* fixing tests

* fixed tests

* fixing CLI, Browser, Desktop builds

* fixed factory options

* reverting exports

* implemented UserKeyDefinition clearOn

* Initial Kdf Service Changes

* rename and account setting kdfconfig

* fixing tests and renaming migration

* fixed DI ordering for browser

* rename and fix DI

* Clean up Migrations

* fixing migrations

* begin data structure changes for kdf config

* Make KDF more type safe; co-author: jlf0dev

* fixing tests

* Fixed CLI login and comments

* set now accepts userId and test updates

---------

Co-authored-by: Jake Fink <jfink@bitwarden.com>
2024-04-25 11:26:01 -07:00
Matt Gibson 929a209af2
Capitalize const values 2024-04-25 12:24:02 -04:00
Matt Gibson 701de74adf
Ensure accounts object has only valid userIds 2024-04-25 12:21:01 -04:00
Matt Gibson 4aef033a81
Handle no users case for auth service statuses 2024-04-25 08:49:24 -04:00
Matt Gibson 92f85beb8d
Merge remote-tracking branch 'origin/main' into auth/pm-6688/account-service-as-account-source 2024-04-25 08:21:40 -04:00
Matt Gibson 81693127f9
Be more precise about log out messaging 2024-04-24 14:35:29 -04:00
Jared Snider 5dc83cd34c
PM-6787 - Rename DeviceTrustCryptoService to DeviceTrustService (#8819) 2024-04-24 12:54:54 -04:00
Jared Snider a12c140792
Revert "Revert "Auth/PM-6689 - Migrate Security Stamp to Token Service and St…" (#8889)
This reverts commit 100b43dd8f.
2024-04-24 12:37:19 -04:00
Matt Gibson 7d11fbcd52
Correct user activity sorting test 2024-04-24 06:51:34 -04:00
Matt Gibson 32b34d6014
Fix `npm run test:types` 2024-04-24 06:49:01 -04:00
Matt Gibson b12c9eeab6
Fold account activity into account service 2024-04-23 18:53:59 -04:00
Matt Gibson ee0559028b
Move active and next active user to account service 2024-04-23 17:08:15 -04:00
Matt Gibson 16f03dbbcc
Add `AccountActivityService` that handles storing account last active data 2024-04-23 17:08:14 -04:00
Matt Gibson dff58167b8
Use last account activity from account service 2024-04-23 17:07:11 -04:00
Matt Gibson 403b2c8c0c
Add account activity tracking to account service 2024-04-23 17:07:11 -04:00
Matt Gibson e7678de6e7
Do not store account info on logged out accounts 2024-04-23 17:07:11 -04:00
Matt Gibson f67fc12f8a
Add email verified to account service 2024-04-23 17:07:11 -04:00
Matt Gibson 08399ea86e
Use account service to track accounts and active account 2024-04-23 17:07:10 -04:00
Jared Snider 100b43dd8f
Revert "Auth/PM-6689 - Migrate Security Stamp to Token Service and State Prov…" (#8860)
This reverts commit 91f1d9fb86.
2024-04-22 12:06:43 -04:00
Jared Snider 91f1d9fb86
Auth/PM-6689 - Migrate Security Stamp to Token Service and State Provider (#8792)
* PM-6689 - Add security stamp to Token state

* PM-6689 - Remove Security Stamp from account and state service

* PM-6689 - Add security stamp get and set to token service + abstraction + tests

* PM-6689 - Add migration for security stamp, test it, and register it with migrator

* PM-6689 - Update sync service + deps to use token service.

* PM-6689 - Cleanup missed usages of account tokens which has been removed.

* PM-6689 - Per PR feedback, remove unnecessary data migration as the security stamp is only in memory and doesn't need to be migrated.
2024-04-19 16:44:24 -04:00
Jared Snider fffef95c5e
Auth/PM-7235 - Refactor AuthService.getAuthStatus, deprecate everBeenUnlocked, and handle initialization of auto user key on client init (#8590)
* PM-7235 - AuthSvc - Refactor getAuthStatus to simply use the cryptoService.hasUserKey check to determine the user's auth status.

* PM-7235 - CryptoSvc - getUserKey - remove setUserKey side effect if auto key is stored. Will move to app init

* PM-7235 - For each client init service, add setUserKeyInMemoryIfAutoUserKeySet logic

* PM-7235 - CryptoSvc tests - remove uncessary test.

* PM-7235 - Create UserKeyInitService and inject into all init services with new listening logic to support acct switching.

* PM-7235 - UserKeyInitSvc - minor refactor of setUserKeyInMemoryIfAutoUserKeySet

* PM-7235 - Add test suite for UserKeyInitService

* PM-7235 - Remove everBeenUnlocked as it is no longer needed

* PM-7235 - Fix tests

* PM-7235 - UserKeyInitSvc - per PR feedback, add error handling to protect observable stream from being cancelled in case of an error

* PM-7235 - Fix tests

* Update libs/common/src/platform/services/user-key-init.service.ts

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* Update libs/common/src/platform/services/user-key-init.service.ts

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>

* PM-7235 - AuthSvc - Per PR review, for getAuthStatus, only check user key existence in memory.

* PM-7235 - remove not useful test per PR feedback.

* PM-7235 - Per PR feedback, update cryptoService.hasUserKey to only check memory for the user key.

* PM-7235 - Per PR feedback, move user key init service listener to main.background instead of init service

* PM-7235 - UserKeyInitSvc tests - fix tests to plass

---------

Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
2024-04-19 11:20:13 -04:00
Jared Snider 51a6b34cc2
Auth/PM-7467- Fix Refresh token issues (#8757)
* PM-7467 - Login Strategy bug - VaultTimeoutSettings will be undefined before the account is activated unless you pass in user ids to retrieve the data. This resulted in refresh tokens always being set into secure storage regardless of a user's vault timeout settings (logout should translate to memory)

* PM-7467 - TokenSvc - Fix bug in getRefreshToken which would retrieve the user's refresh token from secure storage even if the user had changed their vault timeout setting to log out which moved the refresh token into memory. Includes a migration to remove the no longer required REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE state provider flag.

* PM-7467 - Per PR feedback, use IRREVERSIBLE for rollback.

Co-authored-by: Jake Fink <jfink@bitwarden.com>

* PM-7467 - fix tests

* PM-7467 - Fix migrator based on PR feedback.

* PM-7467 - Bump migration version

---------

Co-authored-by: Jake Fink <jfink@bitwarden.com>
2024-04-16 14:05:47 -04:00
Matt Gibson 8d698d9d84
[PM-7169][PM-5267] Remove auth status from account info (#8539)
* remove active account unlocked from state service

* Remove status from account service `AccountInfo`

* Fixup lingering usages of status

Fixup missed factories

* Fixup account info usage

* fixup CLI build

* Fixup current account type

* Add helper for all auth statuses to auth service

* Fix tests

* Uncomment mistakenly commented code

* Rework logged out account exclusion tests

* Correct test description

* Avoid getters returning observables

* fixup type
2024-04-12 09:25:45 +02:00
Justin Baur 84cd01165c
Auth `UserKeyDefinition` Migration (#8587)
* Migrate DeviceTrustCryptoService

* Migrate SsoLoginService

* Migrate TokenService

* Update libs/common/src/auth/services/token.state.ts

Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>

* Fix Test

* Actually Fix Tests

---------

Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
2024-04-10 09:59:20 -04:00
Jake Fink 9d10825dbd
[PM-5362] Add MP Service (attempt #2) (#8619)
* create mp and kdf service

* update mp service interface to not rely on active user

* rename observable methods

* update crypto service with new MP service

* add master password service to login strategies
- make fake service for easier testing
- fix crypto service tests

* update auth service and finish strategies

* auth request refactors

* more service refactors and constructor updates

* setMasterKey refactors

* remove master key methods from crypto service

* remove master key and hash from state service

* missed fixes

* create migrations and fix references

* fix master key imports

* default force set password reason to none

* add password reset reason observable factory to service

* remove kdf changes and migrate only disk data

* update migration number

* fix sync service deps

* use disk for force set password state

* fix desktop migration

* fix sso test

* fix tests

* fix more tests

* fix even more tests

* fix even more tests

* fix cli

* remove kdf service abstraction

* add missing deps for browser

* fix merge conflicts

* clear reset password reason on lock or logout

* fix tests

* fix other tests

* add jsdocs to abstraction

* use state provider in crypto service

* inverse master password service factory

* add clearOn to master password service

* add parameter validation to master password service

* add component level userId

* add missed userId

* migrate key hash

* fix login strategy service

* delete crypto master key from account

* migrate master key encrypted user key

* rename key hash to master key hash

* use mp service for getMasterKeyEncryptedUserKey

* fix tests

* fix user key decryption logic

* add clear methods to mp service

* fix circular dep and encryption issue

* fix test

* remove extra account service call

* use EncString in state provider

* fix tests

* return to using encrypted string for serialization
2024-04-09 20:50:20 -04:00
Jake Fink 775c8a1bbe
Revert "[PM-5362]Create MP Service for state provider migration (#7623)" (#8617)
This reverts commit b1abfb0a5c.
2024-04-04 16:17:09 +00:00
Jake Fink b1abfb0a5c
[PM-5362]Create MP Service for state provider migration (#7623)
* create mp and kdf service

* update mp service interface to not rely on active user

* rename observable methods

* update crypto service with new MP service

* add master password service to login strategies
- make fake service for easier testing
- fix crypto service tests

* update auth service and finish strategies

* auth request refactors

* more service refactors and constructor updates

* setMasterKey refactors

* remove master key methods from crypto service

* remove master key and hash from state service

* missed fixes

* create migrations and fix references

* fix master key imports

* default force set password reason to none

* add password reset reason observable factory to service

* remove kdf changes and migrate only disk data

* update migration number

* fix sync service deps

* use disk for force set password state

* fix desktop migration

* fix sso test

* fix tests

* fix more tests

* fix even more tests

* fix even more tests

* fix cli

* remove kdf service abstraction

* add missing deps for browser

* fix merge conflicts

* clear reset password reason on lock or logout

* fix tests

* fix other tests

* add jsdocs to abstraction

* use state provider in crypto service

* inverse master password service factory

* add clearOn to master password service

* add parameter validation to master password service

* add component level userId

* add missed userId

* migrate key hash

* fix login strategy service

* delete crypto master key from account

* migrate master key encrypted user key

* rename key hash to master key hash

* use mp service for getMasterKeyEncryptedUserKey

* fix tests
2024-04-04 14:22:41 +00:00
Jared Snider c202c93378
Auth/PM-5268 - DeviceTrustCryptoService state provider migration (#7882)
* PM-5268 - Add DEVICE_TRUST_DISK to state definitions

* PM-5268 - DeviceTrustCryptoService - Get most of state provider refactor done - WIP - commented out stuff for now.

* PM-5268 - DeviceTrustCryptoServiceStateProviderMigrator - WIP - got first draft of migrator in place and working on tests. Rollback tests are failing for some reason TBD.

* PM-5268 - more WIP on device trust crypto service migrator tests

* PM-5268 - DeviceTrustCryptoServiceStateProviderMigrator - Refactor based on call with platform

* PM-5268 - DeviceTrustCryptoServiceStateProviderMigrator - tests passing

* PM-5268 - Update DeviceTrustCryptoService to convert over to state providers + update all service instantiations / dependencies to ensure state provider is passed in or injected.

* PM-5268 - Register new migration

* PM-5268 - Temporarily remove device trust crypto service from migrator to ease merge conflicts as there are 6 more migrators before I can apply mine in main.

* PM-5268 - Update migration numbers of DeviceTrustCryptoServiceStateProviderMigrator based on latest migrations from main.

* PM-5268 - (1) Export new KeyDefinitions from DeviceTrustCryptoService for use in test suite (2) Update DeviceTrustCryptoService test file to use state provider.

* PM-5268 - Fix DeviceTrustCryptoServiceStateProviderMigrator tests to use proper versions

* PM-5268 - Actually fix all instances of DeviceTrustCryptoServiceStateProviderMigrator test failures

* PM-5268 - Clean up state service, account, and login strategy of all migrated references

* PM-5268 - Account - finish cleaning up device key

* PM-5268 - StateService - clean up last reference to device key

* PM-5268 - Remove even more device key refs. *facepalm*

* PM-5268 - Finish resolving merge conflicts by incrementing migration version from 22 to 23

* PM-5268 - bump migration versions

* PM-5268 - DeviceTrustCryptoService - Implement secure storage functionality for getDeviceKey and setDeviceKey (to achieve feature parity with the ElectronStateService implementation prior to the state provider migration). Tests to follow shortly.

* PM-5268 - DeviceTrustCryptoService tests - getDeviceKey now tested with all new secure storage scenarios. SetDeviceKey tests to follow.

* PM-5268 - DeviceTrustCryptoService tests - test all setDeviceKey scenarios with state provider & secure storage

* PM-5268 - Update DeviceTrustCryptoService deps to actually use secure storage svc on platforms that support it.

* PM-5268 - Bump migration version due to merge conflicts.

* PM-5268 - Bump migration version

* PM-5268 - tweak jsdocs to be single line per PR feedback

* PM-5268 - DeviceTrustCryptoSvc - improve debuggability.

* PM-5268 - Remove state service as a dependency on the device trust crypto service (woo!)

* PM-5268 - Update migration test json to correctly reflect reality.

* PM-5268 - DeviceTrustCryptoSvc - getDeviceKey - add throw error for active user id missing.

* PM-5268 - Fix tests

* PM-5268 - WIP start on adding user id to every method on device trust crypto service.

* PM-5268 - Update lock comp dependencies across clients

* PM-5268 - Update login via auth request deps across clients to add acct service.

* PM-5268 - UserKeyRotationSvc - add acct service to get active acct id for call to rotateDevicesTrust and then update tests.

* PM-5268 - WIP on trying to fix device trust crypto svc tests.

* PM-5268 - More WIP device trust crypto svc tests passing

* PM-5268 - Device Trust crypto service - get all tests passing

* PM-5268 - DeviceTrustCryptoService.getDeviceKey - fix secure storage b64 to symmetric crypto key conversion

* PM-5268 - Add more tests and update test names

* PM-5268 - rename state to indicate it was disk local

* PM-5268 - DeviceTrustCryptoService - save symmetric key in JSON format

* PM-5268 - Fix lock comp tests by adding acct service dep

* PM-5268 - Update set device key tests to pass

* PM-5268 - Bump migration versions again

* PM-5268 - Fix user key rotation svc tests

* PM-5268 - Update web jest config to allow use of common spec in user-key-rotation-svc tests

* PM-5268 - Bump migration version

* PM-5268 - Per PR feedback, save off user id

* PM-5268 - bump migration version

* PM-5268 - Per PR feedback, remove unnecessary await.

* PM-5268 - Bump migration verson
2024-04-01 16:02:58 -04:00
Matt Gibson 136226b6be
Observable auth statuses (#8537)
* Observable has token

* Allow access to user key state observable

* Create observable auth status

* Fix DI
2024-04-01 19:15:54 +00:00
rr-bw 2e51d96416
[PM-5264] Implement StateProvider in LoginEmailService (#7662)
* setup StateProvider in LoginService

* replace implementations

* replace implementation

* remove stateService

* change storage location for web to 'disk-local'

* implement migrate() method of Migrator

* add RememberedEmailMigrator to migrate.ts

* add rollback

* add tests

* replace implementation

* replace implementation

* add StateProvider to Desktop services

* rename LoginService to RememberEmailService

* update state definition

* rename file

* rename to storedEmail

* rename service to EmailService to avoid confusion

* add jsdocs

* refactor login.component.ts

* fix typos

* fix test

* rename to LoginEmailService

* update factory

* more renaming

* remove duplicate logic and rename method

* convert storedEmail to observable

* refactor to remove setStoredEmail() method

* move service to libs/auth/common

* address floating promises

* remove comment

* remove unnecessary deps in service registration
2024-03-30 11:00:27 -07:00
Jared Snider ebe5a46b57
PM-5263 - Clear all tokens on logout (#8536) 2024-03-28 16:56:02 -04:00
Ike 3d19e3489c
[PM-5269] Key Connector state migration (#8327)
* key connector migration initial

* migrator complete

* fix dependencies

* finalized tests

* fix deps and sync main

* clean up definition file

* fixing tests

* fixed tests

* fixing CLI, Browser, Desktop builds

* fixed factory options

* reverting exports

* implemented UserKeyDefinition clearOn

* Update KeyConnector MIgration

* updated migrator and tests to match profile object

* removed unused service and updated clear

* dep fix

* dep fixes

* clear usesKeyConnector on logout
2024-03-28 12:50:24 -04:00
Jake Fink bd6b3266d4
move auth request notification to service (#8451)
- cleanup hanging promises
2024-03-28 09:34:21 -04:00
Jared Snider 8cdc94076e
Auth/PM-7092 - Fix CLI login via API key not working due to TokenService changes (#8499)
* PM-7092 - Fix CLI login via API key not working (it apparently receives an undefined refresh token which was rejected by setTokens)

* PM-7092 - Fix base login strategy tests

* PM-7092 - per discucssion with jake, refactor setTokens to accept optional refresh token instead of exposing setRefreshToken as public.
2024-03-27 17:46:56 -04:00
Jared Snider a66e224d32
Auth/PM-7072 - Token Service - Access Token Secure Storage Refactor (#8412)
* PM-5263 - TokenSvc - WIP on access token secure storage refactor

* PM-5263 - Add key generation svc to token svc.

* PM-5263 - TokenSvc - more progress on encrypt access token work.

* PM-5263 - TokenSvc TODO cleanup

* PM-5263 - TokenSvc - rename

* PM-5263 - TokenSvc - decryptAccess token must return null as that is a valid case.

* PM-5263 - Add EncryptSvc dep to TokenSvc

* PM-5263 - Add secure storage to token service

* PM-5263 - TokenSvc - (1) Finish implementing accessTokenKey stored in secure storage + encrypted access token stored on disk  (2) Remove no longer necessary migration flag as the presence of the accessTokenKey now serves the same purpose.

Co-authored-by: Jake Fink <jfink@bitwarden.com>

* PM-5263 - TokenSvc - (1) Tweak return structure of decryptAccessToken to be more debuggable (2) Add TODO to add more error handling.

* PM-5263 - TODO: update tests

* PM-5263 - add temp logs

* PM-5263 - TokenSvc - remove logs now that I don't need them.

* fix tests for access token

* PM-5263 - TokenSvc test cleanup - small tweaks / cleanup

* PM-5263 - TokenService - per PR feedback from Justin - add error message to error message if possible.

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>

---------

Co-authored-by: Jake Fink <jfink@bitwarden.com>
Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
2024-03-26 18:41:14 -04:00
Jake Fink 7f14ee4994
add back call to verify by PIN (#8495) 2024-03-26 12:00:30 -04:00
Jake Fink a46767dee2
add auth status to auth service (#8377)
* add auth status to auth service

* fix auth service factory
2024-03-26 13:56:20 +00:00
Oscar Hinton e767295c86
[PM-5979] Refactor EnvironmentService (#8040)
Refactor environment service to emit a single observable. This required significant changes to how the environment service behaves and tackles much of the tech debt planned for it.
2024-03-21 09:09:44 -07:00
Jake Fink 2111b37c32
[PM-5404, PM-3518] Migrate user decryption options to new service (#7344)
* create new user decryption options service

* rename new service to user decryption options

* add hasMasterPassword to user decryption options service

* migrate device trust service to new user decryption options service

* add migration for user-decryption-options

* migrate sync service and calls to trust-device-service

* rename abstraction file

* migrate two factor component

* migrate two factor spec

* migrate sso component

* migrate set-password component

* migrate base login decryption component

* migrate organization options component

* fix component imports

* add missing imports
- remove state service calls
- add update user decryption options method

* remove acct decryption options from account

* lint

* fix tests and linting

* fix browser

* fix desktop

* add user decryption options service to cli

* remove default value from migration

* bump migration number

* fix merge conflict

* fix vault timeout settings

* fix cli

* more fixes

* add user decryption options service to deps of vault timeout settings service

* update login strategy service with user decryption options

* remove early return from sync bandaid for user decryption options

* move user decryption options service to lib/auth

* move user decryption options to libs/auth

* fix reference

* fix browser

* check user decryption options after 2fa check

* update migration and revert tsconfig changes

* add more documentation

* clear user decryption options on logout

* fix tests by creating helper for user decryption options

* fix tests

* pr feedback

* fix factory

* update migration

* add tests

* update missed migration num in test
2024-03-21 00:33:57 +00:00
Justin Baur ec5c6b6797
Create New Method for Setting Avatar Color from Sync (#8403) 2024-03-20 15:28:22 -04:00
Jared Snider 161fb1da5d
Auth/PM-5263 - TokenService State Provider Migration (#7975)
* PM-5263 - Token Service state migration - (1) Got key and state definitions setup (2) Ported over core state service getTimeoutBasedStorageOptions method logic into local determineStorageLocation method (3) Updated majority of methods to use state provider state

* PM-5263 - StateSvc - add TODO to remove timeoutBasedStorageOptions + other state methods after migration code complete.

* PM-5263 - TokenSvc - ClearToken method - (1) Update signature to remove user id as it wasn't used and it simplifies the new state provider implementation (2) Convert away from state svc to state provider state.

* PM-5263 - TokenService - update deps - WIP on circular dep issues.

* PM-5263 -  To resolve circular dep issues between VaultTimeoutSettingsSvc and TokenService: (1) For writes, require callers to pass in vault timeout data (2) For reads, we can just check both locations. This approach has 1 less state call than the previous implementation and is safe as long as the clear logic properly works and is executed anytime a user changes their vault timeout action (lock or log out) & vault timeout (numeric value)

* PM-5263 - VaultTimeoutSettingsSvc - Set token calls now updated to include vault timeout info.

* PM-5263 - Update API Service - add state service and look up vault timeout details and pass to token service when setting token info.

* PM-5263 - TokenService - update service dependencies.

* PM-5263 - TokenService - Add new getAccessTokenByUserId method for state service use case.

* PM-5263 - StateSvc - remove migrated methods and try to replace all usages of getAccessToken. WIP

* PM-5263 - TokenSvc Migration - start on migrator

* PM-5263 - (1) TokenSvc - Build new clearAccessTokenByUserId which is required by state service (2) TokenSvc - Update getToken to take an optional userId to handle another state service case (3) Add some documentation to TokenSvc abstraction.

* PM-5263 - StateService - finish updating all calls within the state service which accessed token service state directly with calls to the new token service methods instead.

* PM-5263 - TokenSvc Abstraction - Add more docs

* PM-5263 - TokenSvc abstraction - more doc tweaks

* PM-5263 - Web state service - add new token service dependency.

* PM-5263  - User API Key Login Strategy - Update to pull vault timeout action and vault timeout from state service in order to pass to new token service endpoints for setting API key client id and secret.

* PM-5263  - (1) Remove TokenSvc owned state from account (2) StateSvc - remove account scaffold logic for clearing removed account data. The same functionality will exist in the state provider framework via lifecycle hooks cleaning up this data and users getting initialized with null data by default.

* PM-5263 - Add token service dependency to state service (WIP - desktop deps not working)

* PM-5263 - Update services module on desktop and browser to add token svc dependency

* PM-5263 - API service factory - add state service factory dependency that I missed initially to get browser building.

* PM-5263 - TokenSvc - getToken/setToken/decodeToken --> getAccessToken/setAccessToken/decodeAccessToken

* PM-5263 - TokenSvc State Provider Migrator - WIP - update expected acct type to match actual account

* PM-5263 - TokenService - clearToken renamed to clearTokens

* PM-5263 - CLI - NodeApiService - add state service dep to get CLI building.

* PM-5263 - StateDefinitions - use unique state definition names

* PM-5263 - StateSvc - remove getTimeoutBasedStorageOptions as no longer used.

* PM-5263 - TokenSvc - Add TODO for figuring out how to store tokens in secure storage.

* PM-5263 - StateSvc - remove get/set 2FA token - references migrated later.

* PM-5263 - TODO: figure out if using same key definition names is an issue

* PM-5263 - TokenServiceStateProviderMigrator written

* PM-5263 - TokenServiceStateProviderMigrator - (1) Don't update legacy account if we only added a new state in state provider for 2FA token (2) Use for loop for easier debugging

* PM-5263 - TokenServiceStateProviderMigrator test - WIP - migration testing mostly complete and passing. Rollback logic TODO.

* PM-5263 - TokenServiceStateProviderMigrator - Add rollback logic to restore 2FA token from users to global.

* PM-5263 - TokenServiceStateProviderMigrator - Refactor rollback to only set account once as not necessary to set it every time.

* PM-5263 - TokenServiceStateProviderMigrator tests - test all rollback scenarios

* PM-5263 - Remove TODO as don't need unique key def names as long as state def keys are unique.

* PM-5263 - TokenSvc - update clearAccessTokenByUserId to use proper state provider helper method to set state.

* PM-5263 - Revert accidentally committing settings.json changes.

* PM-5263 - TokenSvc - update all 2FA token methods to require email so we can user specifically scope 2FA tokens while still storing them in global storage.

* PM-5263 - Update all token service 2FA set / get / clear methods to pass in email.

* PM-5263  - JslibServices module - add missed login service to login strategy svc deps.

* PM-5263 - VaultTimeoutSettingsService - setVaultTimeoutOptions - rename token to accesToken for clarity.

* PM-5263 - (1) TokenSvc - remove getAccessTokenByUserId and force consumers to use getAccessToken w/ optional user id to keep interface small (2) TokenSvc - attempt to implement secure storage on platforms that support it for access & refresh token storage (3) StateSvc - replace usage of getAccessTokenByUserId with getAccessToken

* PM-5263 - TokenSvc - add platform utils and secure storage svc deps

* PM-5263 - TODO: figure out what to do with broken migration

* PM-5263 - TODO: update tests in light of latest 2FA token changes.

* PM-5263 - TokenSvc - clean up TODO

* PM-5263 - We should have tests for the token service.

* PM-5263 - TokenSvc - setAccessToken - If platform supports secure storage and we are saving an access token, remove the access token from memory and disk to fully migrate to secure storage.

* PM-5263 - TokenSvc - getAccessToken - Update logic to look at memory and disk first always and secure storage last to support the secure storage migration

* PM-5263 - TokenSvc - setAccesToken - if user id null on a secure storage supporting platform, throw error.

* PM-5263 - TokenService - (1) Refresh token now stored in secure storage (2) Refresh token set now private as we require a user id to store it in secure storage and we can use the setTokens method to enforce always setting the access token and refresh token together in order to extract a user id from the refresh token. (3) setTokens clientIdClientSecret param now optional

* PM-5263 - TokenServiceStateProviderMigrator - update migration to take global but user scoped 2FA token storage changes into account.

* PM-5263 - Remove old migration as it references state we are removing. Bump min version.

Co-authored-by: Matt Gibson <git@mgibson.dev>

* PM-5263 - TokenService - 2FA token methods now backed by global state record which maps email to individual tokens.

* PM-5263 - WIP on Token Svc migrator and test updates based on new 2FA token storage changes.

* PM-5263 - TokenSvc - (1) Add jira tickets to clean up state migration (2) Add state to track secure storage migration to improve # of reads to get data

* PM-5263 - StateDef - consolidate name of token domain state defs per feedback from Justin + update migration tests

* PM-5263 - TokenSvc - fix error message and add TODO

* PM-5263 - Update token service migration + tests to pass after all 2FA token changes.

* PM-5263 - Fix all login strategy tests which were failing due to token state provider changes + the addition of the loginService as a dependency in the base login strategy.

* PM-5263 - Register TokenService state provider migration with migrator

* PM-5263 - TokenSvc state migration - set tokens after initializing account

* PM-5263 - TokenService changes - WIP - convert from ActiveUserStateProvider to just SingleUserStateProvider to avoid future circ dependency issues.

Co-authored-by: Jake Fink <jlf0dev@users.noreply.github.com>

* PM-5263 - TokenSvc - create getSecureStorageOptions for centralizing all logic for getting data out of SecureStorage.

* PM-5263 - TokenSvc - (1) Refactor determineStorageLocation to also determine secure storage - created a TokenStorageLocation string enum to remove magic strings (2) Refactor setAccessToken to use switch (3) Refactor clearAccessTokenByUserId to clear all locations and not early return on secure storage b/c we only use secure storage if disk is the location but I don't want to require vault timeout data for this method.

* PM-5263 - TokenSvc - getDataFromSecureStorage - Refactor to be more generic for easier re-use

* PM-5263 - TokenSvc - Convert refresh token methods to use single user state and require user ids

* PM-5263 - VaultTimeoutSettingsSvc - get user id and pass to access and refresh token methods.

* PM-5263 - TokenSvc - refactor save secure storage logic into private helper.

* PM-5263 - Base Login Strategy - per discussion with Justin, move save of tokens to before account initialization as we can always derive the user id from the access token. This will ensure that the account is initialized with the proper authN status.

* PM-5263 - TokenSvc - latest refactor - update all methods to accept optional userId now as we can read active user id from global state provider without using activeUserStateProvider (thus, avoiding a circular dep and having to have every method accept in a mandatory user id).

* PM-5263 - VaultTimeoutSettingsService - remove user id from token calls

* PM-5263 - TokenSvc - update all places we instantiate token service to properly pass in new deps.

* PM-5263 - TokenSvc migration is now 27th instead of 23rd.

* PM-5263  - Browser - MainContextMenuHandler - Update service options to include PlatformUtilsServiceInitOptions as the TokenService requires that and the TokenService is now injected on the StateService

* PM-5263 - TokenSvc migration test - update rollback tests to start with correct current version

* PM-5263 - Create token service test file - WIP

* PM-5263 - TokenSvc - tests WIP - instantiates working.

* PM-5263 - TokenSvc - set2FAToken - use null coalesce to ensure record is instantiated for new users before setting data on it.

* PM-5263 - TokenService tests - WIP - 2FA token tests.

* PM-5263 - Worked with Justin to resolve desktop circular dependency issue by adding SUPPORTS_SECURE_STORAGE injection token instead of injecting PlatformUtilsService directly into TokenService.

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>

* PM-5263 - TokenSvc tests - WIP - (1) Update TokenSvc instantiation to use new supportsSecureStorage (2) Test TwoFactorToken methods

* PM-5263 - Fix SUPPORTS_SECURE_STORAGE injection token to properly call supportsSecureStorage message

* PM-5263 - Token state testing

* PM-5263 - TokenState fix name of describe

* PM-5263 - TokenService - export TokenStorageLocation for use in tests.

* PM-5263 - TokenSvc Tests WIP

* PM-5263 - TokenSvc tests - access token logic mostly completed.

* PM-5263 - TokenSvc Tests - more WIP - finish testing access token methods.

* PM-5263 - TokenSvc WIP - another clear access token test.

* PM-5263 - TokenSvc tests - WIP - SetTokens tested.

* PM-5263 - Tweak test name

* PM-5263 - TokenSvc tests - remove unnecessary describe around 2FA token methods.

* PM-5263 - TokenSvc.clearAccessTokenByUserId renamed to just clearAccessToken

* PM-5263 - TokenSvc - refactor clearTokens logic and implement individual clear logic which doesn't require vault timeout setting information.

* PM-5263 - TokenSvc - Replace all places we have vaultTimeout: number with vaultTimeout: number | null to be accurate.

* PM-5263 - TokenSvc.clearTokens - add check for user id; throw if not found

* PM-5263 - TokenService - test clearTokens

* PM-5263 - TokenSvc Tests - setRefreshToken tested

* PM-5263 - TokenSvc tests - getRefreshToken tested + added a new getAccessToken test

* PM-5263 - TokenSvc - ClearRefreshToken scenarios tested.

* PM-5263 - TokenSvc.clearRefreshToken tests - fix copy pasta

* PM-5263 - TokenSvc tests - (1) Fix mistakes in refresh token testing (2) Test setClientId for all scenarios

* PM-5263 - TokenSvc tests - (1) Add some getClientId tests (2) clarify lack of awaits

* PM-5263 - TokenSvc Tests - WIP - getClientId && clearClientId

* PM-5263 - TokenService - getClientSecret - fix error message

* PM-5263 - TokenService tests - test all client secret methods

* PM-5263 - Update TokenSvc migration to 30th migration

* PM-5263 - TokenService - update all tests to initialize data to undefined now that fake state provider supports faking data based on specific key definitions.

* PM-5263 - (1) TokenSvc.decodeAccessToken - update static method's error handling (2) TokenSvc tests - test all decodeAccessToken scenarios

* PM-5263 - TokenSvc - (1) Add DecodedAccessToken type (2) Refactor getTokenExpirationDate logic to use new type and make proper type checks for numbers for exp claim values.

* PM-5263 - TokenSvc tests - test getTokenExpirationDate method.

* PM-5263 - TokenSvc - (1) Update DecodedAccessToken docs (2) Tweak naming in tokenSecondsRemaining

* PM-5263 - TokenSvc abstraction - add jsdoc for tokenSecondsRemaining

* PM-5263 - TokenSvc tests - test tokenSecondsRemaining

* PM-5263 - TokenSvc - DecodedAccessToken type - update sstamp info

* PM-5263 - TokenService - fix flaky tokenSecondsRemaining tests by locking time

* PM-5263 - TokenSvc Tests - Test tokenNeedsRefresh

* PM-5263 - (1) TokenSvc - Refactor getUserId to add extra safety (2) TokenSvc tests - test getUserId

* PM-5263  - (1) TokenSvc - refactor getUserIdFromAccessToken to handle decoding errors (2) TokenSvc tests - test getUserIdFromAccessToken

* PM-5263 - (1) TokenSvc - Refactor getEmail to handle decoding errors + check for specific, expected type (2) TokenSvc tests - test getEmail

* PM-5263 - TokenSvc tests - clean up comment

* PM-5263 - (1) TokenSvc - getEmailVerified - refactor (2) TokenSvc tests - add getEmailVerified tests

* PM-5263  - (1) TokenSvc - refactor getName (2) TokenSvc tests - test getName

* PM-5263 - (1) TokenSvc - refactor getIssuer (2) TokenSvc tests - test getIssuer

* PM-5263 - TokenSvc - remove unnecessary "as type" statements now that we have a decoded access token type

* PM-5263  - (1) TokenSvc - refactor getIsExternal (2) TokenSvc Tests - test getIsExternal

* PM-5263  - TokenSvc abstraction - tune up rest of docs.

* PM-5263 - TokenSvc - clean up promise<any> and replace with promise<void>

* PM-5263 - TokenSvc abstraction - more docs.

* PM-5263  - Clean up TODO as I've tested every method in token svc.

* PM-5263 - (1) Extract JWT decode logic into auth owned utility function out of the token service (2) Update TokenService decode logic to use new utility function (3) Update LastPassDirectImportService + vault.ts to use new utility function and remove token service dependency.  (4) Update tests + migrate tests to new utility test file.

* PM-5263 - Rename decodeJwtTokenToJson to decode-jwt-token-to-json to meet lint rules excluding capitals

* PM-5263 - TokenSvc + tests - fix all get methods to return undefined like they did before instead of throwing an error if a user id isn't provided.

* PM-5263 - Services.module - add missing token service dep

* PM-5263 - Update token svc migrations to be 32nd migration

* PM-5263 - Popup - Services.module - Remove token service as it no longer requires a background service due to the migration to state provider. The service definition in jslib-services module is enough.

* PM-5263 - BaseLoginStrategy - Extract email out of getTwoFactorToken method call for easier debugging.

* PM-5263 - Login Comp - Set email into memory on login service so that base login strategy can access user email for looking up 2FA token stored in global state.

* PM-5263 - (1) LoginComp - remove loginSvc.setEmail call as no longer necessary + introduced issues w/ popup and background in browser extension (2) AuthReq & Password login strategies now just pass in email to buildTwoFactor method.

* PM-5263 - SsoLoginSvc + abstraction - Add key definition and get/set methods for saving user email in session storage so it persists across the SSO redirect.

* PM-5263 - Base Login Strategy - BuildTwoFactor - only try to get 2FA token if we have an email to look up their token

* PM-5263 - Remove LoginService dependency from LoginStrategyService

* PM-5263 - (1) Save off user email when they click enterprise SSO on all clients in login comp (2) Retrieve it and pass it into login strategy in SSO comp

* PM-5263 - (1) TokenSvc - update 2FA token methods to be more safe in case user removes record from local storage (2) Add test cases + missing clearTwoFactorToken tests

* PM-5263 - Browser SSO login - save user email for browser SSO process

* PM-5263 - Finish removing login service from login strategy tests.

* PM-5263 - More removals of the login service from the login strategy tests.

* PM-5263 - Main.ts - platformUtilsSvc no longer used in TokenSvc so remove it from desktop main.ts

* PM-5263 - Fix failing login strategy service tests

* PM-5263 - Bump token svc migration values to migration 35 after merging in main

* PM-5263 - Bump token svc migration version

* PM-5263 - TokenService.clearTwoFactorToken - use delete instead of setting values to null per discussion with Justin

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>

* PM-5263 - TokenSvc + decode JWT token tests - anonymize my information

Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>

* PM-5263 - TokenSvc tests - update clear token tests based on actual deletion

* PM-5263 - Add docs per PR feedback

* PM-5263 - (1) Move ownership of clearing two factor token on rejection from server to base login strategy (2) Each login strategy that supports remember 2FA logic now persists user entered email in its data (3) Base login strategy processTwoFactorResponse now clears 2FA token (4) Updated base login strategy tests to affirm the clearing of the 2FA token

* Update libs/auth/src/common/login-strategies/login.strategy.ts

Co-authored-by: Jake Fink <jfink@bitwarden.com>

* Update libs/auth/src/common/login-strategies/password-login.strategy.ts

Co-authored-by: Jake Fink <jfink@bitwarden.com>

* PM-5263 - Login Strategy - per PR feedback, add jsdoc comments to each method I've touched for this PR.

* PM-5263 - (1) TokenSvc - adjust setTokens, setAccessToken, setRefreshToken, and clearRefreshToken based on PR feedback to remove optional user ids where possible and improve public interface (2) TokenSvc Abstraction - update docs and abstractions based on removed user ids and changed logic (3) TokenSvc tests - update tests to add new test cases, remove no longer relevant ones, and update test names.

* PM-5263 - Bump migrations again

---------

Co-authored-by: Matt Gibson <git@mgibson.dev>
Co-authored-by: Jake Fink <jlf0dev@users.noreply.github.com>
Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
Co-authored-by: Jake Fink <jfink@bitwarden.com>
2024-03-15 11:50:04 -04:00
rr-bw 65b7ca7177
[PM-5266] Create Avatar Service (#7905)
* rename file, move, and update imports

* refactor and implement StateProvider

* remove comments

* add migration

* use 'disk-local' for web

* add JSDoc comments

* move AvatarService before SyncService

* create factory

* replace old method with observable in story

* fix tests

* add tests for migration

* receive most recent avatarColor emission

* move logic to component

* fix CLI dependency

* remove BehaviorSubject

* cleanup

* use UserKeyDefinition

* avoid extra write

* convert to observable

* fix tests
2024-03-14 09:56:48 -07:00
Jake Fink a0e0637bb6
[PM-5255, PM-3339] Refactor login strategy to use state providers (#7821)
* add key definition and StrategyData classes

* use state providers for login strategies

* serialize login data for cache

* use state providers for auth request notification

* fix registrations

* add docs to abstraction

* fix sso strategy

* fix password login strategy tests

* fix base login strategy tests

* fix user api login strategy tests

* PM-3339 add tests for admin auth request in sso strategy

* fix auth request login strategy tests

* fix webauthn login strategy tests

* create login strategy state

* use barrel file in common/spec

* test login strategy cache deserialization

* use global state provider

* add test for login strategy service

* fix auth request storage

* add recursive prototype checking and json deserializers to nested objects

* fix CLI

* Create wrapper for login strategy cache

* use behavior subjects in strategies instead of global state

* rename userApi to userApiKey

* pr feedback

* fix tests

* fix deserialization tests

* fix tests

---------

Co-authored-by: rr-bw <102181210+rr-bw@users.noreply.github.com>
2024-03-12 14:19:50 -04:00
rr-bw 790b310d22
[PM-5270] Update PasswordResetEnrollmentService to take dependency on AccountService (#8080)
* take dependency on AccountService

* update test
2024-03-12 10:11:42 -07:00
Jake Fink 1435203e12
[PM-5499] Create Auth Request Service (#8056)
* create auth request service

* copy methods from auth crypto service

* register new auth request service

* remove refs to auth request crypto service

* remove auth request crypto service

* remove passwordless login method from login strategy service

* add docs to auth request service
2024-02-26 10:07:08 -05:00
Jake Fink 19a373d87e
[PM-6211] Create key generation service (#7939)
* create key generation service

* replace old key generation service and add references

* use key generation service in key connector service

* use key generation service in send service

* user key generation service in access service

* use key generation service in device trust service

* fix tests

* fix browser

* add createKeyFromMaterial and tests

* create ephemeral key

* fix tests

* rename method and add returns docs

* ignore material in destructure

* modify test

* specify material as key material

* pull out magic strings to properties

* make salt optional and generate if not provided

* fix test

* fix parameters

* update docs to include link to HKDF rfc
2024-02-23 08:48:15 -05:00
Matt Gibson 56bffb04bb
Ps/pm 5533/migrate decrypted user key (#7970)
* Move user key memory state to state providers

Note: state service observable change is because these updates are no longer internal to the class, but reporter directly to account service through crypto service on update of a user key

* remove decrypted user key state

Note, we're going to move the encrypted cryptoSymmetric key (and associated master key encrypted user keys)  as part of the master key service creation. Crypto service will no longer be responsible for the encrypted forms of user key.

* Deprecate notices belong on abstraction

* Allow for single-direction status updates

This is necessary since we don't want to have to guarantee that the update to logged out occurs after the update to locked.

* Remove deprecated subject

It turns out the set for cryptoMasterKey was also unused 🎉
2024-02-22 15:07:26 -05:00
Todd Martin bdc951194e
[PM-5800] Remove passwordless-login feature flag (#7626)
* Removed passwordless-login feature flag

* Removed conditional on login component.

* Added back reference accidentally deleted.

* Fixed initialization of the service in tests.

* Removed unused private variable.

* Updated DI to remove configService

* Undid changes to workspace file.

* Undid all changes to workspace file

* Undid merge changes to collection dialog

* Linting
2024-02-13 11:15:16 -05:00
Jake Fink 304c492f24
[PM-5364] Create SSO Login Service and add state ownership (#7485)
* create sso service

* rename sso service to sso-login service

* rename service

* add references to sso login service and update state calls

* fix browser

* fix desktop

* return promises

* remove sso state from account and global objects

* more descriptive org sso identifier method names

* fix sso tests

* fix tests
2024-02-08 12:44:35 -05:00