diff --git a/.github/workflows/scan.yml b/.github/workflows/scan.yml new file mode 100644 index 00000000000..ea9e69226ad --- /dev/null +++ b/.github/workflows/scan.yml @@ -0,0 +1,72 @@ +name: Scan + +on: + workflow_dispatch: + push: + branches: + - "main" + - "rc" + - "hotfix-rc" + pull_request_target: + types: [opened, synchronize] + +permissions: read-all + +jobs: + check-run: + name: Check PR run + uses: bitwarden/gh-actions/.github/workflows/check-run.yml@main + + sast: + name: SAST scan + runs-on: ubuntu-22.04 + needs: check-run + permissions: + security-events: write + + steps: + - name: Check out repo + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + ref: ${{ github.event.pull_request.head.sha }} + + - name: Scan with Checkmarx + uses: checkmarx/ast-github-action@749fec53e0db0f6404a97e2e0807c3e80e3583a7 #2.0.23 + env: + INCREMENTAL: "${{ contains(github.event_name, 'pull_request') && '--sast-incremental' || '' }}" + with: + project_name: ${{ github.repository }} + cx_tenant: ${{ secrets.CHECKMARX_TENANT }} + base_uri: https://ast.checkmarx.net/ + cx_client_id: ${{ secrets.CHECKMARX_CLIENT_ID }} + cx_client_secret: ${{ secrets.CHECKMARX_SECRET }} + additional_params: --report-format sarif --output-path . ${{ env.INCREMENTAL }} + + - name: Upload Checkmarx results to GitHub + uses: github/codeql-action/upload-sarif@8a470fddafa5cbb6266ee11b37ef4d8aae19c571 # v3.24.6 + with: + sarif_file: cx_result.sarif + + quality: + name: Quality scan + runs-on: ubuntu-22.04 + needs: check-run + + steps: + - name: Check out repo + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.sha }} + + - name: Scan with SonarCloud + uses: sonarsource/sonarcloud-github-action@49e6cd3b187936a73b8280d59ffd9da69df63ec9 # v2.1.1 + env: + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: > + -Dsonar.organization=${{ github.repository_owner }} + -Dsonar.projectKey=${{ github.repository_owner }}_${{ github.event.repository.name }} + -Dsonar.test.inclusions=**/*.spec.ts + -Dsonar.tests=. diff --git a/apps/browser/src/admin-console/background/service-factories/organization-service.factory.ts b/apps/browser/src/admin-console/background/service-factories/organization-service.factory.ts index c77508b0f88..b7f6f98ea23 100644 --- a/apps/browser/src/admin-console/background/service-factories/organization-service.factory.ts +++ b/apps/browser/src/admin-console/background/service-factories/organization-service.factory.ts @@ -1,4 +1,5 @@ import { OrganizationService as AbstractOrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; +import { OrganizationService } from "@bitwarden/common/admin-console/services/organization/organization.service"; import { FactoryOptions, @@ -6,11 +7,7 @@ import { factory, } from "../../../platform/background/service-factories/factory-options"; import { stateProviderFactory } from "../../../platform/background/service-factories/state-provider.factory"; -import { - stateServiceFactory, - StateServiceInitOptions, -} from "../../../platform/background/service-factories/state-service.factory"; -import { BrowserOrganizationService } from "../../services/browser-organization.service"; +import { StateServiceInitOptions } from "../../../platform/background/service-factories/state-service.factory"; type OrganizationServiceFactoryOptions = FactoryOptions; @@ -25,10 +22,6 @@ export function organizationServiceFactory( cache, "organizationService", opts, - async () => - new BrowserOrganizationService( - await stateServiceFactory(cache, opts), - await stateProviderFactory(cache, opts), - ), + async () => new OrganizationService(await stateProviderFactory(cache, opts)), ); } diff --git a/apps/browser/src/admin-console/services/browser-organization.service.ts b/apps/browser/src/admin-console/services/browser-organization.service.ts deleted file mode 100644 index 6294756cdf7..00000000000 --- a/apps/browser/src/admin-console/services/browser-organization.service.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { BehaviorSubject } from "rxjs"; - -import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; -import { OrganizationService } from "@bitwarden/common/admin-console/services/organization/organization.service"; - -import { browserSession, sessionSync } from "../../platform/decorators/session-sync-observable"; - -@browserSession -export class BrowserOrganizationService extends OrganizationService { - @sessionSync({ initializer: Organization.fromJSON, initializeAs: "array" }) - protected _organizations: BehaviorSubject; -} diff --git a/apps/browser/src/auth/background/service-factories/login-strategy-service.factory.ts b/apps/browser/src/auth/background/service-factories/login-strategy-service.factory.ts index 078bfb8a637..b0ae87a75f7 100644 --- a/apps/browser/src/auth/background/service-factories/login-strategy-service.factory.ts +++ b/apps/browser/src/auth/background/service-factories/login-strategy-service.factory.ts @@ -9,6 +9,7 @@ import { ApiServiceInitOptions, } from "../../../platform/background/service-factories/api-service.factory"; import { appIdServiceFactory } from "../../../platform/background/service-factories/app-id-service.factory"; +import { billingAccountProfileStateServiceFactory } from "../../../platform/background/service-factories/billing-account-profile-state-service.factory"; import { CryptoServiceInitOptions, cryptoServiceFactory, @@ -119,6 +120,7 @@ export function loginStrategyServiceFactory( await deviceTrustCryptoServiceFactory(cache, opts), await authRequestServiceFactory(cache, opts), await globalStateProviderFactory(cache, opts), + await billingAccountProfileStateServiceFactory(cache, opts), ), ); } diff --git a/apps/browser/src/auth/background/service-factories/token-service.factory.ts b/apps/browser/src/auth/background/service-factories/token-service.factory.ts index 476b8e2d785..25c30460f06 100644 --- a/apps/browser/src/auth/background/service-factories/token-service.factory.ts +++ b/apps/browser/src/auth/background/service-factories/token-service.factory.ts @@ -7,13 +7,29 @@ import { factory, } from "../../../platform/background/service-factories/factory-options"; import { - stateServiceFactory, - StateServiceInitOptions, -} from "../../../platform/background/service-factories/state-service.factory"; + GlobalStateProviderInitOptions, + globalStateProviderFactory, +} from "../../../platform/background/service-factories/global-state-provider.factory"; +import { + PlatformUtilsServiceInitOptions, + platformUtilsServiceFactory, +} from "../../../platform/background/service-factories/platform-utils-service.factory"; +import { + SingleUserStateProviderInitOptions, + singleUserStateProviderFactory, +} from "../../../platform/background/service-factories/single-user-state-provider.factory"; +import { + SecureStorageServiceInitOptions, + secureStorageServiceFactory, +} from "../../../platform/background/service-factories/storage-service.factory"; type TokenServiceFactoryOptions = FactoryOptions; -export type TokenServiceInitOptions = TokenServiceFactoryOptions & StateServiceInitOptions; +export type TokenServiceInitOptions = TokenServiceFactoryOptions & + SingleUserStateProviderInitOptions & + GlobalStateProviderInitOptions & + PlatformUtilsServiceInitOptions & + SecureStorageServiceInitOptions; export function tokenServiceFactory( cache: { tokenService?: AbstractTokenService } & CachedServices, @@ -23,6 +39,12 @@ export function tokenServiceFactory( cache, "tokenService", opts, - async () => new TokenService(await stateServiceFactory(cache, opts)), + async () => + new TokenService( + await singleUserStateProviderFactory(cache, opts), + await globalStateProviderFactory(cache, opts), + (await platformUtilsServiceFactory(cache, opts)).supportsSecureStorage(), + await secureStorageServiceFactory(cache, opts), + ), ); } diff --git a/apps/browser/src/auth/popup/login.component.ts b/apps/browser/src/auth/popup/login.component.ts index 857dae66306..c1dd9526589 100644 --- a/apps/browser/src/auth/popup/login.component.ts +++ b/apps/browser/src/auth/popup/login.component.ts @@ -91,6 +91,8 @@ export class LoginComponent extends BaseLoginComponent { } async launchSsoBrowser() { + // Save off email for SSO + await this.ssoLoginService.setSsoEmail(this.formGroup.value.email); await this.loginService.saveEmailSettings(); // Generate necessary sso params const passwordOptions: any = { diff --git a/apps/browser/src/autofill/background/overlay.background.spec.ts b/apps/browser/src/autofill/background/overlay.background.spec.ts index f0da1af11d4..ebad626d8df 100644 --- a/apps/browser/src/autofill/background/overlay.background.spec.ts +++ b/apps/browser/src/autofill/background/overlay.background.spec.ts @@ -8,11 +8,21 @@ import { AutofillOverlayVisibility, } from "@bitwarden/common/autofill/constants"; import { AutofillSettingsService } from "@bitwarden/common/autofill/services/autofill-settings.service"; +import { + DefaultDomainSettingsService, + DomainSettingsService, +} from "@bitwarden/common/autofill/services/domain-settings.service"; import { ThemeType } from "@bitwarden/common/platform/enums"; +import { Utils } from "@bitwarden/common/platform/misc/utils"; import { EnvironmentService } from "@bitwarden/common/platform/services/environment.service"; import { I18nService } from "@bitwarden/common/platform/services/i18n.service"; import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service"; -import { SettingsService } from "@bitwarden/common/services/settings.service"; +import { + FakeStateProvider, + FakeAccountService, + mockAccountServiceWith, +} from "@bitwarden/common/spec"; +import { UserId } from "@bitwarden/common/types/guid"; import { CipherType } from "@bitwarden/common/vault/enums"; import { CipherRepromptType } from "@bitwarden/common/vault/enums/cipher-reprompt-type"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; @@ -41,6 +51,10 @@ import OverlayBackground from "./overlay.background"; const iconServerUrl = "https://icons.bitwarden.com/"; describe("OverlayBackground", () => { + const mockUserId = Utils.newGuid() as UserId; + const accountService: FakeAccountService = mockAccountServiceWith(mockUserId); + const fakeStateProvider: FakeStateProvider = new FakeStateProvider(accountService); + let domainSettingsService: DomainSettingsService; let buttonPortSpy: chrome.runtime.Port; let listPortSpy: chrome.runtime.Port; let overlayBackground: OverlayBackground; @@ -50,7 +64,6 @@ describe("OverlayBackground", () => { const environmentService = mock({ getIconsUrl: () => iconServerUrl, }); - const settingsService = mock(); const stateService = mock(); const autofillSettingsService = mock(); const i18nService = mock(); @@ -72,12 +85,13 @@ describe("OverlayBackground", () => { }; beforeEach(() => { + domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider); overlayBackground = new OverlayBackground( cipherService, autofillService, authService, environmentService, - settingsService, + domainSettingsService, stateService, autofillSettingsService, i18nService, @@ -90,6 +104,7 @@ describe("OverlayBackground", () => { .mockResolvedValue(AutofillOverlayVisibility.OnFieldFocus); themeStateService.selectedTheme$ = of(ThemeType.Light); + domainSettingsService.showFavicons$ = of(true); void overlayBackground.init(); }); @@ -274,7 +289,7 @@ describe("OverlayBackground", () => { card: { subTitle: "Mastercard, *1234" }, }); - it("formats and returns the cipher data", () => { + it("formats and returns the cipher data", async () => { overlayBackground["overlayLoginCiphers"] = new Map([ ["overlay-cipher-0", cipher2], ["overlay-cipher-1", cipher1], @@ -282,7 +297,7 @@ describe("OverlayBackground", () => { ["overlay-cipher-3", cipher4], ]); - const overlayCipherData = overlayBackground["getOverlayCipherData"](); + const overlayCipherData = await overlayBackground["getOverlayCipherData"](); expect(overlayCipherData).toStrictEqual([ { diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts index 00be50c3284..6c3aea9dc71 100644 --- a/apps/browser/src/autofill/background/overlay.background.ts +++ b/apps/browser/src/autofill/background/overlay.background.ts @@ -1,10 +1,10 @@ import { firstValueFrom } from "rxjs"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { SHOW_AUTOFILL_BUTTON } from "@bitwarden/common/autofill/constants"; import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service"; +import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { InlineMenuVisibilitySetting } from "@bitwarden/common/autofill/types"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -92,7 +92,7 @@ class OverlayBackground implements OverlayBackgroundInterface { private autofillService: AutofillService, private authService: AuthService, private environmentService: EnvironmentService, - private settingsService: SettingsService, + private domainSettingsService: DomainSettingsService, private stateService: StateService, private autofillSettingsService: AutofillSettingsServiceAbstraction, private i18nService: I18nService, @@ -145,7 +145,7 @@ class OverlayBackground implements OverlayBackgroundInterface { this.overlayLoginCiphers.set(`overlay-cipher-${cipherIndex}`, ciphersViews[cipherIndex]); } - const ciphers = this.getOverlayCipherData(); + const ciphers = await this.getOverlayCipherData(); this.overlayListPort?.postMessage({ command: "updateOverlayListCiphers", ciphers }); await BrowserApi.tabSendMessageData(currentTab, "updateIsOverlayCiphersPopulated", { isOverlayCiphersPopulated: Boolean(ciphers.length), @@ -156,8 +156,8 @@ class OverlayBackground implements OverlayBackgroundInterface { * Strips out unnecessary data from the ciphers and returns an array of * objects that contain the cipher data needed for the overlay list. */ - private getOverlayCipherData(): OverlayCipherData[] { - const isFaviconDisabled = this.settingsService.getDisableFavicon(); + private async getOverlayCipherData(): Promise { + const showFavicons = await firstValueFrom(this.domainSettingsService.showFavicons$); const overlayCiphersArray = Array.from(this.overlayLoginCiphers); const overlayCipherData = []; let loginCipherIcon: WebsiteIconData; @@ -165,7 +165,7 @@ class OverlayBackground implements OverlayBackgroundInterface { for (let cipherIndex = 0; cipherIndex < overlayCiphersArray.length; cipherIndex++) { const [overlayCipherId, cipher] = overlayCiphersArray[cipherIndex]; if (!loginCipherIcon && cipher.type === CipherType.Login) { - loginCipherIcon = buildCipherIcon(this.iconsServerUrl, cipher, isFaviconDisabled); + loginCipherIcon = buildCipherIcon(this.iconsServerUrl, cipher, showFavicons); } overlayCipherData.push({ @@ -177,7 +177,7 @@ class OverlayBackground implements OverlayBackgroundInterface { icon: cipher.type === CipherType.Login ? loginCipherIcon - : buildCipherIcon(this.iconsServerUrl, cipher, isFaviconDisabled), + : buildCipherIcon(this.iconsServerUrl, cipher, showFavicons), login: cipher.type === CipherType.Login ? { username: cipher.login.username } : null, card: cipher.type === CipherType.Card ? cipher.card.subTitle : null, }); @@ -699,7 +699,7 @@ class OverlayBackground implements OverlayBackgroundInterface { styleSheetUrl: chrome.runtime.getURL(`overlay/${isOverlayListPort ? "list" : "button"}.css`), theme: await firstValueFrom(this.themeStateService.selectedTheme$), translations: this.getTranslations(), - ciphers: isOverlayListPort ? this.getOverlayCipherData() : null, + ciphers: isOverlayListPort ? await this.getOverlayCipherData() : null, }); this.updateOverlayPosition({ overlayElement: isOverlayListPort diff --git a/apps/browser/src/autofill/background/service_factories/autofill-service.factory.ts b/apps/browser/src/autofill/background/service_factories/autofill-service.factory.ts index bbbca2f16a4..d62e4857224 100644 --- a/apps/browser/src/autofill/background/service_factories/autofill-service.factory.ts +++ b/apps/browser/src/autofill/background/service_factories/autofill-service.factory.ts @@ -6,6 +6,7 @@ import { EventCollectionServiceInitOptions, eventCollectionServiceFactory, } from "../../../background/service-factories/event-collection-service.factory"; +import { billingAccountProfileStateServiceFactory } from "../../../platform/background/service-factories/billing-account-profile-state-service.factory"; import { CachedServices, factory, @@ -69,6 +70,7 @@ export function autofillServiceFactory( await logServiceFactory(cache, opts), await domainSettingsServiceFactory(cache, opts), await userVerificationServiceFactory(cache, opts), + await billingAccountProfileStateServiceFactory(cache, opts), ), ); } diff --git a/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts b/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts index b827788d75c..67637da2fdd 100644 --- a/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts +++ b/apps/browser/src/autofill/browser/main-context-menu-handler.spec.ts @@ -3,6 +3,7 @@ import { of } from "rxjs"; import { NOOP_COMMAND_SUFFIX } from "@bitwarden/common/autofill/constants"; import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { CipherType } from "@bitwarden/common/vault/enums"; @@ -18,6 +19,7 @@ describe("context-menu", () => { let autofillSettingsService: MockProxy; let i18nService: MockProxy; let logService: MockProxy; + let billingAccountProfileStateService: MockProxy; let removeAllSpy: jest.SpyInstance void]>; let createSpy: jest.SpyInstance< @@ -32,6 +34,7 @@ describe("context-menu", () => { autofillSettingsService = mock(); i18nService = mock(); logService = mock(); + billingAccountProfileStateService = mock(); removeAllSpy = jest .spyOn(chrome.contextMenus, "removeAll") @@ -50,6 +53,7 @@ describe("context-menu", () => { autofillSettingsService, i18nService, logService, + billingAccountProfileStateService, ); autofillSettingsService.enableContextMenu$ = of(true); }); @@ -66,7 +70,7 @@ describe("context-menu", () => { }); it("has menu enabled, but does not have premium", async () => { - stateService.getCanAccessPremium.mockResolvedValue(false); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(false); const createdMenu = await sut.init(); expect(createdMenu).toBeTruthy(); @@ -74,7 +78,7 @@ describe("context-menu", () => { }); it("has menu enabled and has premium", async () => { - stateService.getCanAccessPremium.mockResolvedValue(true); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); const createdMenu = await sut.init(); expect(createdMenu).toBeTruthy(); @@ -128,7 +132,7 @@ describe("context-menu", () => { }); it("create entry for each cipher piece", async () => { - stateService.getCanAccessPremium.mockResolvedValue(true); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); await sut.loadOptions("TEST_TITLE", "1", createCipher()); @@ -137,7 +141,7 @@ describe("context-menu", () => { }); it("creates a login/unlock item for each context menu action option when user is not authenticated", async () => { - stateService.getCanAccessPremium.mockResolvedValue(true); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); await sut.loadOptions("TEST_TITLE", "NOOP"); diff --git a/apps/browser/src/autofill/browser/main-context-menu-handler.ts b/apps/browser/src/autofill/browser/main-context-menu-handler.ts index 998b5c7258b..9422756e07b 100644 --- a/apps/browser/src/autofill/browser/main-context-menu-handler.ts +++ b/apps/browser/src/autofill/browser/main-context-menu-handler.ts @@ -17,6 +17,7 @@ import { SEPARATOR_ID, } from "@bitwarden/common/autofill/constants"; import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { StateFactory } from "@bitwarden/common/platform/factories/state-factory"; @@ -27,6 +28,7 @@ import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { autofillSettingsServiceFactory } from "../../autofill/background/service_factories/autofill-settings-service.factory"; import { Account } from "../../models/account"; +import { billingAccountProfileStateServiceFactory } from "../../platform/background/service-factories/billing-account-profile-state-service.factory"; import { CachedServices } from "../../platform/background/service-factories/factory-options"; import { i18nServiceFactory, @@ -163,6 +165,7 @@ export class MainContextMenuHandler { private autofillSettingsService: AutofillSettingsServiceAbstraction, private i18nService: I18nService, private logService: LogService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} static async mv3Create(cachedServices: CachedServices) { @@ -184,6 +187,11 @@ export class MainContextMenuHandler { stateServiceOptions: { stateFactory: stateFactory, }, + platformUtilsServiceOptions: { + clipboardWriteCallback: () => Promise.resolve(), + biometricCallback: () => Promise.resolve(false), + win: self, + }, }; return new MainContextMenuHandler( @@ -191,6 +199,7 @@ export class MainContextMenuHandler { await autofillSettingsServiceFactory(cachedServices, serviceOptions), await i18nServiceFactory(cachedServices, serviceOptions), await logServiceFactory(cachedServices, serviceOptions), + await billingAccountProfileStateServiceFactory(cachedServices, serviceOptions), ); } @@ -212,7 +221,10 @@ export class MainContextMenuHandler { try { for (const options of this.initContextMenuItems) { - if (options.checkPremiumAccess && !(await this.stateService.getCanAccessPremium())) { + if ( + options.checkPremiumAccess && + !(await firstValueFrom(this.billingAccountProfileStateService.hasPremiumFromAnySource$)) + ) { continue; } @@ -307,7 +319,9 @@ export class MainContextMenuHandler { await createChildItem(COPY_USERNAME_ID); } - const canAccessPremium = await this.stateService.getCanAccessPremium(); + const canAccessPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); if (canAccessPremium && (!cipher || !Utils.isNullOrEmpty(cipher.login?.totp))) { await createChildItem(COPY_VERIFICATION_CODE_ID); } diff --git a/apps/browser/src/autofill/services/autofill.service.spec.ts b/apps/browser/src/autofill/services/autofill.service.spec.ts index eb70f0f7dc4..f6c1fa90677 100644 --- a/apps/browser/src/autofill/services/autofill.service.spec.ts +++ b/apps/browser/src/autofill/services/autofill.service.spec.ts @@ -8,6 +8,7 @@ import { DefaultDomainSettingsService, DomainSettingsService, } from "@bitwarden/common/autofill/services/domain-settings.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType } from "@bitwarden/common/enums"; import { UriMatchStrategy } from "@bitwarden/common/models/domain/domain-service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -72,6 +73,7 @@ describe("AutofillService", () => { const eventCollectionService = mock(); const logService = mock(); const userVerificationService = mock(); + const billingAccountProfileStateService = mock(); beforeEach(() => { autofillService = new AutofillService( @@ -83,6 +85,7 @@ describe("AutofillService", () => { logService, domainSettingsService, userVerificationService, + billingAccountProfileStateService, ); domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider); @@ -476,6 +479,7 @@ describe("AutofillService", () => { it("throws an error if an autofill did not occur for any of the passed pages", async () => { autofillOptions.tab.url = "https://a-different-url.com"; + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); try { await autofillService.doAutoFill(autofillOptions); @@ -487,7 +491,6 @@ describe("AutofillService", () => { }); it("will autofill login data for a page", async () => { - jest.spyOn(stateService, "getCanAccessPremium"); jest.spyOn(autofillService as any, "generateFillScript"); jest.spyOn(autofillService as any, "generateLoginFillScript"); jest.spyOn(logService, "info"); @@ -497,8 +500,6 @@ describe("AutofillService", () => { const autofillResult = await autofillService.doAutoFill(autofillOptions); const currentAutofillPageDetails = autofillOptions.pageDetails[0]; - expect(stateService.getCanAccessPremium).toHaveBeenCalled(); - expect(autofillService["getDefaultUriMatchStrategy"]).toHaveBeenCalled(); expect(autofillService["generateFillScript"]).toHaveBeenCalledWith( currentAutofillPageDetails.details, { @@ -660,7 +661,7 @@ describe("AutofillService", () => { it("returns a TOTP value", async () => { const totpCode = "123456"; autofillOptions.cipher.login.totp = "totp"; - jest.spyOn(stateService, "getCanAccessPremium").mockResolvedValue(true); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); jest.spyOn(autofillService, "getShouldAutoCopyTotp").mockResolvedValue(true); jest.spyOn(totpService, "getCode").mockResolvedValue(totpCode); @@ -673,7 +674,7 @@ describe("AutofillService", () => { it("does not return a TOTP value if the user does not have premium features", async () => { autofillOptions.cipher.login.totp = "totp"; - jest.spyOn(stateService, "getCanAccessPremium").mockResolvedValue(false); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(false); jest.spyOn(autofillService, "getShouldAutoCopyTotp").mockResolvedValue(true); const autofillResult = await autofillService.doAutoFill(autofillOptions); @@ -707,7 +708,7 @@ describe("AutofillService", () => { it("returns a null value if the user cannot access premium and the organization does not use TOTP", async () => { autofillOptions.cipher.login.totp = "totp"; autofillOptions.cipher.organizationUseTotp = false; - jest.spyOn(stateService, "getCanAccessPremium").mockResolvedValueOnce(false); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(false); const autofillResult = await autofillService.doAutoFill(autofillOptions); @@ -717,13 +718,12 @@ describe("AutofillService", () => { it("returns a null value if the user has disabled `auto TOTP copy`", async () => { autofillOptions.cipher.login.totp = "totp"; autofillOptions.cipher.organizationUseTotp = true; - jest.spyOn(stateService, "getCanAccessPremium").mockResolvedValue(true); + billingAccountProfileStateService.hasPremiumFromAnySource$ = of(true); jest.spyOn(autofillService, "getShouldAutoCopyTotp").mockResolvedValue(false); jest.spyOn(totpService, "getCode"); const autofillResult = await autofillService.doAutoFill(autofillOptions); - expect(stateService.getCanAccessPremium).toHaveBeenCalled(); expect(autofillService.getShouldAutoCopyTotp).toHaveBeenCalled(); expect(totpService.getCode).not.toHaveBeenCalled(); expect(autofillResult).toBeNull(); diff --git a/apps/browser/src/autofill/services/autofill.service.ts b/apps/browser/src/autofill/services/autofill.service.ts index 3a809af0c38..e353a34ea07 100644 --- a/apps/browser/src/autofill/services/autofill.service.ts +++ b/apps/browser/src/autofill/services/autofill.service.ts @@ -5,6 +5,7 @@ import { UserVerificationService } from "@bitwarden/common/auth/abstractions/use import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service"; import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { InlineMenuVisibilitySetting } from "@bitwarden/common/autofill/types"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType } from "@bitwarden/common/enums"; import { UriMatchStrategySetting, @@ -55,6 +56,7 @@ export default class AutofillService implements AutofillServiceInterface { private logService: LogService, private domainSettingsService: DomainSettingsService, private userVerificationService: UserVerificationService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} /** @@ -240,7 +242,9 @@ export default class AutofillService implements AutofillServiceInterface { let totp: string | null = null; - const canAccessPremium = await this.stateService.getCanAccessPremium(); + const canAccessPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); const defaultUriMatch = await this.getDefaultUriMatchStrategy(); if (!canAccessPremium) { diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index e0dd4e57916..b3b9be3dcd4 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -14,12 +14,12 @@ import { EventCollectionService as EventCollectionServiceAbstraction } from "@bi import { EventUploadService as EventUploadServiceAbstraction } from "@bitwarden/common/abstractions/event/event-upload.service"; import { NotificationsService as NotificationsServiceAbstraction } from "@bitwarden/common/abstractions/notifications.service"; import { SearchService as SearchServiceAbstraction } from "@bitwarden/common/abstractions/search.service"; -import { SettingsService as SettingsServiceAbstraction } from "@bitwarden/common/abstractions/settings.service"; import { VaultTimeoutSettingsService as VaultTimeoutSettingsServiceAbstraction } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout-settings.service"; import { InternalOrganizationServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { PolicyApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy-api.service.abstraction"; import { InternalPolicyService as InternalPolicyServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { ProviderService as ProviderServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/provider.service"; +import { OrganizationService } from "@bitwarden/common/admin-console/services/organization/organization.service"; import { PolicyApiService } from "@bitwarden/common/admin-console/services/policy/policy-api.service"; import { PolicyService } from "@bitwarden/common/admin-console/services/policy/policy.service"; import { ProviderService } from "@bitwarden/common/admin-console/services/provider.service"; @@ -64,6 +64,8 @@ import { UserNotificationSettingsService, UserNotificationSettingsServiceAbstraction, } from "@bitwarden/common/autofill/services/user-notification-settings.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; +import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service"; import { AppIdService as AppIdServiceAbstraction } from "@bitwarden/common/platform/abstractions/app-id.service"; import { ConfigApiServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config-api.service.abstraction"; import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from "@bitwarden/common/platform/abstractions/crypto-function.service"; @@ -180,7 +182,6 @@ import { VaultExportServiceAbstraction, } from "@bitwarden/vault-export-core"; -import { BrowserOrganizationService } from "../admin-console/services/browser-organization.service"; import ContextMenusBackground from "../autofill/background/context-menus.background"; import NotificationBackground from "../autofill/background/notification.background"; import OverlayBackground from "../autofill/background/overlay.background"; @@ -211,7 +212,6 @@ import { BrowserPlatformUtilsService } from "../platform/services/platform-utils import { BackgroundDerivedStateProvider } from "../platform/state/background-derived-state.provider"; import { BackgroundMemoryStorageService } from "../platform/storage/background-memory-storage.service"; import { BrowserSendService } from "../services/browser-send.service"; -import { BrowserSettingsService } from "../services/browser-settings.service"; import VaultTimeoutService from "../services/vault-timeout/vault-timeout.service"; import FilelessImporterBackground from "../tools/background/fileless-importer.background"; import { BrowserFido2UserInterfaceService } from "../vault/fido2/browser-fido2-user-interface.service"; @@ -240,7 +240,6 @@ export default class MainBackground { appIdService: AppIdServiceAbstraction; apiService: ApiServiceAbstraction; environmentService: BrowserEnvironmentService; - settingsService: SettingsServiceAbstraction; cipherService: CipherServiceAbstraction; folderService: InternalFolderServiceAbstraction; collectionService: CollectionServiceAbstraction; @@ -311,6 +310,7 @@ export default class MainBackground { biometricStateService: BiometricStateService; stateEventRunnerService: StateEventRunnerService; ssoLoginService: SsoLoginServiceAbstraction; + billingAccountProfileStateService: BillingAccountProfileStateService; onUpdatedRan: boolean; onReplacedRan: boolean; @@ -427,6 +427,21 @@ export default class MainBackground { ); this.biometricStateService = new DefaultBiometricStateService(this.stateProvider); + this.userNotificationSettingsService = new UserNotificationSettingsService(this.stateProvider); + this.platformUtilsService = new BackgroundPlatformUtilsService( + this.messagingService, + (clipboardValue, clearMs) => this.clearClipboard(clipboardValue, clearMs), + async () => this.biometricUnlock(), + self, + ); + + this.tokenService = new TokenService( + this.singleUserStateProvider, + this.globalStateProvider, + this.platformUtilsService.supportsSecureStorage(), + this.secureStorageService, + ); + const migrationRunner = new MigrationRunner( this.storageService, this.logService, @@ -441,15 +456,9 @@ export default class MainBackground { new StateFactory(GlobalState, Account), this.accountService, this.environmentService, + this.tokenService, migrationRunner, ); - this.userNotificationSettingsService = new UserNotificationSettingsService(this.stateProvider); - this.platformUtilsService = new BackgroundPlatformUtilsService( - this.messagingService, - (clipboardValue, clearMs) => this.clearClipboard(clipboardValue, clearMs), - async () => this.biometricUnlock(), - self, - ); const themeStateService = new DefaultThemeStateService(this.globalStateProvider); @@ -465,17 +474,17 @@ export default class MainBackground { this.stateProvider, this.biometricStateService, ); - this.tokenService = new TokenService(this.stateService); + this.appIdService = new AppIdService(this.globalStateProvider); this.apiService = new ApiService( this.tokenService, this.platformUtilsService, this.environmentService, this.appIdService, + this.stateService, (expired: boolean) => this.logout(expired), ); this.domainSettingsService = new DefaultDomainSettingsService(this.stateProvider); - this.settingsService = new BrowserSettingsService(this.stateService); this.fileUploadService = new FileUploadService(this.logService); this.cipherFileUploadService = new CipherFileUploadService( this.apiService, @@ -489,10 +498,7 @@ export default class MainBackground { this.stateProvider, ); this.syncNotifierService = new SyncNotifierService(); - this.organizationService = new BrowserOrganizationService( - this.stateService, - this.stateProvider, - ); + this.organizationService = new OrganizationService(this.stateProvider); this.policyService = new PolicyService(this.stateProvider, this.organizationService); this.autofillSettingsService = new AutofillSettingsService( this.stateProvider, @@ -562,6 +568,10 @@ export default class MainBackground { this.stateService, ); + this.billingAccountProfileStateService = new DefaultBillingAccountProfileStateService( + this.activeUserStateProvider, + ); + this.loginStrategyService = new LoginStrategyService( this.cryptoService, this.apiService, @@ -581,6 +591,7 @@ export default class MainBackground { this.deviceTrustCryptoService, this.authRequestService, this.globalStateProvider, + this.billingAccountProfileStateService, ); this.ssoLoginService = new SsoLoginService(this.stateProvider); @@ -708,17 +719,20 @@ export default class MainBackground { this.sendApiService, this.avatarService, logoutCallback, + this.billingAccountProfileStateService, ); this.eventUploadService = new EventUploadService( this.apiService, - this.stateService, + this.stateProvider, this.logService, + this.accountService, ); this.eventCollectionService = new EventCollectionService( this.cipherService, - this.stateService, + this.stateProvider, this.organizationService, this.eventUploadService, + this.accountService, ); this.totpService = new TotpService(this.cryptoFunctionService, this.logService); @@ -731,6 +745,7 @@ export default class MainBackground { this.logService, this.domainSettingsService, this.userVerificationService, + this.billingAccountProfileStateService, ); this.auditService = new AuditService(this.cryptoFunctionService, this.apiService); @@ -791,7 +806,6 @@ export default class MainBackground { this.fido2AuthenticatorService, this.configService, this.authService, - this.stateService, this.vaultSettingsService, this.domainSettingsService, this.logService, @@ -839,7 +853,6 @@ export default class MainBackground { this.cryptoService, this.cryptoFunctionService, this.runtimeBackground, - this.i18nService, this.messagingService, this.appIdService, this.platformUtilsService, @@ -873,7 +886,7 @@ export default class MainBackground { this.autofillService, this.authService, this.environmentService, - this.settingsService, + this.domainSettingsService, this.stateService, this.autofillSettingsService, this.i18nService, @@ -953,6 +966,7 @@ export default class MainBackground { this.autofillSettingsService, this.i18nService, this.logService, + this.billingAccountProfileStateService, ); this.cipherContextMenuHandler = new CipherContextMenuHandler( @@ -1092,7 +1106,7 @@ export default class MainBackground { async logout(expired: boolean, userId?: UserId) { userId ??= (await firstValueFrom(this.accountService.activeAccount$))?.id; - await this.eventUploadService.uploadEvents(userId); + await this.eventUploadService.uploadEvents(userId as UserId); await Promise.all([ this.syncService.setLastSync(new Date(0), userId), diff --git a/apps/browser/src/background/nativeMessaging.background.ts b/apps/browser/src/background/nativeMessaging.background.ts index 620f9735c8b..240fb1dedea 100644 --- a/apps/browser/src/background/nativeMessaging.background.ts +++ b/apps/browser/src/background/nativeMessaging.background.ts @@ -5,7 +5,6 @@ import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authenticatio import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; -import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; @@ -75,7 +74,6 @@ export class NativeMessagingBackground { private cryptoService: CryptoService, private cryptoFunctionService: CryptoFunctionService, private runtimeBackground: RuntimeBackground, - private i18nService: I18nService, private messagingService: MessagingService, private appIdService: AppIdService, private platformUtilsService: PlatformUtilsService, diff --git a/apps/browser/src/background/service-factories/event-collection-service.factory.ts b/apps/browser/src/background/service-factories/event-collection-service.factory.ts index 7ce77da045b..ec892c73ddc 100644 --- a/apps/browser/src/background/service-factories/event-collection-service.factory.ts +++ b/apps/browser/src/background/service-factories/event-collection-service.factory.ts @@ -5,15 +5,14 @@ import { organizationServiceFactory, OrganizationServiceInitOptions, } from "../../admin-console/background/service-factories/organization-service.factory"; +import { accountServiceFactory } from "../../auth/background/service-factories/account-service.factory"; import { FactoryOptions, CachedServices, factory, } from "../../platform/background/service-factories/factory-options"; -import { - stateServiceFactory, - StateServiceInitOptions, -} from "../../platform/background/service-factories/state-service.factory"; +import { stateProviderFactory } from "../../platform/background/service-factories/state-provider.factory"; +import { StateServiceInitOptions } from "../../platform/background/service-factories/state-service.factory"; import { cipherServiceFactory, CipherServiceInitOptions, @@ -43,9 +42,10 @@ export function eventCollectionServiceFactory( async () => new EventCollectionService( await cipherServiceFactory(cache, opts), - await stateServiceFactory(cache, opts), + await stateProviderFactory(cache, opts), await organizationServiceFactory(cache, opts), await eventUploadServiceFactory(cache, opts), + await accountServiceFactory(cache, opts), ), ); } diff --git a/apps/browser/src/background/service-factories/event-upload-service.factory.ts b/apps/browser/src/background/service-factories/event-upload-service.factory.ts index fcaec459c0d..4e1d7949be1 100644 --- a/apps/browser/src/background/service-factories/event-upload-service.factory.ts +++ b/apps/browser/src/background/service-factories/event-upload-service.factory.ts @@ -1,6 +1,7 @@ import { EventUploadService as AbstractEventUploadService } from "@bitwarden/common/abstractions/event/event-upload.service"; import { EventUploadService } from "@bitwarden/common/services/event/event-upload.service"; +import { accountServiceFactory } from "../../auth/background/service-factories/account-service.factory"; import { ApiServiceInitOptions, apiServiceFactory, @@ -14,10 +15,8 @@ import { logServiceFactory, LogServiceInitOptions, } from "../../platform/background/service-factories/log-service.factory"; -import { - stateServiceFactory, - StateServiceInitOptions, -} from "../../platform/background/service-factories/state-service.factory"; +import { stateProviderFactory } from "../../platform/background/service-factories/state-provider.factory"; +import { StateServiceInitOptions } from "../../platform/background/service-factories/state-service.factory"; type EventUploadServiceOptions = FactoryOptions; @@ -37,8 +36,9 @@ export function eventUploadServiceFactory( async () => new EventUploadService( await apiServiceFactory(cache, opts), - await stateServiceFactory(cache, opts), + await stateProviderFactory(cache, opts), await logServiceFactory(cache, opts), + await accountServiceFactory(cache, opts), ), ); } diff --git a/apps/browser/src/background/service-factories/settings-service.factory.ts b/apps/browser/src/background/service-factories/settings-service.factory.ts deleted file mode 100644 index 28e97de51fa..00000000000 --- a/apps/browser/src/background/service-factories/settings-service.factory.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { SettingsService as AbstractSettingsService } from "@bitwarden/common/abstractions/settings.service"; - -import { - FactoryOptions, - CachedServices, - factory, -} from "../../platform/background/service-factories/factory-options"; -import { - stateServiceFactory, - StateServiceInitOptions, -} from "../../platform/background/service-factories/state-service.factory"; -import { BrowserSettingsService } from "../../services/browser-settings.service"; - -type SettingsServiceFactoryOptions = FactoryOptions; - -export type SettingsServiceInitOptions = SettingsServiceFactoryOptions & StateServiceInitOptions; - -export function settingsServiceFactory( - cache: { settingsService?: AbstractSettingsService } & CachedServices, - opts: SettingsServiceInitOptions, -): Promise { - return factory( - cache, - "settingsService", - opts, - async () => new BrowserSettingsService(await stateServiceFactory(cache, opts)), - ); -} diff --git a/apps/browser/src/platform/background/service-factories/api-service.factory.ts b/apps/browser/src/platform/background/service-factories/api-service.factory.ts index 649fe1f7fe6..57cd5004413 100644 --- a/apps/browser/src/platform/background/service-factories/api-service.factory.ts +++ b/apps/browser/src/platform/background/service-factories/api-service.factory.ts @@ -20,6 +20,7 @@ import { PlatformUtilsServiceInitOptions, platformUtilsServiceFactory, } from "./platform-utils-service.factory"; +import { stateServiceFactory, StateServiceInitOptions } from "./state-service.factory"; type ApiServiceFactoryOptions = FactoryOptions & { apiServiceOptions: { @@ -32,7 +33,8 @@ export type ApiServiceInitOptions = ApiServiceFactoryOptions & TokenServiceInitOptions & PlatformUtilsServiceInitOptions & EnvironmentServiceInitOptions & - AppIdServiceInitOptions; + AppIdServiceInitOptions & + StateServiceInitOptions; export function apiServiceFactory( cache: { apiService?: AbstractApiService } & CachedServices, @@ -48,6 +50,7 @@ export function apiServiceFactory( await platformUtilsServiceFactory(cache, opts), await environmentServiceFactory(cache, opts), await appIdServiceFactory(cache, opts), + await stateServiceFactory(cache, opts), opts.apiServiceOptions.logoutCallback, opts.apiServiceOptions.customUserAgent, ), diff --git a/apps/browser/src/platform/background/service-factories/billing-account-profile-state-service.factory.ts b/apps/browser/src/platform/background/service-factories/billing-account-profile-state-service.factory.ts new file mode 100644 index 00000000000..80482eacb67 --- /dev/null +++ b/apps/browser/src/platform/background/service-factories/billing-account-profile-state-service.factory.ts @@ -0,0 +1,28 @@ +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; +import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service"; + +import { activeUserStateProviderFactory } from "./active-user-state-provider.factory"; +import { FactoryOptions, CachedServices, factory } from "./factory-options"; +import { StateProviderInitOptions } from "./state-provider.factory"; + +type BillingAccountProfileStateServiceFactoryOptions = FactoryOptions; + +export type BillingAccountProfileStateServiceInitOptions = + BillingAccountProfileStateServiceFactoryOptions & StateProviderInitOptions; + +export function billingAccountProfileStateServiceFactory( + cache: { + billingAccountProfileStateService?: BillingAccountProfileStateService; + } & CachedServices, + opts: BillingAccountProfileStateServiceInitOptions, +): Promise { + return factory( + cache, + "billingAccountProfileStateService", + opts, + async () => + new DefaultBillingAccountProfileStateService( + await activeUserStateProviderFactory(cache, opts), + ), + ); +} diff --git a/apps/browser/src/platform/background/service-factories/state-service.factory.ts b/apps/browser/src/platform/background/service-factories/state-service.factory.ts index 8bcb65a3209..20a9ac074a7 100644 --- a/apps/browser/src/platform/background/service-factories/state-service.factory.ts +++ b/apps/browser/src/platform/background/service-factories/state-service.factory.ts @@ -5,6 +5,10 @@ import { accountServiceFactory, AccountServiceInitOptions, } from "../../../auth/background/service-factories/account-service.factory"; +import { + tokenServiceFactory, + TokenServiceInitOptions, +} from "../../../auth/background/service-factories/token-service.factory"; import { Account } from "../../../models/account"; import { BrowserStateService } from "../../services/browser-state.service"; @@ -38,6 +42,7 @@ export type StateServiceInitOptions = StateServiceFactoryOptions & LogServiceInitOptions & AccountServiceInitOptions & EnvironmentServiceInitOptions & + TokenServiceInitOptions & MigrationRunnerInitOptions; export async function stateServiceFactory( @@ -57,6 +62,7 @@ export async function stateServiceFactory( opts.stateServiceOptions.stateFactory, await accountServiceFactory(cache, opts), await environmentServiceFactory(cache, opts), + await tokenServiceFactory(cache, opts), await migrationRunnerFactory(cache, opts), opts.stateServiceOptions.useAccountCache, ), diff --git a/apps/browser/src/platform/popup/locales.ts b/apps/browser/src/platform/popup/locales.ts index f24f7368db3..5fea00b97b7 100644 --- a/apps/browser/src/platform/popup/locales.ts +++ b/apps/browser/src/platform/popup/locales.ts @@ -7,6 +7,7 @@ import localeBn from "@angular/common/locales/bn"; import localeBs from "@angular/common/locales/bs"; import localeCa from "@angular/common/locales/ca"; import localeCs from "@angular/common/locales/cs"; +import localeCy from "@angular/common/locales/cy"; import localeDa from "@angular/common/locales/da"; import localeDe from "@angular/common/locales/de"; import localeEl from "@angular/common/locales/el"; @@ -19,6 +20,7 @@ import localeFa from "@angular/common/locales/fa"; import localeFi from "@angular/common/locales/fi"; import localeFil from "@angular/common/locales/fil"; import localeFr from "@angular/common/locales/fr"; +import localeGl from "@angular/common/locales/gl"; import localeHe from "@angular/common/locales/he"; import localeHi from "@angular/common/locales/hi"; import localeHr from "@angular/common/locales/hr"; @@ -33,9 +35,13 @@ import localeKo from "@angular/common/locales/ko"; import localeLt from "@angular/common/locales/lt"; import localeLv from "@angular/common/locales/lv"; import localeMl from "@angular/common/locales/ml"; +import localeMr from "@angular/common/locales/mr"; +import localeMy from "@angular/common/locales/my"; import localeNb from "@angular/common/locales/nb"; +import localeNe from "@angular/common/locales/ne"; import localeNl from "@angular/common/locales/nl"; import localeNn from "@angular/common/locales/nn"; +import localeOr from "@angular/common/locales/or"; import localePl from "@angular/common/locales/pl"; import localePtBr from "@angular/common/locales/pt"; import localePtPt from "@angular/common/locales/pt-PT"; @@ -46,6 +52,7 @@ import localeSk from "@angular/common/locales/sk"; import localeSl from "@angular/common/locales/sl"; import localeSr from "@angular/common/locales/sr"; import localeSv from "@angular/common/locales/sv"; +import localeTe from "@angular/common/locales/te"; import localeTh from "@angular/common/locales/th"; import localeTr from "@angular/common/locales/tr"; import localeUk from "@angular/common/locales/uk"; @@ -61,6 +68,7 @@ registerLocaleData(localeBn, "bn"); registerLocaleData(localeBs, "bs"); registerLocaleData(localeCa, "ca"); registerLocaleData(localeCs, "cs"); +registerLocaleData(localeCy, "cy"); registerLocaleData(localeDa, "da"); registerLocaleData(localeDe, "de"); registerLocaleData(localeEl, "el"); @@ -73,6 +81,7 @@ registerLocaleData(localeFa, "fa"); registerLocaleData(localeFi, "fi"); registerLocaleData(localeFil, "fil"); registerLocaleData(localeFr, "fr"); +registerLocaleData(localeGl, "gl"); registerLocaleData(localeHe, "he"); registerLocaleData(localeHi, "hi"); registerLocaleData(localeHr, "hr"); @@ -87,9 +96,13 @@ registerLocaleData(localeKo, "ko"); registerLocaleData(localeLt, "lt"); registerLocaleData(localeLv, "lv"); registerLocaleData(localeMl, "ml"); +registerLocaleData(localeMr, "mr"); +registerLocaleData(localeMy, "my"); registerLocaleData(localeNb, "nb"); +registerLocaleData(localeNe, "ne"); registerLocaleData(localeNl, "nl"); registerLocaleData(localeNn, "nn"); +registerLocaleData(localeOr, "or"); registerLocaleData(localePl, "pl"); registerLocaleData(localePtBr, "pt-BR"); registerLocaleData(localePtPt, "pt-PT"); @@ -100,6 +113,7 @@ registerLocaleData(localeSk, "sk"); registerLocaleData(localeSl, "sl"); registerLocaleData(localeSr, "sr"); registerLocaleData(localeSv, "sv"); +registerLocaleData(localeTe, "te"); registerLocaleData(localeTh, "th"); registerLocaleData(localeTr, "tr"); registerLocaleData(localeUk, "uk"); diff --git a/apps/browser/src/platform/services/browser-state.service.spec.ts b/apps/browser/src/platform/services/browser-state.service.spec.ts index b5d1b9c38a0..3069b8f1749 100644 --- a/apps/browser/src/platform/services/browser-state.service.spec.ts +++ b/apps/browser/src/platform/services/browser-state.service.spec.ts @@ -1,5 +1,6 @@ import { mock, MockProxy } from "jest-mock-extended"; +import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { @@ -32,6 +33,7 @@ describe("Browser State Service", () => { let stateFactory: MockProxy>; let useAccountCache: boolean; let environmentService: MockProxy; + let tokenService: MockProxy; let migrationRunner: MockProxy; let state: State; @@ -46,6 +48,7 @@ describe("Browser State Service", () => { logService = mock(); stateFactory = mock(); environmentService = mock(); + tokenService = mock(); migrationRunner = mock(); // turn off account cache for tests useAccountCache = false; @@ -77,6 +80,7 @@ describe("Browser State Service", () => { stateFactory, accountService, environmentService, + tokenService, migrationRunner, useAccountCache, ); diff --git a/apps/browser/src/platform/services/browser-state.service.ts b/apps/browser/src/platform/services/browser-state.service.ts index c544915e26c..f7ee74be217 100644 --- a/apps/browser/src/platform/services/browser-state.service.ts +++ b/apps/browser/src/platform/services/browser-state.service.ts @@ -1,6 +1,7 @@ import { BehaviorSubject } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { @@ -45,6 +46,7 @@ export class BrowserStateService stateFactory: StateFactory, accountService: AccountService, environmentService: EnvironmentService, + tokenService: TokenService, migrationRunner: MigrationRunner, useAccountCache = true, ) { @@ -56,6 +58,7 @@ export class BrowserStateService stateFactory, accountService, environmentService, + tokenService, migrationRunner, useAccountCache, ); diff --git a/apps/browser/src/popup/services/services.module.ts b/apps/browser/src/popup/services/services.module.ts index 5f97b578824..52de0303fae 100644 --- a/apps/browser/src/popup/services/services.module.ts +++ b/apps/browser/src/popup/services/services.module.ts @@ -17,11 +17,8 @@ import { LoginStrategyServiceAbstraction, } from "@bitwarden/auth/common"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; -import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; -import { EventUploadService } from "@bitwarden/common/abstractions/event/event-upload.service"; import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service"; import { SearchService as SearchServiceAbstraction } from "@bitwarden/common/abstractions/search.service"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; import { VaultTimeoutSettingsService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout-settings.service"; import { VaultTimeoutService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; @@ -42,6 +39,10 @@ import { AutofillSettingsService, AutofillSettingsServiceAbstraction, } from "@bitwarden/common/autofill/services/autofill-settings.service"; +import { + DefaultDomainSettingsService, + DomainSettingsService, +} from "@bitwarden/common/autofill/services/domain-settings.service"; import { UserNotificationSettingsService, UserNotificationSettingsServiceAbstraction, @@ -98,7 +99,6 @@ import { DialogService } from "@bitwarden/components"; import { ImportServiceAbstraction } from "@bitwarden/importer/core"; import { VaultExportServiceAbstraction } from "@bitwarden/vault-export-core"; -import { BrowserOrganizationService } from "../../admin-console/services/browser-organization.service"; import { UnauthGuardService } from "../../auth/popup/services"; import { AutofillService } from "../../autofill/services/abstractions/autofill.service"; import MainBackground from "../../background/main.background"; @@ -118,7 +118,6 @@ import { ForegroundPlatformUtilsService } from "../../platform/services/platform import { ForegroundDerivedStateProvider } from "../../platform/state/foreground-derived-state.provider"; import { ForegroundMemoryStorageService } from "../../platform/storage/foreground-memory-storage.service"; import { BrowserSendService } from "../../services/browser-send.service"; -import { BrowserSettingsService } from "../../services/browser-settings.service"; import { FilePopoutUtilsService } from "../../tools/popup/services/file-popout-utils.service"; import { VaultFilterService } from "../../vault/services/vault-filter.service"; @@ -233,7 +232,6 @@ function getBgService(service: keyof MainBackground) { deps: [], }, { provide: TotpService, useFactory: getBgService("totpService"), deps: [] }, - { provide: TokenService, useFactory: getBgService("tokenService"), deps: [] }, { provide: I18nServiceAbstraction, useFactory: (globalStateProvider: GlobalStateProvider) => { @@ -265,16 +263,6 @@ function getBgService(service: keyof MainBackground) { useFactory: getBgService("devicesService"), deps: [], }, - { - provide: EventUploadService, - useFactory: getBgService("eventUploadService"), - deps: [], - }, - { - provide: EventCollectionService, - useFactory: getBgService("eventCollectionService"), - deps: [], - }, { provide: PlatformUtilsService, useExisting: ForegroundPlatformUtilsService, @@ -348,11 +336,9 @@ function getBgService(service: keyof MainBackground) { }, { provide: SyncService, useFactory: getBgService("syncService"), deps: [] }, { - provide: SettingsService, - useFactory: (stateService: StateServiceAbstraction) => { - return new BrowserSettingsService(stateService); - }, - deps: [StateServiceAbstraction], + provide: DomainSettingsService, + useClass: DefaultDomainSettingsService, + deps: [StateProvider], }, { provide: AbstractStorageService, @@ -399,13 +385,6 @@ function getBgService(service: keyof MainBackground) { useFactory: getBgService("notificationsService"), deps: [], }, - { - provide: OrganizationService, - useFactory: (stateService: StateServiceAbstraction, stateProvider: StateProvider) => { - return new BrowserOrganizationService(stateService, stateProvider); - }, - deps: [StateServiceAbstraction, StateProvider], - }, { provide: VaultFilterService, useClass: VaultFilterService, @@ -445,6 +424,7 @@ function getBgService(service: keyof MainBackground) { logService: LogServiceAbstraction, accountService: AccountServiceAbstraction, environmentService: EnvironmentService, + tokenService: TokenService, migrationRunner: MigrationRunner, ) => { return new BrowserStateService( @@ -455,6 +435,7 @@ function getBgService(service: keyof MainBackground) { new StateFactory(GlobalState, Account), accountService, environmentService, + tokenService, migrationRunner, ); }, @@ -465,6 +446,7 @@ function getBgService(service: keyof MainBackground) { LogServiceAbstraction, AccountServiceAbstraction, EnvironmentService, + TokenService, MigrationRunner, ], }, diff --git a/apps/browser/src/popup/settings/excluded-domains.component.ts b/apps/browser/src/popup/settings/excluded-domains.component.ts index 6fa28d10ab6..1741fbaa659 100644 --- a/apps/browser/src/popup/settings/excluded-domains.component.ts +++ b/apps/browser/src/popup/settings/excluded-domains.component.ts @@ -6,7 +6,6 @@ import { DomainSettingsService } from "@bitwarden/common/autofill/services/domai import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { BrowserApi } from "../../platform/browser/browser-api"; @@ -31,7 +30,6 @@ export class ExcludedDomainsComponent implements OnInit, OnDestroy { accountSwitcherEnabled = false; constructor( - private stateService: StateService, private domainSettingsService: DomainSettingsService, private i18nService: I18nService, private router: Router, diff --git a/apps/browser/src/popup/settings/options.component.ts b/apps/browser/src/popup/settings/options.component.ts index a4260b688f3..dbdb94c5860 100644 --- a/apps/browser/src/popup/settings/options.component.ts +++ b/apps/browser/src/popup/settings/options.component.ts @@ -1,7 +1,6 @@ import { Component, OnInit } from "@angular/core"; import { firstValueFrom } from "rxjs"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; import { AutofillSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/autofill-settings.service"; import { BadgeSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/badge-settings.service"; import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; @@ -13,7 +12,6 @@ import { } from "@bitwarden/common/models/domain/domain-service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { ThemeType } from "@bitwarden/common/platform/enums"; import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service"; import { VaultSettingsService } from "@bitwarden/common/vault/abstractions/vault-settings/vault-settings.service"; @@ -51,14 +49,12 @@ export class OptionsComponent implements OnInit { constructor( private messagingService: MessagingService, - private stateService: StateService, private userNotificationSettingsService: UserNotificationSettingsServiceAbstraction, private autofillSettingsService: AutofillSettingsServiceAbstraction, private domainSettingsService: DomainSettingsService, private badgeSettingsService: BadgeSettingsServiceAbstraction, i18nService: I18nService, private themeStateService: ThemeStateService, - private settingsService: SettingsService, private vaultSettingsService: VaultSettingsService, ) { this.themeOptions = [ @@ -121,7 +117,7 @@ export class OptionsComponent implements OnInit { this.enableAutoTotpCopy = await firstValueFrom(this.autofillSettingsService.autoCopyTotp$); - this.enableFavicon = !this.settingsService.getDisableFavicon(); + this.enableFavicon = await firstValueFrom(this.domainSettingsService.showFavicons$); this.enableBadgeCounter = await firstValueFrom(this.badgeSettingsService.enableBadgeCounter$); @@ -171,7 +167,7 @@ export class OptionsComponent implements OnInit { } async updateFavicon() { - await this.settingsService.setDisableFavicon(!this.enableFavicon); + await this.domainSettingsService.setShowFavicons(this.enableFavicon); } async updateBadgeCounter() { diff --git a/apps/browser/src/popup/settings/premium.component.html b/apps/browser/src/popup/settings/premium.component.html index 2727ee405b9..a8f9855e62d 100644 --- a/apps/browser/src/popup/settings/premium.component.html +++ b/apps/browser/src/popup/settings/premium.component.html @@ -12,7 +12,7 @@
- +

{{ "premiumNotCurrentMember" | i18n }}

{{ "premiumSignUpAndGet" | i18n }}

    @@ -61,7 +61,7 @@ > - +

    {{ "premiumCurrentMember" | i18n }}

    {{ "premiumCurrentMemberThanks" | i18n }}

    -
    +
    diff --git a/apps/web/src/app/billing/individual/premium.component.ts b/apps/web/src/app/billing/individual/premium.component.ts index 8f40c3f1c2c..e6e63264d5b 100644 --- a/apps/web/src/app/billing/individual/premium.component.ts +++ b/apps/web/src/app/billing/individual/premium.component.ts @@ -1,14 +1,15 @@ import { Component, OnInit, ViewChild } from "@angular/core"; import { Router } from "@angular/router"; +import { firstValueFrom, Observable } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction"; import { PaymentComponent, TaxInfoComponent } from "../shared"; @@ -20,7 +21,7 @@ export class PremiumComponent implements OnInit { @ViewChild(PaymentComponent) paymentComponent: PaymentComponent; @ViewChild(TaxInfoComponent) taxInfoComponent: TaxInfoComponent; - canAccessPremium = false; + canAccessPremium$: Observable; selfHosted = false; premiumPrice = 10; familyPlanMaxUserCount = 6; @@ -39,17 +40,16 @@ export class PremiumComponent implements OnInit { private messagingService: MessagingService, private syncService: SyncService, private logService: LogService, - private stateService: StateService, private environmentService: EnvironmentService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) { this.selfHosted = platformUtilsService.isSelfHost(); this.cloudWebVaultUrl = this.environmentService.getCloudWebVaultUrl(); + this.canAccessPremium$ = billingAccountProfileStateService.hasPremiumFromAnySource$; } async ngOnInit() { - this.canAccessPremium = await this.stateService.getCanAccessPremium(); - const premiumPersonally = await this.stateService.getHasPremiumPersonally(); - if (premiumPersonally) { + if (await firstValueFrom(this.billingAccountProfileStateService.hasPremiumPersonally$)) { // FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. // eslint-disable-next-line @typescript-eslint/no-floating-promises this.router.navigate(["/settings/subscription/user-subscription"]); diff --git a/apps/web/src/app/billing/individual/subscription.component.html b/apps/web/src/app/billing/individual/subscription.component.html index 59326cb70ae..934a24570f4 100644 --- a/apps/web/src/app/billing/individual/subscription.component.html +++ b/apps/web/src/app/billing/individual/subscription.component.html @@ -1,6 +1,8 @@ - {{ "subscription" | i18n }} + {{ + "subscription" | i18n + }} {{ "paymentMethod" | i18n }} {{ "billingHistory" | i18n }} diff --git a/apps/web/src/app/billing/individual/subscription.component.ts b/apps/web/src/app/billing/individual/subscription.component.ts index 143d531e1d6..c316bae4f13 100644 --- a/apps/web/src/app/billing/individual/subscription.component.ts +++ b/apps/web/src/app/billing/individual/subscription.component.ts @@ -1,26 +1,24 @@ -import { Component } from "@angular/core"; +import { Component, OnInit } from "@angular/core"; +import { Observable } from "rxjs"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; @Component({ templateUrl: "subscription.component.html", }) -export class SubscriptionComponent { - hasPremium: boolean; +export class SubscriptionComponent implements OnInit { + hasPremium$: Observable; selfHosted: boolean; constructor( - private stateService: StateService, private platformUtilsService: PlatformUtilsService, - ) {} + billingAccountProfileStateService: BillingAccountProfileStateService, + ) { + this.hasPremium$ = billingAccountProfileStateService.hasPremiumPersonally$; + } - async ngOnInit() { - this.hasPremium = await this.stateService.getHasPremiumPersonally(); + ngOnInit() { this.selfHosted = this.platformUtilsService.isSelfHost(); } - - get subscriptionRoute(): string { - return this.hasPremium ? "user-subscription" : "premium"; - } } diff --git a/apps/web/src/app/billing/individual/user-subscription.component.ts b/apps/web/src/app/billing/individual/user-subscription.component.ts index fd4bb62a5ee..3ec0cd54d12 100644 --- a/apps/web/src/app/billing/individual/user-subscription.component.ts +++ b/apps/web/src/app/billing/individual/user-subscription.component.ts @@ -1,8 +1,9 @@ import { Component, OnInit } from "@angular/core"; import { Router } from "@angular/router"; -import { lastValueFrom, Observable } from "rxjs"; +import { firstValueFrom, lastValueFrom, Observable } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { SubscriptionResponse } from "@bitwarden/common/billing/models/response/subscription.response"; import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; import { ConfigServiceAbstraction as ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; @@ -11,7 +12,6 @@ import { FileDownloadService } from "@bitwarden/common/platform/abstractions/fil import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { DialogService } from "@bitwarden/components"; import { @@ -37,7 +37,6 @@ export class UserSubscriptionComponent implements OnInit { presentUserWithOffboardingSurvey$: Observable; constructor( - private stateService: StateService, private apiService: ApiService, private platformUtilsService: PlatformUtilsService, private i18nService: I18nService, @@ -47,6 +46,7 @@ export class UserSubscriptionComponent implements OnInit { private dialogService: DialogService, private environmentService: EnvironmentService, private configService: ConfigService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) { this.selfHosted = platformUtilsService.isSelfHost(); this.cloudWebVaultUrl = this.environmentService.getCloudWebVaultUrl(); @@ -65,8 +65,7 @@ export class UserSubscriptionComponent implements OnInit { return; } - // eslint-disable-next-line @typescript-eslint/no-misused-promises - if (this.stateService.getHasPremiumPersonally()) { + if (await firstValueFrom(this.billingAccountProfileStateService.hasPremiumPersonally$)) { this.loading = true; this.sub = await this.apiService.getUserSubscription(); } else { diff --git a/apps/web/src/app/billing/organizations/organization-plans.component.ts b/apps/web/src/app/billing/organizations/organization-plans.component.ts index b7070be1cfb..1242018673f 100644 --- a/apps/web/src/app/billing/organizations/organization-plans.component.ts +++ b/apps/web/src/app/billing/organizations/organization-plans.component.ts @@ -150,7 +150,7 @@ export class OrganizationPlansComponent implements OnInit, OnDestroy { async ngOnInit() { if (this.organizationId) { - this.organization = this.organizationService.get(this.organizationId); + this.organization = await this.organizationService.get(this.organizationId); this.billing = await this.organizationApiService.getBilling(this.organizationId); this.sub = await this.organizationApiService.getSubscription(this.organizationId); } diff --git a/apps/web/src/app/billing/organizations/organization-subscription-cloud.component.ts b/apps/web/src/app/billing/organizations/organization-subscription-cloud.component.ts index 45c7ea1a210..24374ee8969 100644 --- a/apps/web/src/app/billing/organizations/organization-subscription-cloud.component.ts +++ b/apps/web/src/app/billing/organizations/organization-subscription-cloud.component.ts @@ -94,7 +94,7 @@ export class OrganizationSubscriptionCloudComponent implements OnInit, OnDestroy return; } this.loading = true; - this.userOrg = this.organizationService.get(this.organizationId); + this.userOrg = await this.organizationService.get(this.organizationId); if (this.userOrg.canViewSubscription) { this.sub = await this.organizationApiService.getSubscription(this.organizationId); this.lineItems = this.sub?.subscription?.items; diff --git a/apps/web/src/app/billing/organizations/organization-subscription-selfhost.component.ts b/apps/web/src/app/billing/organizations/organization-subscription-selfhost.component.ts index 5a9a8eb0812..1b66f5400af 100644 --- a/apps/web/src/app/billing/organizations/organization-subscription-selfhost.component.ts +++ b/apps/web/src/app/billing/organizations/organization-subscription-selfhost.component.ts @@ -110,7 +110,7 @@ export class OrganizationSubscriptionSelfhostComponent implements OnInit, OnDest return; } this.loading = true; - this.userOrg = this.organizationService.get(this.organizationId); + this.userOrg = await this.organizationService.get(this.organizationId); if (this.userOrg.canViewSubscription) { const subscriptionResponse = await this.organizationApiService.getSubscription( this.organizationId, diff --git a/apps/web/src/app/core/guards/has-premium.guard.ts b/apps/web/src/app/core/guards/has-premium.guard.ts index bb4d07f1d16..ab544dafb61 100644 --- a/apps/web/src/app/core/guards/has-premium.guard.ts +++ b/apps/web/src/app/core/guards/has-premium.guard.ts @@ -4,32 +4,39 @@ import { RouterStateSnapshot, Router, CanActivateFn, + UrlTree, } from "@angular/router"; +import { Observable } from "rxjs"; +import { tap } from "rxjs/operators"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; /** * CanActivate guard that checks if the user has premium and otherwise triggers the "premiumRequired" * message and blocks navigation. */ export function hasPremiumGuard(): CanActivateFn { - return async (_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot) => { + return ( + _route: ActivatedRouteSnapshot, + _state: RouterStateSnapshot, + ): Observable => { const router = inject(Router); - const stateService = inject(StateService); const messagingService = inject(MessagingService); + const billingAccountProfileStateService = inject(BillingAccountProfileStateService); - const userHasPremium = await stateService.getCanAccessPremium(); - - if (!userHasPremium) { - messagingService.send("premiumRequired"); - } - - // Prevent trapping the user on the login page, since that's an awful UX flow - if (!userHasPremium && router.url === "/login") { - return router.createUrlTree(["/"]); - } - - return userHasPremium; + return billingAccountProfileStateService.hasPremiumFromAnySource$.pipe( + tap((userHasPremium: boolean) => { + if (!userHasPremium) { + messagingService.send("premiumRequired"); + } + }), + // Prevent trapping the user on the login page, since that's an awful UX flow + tap((userHasPremium: boolean) => { + if (!userHasPremium && router.url === "/login") { + return router.createUrlTree(["/"]); + } + }), + ); }; } diff --git a/apps/web/src/app/core/state/state.service.ts b/apps/web/src/app/core/state/state.service.ts index 1ad3bd25c31..a80384d1798 100644 --- a/apps/web/src/app/core/state/state.service.ts +++ b/apps/web/src/app/core/state/state.service.ts @@ -7,6 +7,7 @@ import { STATE_SERVICE_USE_CACHE, } from "@bitwarden/angular/services/injection-tokens"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { @@ -33,6 +34,7 @@ export class StateService extends BaseStateService { @Inject(STATE_FACTORY) stateFactory: StateFactory, accountService: AccountService, environmentService: EnvironmentService, + tokenService: TokenService, migrationRunner: MigrationRunner, @Inject(STATE_SERVICE_USE_CACHE) useAccountCache = true, ) { @@ -44,6 +46,7 @@ export class StateService extends BaseStateService { stateFactory, accountService, environmentService, + tokenService, migrationRunner, useAccountCache, ); diff --git a/apps/web/src/app/layouts/user-layout.component.ts b/apps/web/src/app/layouts/user-layout.component.ts index 1f116ee76e4..2e1813697ef 100644 --- a/apps/web/src/app/layouts/user-layout.component.ts +++ b/apps/web/src/app/layouts/user-layout.component.ts @@ -1,15 +1,16 @@ import { CommonModule } from "@angular/common"; import { Component, NgZone, OnDestroy, OnInit } from "@angular/core"; import { RouterModule } from "@angular/router"; +import { firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; import { ConfigServiceAbstraction as ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction"; import { IconModule, LayoutComponent, NavigationModule } from "@bitwarden/components"; @@ -48,10 +49,10 @@ export class UserLayoutComponent implements OnInit, OnDestroy { private ngZone: NgZone, private platformUtilsService: PlatformUtilsService, private organizationService: OrganizationService, - private stateService: StateService, private apiService: ApiService, private syncService: SyncService, private configService: ConfigService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit() { @@ -79,16 +80,21 @@ export class UserLayoutComponent implements OnInit, OnDestroy { } async load() { - const premium = await this.stateService.getHasPremiumPersonally(); + const hasPremiumPersonally = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumPersonally$, + ); + const hasPremiumFromOrg = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnyOrganization$, + ); const selfHosted = this.platformUtilsService.isSelfHost(); this.hasFamilySponsorshipAvailable = await this.organizationService.canManageSponsorships(); - const hasPremiumFromOrg = await this.stateService.getHasPremiumFromOrganization(); let billing = null; if (!selfHosted) { // TODO: We should remove the need to call this! billing = await this.apiService.getUserBillingHistory(); } - this.hideSubscription = !premium && hasPremiumFromOrg && (selfHosted || billing?.hasNoHistory); + this.hideSubscription = + !hasPremiumPersonally && hasPremiumFromOrg && (selfHosted || billing?.hasNoHistory); } } diff --git a/apps/web/src/app/settings/preferences.component.ts b/apps/web/src/app/settings/preferences.component.ts index fd759cd0368..7f9eabb6b3b 100644 --- a/apps/web/src/app/settings/preferences.component.ts +++ b/apps/web/src/app/settings/preferences.component.ts @@ -2,15 +2,13 @@ import { Component, OnInit } from "@angular/core"; import { FormBuilder } from "@angular/forms"; import { concatMap, filter, firstValueFrom, map, Observable, Subject, takeUntil, tap } from "rxjs"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; import { VaultTimeoutSettingsService } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout-settings.service"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyType } from "@bitwarden/common/admin-console/enums"; +import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; -import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { ThemeType } from "@bitwarden/common/platform/enums"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service"; @@ -48,13 +46,11 @@ export class PreferencesComponent implements OnInit { constructor( private formBuilder: FormBuilder, private policyService: PolicyService, - private stateService: StateService, private i18nService: I18nService, private vaultTimeoutSettingsService: VaultTimeoutSettingsService, private platformUtilsService: PlatformUtilsService, - private messagingService: MessagingService, private themeStateService: ThemeStateService, - private settingsService: SettingsService, + private domainSettingsService: DomainSettingsService, private dialogService: DialogService, ) { this.vaultTimeoutOptions = [ @@ -139,7 +135,7 @@ export class PreferencesComponent implements OnInit { vaultTimeoutAction: await firstValueFrom( this.vaultTimeoutSettingsService.vaultTimeoutAction$(), ), - enableFavicons: !(await this.settingsService.getDisableFavicon()), + enableFavicons: await firstValueFrom(this.domainSettingsService.showFavicons$), theme: await firstValueFrom(this.themeStateService.selectedTheme$), locale: (await firstValueFrom(this.i18nService.userSetLocale$)) ?? null, }; @@ -162,7 +158,7 @@ export class PreferencesComponent implements OnInit { values.vaultTimeout, values.vaultTimeoutAction, ); - await this.settingsService.setDisableFavicon(!values.enableFavicons); + await this.domainSettingsService.setShowFavicons(values.enableFavicons); await this.themeStateService.setSelectedTheme(values.theme); await this.i18nService.setLocale(values.locale); if (values.locale !== this.startingLocale) { diff --git a/apps/web/src/app/settings/settings.component.ts b/apps/web/src/app/settings/settings.component.ts index c2fd1c77faf..b5b198d0ac4 100644 --- a/apps/web/src/app/settings/settings.component.ts +++ b/apps/web/src/app/settings/settings.component.ts @@ -1,13 +1,12 @@ import { Component, NgZone, OnDestroy, OnInit } from "@angular/core"; +import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; -import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; -import { StateService } from "../core"; - const BroadcasterSubscriptionId = "SettingsComponent"; @Component({ @@ -21,13 +20,12 @@ export class SettingsComponent implements OnInit, OnDestroy { hideSubscription: boolean; constructor( - private tokenService: TokenService, private broadcasterService: BroadcasterService, private ngZone: NgZone, private platformUtilsService: PlatformUtilsService, private organizationService: OrganizationService, - private stateService: StateService, private apiService: ApiService, + private billingAccountProfileStateServiceAbstraction: BillingAccountProfileStateService, ) {} async ngOnInit() { @@ -53,9 +51,13 @@ export class SettingsComponent implements OnInit, OnDestroy { } async load() { - this.premium = await this.stateService.getHasPremiumPersonally(); + this.premium = await firstValueFrom( + this.billingAccountProfileStateServiceAbstraction.hasPremiumPersonally$, + ); this.hasFamilySponsorshipAvailable = await this.organizationService.canManageSponsorships(); - const hasPremiumFromOrg = await this.stateService.getHasPremiumFromOrganization(); + const hasPremiumFromOrg = await firstValueFrom( + this.billingAccountProfileStateServiceAbstraction.hasPremiumFromAnyOrganization$, + ); let billing = null; if (!this.selfHosted) { billing = await this.apiService.getUserBillingHistory(); diff --git a/apps/web/src/app/shared/locales.ts b/apps/web/src/app/shared/locales.ts index eddf662a0af..409ad58d3d9 100644 --- a/apps/web/src/app/shared/locales.ts +++ b/apps/web/src/app/shared/locales.ts @@ -8,6 +8,7 @@ import localeBn from "@angular/common/locales/bn"; import localeBs from "@angular/common/locales/bs"; import localeCa from "@angular/common/locales/ca"; import localeCs from "@angular/common/locales/cs"; +import localeCy from "@angular/common/locales/cy"; import localeDa from "@angular/common/locales/da"; import localeDe from "@angular/common/locales/de"; import localeEl from "@angular/common/locales/el"; @@ -17,9 +18,11 @@ import localeEo from "@angular/common/locales/eo"; import localeEs from "@angular/common/locales/es"; import localeEt from "@angular/common/locales/et"; import localeEu from "@angular/common/locales/eu"; +import localeFa from "@angular/common/locales/fa"; import localeFi from "@angular/common/locales/fi"; import localeFil from "@angular/common/locales/fil"; import localeFr from "@angular/common/locales/fr"; +import localeGl from "@angular/common/locales/gl"; import localeHe from "@angular/common/locales/he"; import localeHi from "@angular/common/locales/hi"; import localeHr from "@angular/common/locales/hr"; @@ -33,9 +36,13 @@ import localeKn from "@angular/common/locales/kn"; import localeKo from "@angular/common/locales/ko"; import localeLv from "@angular/common/locales/lv"; import localeMl from "@angular/common/locales/ml"; +import localeMr from "@angular/common/locales/mr"; +import localeMy from "@angular/common/locales/my"; import localeNb from "@angular/common/locales/nb"; +import localeNe from "@angular/common/locales/ne"; import localeNl from "@angular/common/locales/nl"; import localeNn from "@angular/common/locales/nn"; +import localeOr from "@angular/common/locales/or"; import localePl from "@angular/common/locales/pl"; import localePtBr from "@angular/common/locales/pt"; import localePtPt from "@angular/common/locales/pt-PT"; @@ -46,6 +53,8 @@ import localeSk from "@angular/common/locales/sk"; import localeSl from "@angular/common/locales/sl"; import localeSr from "@angular/common/locales/sr"; import localeSv from "@angular/common/locales/sv"; +import localeTe from "@angular/common/locales/te"; +import localeTh from "@angular/common/locales/th"; import localeTr from "@angular/common/locales/tr"; import localeUk from "@angular/common/locales/uk"; import localeVi from "@angular/common/locales/vi"; @@ -61,6 +70,7 @@ registerLocaleData(localeBn, "bn"); registerLocaleData(localeBs, "bs"); registerLocaleData(localeCa, "ca"); registerLocaleData(localeCs, "cs"); +registerLocaleData(localeCy, "cy"); registerLocaleData(localeDa, "da"); registerLocaleData(localeDe, "de"); registerLocaleData(localeEl, "el"); @@ -70,9 +80,11 @@ registerLocaleData(localeEo, "eo"); registerLocaleData(localeEs, "es"); registerLocaleData(localeEt, "et"); registerLocaleData(localeEu, "eu"); +registerLocaleData(localeFa, "fa"); registerLocaleData(localeFi, "fi"); registerLocaleData(localeFil, "fil"); registerLocaleData(localeFr, "fr"); +registerLocaleData(localeGl, "gl"); registerLocaleData(localeHe, "he"); registerLocaleData(localeHi, "hi"); registerLocaleData(localeHr, "hr"); @@ -86,9 +98,13 @@ registerLocaleData(localeKn, "kn"); registerLocaleData(localeKo, "ko"); registerLocaleData(localeLv, "lv"); registerLocaleData(localeMl, "ml"); +registerLocaleData(localeMr, "mr"); +registerLocaleData(localeMy, "my"); registerLocaleData(localeNb, "nb"); +registerLocaleData(localeNe, "ne"); registerLocaleData(localeNl, "nl"); registerLocaleData(localeNn, "nn"); +registerLocaleData(localeOr, "or"); registerLocaleData(localePl, "pl"); registerLocaleData(localePtBr, "pt-BR"); registerLocaleData(localePtPt, "pt-PT"); @@ -99,6 +115,8 @@ registerLocaleData(localeSk, "sk"); registerLocaleData(localeSl, "sl"); registerLocaleData(localeSr, "sr"); registerLocaleData(localeSv, "sv"); +registerLocaleData(localeTe, "te"); +registerLocaleData(localeTh, "th"); registerLocaleData(localeTr, "tr"); registerLocaleData(localeUk, "uk"); registerLocaleData(localeVi, "vi"); diff --git a/apps/web/src/app/tools/reports/pages/reports-home.component.ts b/apps/web/src/app/tools/reports/pages/reports-home.component.ts index 3d85db8cb26..541193fafab 100644 --- a/apps/web/src/app/tools/reports/pages/reports-home.component.ts +++ b/apps/web/src/app/tools/reports/pages/reports-home.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from "@angular/core"; +import { firstValueFrom } from "rxjs"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { reports, ReportType } from "../reports"; import { ReportEntry, ReportVariant } from "../shared"; @@ -12,11 +13,12 @@ import { ReportEntry, ReportVariant } from "../shared"; export class ReportsHomeComponent implements OnInit { reports: ReportEntry[]; - constructor(private stateService: StateService) {} + constructor(private billingAccountProfileStateService: BillingAccountProfileStateService) {} async ngOnInit(): Promise { - const userHasPremium = await this.stateService.getCanAccessPremium(); - + const userHasPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); const reportRequiresPremium = userHasPremium ? ReportVariant.Enabled : ReportVariant.RequiresPremium; diff --git a/apps/web/src/app/tools/send/add-edit.component.ts b/apps/web/src/app/tools/send/add-edit.component.ts index 5eb1d361981..ee4be414889 100644 --- a/apps/web/src/app/tools/send/add-edit.component.ts +++ b/apps/web/src/app/tools/send/add-edit.component.ts @@ -5,6 +5,7 @@ import { FormBuilder } from "@angular/forms"; import { AddEditComponent as BaseAddEditComponent } from "@bitwarden/angular/tools/send/add-edit.component"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -36,6 +37,7 @@ export class AddEditComponent extends BaseAddEditComponent { sendApiService: SendApiService, dialogService: DialogService, formBuilder: FormBuilder, + billingAccountProfileStateService: BillingAccountProfileStateService, protected dialogRef: DialogRef, @Inject(DIALOG_DATA) params: { sendId: string }, ) { @@ -52,6 +54,7 @@ export class AddEditComponent extends BaseAddEditComponent { sendApiService, dialogService, formBuilder, + billingAccountProfileStateService, ); this.sendId = params.sendId; diff --git a/apps/web/src/app/tools/tools.component.ts b/apps/web/src/app/tools/tools.component.ts index 7c6020f6d12..52ef698fd3c 100644 --- a/apps/web/src/app/tools/tools.component.ts +++ b/apps/web/src/app/tools/tools.component.ts @@ -1,22 +1,33 @@ -import { Component, OnInit } from "@angular/core"; +import { Component, OnDestroy, OnInit } from "@angular/core"; +import { Subject, takeUntil } from "rxjs"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; @Component({ selector: "app-tools", templateUrl: "tools.component.html", }) -export class ToolsComponent implements OnInit { +export class ToolsComponent implements OnInit, OnDestroy { + private componentIsDestroyed$ = new Subject(); canAccessPremium = false; constructor( - private stateService: StateService, private messagingService: MessagingService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit() { - this.canAccessPremium = await this.stateService.getCanAccessPremium(); + this.billingAccountProfileStateService.hasPremiumFromAnySource$ + .pipe(takeUntil(this.componentIsDestroyed$)) + .subscribe((canAccessPremium: boolean) => { + this.canAccessPremium = canAccessPremium; + }); + } + + ngOnDestroy() { + this.componentIsDestroyed$.next(true); + this.componentIsDestroyed$.complete(); } premiumRequired() { diff --git a/apps/web/src/app/vault/components/collection-dialog/collection-dialog.component.ts b/apps/web/src/app/vault/components/collection-dialog/collection-dialog.component.ts index b10d6b1e28e..357d2217e47 100644 --- a/apps/web/src/app/vault/components/collection-dialog/collection-dialog.component.ts +++ b/apps/web/src/app/vault/components/collection-dialog/collection-dialog.component.ts @@ -139,9 +139,9 @@ export class CollectionDialogComponent implements OnInit, OnDestroy { } async loadOrg(orgId: string, collectionIds: string[]) { - const organization$ = of(this.organizationService.get(orgId)).pipe( - shareReplay({ refCount: true, bufferSize: 1 }), - ); + const organization$ = this.organizationService + .get$(orgId) + .pipe(shareReplay({ refCount: true, bufferSize: 1 })); const groups$ = organization$.pipe( switchMap((organization) => { if (!organization.useGroups) { diff --git a/apps/web/src/app/vault/components/premium-badge.stories.ts b/apps/web/src/app/vault/components/premium-badge.stories.ts index 4585f235ba5..5433dd99813 100644 --- a/apps/web/src/app/vault/components/premium-badge.stories.ts +++ b/apps/web/src/app/vault/components/premium-badge.stories.ts @@ -1,10 +1,10 @@ import { Meta, moduleMetadata, Story } from "@storybook/angular"; +import { of } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; -import { StorageOptions } from "@bitwarden/common/platform/models/domain/storage-options"; import { BadgeModule, I18nMockService } from "@bitwarden/components"; import { PremiumBadgeComponent } from "./premium-badge.component"; @@ -15,12 +15,6 @@ class MockMessagingService implements MessagingService { } } -class MockedStateService implements Partial { - async getCanAccessPremium(options?: StorageOptions) { - return false; - } -} - export default { title: "Web/Premium Badge", component: PremiumBadgeComponent, @@ -43,9 +37,9 @@ export default { }, }, { - provide: StateService, - useFactory: () => { - return new MockedStateService(); + provide: BillingAccountProfileStateService, + useValue: { + hasPremiumFromAnySource$: of(false), }, }, ], diff --git a/apps/web/src/app/vault/components/vault-items/vault-items.stories.ts b/apps/web/src/app/vault/components/vault-items/vault-items.stories.ts index 8f33c501646..05659de073c 100644 --- a/apps/web/src/app/vault/components/vault-items/vault-items.stories.ts +++ b/apps/web/src/app/vault/components/vault-items/vault-items.stories.ts @@ -3,11 +3,11 @@ import { RouterModule } from "@angular/router"; import { applicationConfig, Meta, moduleMetadata, Story } from "@storybook/angular"; import { BehaviorSubject, of } from "rxjs"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; import { OrganizationUserType } from "@bitwarden/common/admin-console/enums"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AvatarService } from "@bitwarden/common/auth/abstractions/avatar.service"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; @@ -57,19 +57,19 @@ export default { useValue: { activeAccount$: new BehaviorSubject("1").asObservable(), accounts$: new BehaviorSubject({ "1": { profile: { name: "Foo" } } }).asObservable(), - async getDisableFavicon() { - return false; + async getShowFavicon() { + return true; }, } as Partial, }, { - provide: SettingsService, + provide: DomainSettingsService, useValue: { - disableFavicon$: new BehaviorSubject(false).asObservable(), - getDisableFavicon() { - return false; + showFavicons$: new BehaviorSubject(true).asObservable(), + getShowFavicon() { + return true; }, - } as Partial, + } as Partial, }, { provide: AvatarService, diff --git a/apps/web/src/app/vault/individual-vault/add-edit.component.ts b/apps/web/src/app/vault/individual-vault/add-edit.component.ts index 00464882aef..8332b7e95f1 100644 --- a/apps/web/src/app/vault/individual-vault/add-edit.component.ts +++ b/apps/web/src/app/vault/individual-vault/add-edit.component.ts @@ -1,11 +1,13 @@ import { DatePipe } from "@angular/common"; import { Component, OnDestroy, OnInit } from "@angular/core"; +import { firstValueFrom } from "rxjs"; import { AddEditComponent as BaseAddEditComponent } from "@bitwarden/angular/vault/components/add-edit.component"; import { AuditService } from "@bitwarden/common/abstractions/audit.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType, ProductType } from "@bitwarden/common/enums"; import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -64,6 +66,7 @@ export class AddEditComponent extends BaseAddEditComponent implements OnInit, On dialogService: DialogService, datePipe: DatePipe, configService: ConfigServiceAbstraction, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cipherService, @@ -98,7 +101,9 @@ export class AddEditComponent extends BaseAddEditComponent implements OnInit, On this.hasPasswordHistory = this.cipher.hasPasswordHistory; this.cleanUp(); - this.canAccessPremium = await this.stateService.getCanAccessPremium(); + this.canAccessPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); if (this.showTotp()) { await this.totpUpdateCode(); const interval = this.totpService.getTimeInterval(this.cipher.login.totp); diff --git a/apps/web/src/app/vault/individual-vault/attachments.component.ts b/apps/web/src/app/vault/individual-vault/attachments.component.ts index 0ce66d07fa0..ae4e8fafabe 100644 --- a/apps/web/src/app/vault/individual-vault/attachments.component.ts +++ b/apps/web/src/app/vault/individual-vault/attachments.component.ts @@ -2,6 +2,7 @@ import { Component } from "@angular/core"; import { AttachmentsComponent as BaseAttachmentsComponent } from "@bitwarden/angular/vault/components/attachments.component"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -30,6 +31,7 @@ export class AttachmentsComponent extends BaseAttachmentsComponent { logService: LogService, fileDownloadService: FileDownloadService, dialogService: DialogService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cipherService, @@ -42,6 +44,7 @@ export class AttachmentsComponent extends BaseAttachmentsComponent { stateService, fileDownloadService, dialogService, + billingAccountProfileStateService, ); } diff --git a/apps/web/src/app/vault/individual-vault/vault.component.ts b/apps/web/src/app/vault/individual-vault/vault.component.ts index 7894677b69c..d7d9ab8074a 100644 --- a/apps/web/src/app/vault/individual-vault/vault.component.ts +++ b/apps/web/src/app/vault/individual-vault/vault.component.ts @@ -37,11 +37,11 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType } from "@bitwarden/common/enums"; import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; -import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; @@ -163,7 +163,6 @@ export class VaultComponent implements OnInit, OnDestroy { private modalService: ModalService, private dialogService: DialogService, private tokenService: TokenService, - private cryptoService: CryptoService, private messagingService: MessagingService, private platformUtilsService: PlatformUtilsService, private broadcasterService: BroadcasterService, @@ -184,6 +183,7 @@ export class VaultComponent implements OnInit, OnDestroy { private configService: ConfigServiceAbstraction, private apiService: ApiService, private userVerificationService: UserVerificationService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit() { @@ -203,7 +203,9 @@ export class VaultComponent implements OnInit, OnDestroy { : false; await this.syncService.fullSync(false); - const canAccessPremium = await this.stateService.getCanAccessPremium(); + const canAccessPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); this.showPremiumCallout = !this.showVerifyEmail && !canAccessPremium && !this.platformUtilsService.isSelfHost(); @@ -244,9 +246,6 @@ export class VaultComponent implements OnInit, OnDestroy { }); const filter$ = this.routedVaultFilterService.filter$; - const canAccessPremium$ = Utils.asyncToObservable(() => - this.stateService.getCanAccessPremium(), - ).pipe(shareReplay({ refCount: true, bufferSize: 1 })); const allCollections$ = Utils.asyncToObservable(() => this.collectionService.getAllDecrypted()); const nestedCollections$ = allCollections$.pipe( map((collections) => getNestedCollectionTree(collections)), @@ -370,7 +369,7 @@ export class VaultComponent implements OnInit, OnDestroy { switchMap(() => combineLatest([ filter$, - canAccessPremium$, + this.billingAccountProfileStateService.hasPremiumFromAnySource$, allCollections$, this.organizationService.organizations$, ciphers$, @@ -515,12 +514,11 @@ export class VaultComponent implements OnInit, OnDestroy { return; } - const canAccessPremium = await this.stateService.getCanAccessPremium(); - if (cipher.organizationId == null && !canAccessPremium) { + if (cipher.organizationId == null && !this.canAccessPremium) { this.messagingService.send("premiumRequired"); return; } else if (cipher.organizationId != null) { - const org = this.organizationService.get(cipher.organizationId); + const org = await this.organizationService.get(cipher.organizationId); if (org != null && (org.maxStorageGb == null || org.maxStorageGb === 0)) { this.messagingService.send("upgradeOrganization", { organizationId: cipher.organizationId, @@ -699,7 +697,7 @@ export class VaultComponent implements OnInit, OnDestroy { } async deleteCollection(collection: CollectionView): Promise { - const organization = this.organizationService.get(collection.organizationId); + const organization = await this.organizationService.get(collection.organizationId); if (!collection.canDelete(organization)) { this.platformUtilsService.showToast( "error", diff --git a/apps/web/src/app/vault/org-vault/add-edit.component.ts b/apps/web/src/app/vault/org-vault/add-edit.component.ts index 567dcf05df5..cb879dfcc75 100644 --- a/apps/web/src/app/vault/org-vault/add-edit.component.ts +++ b/apps/web/src/app/vault/org-vault/add-edit.component.ts @@ -6,6 +6,7 @@ import { AuditService } from "@bitwarden/common/abstractions/audit.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { ConfigServiceAbstraction } from "@bitwarden/common/platform/abstractions/config/config.service.abstraction"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -54,6 +55,7 @@ export class AddEditComponent extends BaseAddEditComponent { dialogService: DialogService, datePipe: DatePipe, configService: ConfigServiceAbstraction, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cipherService, @@ -75,6 +77,7 @@ export class AddEditComponent extends BaseAddEditComponent { dialogService, datePipe, configService, + billingAccountProfileStateService, ); } diff --git a/apps/web/src/app/vault/org-vault/attachments.component.ts b/apps/web/src/app/vault/org-vault/attachments.component.ts index ca6e0faccd5..f7ef372a2e3 100644 --- a/apps/web/src/app/vault/org-vault/attachments.component.ts +++ b/apps/web/src/app/vault/org-vault/attachments.component.ts @@ -2,6 +2,7 @@ import { Component } from "@angular/core"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -34,6 +35,7 @@ export class AttachmentsComponent extends BaseAttachmentsComponent { logService: LogService, fileDownloadService: FileDownloadService, dialogService: DialogService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cipherService, @@ -45,6 +47,7 @@ export class AttachmentsComponent extends BaseAttachmentsComponent { logService, fileDownloadService, dialogService, + billingAccountProfileStateService, ); } diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/guards/sm-org-enabled.guard.ts b/bitwarden_license/bit-web/src/app/secrets-manager/guards/sm-org-enabled.guard.ts index 3ff4d998a3d..a1f7564156c 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/guards/sm-org-enabled.guard.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/guards/sm-org-enabled.guard.ts @@ -16,7 +16,7 @@ export const organizationEnabledGuard: CanActivateFn = async (route: ActivatedRo await syncService.fullSync(false); } - const org = orgService.get(route.params.organizationId); + const org = await orgService.get(route.params.organizationId); if (org == null || !org.canAccessSecretsManager) { return createUrlTreeFromSnapshot(route, ["/"]); } diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/layout/navigation.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/layout/navigation.component.ts index 90faf165381..cd117819a9f 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/layout/navigation.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/layout/navigation.component.ts @@ -14,7 +14,7 @@ export class NavigationComponent { protected readonly logo = SecretsManagerLogo; protected orgFilter = (org: Organization) => org.canAccessSecretsManager; protected isAdmin$ = this.route.params.pipe( - map((params) => this.organizationService.get(params.organizationId)?.isAdmin), + map(async (params) => (await this.organizationService.get(params.organizationId))?.isAdmin), ); constructor( diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/overview/overview.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/overview/overview.component.ts index 8dff4cf92f8..95c17642538 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/overview/overview.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/overview/overview.component.ts @@ -12,6 +12,7 @@ import { take, share, firstValueFrom, + concatMap, } from "rxjs"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; @@ -105,7 +106,7 @@ export class OverviewComponent implements OnInit, OnDestroy { orgId$ .pipe( - map((orgId) => this.organizationService.get(orgId)), + concatMap(async (orgId) => await this.organizationService.get(orgId)), takeUntil(this.destroy$), ) .subscribe((org) => { diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project-secrets.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project-secrets.component.ts index ebfb006c0ad..07d50b28ee1 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project-secrets.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project-secrets.component.ts @@ -63,7 +63,9 @@ export class ProjectSecretsComponent { switchMap(async ([_, params]) => { this.organizationId = params.organizationId; this.projectId = params.projectId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; + this.organizationEnabled = ( + await this.organizationService.get(params.organizationId) + )?.enabled; return await this.getSecretsByProject(); }), ); diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project.component.ts index d24ff7a3b09..742c2bea1d8 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/projects/project/project.component.ts @@ -10,6 +10,8 @@ import { Subject, switchMap, takeUntil, + map, + concatMap, } from "rxjs"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; @@ -70,11 +72,18 @@ export class ProjectComponent implements OnInit, OnDestroy { }), ); - this.route.params.pipe(takeUntil(this.destroy$)).subscribe((params) => { - this.organizationId = params.organizationId; - this.projectId = params.projectId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; - }); + const projectId$ = this.route.params.pipe(map((p) => p.projectId)); + const organization$ = this.route.params.pipe( + concatMap((params) => this.organizationService.get$(params.organizationId)), + ); + + combineLatest([projectId$, organization$]) + .pipe(takeUntil(this.destroy$)) + .subscribe(([projectId, organization]) => { + this.organizationId = organization.id; + this.projectId = projectId; + this.organizationEnabled = organization.enabled; + }); } ngOnDestroy(): void { diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/projects/projects/projects.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/projects/projects/projects.component.ts index 83541a37697..831ee4df9bc 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/projects/projects/projects.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/projects/projects/projects.component.ts @@ -51,7 +51,9 @@ export class ProjectsComponent implements OnInit { ]).pipe( switchMap(async ([params]) => { this.organizationId = params.organizationId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; + this.organizationEnabled = ( + await this.organizationService.get(params.organizationId) + )?.enabled; return await this.getProjects(); }), diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/secrets/dialog/secret-dialog.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/secrets/dialog/secret-dialog.component.ts index e944fbce0c7..b1bd91a04fb 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/secrets/dialog/secret-dialog.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/secrets/dialog/secret-dialog.component.ts @@ -87,7 +87,7 @@ export class SecretDialogComponent implements OnInit { this.formGroup.get("project").setValue(this.data.projectId); } - if (this.organizationService.get(this.data.organizationId)?.isAdmin) { + if ((await this.organizationService.get(this.data.organizationId))?.isAdmin) { this.formGroup.get("project").removeValidators(Validators.required); this.formGroup.get("project").updateValueAndValidity(); } diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/secrets/secrets.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/secrets/secrets.component.ts index 64bbf479d88..a7413c9b59f 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/secrets/secrets.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/secrets/secrets.component.ts @@ -47,7 +47,9 @@ export class SecretsComponent implements OnInit { combineLatestWith(this.route.params), switchMap(async ([_, params]) => { this.organizationId = params.organizationId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; + this.organizationEnabled = ( + await this.organizationService.get(params.organizationId) + )?.enabled; return await this.getSecrets(); }), diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/service-accounts.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/service-accounts.component.ts index a5e3cd29d24..d015cccd99d 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/service-accounts.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/service-accounts.component.ts @@ -46,7 +46,9 @@ export class ServiceAccountsComponent implements OnInit { ]).pipe( switchMap(async ([params]) => { this.organizationId = params.organizationId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; + this.organizationEnabled = ( + await this.organizationService.get(params.organizationId) + )?.enabled; return await this.getServiceAccounts(); }), diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.spec.ts b/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.spec.ts index 482d2bb06b4..8c0f9c37311 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.spec.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.spec.ts @@ -26,7 +26,7 @@ describe("AccessPolicySelectorService", () => { describe("showAccessRemovalWarning", () => { it("returns false when current user is admin", async () => { const org = orgFactory(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = []; @@ -38,7 +38,7 @@ describe("AccessPolicySelectorService", () => { it("returns false when current user is owner", async () => { const org = orgFactory(); org.type = OrganizationUserType.Owner; - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = []; @@ -49,7 +49,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin and all policies are removed", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = []; @@ -60,7 +60,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin and user policy is set to canRead", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = []; selectedPolicyValues.push( @@ -77,7 +77,7 @@ describe("AccessPolicySelectorService", () => { it("returns false when current user isn't owner/admin and user policy is set to canReadWrite", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -93,7 +93,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin and a group Read policy is submitted that the user is a member of", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -111,7 +111,7 @@ describe("AccessPolicySelectorService", () => { it("returns false when current user isn't owner/admin and a group ReadWrite policy is submitted that the user is a member of", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -129,7 +129,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin and a group ReadWrite policy is submitted that the user is not a member of", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -147,7 +147,7 @@ describe("AccessPolicySelectorService", () => { it("returns false when current user isn't owner/admin, user policy is set to CanRead, and user is in read write group", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -169,7 +169,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin, user policy is set to CanRead, and user is not in ReadWrite group", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ @@ -191,7 +191,7 @@ describe("AccessPolicySelectorService", () => { it("returns true when current user isn't owner/admin, user policy is set to CanRead, and user is in Read group", async () => { const org = setupUserOrg(); - organizationService.get.calledWith(org.id).mockReturnValue(org); + organizationService.get.calledWith(org.id).mockResolvedValue(org); const selectedPolicyValues: ApItemValueType[] = [ createApItemValueType({ diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.ts b/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.ts index 4a90172d45b..b219bfd33d2 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/shared/access-policies/access-policy-selector/access-policy-selector.service.ts @@ -17,7 +17,7 @@ export class AccessPolicySelectorService { organizationId: string, selectedPoliciesValues: ApItemValueType[], ): Promise { - const organization = this.organizationService.get(organizationId); + const organization = await this.organizationService.get(organizationId); if (organization.isOwner || organization.isAdmin) { return false; } diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/shared/new-menu.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/shared/new-menu.component.ts index 6cb5722be04..d2533d8dbf7 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/shared/new-menu.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/shared/new-menu.component.ts @@ -1,6 +1,6 @@ import { Component, OnDestroy, OnInit } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; -import { Subject, takeUntil } from "rxjs"; +import { Subject, takeUntil, concatMap, map } from "rxjs"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { DialogService } from "@bitwarden/components"; @@ -34,10 +34,19 @@ export class NewMenuComponent implements OnInit, OnDestroy { ) {} ngOnInit() { - this.route.params.pipe(takeUntil(this.destroy$)).subscribe((params: any) => { - this.organizationId = params.organizationId; - this.organizationEnabled = this.organizationService.get(params.organizationId)?.enabled; - }); + this.route.params + .pipe( + concatMap((params) => + this.organizationService + .get$(params.organizationId) + .pipe(map((organization) => ({ params, organization }))), + ), + takeUntil(this.destroy$), + ) + .subscribe((mapResult) => { + this.organizationId = mapResult?.params?.organizationId; + this.organizationEnabled = mapResult?.organization?.enabled; + }); } ngOnDestroy(): void { diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/shared/org-suspended.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/shared/org-suspended.component.ts index ee94a78bb83..1683d447906 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/shared/org-suspended.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/shared/org-suspended.component.ts @@ -1,6 +1,6 @@ import { Component } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; -import { map } from "rxjs"; +import { map, concatMap } from "rxjs"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Icon, Icons } from "@bitwarden/components"; @@ -16,6 +16,7 @@ export class OrgSuspendedComponent { protected NoAccess: Icon = Icons.NoAccess; protected organizationName$ = this.route.params.pipe( - map((params) => this.organizationService.get(params.organizationId)?.name), + concatMap((params) => this.organizationService.get$(params.organizationId)), + map((org) => org?.name), ); } diff --git a/libs/angular/src/auth/components/login.component.ts b/libs/angular/src/auth/components/login.component.ts index 8314bdb2dcc..0ed1fd1dfe8 100644 --- a/libs/angular/src/auth/components/login.component.ts +++ b/libs/angular/src/auth/components/login.component.ts @@ -149,6 +149,7 @@ export class LoginComponent extends CaptchaProtectedComponent implements OnInit, this.captchaToken, null, ); + this.formPromise = this.loginStrategyService.logIn(credentials); const response = await this.formPromise; this.setFormValues(); @@ -302,6 +303,9 @@ export class LoginComponent extends CaptchaProtectedComponent implements OnInit, async saveEmailSettings() { this.setFormValues(); await this.loginService.saveEmailSettings(); + + // Save off email for SSO + await this.ssoLoginService.setSsoEmail(this.formGroup.value.email); } // Legacy accounts used the master key to encrypt data. Migration is required diff --git a/libs/angular/src/auth/components/sso.component.ts b/libs/angular/src/auth/components/sso.component.ts index 2f50288f048..a5a08f9aefa 100644 --- a/libs/angular/src/auth/components/sso.component.ts +++ b/libs/angular/src/auth/components/sso.component.ts @@ -182,11 +182,14 @@ export class SsoComponent { private async logIn(code: string, codeVerifier: string, orgSsoIdentifier: string): Promise { this.loggingIn = true; try { + const email = await this.ssoLoginService.getSsoEmail(); + const credentials = new SsoLoginCredentials( code, codeVerifier, this.redirectUri, orgSsoIdentifier, + email, ); this.formPromise = this.loginStrategyService.logIn(credentials); const authResult = await this.formPromise; diff --git a/libs/angular/src/directives/not-premium.directive.ts b/libs/angular/src/directives/not-premium.directive.ts index 46fbaa17619..3aee9b192d2 100644 --- a/libs/angular/src/directives/not-premium.directive.ts +++ b/libs/angular/src/directives/not-premium.directive.ts @@ -1,6 +1,7 @@ import { Directive, OnInit, TemplateRef, ViewContainerRef } from "@angular/core"; +import { firstValueFrom } from "rxjs"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; /** * Hides the element if the user has premium. @@ -12,11 +13,13 @@ export class NotPremiumDirective implements OnInit { constructor( private templateRef: TemplateRef, private viewContainer: ViewContainerRef, - private stateService: StateService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit(): Promise { - const premium = await this.stateService.getCanAccessPremium(); + const premium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); if (premium) { this.viewContainer.clear(); diff --git a/libs/angular/src/directives/premium.directive.ts b/libs/angular/src/directives/premium.directive.ts index 9e2991e97c9..d475669a1ab 100644 --- a/libs/angular/src/directives/premium.directive.ts +++ b/libs/angular/src/directives/premium.directive.ts @@ -1,6 +1,7 @@ -import { Directive, OnInit, TemplateRef, ViewContainerRef } from "@angular/core"; +import { Directive, OnDestroy, OnInit, TemplateRef, ViewContainerRef } from "@angular/core"; +import { Subject, takeUntil } from "rxjs"; -import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; /** * Only shows the element if the user has premium. @@ -8,20 +9,29 @@ import { StateService } from "@bitwarden/common/platform/abstractions/state.serv @Directive({ selector: "[appPremium]", }) -export class PremiumDirective implements OnInit { +export class PremiumDirective implements OnInit, OnDestroy { + private directiveIsDestroyed$ = new Subject(); + constructor( private templateRef: TemplateRef, private viewContainer: ViewContainerRef, - private stateService: StateService, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit(): Promise { - const premium = await this.stateService.getCanAccessPremium(); + this.billingAccountProfileStateService.hasPremiumFromAnySource$ + .pipe(takeUntil(this.directiveIsDestroyed$)) + .subscribe((premium: boolean) => { + if (premium) { + this.viewContainer.clear(); + } else { + this.viewContainer.createEmbeddedView(this.templateRef); + } + }); + } - if (premium) { - this.viewContainer.createEmbeddedView(this.templateRef); - } else { - this.viewContainer.clear(); - } + ngOnDestroy() { + this.directiveIsDestroyed$.next(true); + this.directiveIsDestroyed$.complete(); } } diff --git a/libs/angular/src/services/injection-tokens.ts b/libs/angular/src/services/injection-tokens.ts index 50aafc23260..7d39078797e 100644 --- a/libs/angular/src/services/injection-tokens.ts +++ b/libs/angular/src/services/injection-tokens.ts @@ -42,6 +42,7 @@ export const LOGOUT_CALLBACK = new SafeInjectionToken< export const LOCKED_CALLBACK = new SafeInjectionToken<(userId?: string) => Promise>( "LOCKED_CALLBACK", ); +export const SUPPORTS_SECURE_STORAGE = new SafeInjectionToken("SUPPORTS_SECURE_STORAGE"); export const LOCALES_DIRECTORY = new SafeInjectionToken("LOCALES_DIRECTORY"); export const SYSTEM_LANGUAGE = new SafeInjectionToken("SYSTEM_LANGUAGE"); export const LOG_MAC_FAILURES = new SafeInjectionToken("LOG_MAC_FAILURES"); diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index c5ab77e77b5..a509897fd3a 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -15,7 +15,6 @@ import { EventCollectionService as EventCollectionServiceAbstraction } from "@bi import { EventUploadService as EventUploadServiceAbstraction } from "@bitwarden/common/abstractions/event/event-upload.service"; import { NotificationsService as NotificationsServiceAbstraction } from "@bitwarden/common/abstractions/notifications.service"; import { SearchService as SearchServiceAbstraction } from "@bitwarden/common/abstractions/search.service"; -import { SettingsService as SettingsServiceAbstraction } from "@bitwarden/common/abstractions/settings.service"; import { VaultTimeoutSettingsService as VaultTimeoutSettingsServiceAbstraction } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout-settings.service"; import { VaultTimeoutService as VaultTimeoutServiceAbstraction } from "@bitwarden/common/abstractions/vault-timeout/vault-timeout.service"; import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction"; @@ -96,9 +95,11 @@ import { DomainSettingsService, DefaultDomainSettingsService, } from "@bitwarden/common/autofill/services/domain-settings.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions/billilng-api.service.abstraction"; import { OrganizationBillingServiceAbstraction } from "@bitwarden/common/billing/abstractions/organization-billing.service"; import { PaymentMethodWarningsServiceAbstraction } from "@bitwarden/common/billing/abstractions/payment-method-warnings-service.abstraction"; +import { DefaultBillingAccountProfileStateService } from "@bitwarden/common/billing/services/account/billing-account-profile-state.service"; import { BillingApiService } from "@bitwarden/common/billing/services/billing-api.service"; import { OrganizationBillingService } from "@bitwarden/common/billing/services/organization-billing.service"; import { PaymentMethodWarningsService } from "@bitwarden/common/billing/services/payment-method-warnings.service"; @@ -170,7 +171,6 @@ import { EventCollectionService } from "@bitwarden/common/services/event/event-c import { EventUploadService } from "@bitwarden/common/services/event/event-upload.service"; import { NotificationsService } from "@bitwarden/common/services/notifications.service"; import { SearchService } from "@bitwarden/common/services/search.service"; -import { SettingsService } from "@bitwarden/common/services/settings.service"; import { VaultTimeoutSettingsService } from "@bitwarden/common/services/vault-timeout/vault-timeout-settings.service"; import { VaultTimeoutService } from "@bitwarden/common/services/vault-timeout/vault-timeout.service"; import { @@ -250,6 +250,7 @@ import { SECURE_STORAGE, STATE_FACTORY, STATE_SERVICE_USE_CACHE, + SUPPORTS_SECURE_STORAGE, SYSTEM_LANGUAGE, SYSTEM_THEME_OBSERVABLE, WINDOW, @@ -272,6 +273,12 @@ const typesafeProviders: Array = [ useFactory: (i18nService: I18nServiceAbstraction) => i18nService.translationLocale, deps: [I18nServiceAbstraction], }), + safeProvider({ + provide: SUPPORTS_SECURE_STORAGE, + useFactory: (platformUtilsService: PlatformUtilsServiceAbstraction) => + platformUtilsService.supportsSecureStorage(), + deps: [PlatformUtilsServiceAbstraction], + }), safeProvider({ provide: LOCALES_DIRECTORY, useValue: "./locales", @@ -361,6 +368,7 @@ const typesafeProviders: Array = [ DeviceTrustCryptoServiceAbstraction, AuthRequestServiceAbstraction, GlobalStateProvider, + BillingAccountProfileStateService, ], }), safeProvider({ @@ -475,7 +483,12 @@ const typesafeProviders: Array = [ safeProvider({ provide: TokenServiceAbstraction, useClass: TokenService, - deps: [StateServiceAbstraction], + deps: [ + SingleUserStateProvider, + GlobalStateProvider, + SUPPORTS_SECURE_STORAGE, + AbstractStorageService, + ], }), safeProvider({ provide: KeyGenerationServiceAbstraction, @@ -519,6 +532,7 @@ const typesafeProviders: Array = [ PlatformUtilsServiceAbstraction, EnvironmentServiceAbstraction, AppIdServiceAbstraction, + StateServiceAbstraction, LOGOUT_CALLBACK, ], }), @@ -563,14 +577,10 @@ const typesafeProviders: Array = [ SendApiServiceAbstraction, AvatarServiceAbstraction, LOGOUT_CALLBACK, + BillingAccountProfileStateService, ], }), safeProvider({ provide: BroadcasterServiceAbstraction, useClass: BroadcasterService, deps: [] }), - safeProvider({ - provide: SettingsServiceAbstraction, - useClass: SettingsService, - deps: [StateServiceAbstraction], - }), safeProvider({ provide: VaultTimeoutSettingsServiceAbstraction, useClass: VaultTimeoutSettingsService, @@ -621,6 +631,7 @@ const typesafeProviders: Array = [ STATE_FACTORY, AccountServiceAbstraction, EnvironmentServiceAbstraction, + TokenServiceAbstraction, MigrationRunner, STATE_SERVICE_USE_CACHE, ], @@ -703,16 +714,17 @@ const typesafeProviders: Array = [ safeProvider({ provide: EventUploadServiceAbstraction, useClass: EventUploadService, - deps: [ApiServiceAbstraction, StateServiceAbstraction, LogService], + deps: [ApiServiceAbstraction, StateProvider, LogService, AccountServiceAbstraction], }), safeProvider({ provide: EventCollectionServiceAbstraction, useClass: EventCollectionService, deps: [ CipherServiceAbstraction, - StateServiceAbstraction, + StateProvider, OrganizationServiceAbstraction, EventUploadServiceAbstraction, + AccountServiceAbstraction, ], }), safeProvider({ @@ -760,7 +772,7 @@ const typesafeProviders: Array = [ safeProvider({ provide: InternalOrganizationServiceAbstraction, useClass: OrganizationService, - deps: [StateServiceAbstraction, StateProvider], + deps: [StateProvider], }), safeProvider({ provide: OrganizationServiceAbstraction, @@ -1031,6 +1043,11 @@ const typesafeProviders: Array = [ useClass: PaymentMethodWarningsService, deps: [BillingApiServiceAbstraction, StateProvider], }), + safeProvider({ + provide: BillingAccountProfileStateService, + useClass: DefaultBillingAccountProfileStateService, + deps: [ActiveUserStateProvider], + }), ]; function encryptServiceFactory( diff --git a/libs/angular/src/tools/export/components/export-scope-callout.component.ts b/libs/angular/src/tools/export/components/export-scope-callout.component.ts index 48651d822ab..545dfe4560a 100644 --- a/libs/angular/src/tools/export/components/export-scope-callout.component.ts +++ b/libs/angular/src/tools/export/components/export-scope-callout.component.ts @@ -34,7 +34,7 @@ export class ExportScopeCalloutComponent implements OnInit { ) {} async ngOnInit(): Promise { - if (!this.organizationService.hasOrganizations()) { + if (!(await this.organizationService.hasOrganizations())) { return; } @@ -48,7 +48,7 @@ export class ExportScopeCalloutComponent implements OnInit { ? { title: "exportingOrganizationVaultTitle", description: "exportingOrganizationVaultDesc", - scopeIdentifier: this.organizationService.get(organizationId).name, + scopeIdentifier: (await this.organizationService.get(organizationId)).name, } : { title: "exportingPersonalVaultTitle", diff --git a/libs/angular/src/tools/send/add-edit.component.ts b/libs/angular/src/tools/send/add-edit.component.ts index 9742de1a7b8..dafac1e92ba 100644 --- a/libs/angular/src/tools/send/add-edit.component.ts +++ b/libs/angular/src/tools/send/add-edit.component.ts @@ -5,6 +5,7 @@ import { BehaviorSubject, Subject, concatMap, firstValueFrom, map, takeUntil } f import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyType } from "@bitwarden/common/admin-console/enums"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -116,6 +117,7 @@ export class AddEditComponent implements OnInit, OnDestroy { protected sendApiService: SendApiService, protected dialogService: DialogService, protected formBuilder: FormBuilder, + protected billingAccountProfileStateService: BillingAccountProfileStateService, ) { this.typeOptions = [ { name: i18nService.t("sendTypeFile"), value: SendType.File, premium: true }, @@ -188,6 +190,12 @@ export class AddEditComponent implements OnInit, OnDestroy { } }); + this.billingAccountProfileStateService.hasPremiumFromAnySource$ + .pipe(takeUntil(this.destroy$)) + .subscribe((hasPremiumFromAnySource) => { + this.canAccessPremium = hasPremiumFromAnySource; + }); + await this.load(); } @@ -205,7 +213,6 @@ export class AddEditComponent implements OnInit, OnDestroy { } async load() { - this.canAccessPremium = await this.stateService.getCanAccessPremium(); this.emailVerified = await this.stateService.getEmailVerified(); this.type = !this.canAccessPremium || !this.emailVerified ? SendType.Text : SendType.File; diff --git a/libs/angular/src/vault/components/attachments.component.ts b/libs/angular/src/vault/components/attachments.component.ts index 2c81dccdc77..fc86f2f5277 100644 --- a/libs/angular/src/vault/components/attachments.component.ts +++ b/libs/angular/src/vault/components/attachments.component.ts @@ -1,6 +1,8 @@ import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core"; +import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service"; @@ -42,6 +44,7 @@ export class AttachmentsComponent implements OnInit { protected stateService: StateService, protected fileDownloadService: FileDownloadService, protected dialogService: DialogService, + protected billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async ngOnInit() { @@ -185,7 +188,9 @@ export class AttachmentsComponent implements OnInit { await this.cipherService.getKeyForCipherKeyDecryption(this.cipherDomain), ); - const canAccessPremium = await this.stateService.getCanAccessPremium(); + const canAccessPremium = await firstValueFrom( + this.billingAccountProfileStateService.hasPremiumFromAnySource$, + ); this.canAccessAttachments = canAccessPremium || this.cipher.organizationId != null; if (!this.canAccessAttachments) { diff --git a/libs/angular/src/vault/components/icon.component.ts b/libs/angular/src/vault/components/icon.component.ts index 33cd2bc71a0..8323c55d4e4 100644 --- a/libs/angular/src/vault/components/icon.component.ts +++ b/libs/angular/src/vault/components/icon.component.ts @@ -8,7 +8,7 @@ import { Observable, } from "rxjs"; -import { SettingsService } from "@bitwarden/common/abstractions/settings.service"; +import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { buildCipherIcon } from "@bitwarden/common/vault/icon/build-cipher-icon"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; @@ -35,15 +35,15 @@ export class IconComponent implements OnInit { constructor( private environmentService: EnvironmentService, - private settingsService: SettingsService, + private domainSettingsService: DomainSettingsService, ) {} async ngOnInit() { const iconsUrl = this.environmentService.getIconsUrl(); this.data$ = combineLatest([ - this.settingsService.disableFavicon$.pipe(distinctUntilChanged()), + this.domainSettingsService.showFavicons$.pipe(distinctUntilChanged()), this.cipher$.pipe(filter((c) => c !== undefined)), - ]).pipe(map(([disableFavicon, cipher]) => buildCipherIcon(iconsUrl, cipher, disableFavicon))); + ]).pipe(map(([showFavicon, cipher]) => buildCipherIcon(iconsUrl, cipher, showFavicon))); } } diff --git a/libs/angular/src/vault/components/premium.component.ts b/libs/angular/src/vault/components/premium.component.ts index 526e453b2c1..974a2b6cdd1 100644 --- a/libs/angular/src/vault/components/premium.component.ts +++ b/libs/angular/src/vault/components/premium.component.ts @@ -1,6 +1,8 @@ -import { Directive, OnInit } from "@angular/core"; +import { Directive } from "@angular/core"; +import { Observable, Subject } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -9,11 +11,12 @@ import { StateService } from "@bitwarden/common/platform/abstractions/state.serv import { DialogService } from "@bitwarden/components"; @Directive() -export class PremiumComponent implements OnInit { - isPremium = false; +export class PremiumComponent { + isPremium$: Observable; price = 10; refreshPromise: Promise; cloudWebVaultUrl: string; + private directiveIsDestroyed$ = new Subject(); constructor( protected i18nService: I18nService, @@ -22,13 +25,11 @@ export class PremiumComponent implements OnInit { private logService: LogService, protected stateService: StateService, protected dialogService: DialogService, - private environmentService: EnvironmentService, + environmentService: EnvironmentService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { - this.cloudWebVaultUrl = this.environmentService.getCloudWebVaultUrl(); - } - - async ngOnInit() { - this.isPremium = await this.stateService.getCanAccessPremium(); + this.cloudWebVaultUrl = environmentService.getCloudWebVaultUrl(); + this.isPremium$ = billingAccountProfileStateService.hasPremiumFromAnySource$; } async refresh() { @@ -36,7 +37,6 @@ export class PremiumComponent implements OnInit { this.refreshPromise = this.apiService.refreshIdentityToken(); await this.refreshPromise; this.platformUtilsService.showToast("success", null, this.i18nService.t("refreshComplete")); - this.isPremium = await this.stateService.getCanAccessPremium(); } catch (e) { this.logService.error(e); } diff --git a/libs/angular/src/vault/components/view.component.ts b/libs/angular/src/vault/components/view.component.ts index 365041010a7..42349737f0d 100644 --- a/libs/angular/src/vault/components/view.component.ts +++ b/libs/angular/src/vault/components/view.component.ts @@ -9,12 +9,13 @@ import { OnInit, Output, } from "@angular/core"; -import { firstValueFrom } from "rxjs"; +import { firstValueFrom, Subject, takeUntil } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AuditService } from "@bitwarden/common/abstractions/audit.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType } from "@bitwarden/common/enums"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; @@ -68,6 +69,7 @@ export class ViewComponent implements OnDestroy, OnInit { private totpInterval: any; private previousCipherId: string; private passwordReprompted = false; + private directiveIsDestroyed$ = new Subject(); get fido2CredentialCreationDateValue(): string { const dateCreated = this.i18nService.t("dateCreated"); @@ -99,6 +101,7 @@ export class ViewComponent implements OnDestroy, OnInit { protected fileDownloadService: FileDownloadService, protected dialogService: DialogService, protected datePipe: DatePipe, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} ngOnInit() { @@ -116,11 +119,19 @@ export class ViewComponent implements OnDestroy, OnInit { } }); }); + + this.billingAccountProfileStateService.hasPremiumFromAnySource$ + .pipe(takeUntil(this.directiveIsDestroyed$)) + .subscribe((canAccessPremium: boolean) => { + this.canAccessPremium = canAccessPremium; + }); } ngOnDestroy() { this.broadcasterService.unsubscribe(BroadcasterSubscriptionId); this.cleanUp(); + this.directiveIsDestroyed$.next(true); + this.directiveIsDestroyed$.complete(); } async load() { @@ -130,7 +141,6 @@ export class ViewComponent implements OnDestroy, OnInit { this.cipher = await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher), ); - this.canAccessPremium = await this.stateService.getCanAccessPremium(); this.showPremiumRequiredTotp = this.cipher.login.totp && !this.canAccessPremium && !this.cipher.organizationUseTotp; diff --git a/libs/auth/src/common/index.ts b/libs/auth/src/common/index.ts index f70f8be2153..936666e1a81 100644 --- a/libs/auth/src/common/index.ts +++ b/libs/auth/src/common/index.ts @@ -4,3 +4,4 @@ export * from "./abstractions"; export * from "./models"; export * from "./services"; +export * from "./utilities"; diff --git a/libs/auth/src/common/login-strategies/auth-request-login.strategy.spec.ts b/libs/auth/src/common/login-strategies/auth-request-login.strategy.spec.ts index dd046195aae..18ac9f0bf78 100644 --- a/libs/auth/src/common/login-strategies/auth-request-login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/auth-request-login.strategy.spec.ts @@ -5,6 +5,7 @@ import { DeviceTrustCryptoServiceAbstraction } from "@bitwarden/common/auth/abst import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { TwoFactorService } from "@bitwarden/common/auth/abstractions/two-factor.service"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -37,6 +38,7 @@ describe("AuthRequestLoginStrategy", () => { let stateService: MockProxy; let twoFactorService: MockProxy; let deviceTrustCryptoService: MockProxy; + let billingAccountProfileStateService: MockProxy; let authRequestLoginStrategy: AuthRequestLoginStrategy; let credentials: AuthRequestLoginCredentials; @@ -64,10 +66,11 @@ describe("AuthRequestLoginStrategy", () => { stateService = mock(); twoFactorService = mock(); deviceTrustCryptoService = mock(); + billingAccountProfileStateService = mock(); tokenService.getTwoFactorToken.mockResolvedValue(null); appIdService.getAppId.mockResolvedValue(deviceId); - tokenService.decodeToken.mockResolvedValue({}); + tokenService.decodeAccessToken.mockResolvedValue({}); authRequestLoginStrategy = new AuthRequestLoginStrategy( cache, @@ -81,6 +84,7 @@ describe("AuthRequestLoginStrategy", () => { stateService, twoFactorService, deviceTrustCryptoService, + billingAccountProfileStateService, ); tokenResponse = identityTokenResponseFactory(); diff --git a/libs/auth/src/common/login-strategies/auth-request-login.strategy.ts b/libs/auth/src/common/login-strategies/auth-request-login.strategy.ts index acf21219c20..09312226d8b 100644 --- a/libs/auth/src/common/login-strategies/auth-request-login.strategy.ts +++ b/libs/auth/src/common/login-strategies/auth-request-login.strategy.ts @@ -9,6 +9,7 @@ import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result"; import { PasswordTokenRequest } from "@bitwarden/common/auth/models/request/identity-token/password-token.request"; import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/identity-token/token-two-factor.request"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -54,6 +55,7 @@ export class AuthRequestLoginStrategy extends LoginStrategy { stateService: StateService, twoFactorService: TwoFactorService, private deviceTrustCryptoService: DeviceTrustCryptoServiceAbstraction, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cryptoService, @@ -65,6 +67,7 @@ export class AuthRequestLoginStrategy extends LoginStrategy { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); this.cache = new BehaviorSubject(data); @@ -79,7 +82,7 @@ export class AuthRequestLoginStrategy extends LoginStrategy { credentials.email, credentials.accessCode, null, - await this.buildTwoFactor(credentials.twoFactor), + await this.buildTwoFactor(credentials.twoFactor, credentials.email), await this.buildDeviceRequest(), ); data.tokenRequest.setAuthRequestAccessCode(credentials.authRequestId); diff --git a/libs/auth/src/common/login-strategies/login.strategy.spec.ts b/libs/auth/src/common/login-strategies/login.strategy.spec.ts index 5771cb2543f..6f3d480f201 100644 --- a/libs/auth/src/common/login-strategies/login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/login.strategy.spec.ts @@ -14,6 +14,8 @@ import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/id import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response"; import { MasterPasswordPolicyResponse } from "@bitwarden/common/auth/models/response/master-password-policy.response"; import { IUserDecryptionOptionsServerResponse } from "@bitwarden/common/auth/models/response/user-decryption-options/user-decryption-options.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; +import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -108,6 +110,7 @@ describe("LoginStrategy", () => { let twoFactorService: MockProxy; let policyService: MockProxy; let passwordStrengthService: MockProxy; + let billingAccountProfileStateService: MockProxy; let passwordLoginStrategy: PasswordLoginStrategy; let credentials: PasswordLoginCredentials; @@ -123,11 +126,13 @@ describe("LoginStrategy", () => { logService = mock(); stateService = mock(); twoFactorService = mock(); + policyService = mock(); passwordStrengthService = mock(); + billingAccountProfileStateService = mock(); appIdService.getAppId.mockResolvedValue(deviceId); - tokenService.decodeToken.calledWith(accessToken).mockResolvedValue(decodedToken); + tokenService.decodeAccessToken.calledWith(accessToken).mockResolvedValue(decodedToken); // The base class is abstract so we test it via PasswordLoginStrategy passwordLoginStrategy = new PasswordLoginStrategy( @@ -144,6 +149,7 @@ describe("LoginStrategy", () => { passwordStrengthService, policyService, loginStrategyService, + billingAccountProfileStateService, ); credentials = new PasswordLoginCredentials(email, masterPassword); }); @@ -167,8 +173,21 @@ describe("LoginStrategy", () => { const idTokenResponse = identityTokenResponseFactory(); apiService.postIdentityToken.mockResolvedValue(idTokenResponse); + const mockVaultTimeoutAction = VaultTimeoutAction.Lock; + const mockVaultTimeout = 1000; + + stateService.getVaultTimeoutAction.mockResolvedValue(mockVaultTimeoutAction); + stateService.getVaultTimeout.mockResolvedValue(mockVaultTimeout); + await passwordLoginStrategy.logIn(credentials); + expect(tokenService.setTokens).toHaveBeenCalledWith( + accessToken, + refreshToken, + mockVaultTimeoutAction, + mockVaultTimeout, + ); + expect(stateService.addAccount).toHaveBeenCalledWith( new Account({ profile: { @@ -177,17 +196,12 @@ describe("LoginStrategy", () => { userId: userId, name: name, email: email, - hasPremiumPersonally: false, kdfIterations: kdfIterations, kdfType: kdf, }, }, tokens: { ...new AccountTokens(), - ...{ - accessToken: accessToken, - refreshToken: refreshToken, - }, }, keys: new AccountKeys(), decryptionOptions: AccountDecryptionOptions.fromResponse(idTokenResponse), @@ -299,6 +313,7 @@ describe("LoginStrategy", () => { expect(stateService.addAccount).not.toHaveBeenCalled(); expect(messagingService.send).not.toHaveBeenCalled(); + expect(tokenService.clearTwoFactorToken).toHaveBeenCalled(); const expected = new AuthResult(); expected.twoFactorProviders = new Map(); @@ -397,6 +412,7 @@ describe("LoginStrategy", () => { passwordStrengthService, policyService, loginStrategyService, + billingAccountProfileStateService, ); apiService.postIdentityToken.mockResolvedValue(identityTokenResponseFactory()); diff --git a/libs/auth/src/common/login-strategies/login.strategy.ts b/libs/auth/src/common/login-strategies/login.strategy.ts index e6ff1c68a38..f5f28dd0440 100644 --- a/libs/auth/src/common/login-strategies/login.strategy.ts +++ b/libs/auth/src/common/login-strategies/login.strategy.ts @@ -15,7 +15,9 @@ import { WebAuthnLoginTokenRequest } from "@bitwarden/common/auth/models/request import { IdentityCaptchaResponse } from "@bitwarden/common/auth/models/response/identity-captcha.response"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { ClientType } from "@bitwarden/common/enums"; +import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { KeysRequest } from "@bitwarden/common/models/request/keys.request"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; @@ -49,6 +51,9 @@ export abstract class LoginStrategyData { | SsoTokenRequest | WebAuthnLoginTokenRequest; captchaBypassToken?: string; + + /** User's entered email obtained pre-login. */ + abstract userEnteredEmail?: string; } export abstract class LoginStrategy { @@ -64,6 +69,7 @@ export abstract class LoginStrategy { protected logService: LogService, protected stateService: StateService, protected twoFactorService: TwoFactorService, + protected billingAccountProfileStateService: BillingAccountProfileStateService, ) {} abstract exportCache(): CacheData; @@ -110,21 +116,47 @@ export abstract class LoginStrategy { return new DeviceRequest(appId, this.platformUtilsService); } - protected async buildTwoFactor(userProvidedTwoFactor?: TokenTwoFactorRequest) { + /** + * Builds the TokenTwoFactorRequest to be used within other login strategies token requests + * to the server. + * If the user provided a 2FA token in an already created TokenTwoFactorRequest, it will be used. + * If not, and the user has previously remembered a 2FA token, it will be used. + * If neither of these are true, an empty TokenTwoFactorRequest will be returned. + * @param userProvidedTwoFactor - optional - The 2FA token request provided by the caller + * @param email - optional - ensure that email is provided for any login strategies that support remember 2FA functionality + * @returns a promise which resolves to a TokenTwoFactorRequest to be sent to the server + */ + protected async buildTwoFactor( + userProvidedTwoFactor?: TokenTwoFactorRequest, + email?: string, + ): Promise { if (userProvidedTwoFactor != null) { return userProvidedTwoFactor; } - const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(); - if (storedTwoFactorToken != null) { - return new TokenTwoFactorRequest(TwoFactorProviderType.Remember, storedTwoFactorToken, false); + if (email) { + const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email); + if (storedTwoFactorToken != null) { + return new TokenTwoFactorRequest( + TwoFactorProviderType.Remember, + storedTwoFactorToken, + false, + ); + } } return new TokenTwoFactorRequest(); } - protected async saveAccountInformation(tokenResponse: IdentityTokenResponse) { - const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken); + /** + * Initializes the account with information from the IdTokenResponse after successful login. + * It also sets the access token and refresh token in the token service. + * + * @param {IdentityTokenResponse} tokenResponse - The response from the server containing the identity token. + * @returns {Promise} - A promise that resolves when the account information has been successfully saved. + */ + protected async saveAccountInformation(tokenResponse: IdentityTokenResponse): Promise { + const accountInformation = await this.tokenService.decodeAccessToken(tokenResponse.accessToken); // Must persist existing device key if it exists for trusted device decryption to work // However, we must provide a user id so that the device key can be retrieved @@ -141,6 +173,18 @@ export abstract class LoginStrategy { // If you don't persist existing admin auth requests on login, they will get deleted. const adminAuthRequest = await this.stateService.getAdminAuthRequest({ userId }); + const vaultTimeoutAction = await this.stateService.getVaultTimeoutAction(); + const vaultTimeout = await this.stateService.getVaultTimeout(); + + // set access token and refresh token before account initialization so authN status can be accurate + // User id will be derived from the access token. + await this.tokenService.setTokens( + tokenResponse.accessToken, + tokenResponse.refreshToken, + vaultTimeoutAction as VaultTimeoutAction, + vaultTimeout, + ); + await this.stateService.addAccount( new Account({ profile: { @@ -149,7 +193,6 @@ export abstract class LoginStrategy { userId, name: accountInformation.name, email: accountInformation.email, - hasPremiumPersonally: accountInformation.premium, kdfIterations: tokenResponse.kdfIterations, kdfMemory: tokenResponse.kdfMemory, kdfParallelism: tokenResponse.kdfParallelism, @@ -158,16 +201,14 @@ export abstract class LoginStrategy { }, tokens: { ...new AccountTokens(), - ...{ - accessToken: tokenResponse.accessToken, - refreshToken: tokenResponse.refreshToken, - }, }, keys: accountKeys, decryptionOptions: AccountDecryptionOptions.fromResponse(tokenResponse), adminAuthRequest: adminAuthRequest?.toJSON(), }), ); + + await this.billingAccountProfileStateService.setHasPremium(accountInformation.premium, false); } protected async processTokenResponse(response: IdentityTokenResponse): Promise { @@ -193,7 +234,10 @@ export abstract class LoginStrategy { await this.saveAccountInformation(response); if (response.twoFactorToken != null) { - await this.tokenService.setTwoFactorToken(response); + // note: we can read email from access token b/c it was saved in saveAccountInformation + const userEmail = await this.tokenService.getEmail(); + + await this.tokenService.setTwoFactorToken(userEmail, response.twoFactorToken); } await this.setMasterKey(response); @@ -226,7 +270,18 @@ export abstract class LoginStrategy { } } + /** + * Handles the response from the server when a 2FA is required. + * It clears any existing 2FA token, as it's no longer valid, and sets up the necessary data for the 2FA process. + * + * @param {IdentityTwoFactorResponse} response - The response from the server indicating that 2FA is required. + * @returns {Promise} - A promise that resolves to an AuthResult object + */ private async processTwoFactorResponse(response: IdentityTwoFactorResponse): Promise { + // If we get a 2FA required response, then we should clear the 2FA token + // just in case as it is no longer valid. + await this.clearTwoFactorToken(); + const result = new AuthResult(); result.twoFactorProviders = response.twoFactorProviders2; @@ -237,6 +292,16 @@ export abstract class LoginStrategy { return result; } + /** + * Clears the 2FA token from the token service using the user's email if it exists + */ + private async clearTwoFactorToken() { + const email = this.cache.value.userEnteredEmail; + if (email) { + await this.tokenService.clearTwoFactorToken(email); + } + } + private async processCaptchaResponse(response: IdentityCaptchaResponse): Promise { const result = new AuthResult(); result.captchaSiteKey = response.siteKey; diff --git a/libs/auth/src/common/login-strategies/password-login.strategy.spec.ts b/libs/auth/src/common/login-strategies/password-login.strategy.spec.ts index 77ef6792ba0..007c33afc6b 100644 --- a/libs/auth/src/common/login-strategies/password-login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/password-login.strategy.spec.ts @@ -9,6 +9,7 @@ import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/for import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response"; import { MasterPasswordPolicyResponse } from "@bitwarden/common/auth/models/response/master-password-policy.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -61,6 +62,7 @@ describe("PasswordLoginStrategy", () => { let twoFactorService: MockProxy; let policyService: MockProxy; let passwordStrengthService: MockProxy; + let billingAccountProfileStateService: MockProxy; let passwordLoginStrategy: PasswordLoginStrategy; let credentials: PasswordLoginCredentials; @@ -79,9 +81,10 @@ describe("PasswordLoginStrategy", () => { twoFactorService = mock(); policyService = mock(); passwordStrengthService = mock(); + billingAccountProfileStateService = mock(); appIdService.getAppId.mockResolvedValue(deviceId); - tokenService.decodeToken.mockResolvedValue({}); + tokenService.decodeAccessToken.mockResolvedValue({}); loginStrategyService.makePreloginKey.mockResolvedValue(masterKey); @@ -108,6 +111,7 @@ describe("PasswordLoginStrategy", () => { passwordStrengthService, policyService, loginStrategyService, + billingAccountProfileStateService, ); credentials = new PasswordLoginCredentials(email, masterPassword); tokenResponse = identityTokenResponseFactory(masterPasswordPolicy); diff --git a/libs/auth/src/common/login-strategies/password-login.strategy.ts b/libs/auth/src/common/login-strategies/password-login.strategy.ts index c12eb28204b..2104595b450 100644 --- a/libs/auth/src/common/login-strategies/password-login.strategy.ts +++ b/libs/auth/src/common/login-strategies/password-login.strategy.ts @@ -13,6 +13,7 @@ import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/ide import { IdentityCaptchaResponse } from "@bitwarden/common/auth/models/response/identity-captcha.response"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -32,6 +33,10 @@ import { LoginStrategy, LoginStrategyData } from "./login.strategy"; export class PasswordLoginStrategyData implements LoginStrategyData { tokenRequest: PasswordTokenRequest; + + /** User's entered email obtained pre-login. Always present in MP login. */ + userEnteredEmail: string; + captchaBypassToken?: string; /** * The local version of the user's master key hash @@ -82,6 +87,7 @@ export class PasswordLoginStrategy extends LoginStrategy { private passwordStrengthService: PasswordStrengthServiceAbstraction, private policyService: PolicyService, private loginStrategyService: LoginStrategyServiceAbstraction, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cryptoService, @@ -93,6 +99,7 @@ export class PasswordLoginStrategy extends LoginStrategy { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); this.cache = new BehaviorSubject(data); @@ -105,6 +112,7 @@ export class PasswordLoginStrategy extends LoginStrategy { const data = new PasswordLoginStrategyData(); data.masterKey = await this.loginStrategyService.makePreloginKey(masterPassword, email); + data.userEnteredEmail = email; // Hash the password early (before authentication) so we don't persist it in memory in plaintext data.localMasterKeyHash = await this.cryptoService.hashMasterKey( @@ -118,7 +126,7 @@ export class PasswordLoginStrategy extends LoginStrategy { email, masterKeyHash, captchaToken, - await this.buildTwoFactor(twoFactor), + await this.buildTwoFactor(twoFactor, email), await this.buildDeviceRequest(), ); diff --git a/libs/auth/src/common/login-strategies/sso-login.strategy.spec.ts b/libs/auth/src/common/login-strategies/sso-login.strategy.spec.ts index b6cf6db58a0..c987bcc95a6 100644 --- a/libs/auth/src/common/login-strategies/sso-login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/sso-login.strategy.spec.ts @@ -9,6 +9,7 @@ import { AdminAuthRequestStorable } from "@bitwarden/common/auth/models/domain/a import { AuthRequestResponse } from "@bitwarden/common/auth/models/response/auth-request.response"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IUserDecryptionOptionsServerResponse } from "@bitwarden/common/auth/models/response/user-decryption-options/user-decryption-options.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; @@ -42,6 +43,7 @@ describe("SsoLoginStrategy", () => { let deviceTrustCryptoService: MockProxy; let authRequestService: MockProxy; let i18nService: MockProxy; + let billingAccountProfileStateService: MockProxy; let ssoLoginStrategy: SsoLoginStrategy; let credentials: SsoLoginCredentials; @@ -68,10 +70,11 @@ describe("SsoLoginStrategy", () => { deviceTrustCryptoService = mock(); authRequestService = mock(); i18nService = mock(); + billingAccountProfileStateService = mock(); tokenService.getTwoFactorToken.mockResolvedValue(null); appIdService.getAppId.mockResolvedValue(deviceId); - tokenService.decodeToken.mockResolvedValue({}); + tokenService.decodeAccessToken.mockResolvedValue({}); ssoLoginStrategy = new SsoLoginStrategy( null, @@ -88,6 +91,7 @@ describe("SsoLoginStrategy", () => { deviceTrustCryptoService, authRequestService, i18nService, + billingAccountProfileStateService, ); credentials = new SsoLoginCredentials(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId); }); diff --git a/libs/auth/src/common/login-strategies/sso-login.strategy.ts b/libs/auth/src/common/login-strategies/sso-login.strategy.ts index a5ef9222048..b8d1df6f577 100644 --- a/libs/auth/src/common/login-strategies/sso-login.strategy.ts +++ b/libs/auth/src/common/login-strategies/sso-login.strategy.ts @@ -10,6 +10,7 @@ import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/for import { SsoTokenRequest } from "@bitwarden/common/auth/models/request/identity-token/sso-token.request"; import { AuthRequestResponse } from "@bitwarden/common/auth/models/response/auth-request.response"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { HttpStatusCode } from "@bitwarden/common/enums"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; @@ -29,6 +30,10 @@ import { LoginStrategyData, LoginStrategy } from "./login.strategy"; export class SsoLoginStrategyData implements LoginStrategyData { captchaBypassToken: string; tokenRequest: SsoTokenRequest; + /** + * User's entered email obtained pre-login. Present in most SSO flows, but not CLI + SSO Flow. + */ + userEnteredEmail?: string; /** * User email address. Only available after authentication. */ @@ -83,6 +88,7 @@ export class SsoLoginStrategy extends LoginStrategy { private deviceTrustCryptoService: DeviceTrustCryptoServiceAbstraction, private authRequestService: AuthRequestServiceAbstraction, private i18nService: I18nService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cryptoService, @@ -94,6 +100,7 @@ export class SsoLoginStrategy extends LoginStrategy { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); this.cache = new BehaviorSubject(data); @@ -105,11 +112,14 @@ export class SsoLoginStrategy extends LoginStrategy { async logIn(credentials: SsoLoginCredentials) { const data = new SsoLoginStrategyData(); data.orgId = credentials.orgId; + + data.userEnteredEmail = credentials.email; + data.tokenRequest = new SsoTokenRequest( credentials.code, credentials.codeVerifier, credentials.redirectUrl, - await this.buildTwoFactor(credentials.twoFactor), + await this.buildTwoFactor(credentials.twoFactor, credentials.email), await this.buildDeviceRequest(), ); diff --git a/libs/auth/src/common/login-strategies/user-api-login.strategy.spec.ts b/libs/auth/src/common/login-strategies/user-api-login.strategy.spec.ts index d50d2883c76..48f6fd32aba 100644 --- a/libs/auth/src/common/login-strategies/user-api-login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/user-api-login.strategy.spec.ts @@ -4,6 +4,8 @@ import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { KeyConnectorService } from "@bitwarden/common/auth/abstractions/key-connector.service"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { TwoFactorService } from "@bitwarden/common/auth/abstractions/two-factor.service"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; +import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; @@ -35,6 +37,7 @@ describe("UserApiLoginStrategy", () => { let twoFactorService: MockProxy; let keyConnectorService: MockProxy; let environmentService: MockProxy; + let billingAccountProfileStateService: MockProxy; let apiLogInStrategy: UserApiLoginStrategy; let credentials: UserApiLoginCredentials; @@ -56,10 +59,11 @@ describe("UserApiLoginStrategy", () => { twoFactorService = mock(); keyConnectorService = mock(); environmentService = mock(); + billingAccountProfileStateService = mock(); appIdService.getAppId.mockResolvedValue(deviceId); tokenService.getTwoFactorToken.mockResolvedValue(null); - tokenService.decodeToken.mockResolvedValue({}); + tokenService.decodeAccessToken.mockResolvedValue({}); apiLogInStrategy = new UserApiLoginStrategy( cache, @@ -74,6 +78,7 @@ describe("UserApiLoginStrategy", () => { twoFactorService, environmentService, keyConnectorService, + billingAccountProfileStateService, ); credentials = new UserApiLoginCredentials(apiClientId, apiClientSecret); @@ -101,10 +106,23 @@ describe("UserApiLoginStrategy", () => { it("sets the local environment after a successful login", async () => { apiService.postIdentityToken.mockResolvedValue(identityTokenResponseFactory()); + const mockVaultTimeoutAction = VaultTimeoutAction.Lock; + const mockVaultTimeout = 60; + stateService.getVaultTimeoutAction.mockResolvedValue(mockVaultTimeoutAction); + stateService.getVaultTimeout.mockResolvedValue(mockVaultTimeout); + await apiLogInStrategy.logIn(credentials); - expect(stateService.setApiKeyClientId).toHaveBeenCalledWith(apiClientId); - expect(stateService.setApiKeyClientSecret).toHaveBeenCalledWith(apiClientSecret); + expect(tokenService.setClientId).toHaveBeenCalledWith( + apiClientId, + mockVaultTimeoutAction, + mockVaultTimeout, + ); + expect(tokenService.setClientSecret).toHaveBeenCalledWith( + apiClientSecret, + mockVaultTimeoutAction, + mockVaultTimeout, + ); expect(stateService.addAccount).toHaveBeenCalled(); }); diff --git a/libs/auth/src/common/login-strategies/user-api-login.strategy.ts b/libs/auth/src/common/login-strategies/user-api-login.strategy.ts index a26fb41ae96..9bb6d8fb125 100644 --- a/libs/auth/src/common/login-strategies/user-api-login.strategy.ts +++ b/libs/auth/src/common/login-strategies/user-api-login.strategy.ts @@ -7,6 +7,8 @@ import { TokenService } from "@bitwarden/common/auth/abstractions/token.service" import { TwoFactorService } from "@bitwarden/common/auth/abstractions/two-factor.service"; import { UserApiTokenRequest } from "@bitwarden/common/auth/models/request/identity-token/user-api-token.request"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; +import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; @@ -47,6 +49,7 @@ export class UserApiLoginStrategy extends LoginStrategy { twoFactorService: TwoFactorService, private environmentService: EnvironmentService, private keyConnectorService: KeyConnectorService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cryptoService, @@ -58,6 +61,7 @@ export class UserApiLoginStrategy extends LoginStrategy { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); this.cache = new BehaviorSubject(data); } @@ -104,9 +108,21 @@ export class UserApiLoginStrategy extends LoginStrategy { protected async saveAccountInformation(tokenResponse: IdentityTokenResponse) { await super.saveAccountInformation(tokenResponse); + const vaultTimeout = await this.stateService.getVaultTimeout(); + const vaultTimeoutAction = await this.stateService.getVaultTimeoutAction(); + const tokenRequest = this.cache.value.tokenRequest; - await this.stateService.setApiKeyClientId(tokenRequest.clientId); - await this.stateService.setApiKeyClientSecret(tokenRequest.clientSecret); + + await this.tokenService.setClientId( + tokenRequest.clientId, + vaultTimeoutAction as VaultTimeoutAction, + vaultTimeout, + ); + await this.tokenService.setClientSecret( + tokenRequest.clientSecret, + vaultTimeoutAction as VaultTimeoutAction, + vaultTimeout, + ); } exportCache(): CacheData { diff --git a/libs/auth/src/common/login-strategies/webauthn-login.strategy.spec.ts b/libs/auth/src/common/login-strategies/webauthn-login.strategy.spec.ts index 17933a3dcb6..9ab64170c1d 100644 --- a/libs/auth/src/common/login-strategies/webauthn-login.strategy.spec.ts +++ b/libs/auth/src/common/login-strategies/webauthn-login.strategy.spec.ts @@ -7,6 +7,7 @@ import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IUserDecryptionOptionsServerResponse } from "@bitwarden/common/auth/models/response/user-decryption-options/user-decryption-options.response"; import { WebAuthnLoginAssertionResponseRequest } from "@bitwarden/common/auth/services/webauthn-login/request/webauthn-login-assertion-response.request"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -34,6 +35,7 @@ describe("WebAuthnLoginStrategy", () => { let logService!: MockProxy; let stateService!: MockProxy; let twoFactorService!: MockProxy; + let billingAccountProfileStateService: MockProxy; let webAuthnLoginStrategy!: WebAuthnLoginStrategy; @@ -68,10 +70,11 @@ describe("WebAuthnLoginStrategy", () => { logService = mock(); stateService = mock(); twoFactorService = mock(); + billingAccountProfileStateService = mock(); tokenService.getTwoFactorToken.mockResolvedValue(null); appIdService.getAppId.mockResolvedValue(deviceId); - tokenService.decodeToken.mockResolvedValue({}); + tokenService.decodeAccessToken.mockResolvedValue({}); webAuthnLoginStrategy = new WebAuthnLoginStrategy( cache, @@ -84,6 +87,7 @@ describe("WebAuthnLoginStrategy", () => { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); // Create credentials diff --git a/libs/auth/src/common/login-strategies/webauthn-login.strategy.ts b/libs/auth/src/common/login-strategies/webauthn-login.strategy.ts index c42e6d65745..b60342f0b41 100644 --- a/libs/auth/src/common/login-strategies/webauthn-login.strategy.ts +++ b/libs/auth/src/common/login-strategies/webauthn-login.strategy.ts @@ -7,6 +7,7 @@ import { TwoFactorService } from "@bitwarden/common/auth/abstractions/two-factor import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result"; import { WebAuthnLoginTokenRequest } from "@bitwarden/common/auth/models/request/identity-token/webauthn-login-token.request"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; @@ -48,6 +49,7 @@ export class WebAuthnLoginStrategy extends LoginStrategy { logService: LogService, stateService: StateService, twoFactorService: TwoFactorService, + billingAccountProfileStateService: BillingAccountProfileStateService, ) { super( cryptoService, @@ -59,6 +61,7 @@ export class WebAuthnLoginStrategy extends LoginStrategy { logService, stateService, twoFactorService, + billingAccountProfileStateService, ); this.cache = new BehaviorSubject(data); diff --git a/libs/auth/src/common/models/domain/login-credentials.ts b/libs/auth/src/common/models/domain/login-credentials.ts index a56d8e00970..bfe01aea20f 100644 --- a/libs/auth/src/common/models/domain/login-credentials.ts +++ b/libs/auth/src/common/models/domain/login-credentials.ts @@ -25,6 +25,11 @@ export class SsoLoginCredentials { public codeVerifier: string, public redirectUrl: string, public orgId: string, + /** + * Optional email address for SSO login. + * Used for looking up 2FA token on clients that support remembering 2FA token. + */ + public email?: string, public twoFactor?: TokenTwoFactorRequest, ) {} } diff --git a/libs/auth/src/common/services/login-strategies/login-strategy.service.spec.ts b/libs/auth/src/common/services/login-strategies/login-strategy.service.spec.ts index 21509eb83c6..3d4c1b7b7d5 100644 --- a/libs/auth/src/common/services/login-strategies/login-strategy.service.spec.ts +++ b/libs/auth/src/common/services/login-strategies/login-strategy.service.spec.ts @@ -11,6 +11,7 @@ import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result"; import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/identity-token/token-two-factor.request"; import { IdentityTokenResponse } from "@bitwarden/common/auth/models/response/identity-token.response"; import { IdentityTwoFactorResponse } from "@bitwarden/common/auth/models/response/identity-two-factor.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; @@ -50,6 +51,7 @@ describe("LoginStrategyService", () => { let policyService: MockProxy; let deviceTrustCryptoService: MockProxy; let authRequestService: MockProxy; + let billingAccountProfileStateService: MockProxy; let stateProvider: FakeGlobalStateProvider; let loginStrategyCacheExpirationState: FakeGlobalState; @@ -72,6 +74,7 @@ describe("LoginStrategyService", () => { policyService = mock(); deviceTrustCryptoService = mock(); authRequestService = mock(); + billingAccountProfileStateService = mock(); stateProvider = new FakeGlobalStateProvider(); sut = new LoginStrategyService( @@ -93,6 +96,7 @@ describe("LoginStrategyService", () => { deviceTrustCryptoService, authRequestService, stateProvider, + billingAccountProfileStateService, ); loginStrategyCacheExpirationState = stateProvider.getFake(CACHE_EXPIRATION_KEY); @@ -114,7 +118,7 @@ describe("LoginStrategyService", () => { token_type: "Bearer", }), ); - tokenService.decodeToken.calledWith("ACCESS_TOKEN").mockResolvedValue({ + tokenService.decodeAccessToken.calledWith("ACCESS_TOKEN").mockResolvedValue({ sub: "USER_ID", name: "NAME", email: "EMAIL", @@ -161,7 +165,7 @@ describe("LoginStrategyService", () => { }), ); - tokenService.decodeToken.calledWith("ACCESS_TOKEN").mockResolvedValue({ + tokenService.decodeAccessToken.calledWith("ACCESS_TOKEN").mockResolvedValue({ sub: "USER_ID", name: "NAME", email: "EMAIL", diff --git a/libs/auth/src/common/services/login-strategies/login-strategy.service.ts b/libs/auth/src/common/services/login-strategies/login-strategy.service.ts index 7ef8432aa5f..5c0e4140446 100644 --- a/libs/auth/src/common/services/login-strategies/login-strategy.service.ts +++ b/libs/auth/src/common/services/login-strategies/login-strategy.service.ts @@ -20,6 +20,7 @@ import { KdfConfig } from "@bitwarden/common/auth/models/domain/kdf-config"; import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/identity-token/token-two-factor.request"; import { PasswordlessAuthRequest } from "@bitwarden/common/auth/models/request/passwordless-auth.request"; import { AuthRequestResponse } from "@bitwarden/common/auth/models/response/auth-request.response"; +import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { PreloginRequest } from "@bitwarden/common/models/request/prelogin.request"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { AuthRequestPushNotification } from "@bitwarden/common/models/response/notification.response"; @@ -101,6 +102,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { protected deviceTrustCryptoService: DeviceTrustCryptoServiceAbstraction, protected authRequestService: AuthRequestServiceAbstraction, protected stateProvider: GlobalStateProvider, + protected billingAccountProfileStateService: BillingAccountProfileStateService, ) { this.currentAuthnTypeState = this.stateProvider.get(CURRENT_LOGIN_STRATEGY_KEY); this.loginStrategyCacheState = this.stateProvider.get(CACHE_KEY); @@ -355,6 +357,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.passwordStrengthService, this.policyService, this, + this.billingAccountProfileStateService, ); case AuthenticationType.Sso: return new SsoLoginStrategy( @@ -372,6 +375,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.deviceTrustCryptoService, this.authRequestService, this.i18nService, + this.billingAccountProfileStateService, ); case AuthenticationType.UserApiKey: return new UserApiLoginStrategy( @@ -387,6 +391,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.twoFactorService, this.environmentService, this.keyConnectorService, + this.billingAccountProfileStateService, ); case AuthenticationType.AuthRequest: return new AuthRequestLoginStrategy( @@ -401,6 +406,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.stateService, this.twoFactorService, this.deviceTrustCryptoService, + this.billingAccountProfileStateService, ); case AuthenticationType.WebAuthn: return new WebAuthnLoginStrategy( @@ -414,6 +420,7 @@ export class LoginStrategyService implements LoginStrategyServiceAbstraction { this.logService, this.stateService, this.twoFactorService, + this.billingAccountProfileStateService, ); } }), diff --git a/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.spec.ts b/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.spec.ts new file mode 100644 index 00000000000..84778b82f88 --- /dev/null +++ b/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.spec.ts @@ -0,0 +1,90 @@ +import { DecodedAccessToken } from "@bitwarden/common/auth/services/token.service"; +import { Utils } from "@bitwarden/common/platform/misc/utils"; + +import { decodeJwtTokenToJson } from "./decode-jwt-token-to-json.utility"; + +describe("decodeJwtTokenToJson", () => { + const accessTokenJwt = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0IiwibmJmIjoxNzA5MzI0MTExLCJpYXQiOjE3MDkzMjQxMTEsImV4cCI6MTcwOTMyNzcxMSwic2NvcGUiOlsiYXBpIiwib2ZmbGluZV9hY2Nlc3MiXSwiYW1yIjpbIkFwcGxpY2F0aW9uIl0sImNsaWVudF9pZCI6IndlYiIsInN1YiI6ImVjZTcwYTEzLTcyMTYtNDNjNC05OTc3LWIxMDMwMTQ2ZTFlNyIsImF1dGhfdGltZSI6MTcwOTMyNDEwNCwiaWRwIjoiYml0d2FyZGVuIiwicHJlbWl1bSI6ZmFsc2UsImVtYWlsIjoiZXhhbXBsZUBiaXR3YXJkZW4uY29tIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJzc3RhbXAiOiJHWTdKQU82NENLS1RLQkI2WkVBVVlMMldPUVU3QVNUMiIsIm5hbWUiOiJUZXN0IFVzZXIiLCJvcmdvd25lciI6WyI5MmI0OTkwOC1iNTE0LTQ1YTgtYmFkYi1iMTAzMDE0OGZlNTMiLCIzOGVkZTMyMi1iNGI0LTRiZDgtOWUwOS1iMTA3MDExMmRjMTEiLCJiMmQwNzAyOC1hNTgzLTRjM2UtOGQ2MC1iMTA3MDExOThjMjkiLCJiZjkzNGJhMi0wZmQ0LTQ5ZjItYTk1ZS1iMTA3MDExZmM5ZTYiLCJjMGI3Zjc1ZC0wMTVmLTQyYzktYjNhNi1iMTA4MDE3NjA3Y2EiXSwiZGV2aWNlIjoiNGI4NzIzNjctMGRhNi00MWEwLWFkY2ItNzdmMmZlZWZjNGY0IiwianRpIjoiNzUxNjFCRTQxMzFGRjVBMkRFNTExQjhDNEUyRkY4OUEifQ.n7roP8sSbfwcYdvRxZNZds27IK32TW6anorE6BORx_Q"; + + const accessTokenDecoded: DecodedAccessToken = { + iss: "http://localhost", + nbf: 1709324111, + iat: 1709324111, + exp: 1709327711, + scope: ["api", "offline_access"], + amr: ["Application"], + client_id: "web", + sub: "ece70a13-7216-43c4-9977-b1030146e1e7", // user id + auth_time: 1709324104, + idp: "bitwarden", + premium: false, + email: "example@bitwarden.com", + email_verified: false, + sstamp: "GY7JAO64CKKTKBB6ZEAUYL2WOQU7AST2", + name: "Test User", + orgowner: [ + "92b49908-b514-45a8-badb-b1030148fe53", + "38ede322-b4b4-4bd8-9e09-b1070112dc11", + "b2d07028-a583-4c3e-8d60-b10701198c29", + "bf934ba2-0fd4-49f2-a95e-b107011fc9e6", + "c0b7f75d-015f-42c9-b3a6-b108017607ca", + ], + device: "4b872367-0da6-41a0-adcb-77f2feefc4f4", + jti: "75161BE4131FF5A2DE511B8C4E2FF89A", + }; + + it("should decode the JWT token", () => { + // Act + const result = decodeJwtTokenToJson(accessTokenJwt); + + // Assert + expect(result).toEqual(accessTokenDecoded); + }); + + it("should throw an error if the JWT token is null", () => { + // Act && Assert + expect(() => decodeJwtTokenToJson(null)).toThrow("JWT token not found"); + }); + + it("should throw an error if the JWT token is missing 3 parts", () => { + // Act && Assert + expect(() => decodeJwtTokenToJson("invalidToken")).toThrow("JWT must have 3 parts"); + }); + + it("should throw an error if the JWT token payload contains invalid JSON", () => { + // Arrange: Create a token with a valid format but with a payload that's valid Base64 but not valid JSON + const header = btoa(JSON.stringify({ alg: "none" })); + // Create a Base64-encoded string which fails to parse as JSON + const payload = btoa("invalid JSON"); + const signature = "signature"; + const malformedToken = `${header}.${payload}.${signature}`; + + // Act & Assert + expect(() => decodeJwtTokenToJson(malformedToken)).toThrow( + "Cannot parse the token's payload into JSON", + ); + }); + + it("should throw an error if the JWT token cannot be decoded", () => { + // Arrange: Create a token with a valid format + const header = btoa(JSON.stringify({ alg: "none" })); + const payload = "invalidPayloadBecauseWeWillMockTheFailure"; + const signature = "signature"; + const malformedToken = `${header}.${payload}.${signature}`; + + // Mock Utils.fromUrlB64ToUtf8 to throw an error for this specific payload + jest.spyOn(Utils, "fromUrlB64ToUtf8").mockImplementation((input) => { + if (input === payload) { + throw new Error("Mock error"); + } + return input; // Default behavior for other inputs + }); + + // Act & Assert + expect(() => decodeJwtTokenToJson(malformedToken)).toThrow("Cannot decode the token"); + + // Restore original function so other tests are not affected + jest.restoreAllMocks(); + }); +}); diff --git a/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.ts b/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.ts new file mode 100644 index 00000000000..717e80b110d --- /dev/null +++ b/libs/auth/src/common/utilities/decode-jwt-token-to-json.utility.ts @@ -0,0 +1,32 @@ +import { Utils } from "@bitwarden/common/platform/misc/utils"; + +export function decodeJwtTokenToJson(jwtToken: string): any { + if (jwtToken == null) { + throw new Error("JWT token not found"); + } + + const parts = jwtToken.split("."); + if (parts.length !== 3) { + throw new Error("JWT must have 3 parts"); + } + + // JWT has 3 parts: header, payload, signature separated by '.' + // So, grab the payload to decode + const encodedPayload = parts[1]; + + let decodedPayloadJSON: string; + try { + // Attempt to decode from URL-safe Base64 to UTF-8 + decodedPayloadJSON = Utils.fromUrlB64ToUtf8(encodedPayload); + } catch (decodingError) { + throw new Error("Cannot decode the token"); + } + + try { + // Attempt to parse the JSON payload + const decodedToken = JSON.parse(decodedPayloadJSON); + return decodedToken; + } catch (jsonError) { + throw new Error("Cannot parse the token's payload into JSON"); + } +} diff --git a/libs/auth/src/common/utilities/index.ts b/libs/auth/src/common/utilities/index.ts new file mode 100644 index 00000000000..0309e37f394 --- /dev/null +++ b/libs/auth/src/common/utilities/index.ts @@ -0,0 +1 @@ +export * from "./decode-jwt-token-to-json.utility"; diff --git a/libs/common/spec/fake-state-provider.ts b/libs/common/spec/fake-state-provider.ts index 78140ba4af9..2078fe3abde 100644 --- a/libs/common/spec/fake-state-provider.ts +++ b/libs/common/spec/fake-state-provider.ts @@ -1,5 +1,5 @@ import { mock } from "jest-mock-extended"; -import { Observable, map } from "rxjs"; +import { Observable, map, of, switchMap, take } from "rxjs"; import { GlobalState, @@ -171,7 +171,30 @@ export class FakeStateProvider implements StateProvider { if (userId) { return this.getUser(userId, keyDefinition).state$; } - return this.getActive(keyDefinition).state$; + + return this.getActive(keyDefinition).state$; + } + + getUserStateOrDefault$( + keyDefinition: KeyDefinition | UserKeyDefinition, + config: { userId: UserId | undefined; defaultValue?: T }, + ): Observable { + const { userId, defaultValue = null } = config; + if (isUserKeyDefinition(keyDefinition)) { + this.mock.getUserStateOrDefault$(keyDefinition, config); + } else { + this.mock.getUserStateOrDefault$(keyDefinition, config); + } + if (userId) { + return this.getUser(userId, keyDefinition).state$; + } + + return this.activeUserId$.pipe( + take(1), + switchMap((userId) => + userId != null ? this.getUser(userId, keyDefinition).state$ : of(defaultValue), + ), + ); } async setUserState( diff --git a/libs/common/src/abstractions/event/event-upload.service.ts b/libs/common/src/abstractions/event/event-upload.service.ts index b68f6f718d4..5b7a98629a6 100644 --- a/libs/common/src/abstractions/event/event-upload.service.ts +++ b/libs/common/src/abstractions/event/event-upload.service.ts @@ -1,3 +1,5 @@ +import { UserId } from "../../types/guid"; + export abstract class EventUploadService { - uploadEvents: (userId?: string) => Promise; + uploadEvents: (userId?: UserId) => Promise; } diff --git a/libs/common/src/abstractions/settings.service.ts b/libs/common/src/abstractions/settings.service.ts deleted file mode 100644 index e9d8c8b683a..00000000000 --- a/libs/common/src/abstractions/settings.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Observable } from "rxjs"; - -export abstract class SettingsService { - disableFavicon$: Observable; - - setDisableFavicon: (value: boolean) => Promise; - getDisableFavicon: () => boolean; -} diff --git a/libs/common/src/admin-console/abstractions/organization/organization.service.abstraction.ts b/libs/common/src/admin-console/abstractions/organization/organization.service.abstraction.ts index 1ef0430f958..9cc4bba0eb0 100644 --- a/libs/common/src/admin-console/abstractions/organization/organization.service.abstraction.ts +++ b/libs/common/src/admin-console/abstractions/organization/organization.service.abstraction.ts @@ -2,6 +2,7 @@ import { map, Observable } from "rxjs"; import { I18nService } from "../../../platform/abstractions/i18n.service"; import { Utils } from "../../../platform/misc/utils"; +import { UserId } from "../../../types/guid"; import { OrganizationData } from "../../models/data/organization.data"; import { Organization } from "../../models/domain/organization"; @@ -86,34 +87,67 @@ export function canAccessImport(i18nService: I18nService) { /** * Returns `true` if a user is a member of an organization (rather than only being a ProviderUser) - * @deprecated Use organizationService.memberOrganizations$ instead + * @deprecated Use organizationService.organizations$ with a filter instead */ export function isMember(org: Organization): boolean { return org.isMember; } +/** + * Publishes an observable stream of organizations. This service is meant to + * be used widely across Bitwarden as the primary way of fetching organizations. + * Risky operations like updates are isolated to the + * internal extension `InternalOrganizationServiceAbstraction`. + */ export abstract class OrganizationService { + /** + * Publishes state for all organizations under the active user. + * @returns An observable list of organizations + */ organizations$: Observable; - /** - * Organizations that the user is a member of (excludes organizations that they only have access to via a provider) - */ + // @todo Clean these up. Continuing to expand them is not recommended. + // @see https://bitwarden.atlassian.net/browse/AC-2252 memberOrganizations$: Observable; - - get$: (id: string) => Observable; - get: (id: string) => Organization; - getByIdentifier: (identifier: string) => Organization; - getAll: (userId?: string) => Promise; /** - * @deprecated For the CLI only - * @param id id of the organization + * @deprecated This is currently only used in the CLI, and should not be + * used in any new calls. Use get$ instead for the time being, and we'll be + * removing this method soon. See Jira for details: + * https://bitwarden.atlassian.net/browse/AC-2252. */ getFromState: (id: string) => Promise; canManageSponsorships: () => Promise; - hasOrganizations: () => boolean; + hasOrganizations: () => Promise; + get$: (id: string) => Observable; + get: (id: string) => Promise; + getAll: (userId?: string) => Promise; + // } +/** + * Big scary buttons that **update** organization state. These should only be + * called from within admin-console scoped code. Extends the base + * `OrganizationService` for easy access to `get` calls. + * @internal + */ export abstract class InternalOrganizationServiceAbstraction extends OrganizationService { - replace: (organizations: { [id: string]: OrganizationData }) => Promise; - upsert: (OrganizationData: OrganizationData | OrganizationData[]) => Promise; + /** + * Replaces state for the provided organization, or creates it if not found. + * @param organization The organization state being saved. + * @param userId The userId to replace state for. Defaults to the active + * user. + */ + upsert: (OrganizationData: OrganizationData) => Promise; + + /** + * Replaces state for the entire registered organization list for the active user. + * You probably don't want this unless you're calling from a full sync + * operation or a logout. See `upsert` for creating & updating a single + * organization in the state. + * @param organizations A complete list of all organization state for the active + * user. + * @param userId The userId to replace state for. Defaults to the active + * user. + */ + replace: (organizations: { [id: string]: OrganizationData }, userId?: UserId) => Promise; } diff --git a/libs/common/src/admin-console/abstractions/policy/policy.service.abstraction.ts b/libs/common/src/admin-console/abstractions/policy/policy.service.abstraction.ts index 1a85fd79fce..fb805f94cd9 100644 --- a/libs/common/src/admin-console/abstractions/policy/policy.service.abstraction.ts +++ b/libs/common/src/admin-console/abstractions/policy/policy.service.abstraction.ts @@ -1,13 +1,11 @@ import { Observable } from "rxjs"; -import { ListResponse } from "../../../models/response/list.response"; import { UserId } from "../../../types/guid"; import { PolicyType } from "../../enums"; import { PolicyData } from "../../models/data/policy.data"; import { MasterPasswordPolicyOptions } from "../../models/domain/master-password-policy-options"; import { Policy } from "../../models/domain/policy"; import { ResetPasswordPolicyOptions } from "../../models/domain/reset-password-policy-options"; -import { PolicyResponse } from "../../models/response/policy.response"; export abstract class PolicyService { /** @@ -75,18 +73,6 @@ export abstract class PolicyService { policies: Policy[], orgId: string, ) => [ResetPasswordPolicyOptions, boolean]; - - // Helpers - - /** - * Instantiates {@link Policy} objects from {@link PolicyResponse} objects. - */ - mapPolicyFromResponse: (policyResponse: PolicyResponse) => Policy; - - /** - * Instantiates {@link Policy} objects from {@link ListResponse} objects. - */ - mapPoliciesFromToken: (policiesResponse: ListResponse) => Policy[]; } export abstract class InternalPolicyService extends PolicyService { diff --git a/libs/common/src/admin-console/models/domain/organization.ts b/libs/common/src/admin-console/models/domain/organization.ts index 8eba83ba3ef..18b762207a1 100644 --- a/libs/common/src/admin-console/models/domain/organization.ts +++ b/libs/common/src/admin-console/models/domain/organization.ts @@ -320,6 +320,10 @@ export class Organization { return !this.useTotp; } + get canManageSponsorships() { + return this.familySponsorshipAvailable || this.familySponsorshipFriendlyName !== null; + } + static fromJSON(json: Jsonify) { if (json == null) { return null; diff --git a/libs/common/src/admin-console/models/domain/policy.ts b/libs/common/src/admin-console/models/domain/policy.ts index 65af09b5888..c2f9c9c8dfc 100644 --- a/libs/common/src/admin-console/models/domain/policy.ts +++ b/libs/common/src/admin-console/models/domain/policy.ts @@ -1,7 +1,9 @@ +import { ListResponse } from "../../../models/response/list.response"; import Domain from "../../../platform/models/domain/domain-base"; import { PolicyId } from "../../../types/guid"; import { PolicyType } from "../../enums"; import { PolicyData } from "../data/policy.data"; +import { PolicyResponse } from "../response/policy.response"; export class Policy extends Domain { id: PolicyId; @@ -27,4 +29,12 @@ export class Policy extends Domain { this.data = obj.data; this.enabled = obj.enabled; } + + static fromResponse(response: PolicyResponse): Policy { + return new Policy(new PolicyData(response)); + } + + static fromListResponse(response: ListResponse): Policy[] | undefined { + return response.data?.map((d) => Policy.fromResponse(d)) ?? undefined; + } } diff --git a/libs/common/src/admin-console/services/organization/organization.service.spec.ts b/libs/common/src/admin-console/services/organization/organization.service.spec.ts index a4b7e958886..908f4b8e28b 100644 --- a/libs/common/src/admin-console/services/organization/organization.service.spec.ts +++ b/libs/common/src/admin-console/services/organization/organization.service.spec.ts @@ -1,114 +1,142 @@ -import { MockProxy, mock, any, mockClear } from "jest-mock-extended"; -import { BehaviorSubject, firstValueFrom } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../../spec"; import { FakeActiveUserState } from "../../../../spec/fake-state"; -import { StateService } from "../../../platform/abstractions/state.service"; import { Utils } from "../../../platform/misc/utils"; -import { UserId } from "../../../types/guid"; +import { OrganizationId, UserId } from "../../../types/guid"; import { OrganizationData } from "../../models/data/organization.data"; +import { Organization } from "../../models/domain/organization"; import { OrganizationService, ORGANIZATIONS } from "./organization.service"; -describe("Organization Service", () => { +describe("OrganizationService", () => { let organizationService: OrganizationService; - let stateService: MockProxy; - let activeAccount: BehaviorSubject; - let activeAccountUnlocked: BehaviorSubject; + const fakeUserId = Utils.newGuid() as UserId; + let fakeAccountService: FakeAccountService; + let fakeStateProvider: FakeStateProvider; + let fakeActiveUserState: FakeActiveUserState>; - const mockUserId = Utils.newGuid() as UserId; - let accountService: FakeAccountService; - let stateProvider: FakeStateProvider; - let activeUserOrganizationsState: FakeActiveUserState>; - - const resetStateService = async ( - customizeStateService: (stateService: MockProxy) => void, - ) => { - mockClear(stateService); - stateService = mock(); - stateService.activeAccount$ = activeAccount; - stateService.activeAccountUnlocked$ = activeAccountUnlocked; - customizeStateService(stateService); - organizationService = new OrganizationService(stateService, stateProvider); - await new Promise((r) => setTimeout(r, 50)); - }; - - function prepareStateProvider(): void { - accountService = mockAccountServiceWith(mockUserId); - stateProvider = new FakeStateProvider(accountService); + /** + * It is easier to read arrays than records in code, but we store a record + * in state. This helper methods lets us build organization arrays in tests + * and easily map them to records before storing them in state. + */ + function arrayToRecord(input: OrganizationData[]): Record { + if (input == null) { + return undefined; + } + return Object.fromEntries(input?.map((i) => [i.id, i])); } - function seedTestData(): void { - activeUserOrganizationsState = stateProvider.activeUser.getFake(ORGANIZATIONS); - activeUserOrganizationsState.nextState({ "1": organizationData("1", "Test Org") }); + /** + * There are a few assertions in this spec that check for array equality + * but want to ignore a specific index that _should_ be different. This + * function takes two arrays, and an index. It checks for equality of the + * arrays, but splices out the specified index from both arrays first. + */ + function expectIsEqualExceptForIndex(x: any[], y: any[], indexToExclude: number) { + // Clone the arrays to avoid modifying the reference values + const a = [...x]; + const b = [...y]; + delete a[indexToExclude]; + delete b[indexToExclude]; + expect(a).toEqual(b); } - beforeEach(() => { - activeAccount = new BehaviorSubject("123"); - activeAccountUnlocked = new BehaviorSubject(true); + /** + * Builds a simple mock `OrganizationData[]` array that can be used in tests + * to populate state. + * @param count The number of organizations to populate the list with. The + * function returns undefined if this is less than 1. The default value is 1. + * @param suffix A string to append to data fields on each organization. + * This defaults to the index of the organization in the list. + * @returns an `OrganizationData[]` array that can be used to populate + * stateProvider. + */ + function buildMockOrganizations(count = 1, suffix?: string): OrganizationData[] { + if (count < 1) { + return undefined; + } - stateService = mock(); - stateService.activeAccount$ = activeAccount; - stateService.activeAccountUnlocked$ = activeAccountUnlocked; + function buildMockOrganization(id: OrganizationId, name: string, identifier: string) { + const data = new OrganizationData({} as any, {} as any); + data.id = id; + data.name = name; + data.identifier = identifier; - stateService.getOrganizations.calledWith(any()).mockResolvedValue({ - "1": organizationData("1", "Test Org"), - }); + return data; + } - prepareStateProvider(); + const mockOrganizations = []; + for (let i = 0; i < count; i++) { + const s = suffix ? suffix + i.toString() : i.toString(); + mockOrganizations.push( + buildMockOrganization(("org" + s) as OrganizationId, "org" + s, "orgIdentifier" + s), + ); + } - organizationService = new OrganizationService(stateService, stateProvider); + return mockOrganizations; + } - seedTestData(); - }); + /** + * `OrganizationService` deals with multiple accounts at times. This helper + * function can be used to add a new non-active account to the test data. + * This function is **not** needed to handle creation of the first account, + * as that is handled by the `FakeAccountService` in `mockAccountServiceWith()` + * @returns The `UserId` of the newly created state account and the mock data + * created for them as an `Organization[]`. + */ + async function addNonActiveAccountToStateProvider(): Promise<[UserId, OrganizationData[]]> { + const nonActiveUserId = Utils.newGuid() as UserId; - afterEach(() => { - activeAccount.complete(); - activeAccountUnlocked.complete(); + const mockOrganizations = buildMockOrganizations(10); + const fakeNonActiveUserState = fakeStateProvider.singleUser.getFake( + nonActiveUserId, + ORGANIZATIONS, + ); + fakeNonActiveUserState.nextState(arrayToRecord(mockOrganizations)); + + return [nonActiveUserId, mockOrganizations]; + } + + beforeEach(async () => { + fakeAccountService = mockAccountServiceWith(fakeUserId); + fakeStateProvider = new FakeStateProvider(fakeAccountService); + fakeActiveUserState = fakeStateProvider.activeUser.getFake(ORGANIZATIONS); + organizationService = new OrganizationService(fakeStateProvider); }); it("getAll", async () => { + const mockData: OrganizationData[] = buildMockOrganizations(1); + fakeActiveUserState.nextState(arrayToRecord(mockData)); const orgs = await organizationService.getAll(); expect(orgs).toHaveLength(1); const org = orgs[0]; - expect(org).toEqual({ - id: "1", - name: "Test Org", - identifier: "test", - }); + expect(org).toEqual(new Organization(mockData[0])); }); describe("canManageSponsorships", () => { it("can because one is available", async () => { - await resetStateService((stateService) => { - stateService.getOrganizations.mockResolvedValue({ - "1": { ...organizationData("1", "Org"), familySponsorshipAvailable: true }, - }); - }); - + const mockData: OrganizationData[] = buildMockOrganizations(1); + mockData[0].familySponsorshipAvailable = true; + fakeActiveUserState.nextState(arrayToRecord(mockData)); const result = await organizationService.canManageSponsorships(); expect(result).toBe(true); }); it("can because one is used", async () => { - await resetStateService((stateService) => { - stateService.getOrganizations.mockResolvedValue({ - "1": { ...organizationData("1", "Test Org"), familySponsorshipFriendlyName: "Something" }, - }); - }); - + const mockData: OrganizationData[] = buildMockOrganizations(1); + mockData[0].familySponsorshipFriendlyName = "Something"; + fakeActiveUserState.nextState(arrayToRecord(mockData)); const result = await organizationService.canManageSponsorships(); expect(result).toBe(true); }); it("can not because one isn't available or taken", async () => { - await resetStateService((stateService) => { - stateService.getOrganizations.mockResolvedValue({ - "1": { ...organizationData("1", "Org"), familySponsorshipFriendlyName: null }, - }); - }); - + const mockData: OrganizationData[] = buildMockOrganizations(1); + mockData[0].familySponsorshipFriendlyName = null; + fakeActiveUserState.nextState(arrayToRecord(mockData)); const result = await organizationService.canManageSponsorships(); expect(result).toBe(false); }); @@ -116,81 +144,181 @@ describe("Organization Service", () => { describe("get", () => { it("exists", async () => { - const result = organizationService.get("1"); - - expect(result).toEqual({ - id: "1", - name: "Test Org", - identifier: "test", - }); + const mockData = buildMockOrganizations(1); + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const result = await organizationService.get(mockData[0].id); + expect(result).toEqual(new Organization(mockData[0])); }); it("does not exist", async () => { - const result = organizationService.get("2"); - + const result = await organizationService.get("this-org-does-not-exist"); expect(result).toBe(undefined); }); }); - it("upsert", async () => { - await organizationService.upsert(organizationData("2", "Test 2")); + describe("organizations$", () => { + describe("null checking behavior", () => { + it("publishes an empty array if organizations in state = undefined", async () => { + const mockData: OrganizationData[] = undefined; + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual([]); + }); - expect(await firstValueFrom(organizationService.organizations$)).toEqual([ - { - id: "1", - name: "Test Org", - identifier: "test", - }, - { - id: "2", - name: "Test 2", - identifier: "test", - }, - ]); - }); + it("publishes an empty array if organizations in state = null", async () => { + const mockData: OrganizationData[] = null; + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual([]); + }); - describe("getByIdentifier", () => { - it("exists", async () => { - const result = organizationService.getByIdentifier("test"); - - expect(result).toEqual({ - id: "1", - name: "Test Org", - identifier: "test", + it("publishes an empty array if organizations in state = []", async () => { + const mockData: OrganizationData[] = []; + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual([]); }); }); - it("does not exist", async () => { - const result = organizationService.getByIdentifier("blah"); + describe("parameter handling & returns", () => { + it("publishes all organizations for the active user by default", async () => { + const mockData = buildMockOrganizations(10); + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual(mockData); + }); - expect(result).toBeUndefined(); + it("can be used to publish the organizations of a non active user if requested", async () => { + const activeUserMockData = buildMockOrganizations(10, "activeUserState"); + fakeActiveUserState.nextState(arrayToRecord(activeUserMockData)); + + const [nonActiveUserId, nonActiveUserMockOrganizations] = + await addNonActiveAccountToStateProvider(); + // This can be updated to use + // `firstValueFrom(organizations$(nonActiveUserId)` once all the + // promise based methods are removed from `OrganizationService` and the + // main observable is refactored to accept a userId + const result = await organizationService.getAll(nonActiveUserId); + + expect(result).toEqual(nonActiveUserMockOrganizations); + expect(result).not.toEqual(await firstValueFrom(organizationService.organizations$)); + }); }); }); - describe("delete", () => { - it("exists", async () => { - await organizationService.delete("1"); - - expect(stateService.getOrganizations).toHaveBeenCalledTimes(2); - - expect(stateService.setOrganizations).toHaveBeenCalledTimes(1); + describe("upsert()", () => { + it("can create the organization list if necassary", async () => { + // Notice that no default state is provided in this test, so the list in + // `stateProvider` will be null when the `upsert` method is called. + const mockData = buildMockOrganizations(); + await organizationService.upsert(mockData[0]); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual(mockData.map((x) => new Organization(x))); }); - it("does not exist", async () => { - // FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. - // eslint-disable-next-line @typescript-eslint/no-floating-promises - organizationService.delete("1"); + it("updates an organization that already exists in state, defaulting to the active user", async () => { + const mockData = buildMockOrganizations(10); + fakeActiveUserState.nextState(arrayToRecord(mockData)); + const indexToUpdate = 5; + const anUpdatedOrganization = { + ...buildMockOrganizations(1, "UPDATED").pop(), + id: mockData[indexToUpdate].id, + }; + await organizationService.upsert(anUpdatedOrganization); + const result = await firstValueFrom(organizationService.organizations$); + expect(result[indexToUpdate]).not.toEqual(new Organization(mockData[indexToUpdate])); + expect(result[indexToUpdate].id).toEqual(new Organization(mockData[indexToUpdate]).id); + expectIsEqualExceptForIndex( + result, + mockData.map((x) => new Organization(x)), + indexToUpdate, + ); + }); - expect(stateService.getOrganizations).toHaveBeenCalledTimes(2); + it("can also update an organization in state for a non-active user, if requested", async () => { + const activeUserMockData = buildMockOrganizations(10, "activeUserOrganizations"); + fakeActiveUserState.nextState(arrayToRecord(activeUserMockData)); + + const [nonActiveUserId, nonActiveUserMockOrganizations] = + await addNonActiveAccountToStateProvider(); + const indexToUpdate = 5; + const anUpdatedOrganization = { + ...buildMockOrganizations(1, "UPDATED").pop(), + id: nonActiveUserMockOrganizations[indexToUpdate].id, + }; + + await organizationService.upsert(anUpdatedOrganization, nonActiveUserId); + // This can be updated to use + // `firstValueFrom(organizations$(nonActiveUserId)` once all the + // promise based methods are removed from `OrganizationService` and the + // main observable is refactored to accept a userId + const result = await organizationService.getAll(nonActiveUserId); + + expect(result[indexToUpdate]).not.toEqual( + new Organization(nonActiveUserMockOrganizations[indexToUpdate]), + ); + expect(result[indexToUpdate].id).toEqual( + new Organization(nonActiveUserMockOrganizations[indexToUpdate]).id, + ); + expectIsEqualExceptForIndex( + result, + nonActiveUserMockOrganizations.map((x) => new Organization(x)), + indexToUpdate, + ); + + // Just to be safe, lets make sure the active user didn't get updated + // at all + const activeUserState = await firstValueFrom(organizationService.organizations$); + expect(activeUserState).toEqual(activeUserMockData.map((x) => new Organization(x))); + expect(activeUserState).not.toEqual(result); }); }); - function organizationData(id: string, name: string) { - const data = new OrganizationData({} as any, {} as any); - data.id = id; - data.name = name; - data.identifier = "test"; + describe("replace()", () => { + it("replaces the entire organization list in state, defaulting to the active user", async () => { + const originalData = buildMockOrganizations(10); + fakeActiveUserState.nextState(arrayToRecord(originalData)); - return data; - } + const newData = buildMockOrganizations(10, "newData"); + await organizationService.replace(arrayToRecord(newData)); + + const result = await firstValueFrom(organizationService.organizations$); + + expect(result).toEqual(newData); + expect(result).not.toEqual(originalData); + }); + + // This is more or less a test for logouts + it("can replace state with null", async () => { + const originalData = buildMockOrganizations(2); + fakeActiveUserState.nextState(arrayToRecord(originalData)); + await organizationService.replace(null); + const result = await firstValueFrom(organizationService.organizations$); + expect(result).toEqual([]); + expect(result).not.toEqual(originalData); + }); + + it("can also replace state for a non-active user, if requested", async () => { + const activeUserMockData = buildMockOrganizations(10, "activeUserOrganizations"); + fakeActiveUserState.nextState(arrayToRecord(activeUserMockData)); + + const [nonActiveUserId, originalOrganizations] = await addNonActiveAccountToStateProvider(); + const newData = buildMockOrganizations(10, "newData"); + + await organizationService.replace(arrayToRecord(newData), nonActiveUserId); + // This can be updated to use + // `firstValueFrom(organizations$(nonActiveUserId)` once all the + // promise based methods are removed from `OrganizationService` and the + // main observable is refactored to accept a userId + const result = await organizationService.getAll(nonActiveUserId); + expect(result).toEqual(newData); + expect(result).not.toEqual(originalOrganizations); + + // Just to be safe, lets make sure the active user didn't get updated + // at all + const activeUserState = await firstValueFrom(organizationService.organizations$); + expect(activeUserState).toEqual(activeUserMockData.map((x) => new Organization(x))); + expect(activeUserState).not.toEqual(result); + }); + }); }); diff --git a/libs/common/src/admin-console/services/organization/organization.service.ts b/libs/common/src/admin-console/services/organization/organization.service.ts index 2c2c8b4e36a..3c651f4660e 100644 --- a/libs/common/src/admin-console/services/organization/organization.service.ts +++ b/libs/common/src/admin-console/services/organization/organization.service.ts @@ -1,111 +1,105 @@ -import { BehaviorSubject, concatMap, map, Observable } from "rxjs"; +import { map, Observable, firstValueFrom } from "rxjs"; import { Jsonify } from "type-fest"; -import { StateService } from "../../../platform/abstractions/state.service"; -import { KeyDefinition, ORGANIZATIONS_DISK, StateProvider } from "../../../platform/state"; -import { - InternalOrganizationServiceAbstraction, - isMember, -} from "../../abstractions/organization/organization.service.abstraction"; +import { ORGANIZATIONS_DISK, StateProvider, UserKeyDefinition } from "../../../platform/state"; +import { UserId } from "../../../types/guid"; +import { InternalOrganizationServiceAbstraction } from "../../abstractions/organization/organization.service.abstraction"; import { OrganizationData } from "../../models/data/organization.data"; import { Organization } from "../../models/domain/organization"; -export const ORGANIZATIONS = KeyDefinition.record( +/** + * The `KeyDefinition` for accessing organization lists in application state. + * @todo Ideally this wouldn't require a `fromJSON()` call, but `OrganizationData` + * has some properties that contain functions. This should probably get + * cleaned up. + */ +export const ORGANIZATIONS = UserKeyDefinition.record( ORGANIZATIONS_DISK, "organizations", { deserializer: (obj: Jsonify) => OrganizationData.fromJSON(obj), + clearOn: ["logout"], }, ); +/** + * Filter out organizations from an observable that __do not__ offer a + * families-for-enterprise sponsorship to members. + * @returns a function that can be used in `Observable` pipes, + * like `organizationService.organizations$` + */ +function mapToExcludeOrganizationsWithoutFamilySponsorshipSupport() { + return map((orgs) => orgs.filter((o) => o.canManageSponsorships)); +} + +/** + * Filter out organizations from an observable that the organization user + * __is not__ a direct member of. This will exclude organizations only + * accessible as a provider. + * @returns a function that can be used in `Observable` pipes, + * like `organizationService.organizations$` + */ +function mapToExcludeProviderOrganizations() { + return map((orgs) => orgs.filter((o) => o.isMember)); +} + +/** + * Map an observable stream of organizations down to a boolean indicating + * if any organizations exist (`orgs.length > 0`). + * @returns a function that can be used in `Observable` pipes, + * like `organizationService.organizations$` + */ +function mapToBooleanHasAnyOrganizations() { + return map((orgs) => orgs.length > 0); +} + +/** + * Map an observable stream of organizations down to a single organization. + * @param `organizationId` The ID of the organization you'd like to subscribe to + * @returns a function that can be used in `Observable` pipes, + * like `organizationService.organizations$` + */ +function mapToSingleOrganization(organizationId: string) { + return map((orgs) => orgs?.find((o) => o.id === organizationId)); +} + export class OrganizationService implements InternalOrganizationServiceAbstraction { - // marked for removal during AC-2009 - protected _organizations = new BehaviorSubject([]); - // marked for removal during AC-2009 - organizations$ = this._organizations.asObservable(); - // marked for removal during AC-2009 - memberOrganizations$ = this.organizations$.pipe(map((orgs) => orgs.filter(isMember))); + organizations$ = this.getOrganizationsFromState$(); + memberOrganizations$ = this.organizations$.pipe(mapToExcludeProviderOrganizations()); - activeUserOrganizations$: Observable; - activeUserMemberOrganizations$: Observable; - - constructor( - private stateService: StateService, - private stateProvider: StateProvider, - ) { - this.activeUserOrganizations$ = this.stateProvider - .getActive(ORGANIZATIONS) - .state$.pipe(map((data) => Object.values(data).map((o) => new Organization(o)))); - - this.activeUserMemberOrganizations$ = this.activeUserOrganizations$.pipe( - map((orgs) => orgs.filter(isMember)), - ); - - this.stateService.activeAccountUnlocked$ - .pipe( - concatMap(async (unlocked) => { - if (!unlocked) { - this._organizations.next([]); - return; - } - - const data = await this.stateService.getOrganizations(); - this.updateObservables(data); - }), - ) - .subscribe(); - } + constructor(private stateProvider: StateProvider) {} get$(id: string): Observable { - return this.organizations$.pipe(map((orgs) => orgs.find((o) => o.id === id))); + return this.organizations$.pipe(mapToSingleOrganization(id)); } async getAll(userId?: string): Promise { - const organizationsMap = await this.stateService.getOrganizations({ userId: userId }); - return Object.values(organizationsMap || {}).map((o) => new Organization(o)); + return await firstValueFrom(this.getOrganizationsFromState$(userId as UserId)); } async canManageSponsorships(): Promise { - const organizations = this._organizations.getValue(); - return organizations.some( - (o) => o.familySponsorshipAvailable || o.familySponsorshipFriendlyName !== null, + return await firstValueFrom( + this.organizations$.pipe( + mapToExcludeOrganizationsWithoutFamilySponsorshipSupport(), + mapToBooleanHasAnyOrganizations(), + ), ); } - hasOrganizations(): boolean { - const organizations = this._organizations.getValue(); - return organizations.length > 0; + async hasOrganizations(): Promise { + return await firstValueFrom(this.organizations$.pipe(mapToBooleanHasAnyOrganizations())); } - async upsert(organization: OrganizationData): Promise { - let organizations = await this.stateService.getOrganizations(); - if (organizations == null) { - organizations = {}; - } - - organizations[organization.id] = organization; - - await this.replace(organizations); + async upsert(organization: OrganizationData, userId?: UserId): Promise { + await this.stateFor(userId).update((existingOrganizations) => { + const organizations = existingOrganizations ?? {}; + organizations[organization.id] = organization; + return organizations; + }); } - async delete(id: string): Promise { - const organizations = await this.stateService.getOrganizations(); - if (organizations == null) { - return; - } - - if (organizations[id] == null) { - return; - } - - delete organizations[id]; - await this.replace(organizations); - } - - get(id: string): Organization { - const organizations = this._organizations.getValue(); - - return organizations.find((organization) => organization.id === id); + async get(id: string): Promise { + return await firstValueFrom(this.organizations$.pipe(mapToSingleOrganization(id))); } /** @@ -113,28 +107,46 @@ export class OrganizationService implements InternalOrganizationServiceAbstracti * @param id id of the organization */ async getFromState(id: string): Promise { - const organizationsMap = await this.stateService.getOrganizations(); - const organization = organizationsMap[id]; - if (organization == null) { - return null; - } - - return new Organization(organization); + return await firstValueFrom(this.organizations$.pipe(mapToSingleOrganization(id))); } - getByIdentifier(identifier: string): Organization { - const organizations = this._organizations.getValue(); - - return organizations.find((organization) => organization.identifier === identifier); + async replace(organizations: { [id: string]: OrganizationData }, userId?: UserId): Promise { + await this.stateFor(userId).update(() => organizations); } - async replace(organizations: { [id: string]: OrganizationData }) { - await this.stateService.setOrganizations(organizations); - this.updateObservables(organizations); + // Ideally this method would be renamed to organizations$() and the + // $organizations observable as it stands would be removed. This will + // require updates to callers, and so this method exists as a temporary + // workaround until we have time & a plan to update callers. + // + // It can be thought of as "organizations$ but with a userId option". + private getOrganizationsFromState$(userId?: UserId): Observable { + return this.stateFor(userId).state$.pipe(this.mapOrganizationRecordToArray()); } - private updateObservables(organizationsMap: { [id: string]: OrganizationData }) { - const organizations = Object.values(organizationsMap || {}).map((o) => new Organization(o)); - this._organizations.next(organizations); + /** + * Accepts a record of `OrganizationData`, which is how we store the + * organization list as a JSON object on disk, to an array of + * `Organization`, which is how the data is published to callers of the + * service. + * @returns a function that can be used to pipe organization data from + * stored state to an exposed object easily consumable by others. + */ + private mapOrganizationRecordToArray() { + return map, Organization[]>((orgs) => + Object.values(orgs ?? {})?.map((o) => new Organization(o)), + ); + } + + /** + * Fetches the organization list from on disk state for the specified user. + * @param userId the user ID to fetch the organization list for. Defaults to + * the currently active user. + * @returns an observable of organization state as it is stored on disk. + */ + private stateFor(userId?: UserId) { + return userId + ? this.stateProvider.getUser(userId, ORGANIZATIONS) + : this.stateProvider.getActive(ORGANIZATIONS); } } diff --git a/libs/common/src/admin-console/services/policy/policy-api.service.ts b/libs/common/src/admin-console/services/policy/policy-api.service.ts index 0eb37f23051..c7f093286e1 100644 --- a/libs/common/src/admin-console/services/policy/policy-api.service.ts +++ b/libs/common/src/admin-console/services/policy/policy-api.service.ts @@ -10,6 +10,7 @@ import { InternalPolicyService } from "../../abstractions/policy/policy.service. import { PolicyType } from "../../enums"; import { PolicyData } from "../../models/data/policy.data"; import { MasterPasswordPolicyOptions } from "../../models/domain/master-password-policy-options"; +import { Policy } from "../../models/domain/policy"; import { PolicyRequest } from "../../models/request/policy.request"; import { PolicyResponse } from "../../models/response/policy.response"; @@ -86,9 +87,7 @@ export class PolicyApiService implements PolicyApiServiceAbstraction { const masterPasswordPolicyResponse = await this.getMasterPasswordPolicyResponseForOrgUser(orgId); - const masterPasswordPolicy = this.policyService.mapPolicyFromResponse( - masterPasswordPolicyResponse, - ); + const masterPasswordPolicy = Policy.fromResponse(masterPasswordPolicyResponse); if (!masterPasswordPolicy) { return null; diff --git a/libs/common/src/admin-console/services/policy/policy.service.spec.ts b/libs/common/src/admin-console/services/policy/policy.service.spec.ts index 672a53d34e5..8fa79f4d1c4 100644 --- a/libs/common/src/admin-console/services/policy/policy.service.spec.ts +++ b/libs/common/src/admin-console/services/policy/policy.service.spec.ts @@ -16,9 +16,7 @@ import { MasterPasswordPolicyOptions } from "../../../admin-console/models/domai import { Organization } from "../../../admin-console/models/domain/organization"; import { Policy } from "../../../admin-console/models/domain/policy"; import { ResetPasswordPolicyOptions } from "../../../admin-console/models/domain/reset-password-policy-options"; -import { PolicyResponse } from "../../../admin-console/models/response/policy.response"; import { POLICIES, PolicyService } from "../../../admin-console/services/policy/policy.service"; -import { ListResponse } from "../../../models/response/list.response"; import { PolicyId, UserId } from "../../../types/guid"; describe("PolicyService", () => { @@ -265,66 +263,6 @@ describe("PolicyService", () => { }); }); - describe("mapPoliciesFromToken", () => { - it("null", async () => { - const result = policyService.mapPoliciesFromToken(null); - - expect(result).toEqual(null); - }); - - it("null data", async () => { - const model = new ListResponse(null, PolicyResponse); - model.data = null; - const result = policyService.mapPoliciesFromToken(model); - - expect(result).toEqual(null); - }); - - it("empty array", async () => { - const model = new ListResponse(null, PolicyResponse); - const result = policyService.mapPoliciesFromToken(model); - - expect(result).toEqual([]); - }); - - it("success", async () => { - const policyResponse: any = { - Data: [ - { - Id: "1", - OrganizationId: "organization-1", - Type: PolicyType.DisablePersonalVaultExport, - Enabled: true, - Data: { requireUpper: true }, - }, - { - Id: "2", - OrganizationId: "organization-2", - Type: PolicyType.DisableSend, - Enabled: false, - Data: { minComplexity: 5, minLength: 20 }, - }, - ], - }; - const model = new ListResponse(policyResponse, PolicyResponse); - const result = policyService.mapPoliciesFromToken(model); - - expect(result).toEqual([ - new Policy( - policyData("1", "organization-1", PolicyType.DisablePersonalVaultExport, true, { - requireUpper: true, - }), - ), - new Policy( - policyData("2", "organization-2", PolicyType.DisableSend, false, { - minComplexity: 5, - minLength: 20, - }), - ), - ]); - }); - }); - describe("get$", () => { it("returns the specified PolicyType", async () => { activeUserState.nextState( diff --git a/libs/common/src/admin-console/services/policy/policy.service.ts b/libs/common/src/admin-console/services/policy/policy.service.ts index 4672d9b810e..d60d2e32938 100644 --- a/libs/common/src/admin-console/services/policy/policy.service.ts +++ b/libs/common/src/admin-console/services/policy/policy.service.ts @@ -1,6 +1,5 @@ import { combineLatest, firstValueFrom, map, Observable, of } from "rxjs"; -import { ListResponse } from "../../../models/response/list.response"; import { KeyDefinition, POLICIES_DISK, StateProvider } from "../../../platform/state"; import { PolicyId, UserId } from "../../../types/guid"; import { OrganizationService } from "../../abstractions/organization/organization.service.abstraction"; @@ -11,7 +10,6 @@ import { MasterPasswordPolicyOptions } from "../../models/domain/master-password import { Organization } from "../../models/domain/organization"; import { Policy } from "../../models/domain/policy"; import { ResetPasswordPolicyOptions } from "../../models/domain/reset-password-policy-options"; -import { PolicyResponse } from "../../models/response/policy.response"; const policyRecordToArray = (policiesMap: { [id: string]: PolicyData }) => Object.values(policiesMap || {}).map((f) => new Policy(f)); @@ -212,19 +210,6 @@ export class PolicyService implements InternalPolicyServiceAbstraction { return [resetPasswordPolicyOptions, policy?.enabled ?? false]; } - mapPolicyFromResponse(policyResponse: PolicyResponse): Policy { - const policyData = new PolicyData(policyResponse); - return new Policy(policyData); - } - - mapPoliciesFromToken(policiesResponse: ListResponse): Policy[] { - if (policiesResponse?.data == null) { - return null; - } - - return policiesResponse.data.map((response) => this.mapPolicyFromResponse(response)); - } - async upsert(policy: PolicyData): Promise { await this.activeUserPolicyState.update((policies) => { policies ??= {}; diff --git a/libs/common/src/admin-console/services/provider.service.spec.ts b/libs/common/src/admin-console/services/provider.service.spec.ts index 4fc02ee8936..fcba9d5023e 100644 --- a/libs/common/src/admin-console/services/provider.service.spec.ts +++ b/libs/common/src/admin-console/services/provider.service.spec.ts @@ -1,5 +1,5 @@ import { FakeAccountService, FakeStateProvider, mockAccountServiceWith } from "../../../spec"; -import { FakeActiveUserState } from "../../../spec/fake-state"; +import { FakeActiveUserState, FakeSingleUserState } from "../../../spec/fake-state"; import { Utils } from "../../platform/misc/utils"; import { UserId } from "../../types/guid"; import { ProviderUserStatusType, ProviderUserType } from "../enums"; @@ -77,11 +77,13 @@ describe("ProviderService", () => { const fakeUserId = Utils.newGuid() as UserId; let fakeAccountService: FakeAccountService; let fakeStateProvider: FakeStateProvider; + let fakeUserState: FakeSingleUserState>; let fakeActiveUserState: FakeActiveUserState>; beforeEach(async () => { fakeAccountService = mockAccountServiceWith(fakeUserId); fakeStateProvider = new FakeStateProvider(fakeAccountService); + fakeUserState = fakeStateProvider.singleUser.getFake(fakeUserId, PROVIDERS); fakeActiveUserState = fakeStateProvider.activeUser.getFake(PROVIDERS); providerService = new ProviderService(fakeStateProvider); }); @@ -89,7 +91,7 @@ describe("ProviderService", () => { describe("getAll()", () => { it("Returns an array of all providers stored in state", async () => { const mockData: ProviderData[] = buildMockProviders(5); - fakeActiveUserState.nextState(arrayToRecord(mockData)); + fakeUserState.nextState(arrayToRecord(mockData)); const providers = await providerService.getAll(); expect(providers).toHaveLength(5); expect(providers).toEqual(mockData.map((x) => new Provider(x))); @@ -97,7 +99,7 @@ describe("ProviderService", () => { it("Returns an empty array if no providers are found in state", async () => { const mockData: ProviderData[] = undefined; - fakeActiveUserState.nextState(arrayToRecord(mockData)); + fakeUserState.nextState(arrayToRecord(mockData)); const result = await providerService.getAll(); expect(result).toEqual([]); }); @@ -106,7 +108,7 @@ describe("ProviderService", () => { describe("get()", () => { it("Returns a single provider from state that matches the specified id", async () => { const mockData = buildMockProviders(5); - fakeActiveUserState.nextState(arrayToRecord(mockData)); + fakeUserState.nextState(arrayToRecord(mockData)); const result = await providerService.get(mockData[3].id); expect(result).toEqual(new Provider(mockData[3])); }); @@ -120,15 +122,12 @@ describe("ProviderService", () => { describe("save()", () => { it("replaces the entire provider list in state for the active user", async () => { const originalData = buildMockProviders(10); - fakeActiveUserState.nextState(arrayToRecord(originalData)); + fakeUserState.nextState(arrayToRecord(originalData)); - const newData = buildMockProviders(10, "newData"); - await providerService.save(arrayToRecord(newData)); + const newData = arrayToRecord(buildMockProviders(10, "newData")); + await providerService.save(newData); - const result = await providerService.getAll(); - - expect(result).toEqual(newData); - expect(result).not.toEqual(originalData); + expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, newData]); }); // This is more or less a test for logouts @@ -136,9 +135,8 @@ describe("ProviderService", () => { const originalData = buildMockProviders(2); fakeActiveUserState.nextState(arrayToRecord(originalData)); await providerService.save(null); - const result = await providerService.getAll(); - expect(result).toEqual([]); - expect(result).not.toEqual(originalData); + + expect(fakeActiveUserState.nextMock).toHaveBeenCalledWith([fakeUserId, null]); }); }); }); diff --git a/libs/common/src/admin-console/services/provider.service.ts b/libs/common/src/admin-console/services/provider.service.ts index e68ea5bf9d3..47291a5520e 100644 --- a/libs/common/src/admin-console/services/provider.service.ts +++ b/libs/common/src/admin-console/services/provider.service.ts @@ -1,4 +1,4 @@ -import { Observable, map, firstValueFrom } from "rxjs"; +import { Observable, map, firstValueFrom, of, switchMap, take } from "rxjs"; import { KeyDefinition, PROVIDERS_DISK, StateProvider } from "../../platform/state"; import { UserId } from "../../types/guid"; @@ -18,9 +18,17 @@ export class ProviderService implements ProviderServiceAbstraction { constructor(private stateProvider: StateProvider) {} private providers$(userId?: UserId): Observable { - return this.stateProvider - .getUserState$(PROVIDERS, userId) - .pipe(this.mapProviderRecordToArray()); + // FIXME: Can be replaced with `getUserStateOrDefault$` if we weren't trying to pick this. + return ( + userId != null + ? this.stateProvider.getUser(userId, PROVIDERS).state$ + : this.stateProvider.activeUserId$.pipe( + take(1), + switchMap((userId) => + userId != null ? this.stateProvider.getUser(userId, PROVIDERS).state$ : of(null), + ), + ) + ).pipe(this.mapProviderRecordToArray()); } private mapProviderRecordToArray() { diff --git a/libs/common/src/auth/abstractions/sso-login.service.abstraction.ts b/libs/common/src/auth/abstractions/sso-login.service.abstraction.ts index 4d73810320d..c964c8809c3 100644 --- a/libs/common/src/auth/abstractions/sso-login.service.abstraction.ts +++ b/libs/common/src/auth/abstractions/sso-login.service.abstraction.ts @@ -54,6 +54,20 @@ export abstract class SsoLoginServiceAbstraction { * Do not use this value outside of the SSO login flow. */ setOrganizationSsoIdentifier: (organizationIdentifier: string) => Promise; + /** + * Gets the user's email. + * Note: This should only be used during the SSO flow to identify the user that is attempting to log in. + * @returns The user's email. + */ + getSsoEmail: () => Promise; + /** + * Sets the user's email. + * Note: This should only be used during the SSO flow to identify the user that is attempting to log in. + * @param email The user's email. + * @returns A promise that resolves when the email has been set. + * + */ + setSsoEmail: (email: string) => Promise; /** * Gets the value of the active user's organization sso identifier. * diff --git a/libs/common/src/auth/abstractions/token.service.ts b/libs/common/src/auth/abstractions/token.service.ts index 88e6d489b32..d2358314d79 100644 --- a/libs/common/src/auth/abstractions/token.service.ts +++ b/libs/common/src/auth/abstractions/token.service.ts @@ -1,31 +1,208 @@ -import { IdentityTokenResponse } from "../models/response/identity-token.response"; +import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; +import { UserId } from "../../types/guid"; +import { DecodedAccessToken } from "../services/token.service"; export abstract class TokenService { + /** + * Sets the access token, refresh token, API Key Client ID, and API Key Client Secret in memory or disk + * based on the given vaultTimeoutAction and vaultTimeout and the derived access token user id. + * Note: for platforms that support secure storage, the access & refresh tokens are stored in secure storage instead of on disk. + * Note 2: this method also enforces always setting the access token and the refresh token together as + * we can retrieve the user id required to set the refresh token from the access token for efficiency. + * @param accessToken The access token to set. + * @param refreshToken The refresh token to set. + * @param clientIdClientSecret The API Key Client ID and Client Secret to set. + * @param vaultTimeoutAction The action to take when the vault times out. + * @param vaultTimeout The timeout for the vault. + * @returns A promise that resolves when the tokens have been set. + */ setTokens: ( accessToken: string, refreshToken: string, - clientIdClientSecret: [string, string], - ) => Promise; - setToken: (token: string) => Promise; - getToken: () => Promise; - setRefreshToken: (refreshToken: string) => Promise; - getRefreshToken: () => Promise; - setClientId: (clientId: string) => Promise; - getClientId: () => Promise; - setClientSecret: (clientSecret: string) => Promise; - getClientSecret: () => Promise; - setTwoFactorToken: (tokenResponse: IdentityTokenResponse) => Promise; - getTwoFactorToken: () => Promise; - clearTwoFactorToken: () => Promise; - clearToken: (userId?: string) => Promise; - decodeToken: (token?: string) => Promise; - getTokenExpirationDate: () => Promise; + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + clientIdClientSecret?: [string, string], + ) => Promise; + + /** + * Clears the access token, refresh token, API Key Client ID, and API Key Client Secret out of memory, disk, and secure storage if supported. + * @param userId The optional user id to clear the tokens for; if not provided, the active user id is used. + * @returns A promise that resolves when the tokens have been cleared. + */ + clearTokens: (userId?: UserId) => Promise; + + /** + * Sets the access token in memory or disk based on the given vaultTimeoutAction and vaultTimeout + * and the user id read off the access token + * Note: for platforms that support secure storage, the access & refresh tokens are stored in secure storage instead of on disk. + * @param accessToken The access token to set. + * @param vaultTimeoutAction The action to take when the vault times out. + * @param vaultTimeout The timeout for the vault. + * @returns A promise that resolves when the access token has been set. + */ + setAccessToken: ( + accessToken: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + ) => Promise; + + // TODO: revisit having this public clear method approach once the state service is fully deprecated. + /** + * Clears the access token for the given user id out of memory, disk, and secure storage if supported. + * @param userId The optional user id to clear the access token for; if not provided, the active user id is used. + * @returns A promise that resolves when the access token has been cleared. + * + * Note: This method is required so that the StateService doesn't have to inject the VaultTimeoutSettingsService to + * pass in the vaultTimeoutAction and vaultTimeout. + * This avoids a circular dependency between the StateService, TokenService, and VaultTimeoutSettingsService. + */ + clearAccessToken: (userId?: UserId) => Promise; + + /** + * Gets the access token + * @param userId - The optional user id to get the access token for; if not provided, the active user is used. + * @returns A promise that resolves with the access token or undefined. + */ + getAccessToken: (userId?: UserId) => Promise; + + /** + * Gets the refresh token. + * @param userId - The optional user id to get the refresh token for; if not provided, the active user is used. + * @returns A promise that resolves with the refresh token or undefined. + */ + getRefreshToken: (userId?: UserId) => Promise; + + /** + * Sets the API Key Client ID for the active user id in memory or disk based on the given vaultTimeoutAction and vaultTimeout. + * @param clientId The API Key Client ID to set. + * @param vaultTimeoutAction The action to take when the vault times out. + * @param vaultTimeout The timeout for the vault. + * @returns A promise that resolves when the API Key Client ID has been set. + */ + setClientId: ( + clientId: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId?: UserId, + ) => Promise; + + /** + * Gets the API Key Client ID for the active user. + * @returns A promise that resolves with the API Key Client ID or undefined + */ + getClientId: (userId?: UserId) => Promise; + + /** + * Sets the API Key Client Secret for the active user id in memory or disk based on the given vaultTimeoutAction and vaultTimeout. + * @param clientSecret The API Key Client Secret to set. + * @param vaultTimeoutAction The action to take when the vault times out. + * @param vaultTimeout The timeout for the vault. + * @returns A promise that resolves when the API Key Client Secret has been set. + */ + setClientSecret: ( + clientSecret: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId?: UserId, + ) => Promise; + + /** + * Gets the API Key Client Secret for the active user. + * @returns A promise that resolves with the API Key Client Secret or undefined + */ + getClientSecret: (userId?: UserId) => Promise; + + /** + * Sets the two factor token for the given email in global state. + * The two factor token is set when the user checks "remember me" when completing two factor + * authentication and it is used to bypass two factor authentication for a period of time. + * @param email The email to set the two factor token for. + * @param twoFactorToken The two factor token to set. + * @returns A promise that resolves when the two factor token has been set. + */ + setTwoFactorToken: (email: string, twoFactorToken: string) => Promise; + + /** + * Gets the two factor token for the given email. + * @param email The email to get the two factor token for. + * @returns A promise that resolves with the two factor token for the given email or null if it isn't found. + */ + getTwoFactorToken: (email: string) => Promise; + + /** + * Clears the two factor token for the given email out of global state. + * @param email The email to clear the two factor token for. + * @returns A promise that resolves when the two factor token has been cleared. + */ + clearTwoFactorToken: (email: string) => Promise; + + /** + * Decodes the access token. + * @param token The access token to decode. + * @returns A promise that resolves with the decoded access token. + */ + decodeAccessToken: (token?: string) => Promise; + + /** + * Gets the expiration date for the access token. Returns if token can't be decoded or has no expiration + * @returns A promise that resolves with the expiration date for the access token. + */ + getTokenExpirationDate: () => Promise; + + /** + * Calculates the adjusted time in seconds until the access token expires, considering an optional offset. + * + * @param {number} [offsetSeconds=0] Optional seconds to subtract from the remaining time, + * creating a buffer before actual expiration. Useful for preemptive actions + * before token expiry. A value of 0 or omitting this parameter calculates time + * based on the actual expiration. + * @returns {Promise} Promise resolving to the adjusted seconds remaining. + */ tokenSecondsRemaining: (offsetSeconds?: number) => Promise; + + /** + * Checks if the access token needs to be refreshed. + * @param {number} [minutes=5] - Optional number of minutes before the access token expires to consider refreshing it. + * @returns A promise that resolves with a boolean indicating if the access token needs to be refreshed. + */ tokenNeedsRefresh: (minutes?: number) => Promise; - getUserId: () => Promise; + + /** + * Gets the user id for the active user from the access token. + * @returns A promise that resolves with the user id for the active user. + * @deprecated Use AccountService.activeAccount$ instead. + */ + getUserId: () => Promise; + + /** + * Gets the email for the active user from the access token. + * @returns A promise that resolves with the email for the active user. + * @deprecated Use AccountService.activeAccount$ instead. + */ getEmail: () => Promise; + + /** + * Gets the email verified status for the active user from the access token. + * @returns A promise that resolves with the email verified status for the active user. + */ getEmailVerified: () => Promise; + + /** + * Gets the name for the active user from the access token. + * @returns A promise that resolves with the name for the active user. + * @deprecated Use AccountService.activeAccount$ instead. + */ getName: () => Promise; + + /** + * Gets the issuer for the active user from the access token. + * @returns A promise that resolves with the issuer for the active user. + */ getIssuer: () => Promise; + + /** + * Gets whether or not the user authenticated via an external mechanism. + * @returns A promise that resolves with a boolean representing the user's external authN status. + */ getIsExternal: () => Promise; } diff --git a/libs/common/src/auth/services/sso-login.service.ts b/libs/common/src/auth/services/sso-login.service.ts index e693de44fc0..99640e1c6c6 100644 --- a/libs/common/src/auth/services/sso-login.service.ts +++ b/libs/common/src/auth/services/sso-login.service.ts @@ -7,6 +7,7 @@ import { SSO_DISK, StateProvider, } from "../../platform/state"; +import { SsoLoginServiceAbstraction } from "../abstractions/sso-login.service.abstraction"; /** * Uses disk storage so that the code verifier can be persisted across sso redirects. @@ -33,16 +34,25 @@ const ORGANIZATION_SSO_IDENTIFIER = new KeyDefinition( }, ); -export class SsoLoginService { +/** + * Uses disk storage so that the user's email can be persisted across sso redirects. + */ +const SSO_EMAIL = new KeyDefinition(SSO_DISK, "ssoEmail", { + deserializer: (state) => state, +}); + +export class SsoLoginService implements SsoLoginServiceAbstraction { private codeVerifierState: GlobalState; private ssoState: GlobalState; private orgSsoIdentifierState: GlobalState; + private ssoEmailState: GlobalState; private activeUserOrgSsoIdentifierState: ActiveUserState; constructor(private stateProvider: StateProvider) { this.codeVerifierState = this.stateProvider.getGlobal(CODE_VERIFIER); this.ssoState = this.stateProvider.getGlobal(SSO_STATE); this.orgSsoIdentifierState = this.stateProvider.getGlobal(ORGANIZATION_SSO_IDENTIFIER); + this.ssoEmailState = this.stateProvider.getGlobal(SSO_EMAIL); this.activeUserOrgSsoIdentifierState = this.stateProvider.getActive( ORGANIZATION_SSO_IDENTIFIER, ); @@ -72,6 +82,14 @@ export class SsoLoginService { await this.orgSsoIdentifierState.update((_) => organizationIdentifier); } + getSsoEmail(): Promise { + return firstValueFrom(this.ssoEmailState.state$); + } + + async setSsoEmail(email: string): Promise { + await this.ssoEmailState.update((_) => email); + } + getActiveUserOrganizationSsoIdentifier(): Promise { return firstValueFrom(this.activeUserOrgSsoIdentifierState.state$); } diff --git a/libs/common/src/auth/services/token.service.spec.ts b/libs/common/src/auth/services/token.service.spec.ts new file mode 100644 index 00000000000..a7b953f9280 --- /dev/null +++ b/libs/common/src/auth/services/token.service.spec.ts @@ -0,0 +1,2237 @@ +import { mock } from "jest-mock-extended"; + +import { FakeSingleUserStateProvider, FakeGlobalStateProvider } from "../../../spec"; +import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; +import { AbstractStorageService } from "../../platform/abstractions/storage.service"; +import { StorageLocation } from "../../platform/enums"; +import { StorageOptions } from "../../platform/models/domain/storage-options"; +import { UserId } from "../../types/guid"; + +import { ACCOUNT_ACTIVE_ACCOUNT_ID } from "./account.service"; +import { DecodedAccessToken, TokenService } from "./token.service"; +import { + ACCESS_TOKEN_DISK, + ACCESS_TOKEN_MEMORY, + ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE, + API_KEY_CLIENT_ID_DISK, + API_KEY_CLIENT_ID_MEMORY, + API_KEY_CLIENT_SECRET_DISK, + API_KEY_CLIENT_SECRET_MEMORY, + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + REFRESH_TOKEN_DISK, + REFRESH_TOKEN_MEMORY, + REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE, +} from "./token.state"; + +describe("TokenService", () => { + let tokenService: TokenService; + let singleUserStateProvider: FakeSingleUserStateProvider; + let globalStateProvider: FakeGlobalStateProvider; + + const secureStorageService = mock(); + + const memoryVaultTimeoutAction = VaultTimeoutAction.LogOut; + const memoryVaultTimeout = 30; + + const diskVaultTimeoutAction = VaultTimeoutAction.Lock; + const diskVaultTimeout: number = null; + + const accessTokenJwt = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwOi8vbG9jYWxob3N0IiwibmJmIjoxNzA5MzI0MTExLCJpYXQiOjE3MDkzMjQxMTEsImV4cCI6MTcwOTMyNzcxMSwic2NvcGUiOlsiYXBpIiwib2ZmbGluZV9hY2Nlc3MiXSwiYW1yIjpbIkFwcGxpY2F0aW9uIl0sImNsaWVudF9pZCI6IndlYiIsInN1YiI6ImVjZTcwYTEzLTcyMTYtNDNjNC05OTc3LWIxMDMwMTQ2ZTFlNyIsImF1dGhfdGltZSI6MTcwOTMyNDEwNCwiaWRwIjoiYml0d2FyZGVuIiwicHJlbWl1bSI6ZmFsc2UsImVtYWlsIjoiZXhhbXBsZUBiaXR3YXJkZW4uY29tIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJzc3RhbXAiOiJHWTdKQU82NENLS1RLQkI2WkVBVVlMMldPUVU3QVNUMiIsIm5hbWUiOiJUZXN0IFVzZXIiLCJvcmdvd25lciI6WyI5MmI0OTkwOC1iNTE0LTQ1YTgtYmFkYi1iMTAzMDE0OGZlNTMiLCIzOGVkZTMyMi1iNGI0LTRiZDgtOWUwOS1iMTA3MDExMmRjMTEiLCJiMmQwNzAyOC1hNTgzLTRjM2UtOGQ2MC1iMTA3MDExOThjMjkiLCJiZjkzNGJhMi0wZmQ0LTQ5ZjItYTk1ZS1iMTA3MDExZmM5ZTYiLCJjMGI3Zjc1ZC0wMTVmLTQyYzktYjNhNi1iMTA4MDE3NjA3Y2EiXSwiZGV2aWNlIjoiNGI4NzIzNjctMGRhNi00MWEwLWFkY2ItNzdmMmZlZWZjNGY0IiwianRpIjoiNzUxNjFCRTQxMzFGRjVBMkRFNTExQjhDNEUyRkY4OUEifQ.n7roP8sSbfwcYdvRxZNZds27IK32TW6anorE6BORx_Q"; + + const accessTokenDecoded: DecodedAccessToken = { + iss: "http://localhost", + nbf: 1709324111, + iat: 1709324111, + exp: 1709327711, + scope: ["api", "offline_access"], + amr: ["Application"], + client_id: "web", + sub: "ece70a13-7216-43c4-9977-b1030146e1e7", // user id + auth_time: 1709324104, + idp: "bitwarden", + premium: false, + email: "example@bitwarden.com", + email_verified: false, + sstamp: "GY7JAO64CKKTKBB6ZEAUYL2WOQU7AST2", + name: "Test User", + orgowner: [ + "92b49908-b514-45a8-badb-b1030148fe53", + "38ede322-b4b4-4bd8-9e09-b1070112dc11", + "b2d07028-a583-4c3e-8d60-b10701198c29", + "bf934ba2-0fd4-49f2-a95e-b107011fc9e6", + "c0b7f75d-015f-42c9-b3a6-b108017607ca", + ], + device: "4b872367-0da6-41a0-adcb-77f2feefc4f4", + jti: "75161BE4131FF5A2DE511B8C4E2FF89A", + }; + + const userIdFromAccessToken: UserId = accessTokenDecoded.sub as UserId; + + const secureStorageOptions: StorageOptions = { + storageLocation: StorageLocation.Disk, + useSecureStorage: true, + userId: userIdFromAccessToken, + }; + + beforeEach(() => { + jest.clearAllMocks(); + + singleUserStateProvider = new FakeSingleUserStateProvider(); + globalStateProvider = new FakeGlobalStateProvider(); + + const supportsSecureStorage = false; // default to false; tests will override as needed + tokenService = createTokenService(supportsSecureStorage); + }); + + it("instantiates", () => { + expect(tokenService).not.toBeFalsy(); + }); + + describe("Access Token methods", () => { + const accessTokenPartialSecureStorageKey = `_accessToken`; + const accessTokenSecureStorageKey = `${userIdFromAccessToken}${accessTokenPartialSecureStorageKey}`; + + describe("setAccessToken", () => { + it("should throw an error if the access token is null", async () => { + // Act + const result = tokenService.setAccessToken(null, VaultTimeoutAction.Lock, null); + // Assert + await expect(result).rejects.toThrow("Access token is required."); + }); + + it("should throw an error if an invalid token is passed in", async () => { + // Act + const result = tokenService.setAccessToken("invalidToken", VaultTimeoutAction.Lock, null); + // Assert + await expect(result).rejects.toThrow("JWT must have 3 parts"); + }); + + it("should not throw an error as long as the token is valid", async () => { + // Act + const result = tokenService.setAccessToken(accessTokenJwt, VaultTimeoutAction.Lock, null); + // Assert + await expect(result).resolves.not.toThrow(); + }); + + describe("Memory storage tests", () => { + it("should set the access token in memory", async () => { + // Act + await tokenService.setAccessToken( + accessTokenJwt, + memoryVaultTimeoutAction, + memoryVaultTimeout, + ); + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(accessTokenJwt); + }); + }); + + describe("Disk storage tests (secure storage not supported on platform)", () => { + it("should set the access token in disk", async () => { + // Act + await tokenService.setAccessToken( + accessTokenJwt, + diskVaultTimeoutAction, + diskVaultTimeout, + ); + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(accessTokenJwt); + }); + }); + + describe("Disk storage tests (secure storage supported on platform)", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should set the access token in secure storage, null out data on disk or in memory, and set a flag to indicate the token has been migrated", async () => { + // Arrange: + + // For testing purposes, let's assume that the access token is already in disk and memory + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Act + await tokenService.setAccessToken( + accessTokenJwt, + diskVaultTimeoutAction, + diskVaultTimeout, + ); + // Assert + + // assert that the access token was set in secure storage + expect(secureStorageService.save).toHaveBeenCalledWith( + accessTokenSecureStorageKey, + accessTokenJwt, + secureStorageOptions, + ); + + // assert data was migrated out of disk and memory + flag was set + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + + expect( + singleUserStateProvider.getFake( + userIdFromAccessToken, + ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE, + ).nextMock, + ).toHaveBeenCalledWith(true); + }); + }); + }); + + describe("getAccessToken", () => { + it("should return undefined if no user id is provided and there is no active user in global state", async () => { + // Act + const result = await tokenService.getAccessToken(); + // Assert + expect(result).toBeUndefined(); + }); + + it("should return null if no access token is found in memory, disk, or secure storage", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getAccessToken(); + // Assert + expect(result).toBeNull(); + }); + + describe("Memory storage tests", () => { + it("should get the access token from memory with no user id specified (uses global active user)", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getAccessToken(); + + // Assert + expect(result).toEqual(accessTokenJwt); + }); + + it("should get the access token from memory for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Act + const result = await tokenService.getAccessToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(accessTokenJwt); + }); + }); + + describe("Disk storage tests (secure storage not supported on platform)", () => { + it("should get the access token from disk with no user id specified", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getAccessToken(); + // Assert + expect(result).toEqual(accessTokenJwt); + }); + + it("should get the access token from disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Act + const result = await tokenService.getAccessToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(accessTokenJwt); + }); + }); + + describe("Disk storage tests (secure storage supported on platform)", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should get the access token from secure storage when no user id is specified and the migration flag is set to true", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + secureStorageService.get.mockResolvedValue(accessTokenJwt); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // set access token migration flag to true + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, true]); + + // Act + const result = await tokenService.getAccessToken(); + // Assert + expect(result).toEqual(accessTokenJwt); + }); + + it("should get the access token from secure storage when user id is specified and the migration flag set to true", async () => { + // Arrange + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + secureStorageService.get.mockResolvedValue(accessTokenJwt); + + // set access token migration flag to true + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, true]); + + // Act + const result = await tokenService.getAccessToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(accessTokenJwt); + }); + + it("should fallback and get the access token from disk when user id is specified and the migration flag is set to false even if the platform supports secure storage", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // set access token migration flag to false + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, false]); + + // Act + const result = await tokenService.getAccessToken(userIdFromAccessToken); + + // Assert + expect(result).toEqual(accessTokenJwt); + + // assert that secure storage was not called + expect(secureStorageService.get).not.toHaveBeenCalled(); + }); + + it("should fallback and get the access token from disk when no user id is specified and the migration flag is set to false even if the platform supports secure storage", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // set access token migration flag to false + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, false]); + + // Act + const result = await tokenService.getAccessToken(); + + // Assert + expect(result).toEqual(accessTokenJwt); + + // assert that secure storage was not called + expect(secureStorageService.get).not.toHaveBeenCalled(); + }); + }); + }); + + describe("clearAccessToken", () => { + it("should throw an error if no user id is provided and there is no active user in global state", async () => { + // Act + // note: don't await here because we want to test the error + const result = tokenService.clearAccessToken(); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot clear access token."); + }); + + describe("Secure storage enabled", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should clear the access token from all storage locations for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Act + await tokenService.clearAccessToken(userIdFromAccessToken); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(null); + + expect(secureStorageService.remove).toHaveBeenCalledWith( + accessTokenSecureStorageKey, + secureStorageOptions, + ); + }); + + it("should clear the access token from all storage locations for the global active user", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, accessTokenJwt]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await tokenService.clearAccessToken(); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, ACCESS_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(null); + + expect(secureStorageService.remove).toHaveBeenCalledWith( + accessTokenSecureStorageKey, + secureStorageOptions, + ); + }); + }); + }); + + describe("decodeAccessToken", () => { + it("should throw an error if no access token provided or retrieved from state", async () => { + // Access + tokenService.getAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.decodeAccessToken(); + // Assert + await expect(result).rejects.toThrow("Access token not found."); + }); + + it("should decode the access token", async () => { + // Arrange + tokenService.getAccessToken = jest.fn().mockResolvedValue(accessTokenJwt); + + // Act + const result = await tokenService.decodeAccessToken(); + + // Assert + expect(result).toEqual(accessTokenDecoded); + }); + }); + + describe("Data methods", () => { + describe("getTokenExpirationDate", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getTokenExpirationDate(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should return null if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + const result = await tokenService.getTokenExpirationDate(); + + // Assert + expect(result).toBeNull(); + }); + + it("should return null if the decoded access token does not have an expiration date", async () => { + // Arrange + const accessTokenDecodedWithoutExp = { ...accessTokenDecoded }; + delete accessTokenDecodedWithoutExp.exp; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithoutExp); + + // Act + const result = await tokenService.getTokenExpirationDate(); + + // Assert + expect(result).toBeNull(); + }); + + it("should return null if the decoded access token has an non numeric expiration date", async () => { + // Arrange + const accessTokenDecodedWithNonNumericExp = { ...accessTokenDecoded, exp: "non-numeric" }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonNumericExp); + + // Act + const result = await tokenService.getTokenExpirationDate(); + + // Assert + expect(result).toBeNull(); + }); + + it("should return the expiration date of the access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getTokenExpirationDate(); + + // Assert + expect(result).toEqual(new Date(accessTokenDecoded.exp * 1000)); + }); + }); + + describe("tokenSecondsRemaining", () => { + it("should return 0 if the tokenExpirationDate is null", async () => { + // Arrange + tokenService.getTokenExpirationDate = jest.fn().mockResolvedValue(null); + + // Act + const result = await tokenService.tokenSecondsRemaining(); + + // Assert + expect(result).toEqual(0); + }); + + it("should return the number of seconds remaining until the token expires", async () => { + // Arrange + // Lock the time to ensure a consistent test environment + // otherwise we have flaky issues with set system time date and the Date.now() call. + const fixedCurrentTime = new Date("2024-03-06T00:00:00Z"); + jest.useFakeTimers().setSystemTime(fixedCurrentTime); + + const nowInSeconds = Math.floor(Date.now() / 1000); + const expirationInSeconds = nowInSeconds + 3600; // token expires in 1 hr + const expectedSecondsRemaining = expirationInSeconds - nowInSeconds; + + const expirationDate = new Date(0); + expirationDate.setUTCSeconds(expirationInSeconds); + tokenService.getTokenExpirationDate = jest.fn().mockResolvedValue(expirationDate); + + // Act + const result = await tokenService.tokenSecondsRemaining(); + + // Assert + expect(result).toEqual(expectedSecondsRemaining); + + // Reset the timers to be the real ones + jest.useRealTimers(); + }); + + it("should return the number of seconds remaining until the token expires, considering an offset", async () => { + // Arrange + // Lock the time to ensure a consistent test environment + // otherwise we have flaky issues with set system time date and the Date.now() call. + const fixedCurrentTime = new Date("2024-03-06T00:00:00Z"); + jest.useFakeTimers().setSystemTime(fixedCurrentTime); + + const nowInSeconds = Math.floor(Date.now() / 1000); + const offsetSeconds = 300; // 5 minute offset + const expirationInSeconds = nowInSeconds + 3600; // token expires in 1 hr + const expectedSecondsRemaining = expirationInSeconds - nowInSeconds - offsetSeconds; // Adjust for offset + + const expirationDate = new Date(0); + expirationDate.setUTCSeconds(expirationInSeconds); + tokenService.getTokenExpirationDate = jest.fn().mockResolvedValue(expirationDate); + + // Act + const result = await tokenService.tokenSecondsRemaining(offsetSeconds); + + // Assert + expect(result).toEqual(expectedSecondsRemaining); + + // Reset the timers to be the real ones + jest.useRealTimers(); + }); + }); + + describe("tokenNeedsRefresh", () => { + it("should return true if token is within the default refresh threshold (5 min)", async () => { + // Arrange + const tokenSecondsRemaining = 60; + tokenService.tokenSecondsRemaining = jest.fn().mockResolvedValue(tokenSecondsRemaining); + + // Act + const result = await tokenService.tokenNeedsRefresh(); + + // Assert + expect(result).toEqual(true); + }); + + it("should return false if token is outside the default refresh threshold (5 min)", async () => { + // Arrange + const tokenSecondsRemaining = 600; + tokenService.tokenSecondsRemaining = jest.fn().mockResolvedValue(tokenSecondsRemaining); + + // Act + const result = await tokenService.tokenNeedsRefresh(); + + // Assert + expect(result).toEqual(false); + }); + + it("should return true if token is within the specified refresh threshold", async () => { + // Arrange + const tokenSecondsRemaining = 60; + tokenService.tokenSecondsRemaining = jest.fn().mockResolvedValue(tokenSecondsRemaining); + + // Act + const result = await tokenService.tokenNeedsRefresh(2); + + // Assert + expect(result).toEqual(true); + }); + + it("should return false if token is outside the specified refresh threshold", async () => { + // Arrange + const tokenSecondsRemaining = 600; + tokenService.tokenSecondsRemaining = jest.fn().mockResolvedValue(tokenSecondsRemaining); + + // Act + const result = await tokenService.tokenNeedsRefresh(5); + + // Assert + expect(result).toEqual(false); + }); + }); + + describe("getUserId", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getUserId(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should throw an error if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getUserId(); + // Assert + await expect(result).rejects.toThrow("No user id found"); + }); + + it("should throw an error if the decoded access token has a non-string user id", async () => { + // Arrange + const accessTokenDecodedWithNonStringSub = { ...accessTokenDecoded, sub: 123 }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonStringSub); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getUserId(); + // Assert + await expect(result).rejects.toThrow("No user id found"); + }); + + it("should return the user id from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getUserId(); + + // Assert + expect(result).toEqual(userIdFromAccessToken); + }); + }); + + describe("getUserIdFromAccessToken", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).getUserIdFromAccessToken(accessTokenJwt); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should throw an error if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).getUserIdFromAccessToken(accessTokenJwt); + // Assert + await expect(result).rejects.toThrow("No user id found"); + }); + + it("should throw an error if the decoded access token has a non-string user id", async () => { + // Arrange + const accessTokenDecodedWithNonStringSub = { ...accessTokenDecoded, sub: 123 }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonStringSub); + + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).getUserIdFromAccessToken(accessTokenJwt); + // Assert + await expect(result).rejects.toThrow("No user id found"); + }); + + it("should return the user id from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await (tokenService as any).getUserIdFromAccessToken(accessTokenJwt); + + // Assert + expect(result).toEqual(userIdFromAccessToken); + }); + }); + + describe("getEmail", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmail(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should throw an error if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmail(); + // Assert + await expect(result).rejects.toThrow("No email found"); + }); + + it("should throw an error if the decoded access token has a non-string email", async () => { + // Arrange + const accessTokenDecodedWithNonStringEmail = { ...accessTokenDecoded, email: 123 }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonStringEmail); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmail(); + // Assert + await expect(result).rejects.toThrow("No email found"); + }); + + it("should return the email from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getEmail(); + + // Assert + expect(result).toEqual(accessTokenDecoded.email); + }); + }); + + describe("getEmailVerified", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmailVerified(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should throw an error if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmailVerified(); + // Assert + await expect(result).rejects.toThrow("No email verification found"); + }); + + it("should throw an error if the decoded access token has a non-boolean email_verified", async () => { + // Arrange + const accessTokenDecodedWithNonBooleanEmailVerified = { + ...accessTokenDecoded, + email_verified: 123, + }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonBooleanEmailVerified); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getEmailVerified(); + // Assert + await expect(result).rejects.toThrow("No email verification found"); + }); + + it("should return the email_verified from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getEmailVerified(); + + // Assert + expect(result).toEqual(accessTokenDecoded.email_verified); + }); + }); + + describe("getName", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getName(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should return null if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + const result = await tokenService.getName(); + + // Assert + expect(result).toBeNull(); + }); + + it("should return null if the decoded access token has a non-string name", async () => { + // Arrange + const accessTokenDecodedWithNonStringName = { ...accessTokenDecoded, name: 123 }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonStringName); + + // Act + const result = await tokenService.getName(); + + // Assert + expect(result).toBeNull(); + }); + + it("should return the name from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getName(); + + // Assert + expect(result).toEqual(accessTokenDecoded.name); + }); + }); + + describe("getIssuer", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getIssuer(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should throw an error if the decoded access token is null", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(null); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getIssuer(); + // Assert + await expect(result).rejects.toThrow("No issuer found"); + }); + + it("should throw an error if the decoded access token has a non-string iss", async () => { + // Arrange + const accessTokenDecodedWithNonStringIss = { ...accessTokenDecoded, iss: 123 }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithNonStringIss); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getIssuer(); + // Assert + await expect(result).rejects.toThrow("No issuer found"); + }); + + it("should return the issuer from the decoded access token", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockResolvedValue(accessTokenDecoded); + + // Act + const result = await tokenService.getIssuer(); + + // Assert + expect(result).toEqual(accessTokenDecoded.iss); + }); + }); + + describe("getIsExternal", () => { + it("should throw an error if the access token cannot be decoded", async () => { + // Arrange + tokenService.decodeAccessToken = jest.fn().mockRejectedValue(new Error("Mock error")); + + // Act + // note: don't await here because we want to test the error + const result = tokenService.getIsExternal(); + // Assert + await expect(result).rejects.toThrow("Failed to decode access token: Mock error"); + }); + + it("should return false if the amr (Authentication Method Reference) claim does not contain 'external'", async () => { + // Arrange + const accessTokenDecodedWithoutExternalAmr = { + ...accessTokenDecoded, + amr: ["not-external"], + }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithoutExternalAmr); + + // Act + const result = await tokenService.getIsExternal(); + + // Assert + expect(result).toEqual(false); + }); + + it("should return true if the amr (Authentication Method Reference) claim contains 'external'", async () => { + // Arrange + const accessTokenDecodedWithExternalAmr = { + ...accessTokenDecoded, + amr: ["external"], + }; + tokenService.decodeAccessToken = jest + .fn() + .mockResolvedValue(accessTokenDecodedWithExternalAmr); + + // Act + const result = await tokenService.getIsExternal(); + + // Assert + expect(result).toEqual(true); + }); + }); + }); + }); + + describe("Refresh Token methods", () => { + const refreshToken = "refreshToken"; + const refreshTokenPartialSecureStorageKey = `_refreshToken`; + const refreshTokenSecureStorageKey = `${userIdFromAccessToken}${refreshTokenPartialSecureStorageKey}`; + + describe("setRefreshToken", () => { + it("should throw an error if no user id is provided", async () => { + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).setRefreshToken( + refreshToken, + VaultTimeoutAction.Lock, + null, + ); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot save refresh token."); + }); + + describe("Memory storage tests", () => { + it("should set the refresh token in memory for the specified user id", async () => { + // Act + await (tokenService as any).setRefreshToken( + refreshToken, + memoryVaultTimeoutAction, + memoryVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(refreshToken); + }); + }); + + describe("Disk storage tests (secure storage not supported on platform)", () => { + it("should set the refresh token in disk for the specified user id", async () => { + // Act + await (tokenService as any).setRefreshToken( + refreshToken, + diskVaultTimeoutAction, + diskVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(refreshToken); + }); + }); + + describe("Disk storage tests (secure storage supported on platform)", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should set the refresh token in secure storage, null out data on disk or in memory, and set a flag to indicate the token has been migrated for the specified user id", async () => { + // Arrange: + // For testing purposes, let's assume that the token is already in disk and memory + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // Act + await (tokenService as any).setRefreshToken( + refreshToken, + diskVaultTimeoutAction, + diskVaultTimeout, + userIdFromAccessToken, + ); + // Assert + + // assert that the refresh token was set in secure storage + expect(secureStorageService.save).toHaveBeenCalledWith( + refreshTokenSecureStorageKey, + refreshToken, + secureStorageOptions, + ); + + // assert data was migrated out of disk and memory + flag was set + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + + expect( + singleUserStateProvider.getFake( + userIdFromAccessToken, + REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE, + ).nextMock, + ).toHaveBeenCalledWith(true); + }); + }); + }); + + describe("getRefreshToken", () => { + it("should return undefined if no user id is provided and there is no active user in global state", async () => { + // Act + const result = await (tokenService as any).getRefreshToken(); + // Assert + expect(result).toBeUndefined(); + }); + + it("should return null if no refresh token is found in memory, disk, or secure storage", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await (tokenService as any).getRefreshToken(); + // Assert + expect(result).toBeNull(); + }); + + describe("Memory storage tests", () => { + it("should get the refresh token from memory with no user id specified (uses global active user)", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getRefreshToken(); + + // Assert + expect(result).toEqual(refreshToken); + }); + + it("should get the refresh token from memory for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Act + const result = await tokenService.getRefreshToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(refreshToken); + }); + }); + + describe("Disk storage tests (secure storage not supported on platform)", () => { + it("should get the refresh token from disk with no user id specified", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getRefreshToken(); + // Assert + expect(result).toEqual(refreshToken); + }); + + it("should get the refresh token from disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // Act + const result = await tokenService.getRefreshToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(refreshToken); + }); + }); + + describe("Disk storage tests (secure storage supported on platform)", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should get the refresh token from secure storage when no user id is specified and the migration flag is set to true", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + secureStorageService.get.mockResolvedValue(refreshToken); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // set access token migration flag to true + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, true]); + + // Act + const result = await tokenService.getRefreshToken(); + // Assert + expect(result).toEqual(refreshToken); + }); + + it("should get the refresh token from secure storage when user id is specified and the migration flag set to true", async () => { + // Arrange + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + secureStorageService.get.mockResolvedValue(refreshToken); + + // set access token migration flag to true + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, true]); + + // Act + const result = await tokenService.getRefreshToken(userIdFromAccessToken); + // Assert + expect(result).toEqual(refreshToken); + }); + + it("should fallback and get the refresh token from disk when user id is specified and the migration flag is set to false even if the platform supports secure storage", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // set refresh token migration flag to false + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, false]); + + // Act + const result = await tokenService.getRefreshToken(userIdFromAccessToken); + + // Assert + expect(result).toEqual(refreshToken); + + // assert that secure storage was not called + expect(secureStorageService.get).not.toHaveBeenCalled(); + }); + + it("should fallback and get the refresh token from disk when no user id is specified and the migration flag is set to false even if the platform supports secure storage", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // set access token migration flag to false + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .stateSubject.next([userIdFromAccessToken, false]); + + // Act + const result = await tokenService.getRefreshToken(); + + // Assert + expect(result).toEqual(refreshToken); + + // assert that secure storage was not called + expect(secureStorageService.get).not.toHaveBeenCalled(); + }); + }); + }); + + describe("clearRefreshToken", () => { + it("should throw an error if no user id is provided", async () => { + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).clearRefreshToken(); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot clear refresh token."); + }); + + describe("Secure storage enabled", () => { + beforeEach(() => { + const supportsSecureStorage = true; + tokenService = createTokenService(supportsSecureStorage); + }); + + it("should clear the refresh token from all storage locations for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK) + .stateSubject.next([userIdFromAccessToken, refreshToken]); + + // Act + await (tokenService as any).clearRefreshToken(userIdFromAccessToken); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, REFRESH_TOKEN_DISK).nextMock, + ).toHaveBeenCalledWith(null); + + expect(secureStorageService.remove).toHaveBeenCalledWith( + refreshTokenSecureStorageKey, + secureStorageOptions, + ); + }); + }); + }); + }); + + describe("Client Id methods", () => { + const clientId = "clientId"; + + describe("setClientId", () => { + it("should throw an error if no user id is provided and there is no active user in global state", async () => { + // Act + // note: don't await here because we want to test the error + const result = tokenService.setClientId(clientId, VaultTimeoutAction.Lock, null); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot save client id."); + }); + + describe("Memory storage tests", () => { + it("should set the client id in memory when there is an active user in global state", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await tokenService.setClientId(clientId, memoryVaultTimeoutAction, memoryVaultTimeout); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(clientId); + }); + + it("should set the client id in memory for the specified user id", async () => { + // Act + await tokenService.setClientId( + clientId, + memoryVaultTimeoutAction, + memoryVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(clientId); + }); + }); + + describe("Disk storage tests", () => { + it("should set the client id in disk when there is an active user in global state", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await tokenService.setClientId(clientId, diskVaultTimeoutAction, diskVaultTimeout); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK).nextMock, + ).toHaveBeenCalledWith(clientId); + }); + + it("should set the client id in disk for the specified user id", async () => { + // Act + await tokenService.setClientId( + clientId, + diskVaultTimeoutAction, + diskVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK).nextMock, + ).toHaveBeenCalledWith(clientId); + }); + }); + }); + + describe("getClientId", () => { + it("should return undefined if no user id is provided and there is no active user in global state", async () => { + // Act + const result = await tokenService.getClientId(); + // Assert + expect(result).toBeUndefined(); + }); + + it("should return null if no client id is found in memory or disk", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientId(); + // Assert + expect(result).toBeNull(); + }); + + describe("Memory storage tests", () => { + it("should get the client id from memory with no user id specified (uses global active user)", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientId(); + + // Assert + expect(result).toEqual(clientId); + }); + + it("should get the client id from memory for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Act + const result = await tokenService.getClientId(userIdFromAccessToken); + // Assert + expect(result).toEqual(clientId); + }); + }); + + describe("Disk storage tests", () => { + it("should get the client id from disk with no user id specified", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientId(); + // Assert + expect(result).toEqual(clientId); + }); + + it("should get the client id from disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // Act + const result = await tokenService.getClientId(userIdFromAccessToken); + // Assert + expect(result).toEqual(clientId); + }); + }); + }); + + describe("clearClientId", () => { + it("should throw an error if no user id is provided and there is no active user in global state", async () => { + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).clearClientId(); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot clear client id."); + }); + + it("should clear the client id from memory and disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientId]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // Act + await (tokenService as any).clearClientId(userIdFromAccessToken); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK).nextMock, + ).toHaveBeenCalledWith(null); + }); + + it("should clear the client id from memory and disk for the global active user", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientId]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK) + .stateSubject.next([userIdFromAccessToken, clientId]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await (tokenService as any).clearClientId(); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_MEMORY).nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_ID_DISK).nextMock, + ).toHaveBeenCalledWith(null); + }); + }); + }); + + describe("Client Secret methods", () => { + const clientSecret = "clientSecret"; + + describe("setClientSecret", () => { + it("should throw an error if no user id is provided and there is no active user in global state", async () => { + // Act + // note: don't await here because we want to test the error + const result = tokenService.setClientSecret(clientSecret, VaultTimeoutAction.Lock, null); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot save client secret."); + }); + + describe("Memory storage tests", () => { + it("should set the client secret in memory when there is an active user in global state", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await tokenService.setClientSecret( + clientSecret, + memoryVaultTimeoutAction, + memoryVaultTimeout, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(clientSecret); + }); + + it("should set the client secret in memory for the specified user id", async () => { + // Act + await tokenService.setClientSecret( + clientSecret, + memoryVaultTimeoutAction, + memoryVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(clientSecret); + }); + }); + + describe("Disk storage tests", () => { + it("should set the client secret in disk when there is an active user in global state", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await tokenService.setClientSecret( + clientSecret, + diskVaultTimeoutAction, + diskVaultTimeout, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .nextMock, + ).toHaveBeenCalledWith(clientSecret); + }); + + it("should set the client secret in disk for the specified user id", async () => { + // Act + await tokenService.setClientSecret( + clientSecret, + diskVaultTimeoutAction, + diskVaultTimeout, + userIdFromAccessToken, + ); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .nextMock, + ).toHaveBeenCalledWith(clientSecret); + }); + }); + }); + + describe("getClientSecret", () => { + it("should return undefined if no user id is provided and there is no active user in global state", async () => { + // Act + const result = await tokenService.getClientSecret(); + // Assert + expect(result).toBeUndefined(); + }); + + it("should return null if no client secret is found in memory or disk", async () => { + // Arrange + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientSecret(); + // Assert + expect(result).toBeNull(); + }); + + describe("Memory storage tests", () => { + it("should get the client secret from memory with no user id specified (uses global active user)", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientSecret(); + + // Assert + expect(result).toEqual(clientSecret); + }); + + it("should get the client secret from memory for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // set disk to undefined + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, undefined]); + + // Act + const result = await tokenService.getClientSecret(userIdFromAccessToken); + // Assert + expect(result).toEqual(clientSecret); + }); + }); + + describe("Disk storage tests", () => { + it("should get the client secret from disk with no user id specified", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + const result = await tokenService.getClientSecret(); + // Assert + expect(result).toEqual(clientSecret); + }); + + it("should get the client secret from disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, undefined]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // Act + const result = await tokenService.getClientSecret(userIdFromAccessToken); + // Assert + expect(result).toEqual(clientSecret); + }); + }); + }); + + describe("clearClientSecret", () => { + it("should throw an error if no user id is provided and there is no active user in global state", async () => { + // Act + // note: don't await here because we want to test the error + const result = (tokenService as any).clearClientSecret(); + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot clear client secret."); + }); + + it("should clear the client secret from memory and disk for the specified user id", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // Act + await (tokenService as any).clearClientSecret(userIdFromAccessToken); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .nextMock, + ).toHaveBeenCalledWith(null); + }); + + it("should clear the client secret from memory and disk for the global active user", async () => { + // Arrange + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + singleUserStateProvider + .getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .stateSubject.next([userIdFromAccessToken, clientSecret]); + + // Need to have global active id set to the user id + globalStateProvider + .getFake(ACCOUNT_ACTIVE_ACCOUNT_ID) + .stateSubject.next(userIdFromAccessToken); + + // Act + await (tokenService as any).clearClientSecret(); + + // Assert + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_MEMORY) + .nextMock, + ).toHaveBeenCalledWith(null); + expect( + singleUserStateProvider.getFake(userIdFromAccessToken, API_KEY_CLIENT_SECRET_DISK) + .nextMock, + ).toHaveBeenCalledWith(null); + }); + }); + }); + + describe("setTokens", () => { + it("should call to set all passed in tokens after deriving user id from the access token", async () => { + // Arrange + const refreshToken = "refreshToken"; + // specific vault timeout actions and vault timeouts don't change this test so values don't matter. + const vaultTimeoutAction = VaultTimeoutAction.Lock; + const vaultTimeout = 30; + const clientId = "clientId"; + const clientSecret = "clientSecret"; + + (tokenService as any)._setAccessToken = jest.fn(); + // any hack allows for mocking private method. + (tokenService as any).setRefreshToken = jest.fn(); + tokenService.setClientId = jest.fn(); + tokenService.setClientSecret = jest.fn(); + + // Act + // Note: passing a valid access token so that a valid user id can be determined from the access token + await tokenService.setTokens(accessTokenJwt, refreshToken, vaultTimeoutAction, vaultTimeout, [ + clientId, + clientSecret, + ]); + + // Assert + expect((tokenService as any)._setAccessToken).toHaveBeenCalledWith( + accessTokenJwt, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + + // any hack allows for testing private methods + expect((tokenService as any).setRefreshToken).toHaveBeenCalledWith( + refreshToken, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + + expect(tokenService.setClientId).toHaveBeenCalledWith( + clientId, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + expect(tokenService.setClientSecret).toHaveBeenCalledWith( + clientSecret, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + }); + + it("should not try to set client id and client secret if they are not passed in", async () => { + // Arrange + const refreshToken = "refreshToken"; + const vaultTimeoutAction = VaultTimeoutAction.Lock; + const vaultTimeout = 30; + + (tokenService as any)._setAccessToken = jest.fn(); + (tokenService as any).setRefreshToken = jest.fn(); + tokenService.setClientId = jest.fn(); + tokenService.setClientSecret = jest.fn(); + + // Act + await tokenService.setTokens(accessTokenJwt, refreshToken, vaultTimeoutAction, vaultTimeout); + + // Assert + expect((tokenService as any)._setAccessToken).toHaveBeenCalledWith( + accessTokenJwt, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + + // any hack allows for testing private methods + expect((tokenService as any).setRefreshToken).toHaveBeenCalledWith( + refreshToken, + vaultTimeoutAction, + vaultTimeout, + userIdFromAccessToken, + ); + + expect(tokenService.setClientId).not.toHaveBeenCalled(); + expect(tokenService.setClientSecret).not.toHaveBeenCalled(); + }); + + it("should throw an error if the access token is invalid", async () => { + // Arrange + const accessToken = "invalidToken"; + const refreshToken = "refreshToken"; + const vaultTimeoutAction = VaultTimeoutAction.Lock; + const vaultTimeout = 30; + + // Act + const result = tokenService.setTokens( + accessToken, + refreshToken, + vaultTimeoutAction, + vaultTimeout, + ); + + // Assert + await expect(result).rejects.toThrow("JWT must have 3 parts"); + }); + + it("should throw an error if the access token is missing", async () => { + // Arrange + const accessToken: string = null; + const refreshToken = "refreshToken"; + const vaultTimeoutAction = VaultTimeoutAction.Lock; + const vaultTimeout = 30; + + // Act + const result = tokenService.setTokens( + accessToken, + refreshToken, + vaultTimeoutAction, + vaultTimeout, + ); + + // Assert + await expect(result).rejects.toThrow("Access token and refresh token are required."); + }); + + it("should throw an error if the refresh token is missing", async () => { + // Arrange + const accessToken = "accessToken"; + const refreshToken: string = null; + const vaultTimeoutAction = VaultTimeoutAction.Lock; + const vaultTimeout = 30; + + // Act + const result = tokenService.setTokens( + accessToken, + refreshToken, + vaultTimeoutAction, + vaultTimeout, + ); + + // Assert + await expect(result).rejects.toThrow("Access token and refresh token are required."); + }); + }); + + describe("clearTokens", () => { + it("should call to clear all tokens for the specified user id", async () => { + // Arrange + const userId = "userId" as UserId; + + tokenService.clearAccessToken = jest.fn(); + (tokenService as any).clearRefreshToken = jest.fn(); + (tokenService as any).clearClientId = jest.fn(); + (tokenService as any).clearClientSecret = jest.fn(); + + // Act + + await tokenService.clearTokens(userId); + + // Assert + + expect(tokenService.clearAccessToken).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearRefreshToken).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearClientId).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearClientSecret).toHaveBeenCalledWith(userId); + }); + + it("should call to clear all tokens for the active user id", async () => { + // Arrange + const userId = "userId" as UserId; + + globalStateProvider.getFake(ACCOUNT_ACTIVE_ACCOUNT_ID).stateSubject.next(userId); + + tokenService.clearAccessToken = jest.fn(); + (tokenService as any).clearRefreshToken = jest.fn(); + (tokenService as any).clearClientId = jest.fn(); + (tokenService as any).clearClientSecret = jest.fn(); + + // Act + + await tokenService.clearTokens(); + + // Assert + + expect(tokenService.clearAccessToken).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearRefreshToken).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearClientId).toHaveBeenCalledWith(userId); + expect((tokenService as any).clearClientSecret).toHaveBeenCalledWith(userId); + }); + + it("should not call to clear all tokens if no user id is provided and there is no active user in global state", async () => { + // Arrange + tokenService.clearAccessToken = jest.fn(); + (tokenService as any).clearRefreshToken = jest.fn(); + (tokenService as any).clearClientId = jest.fn(); + (tokenService as any).clearClientSecret = jest.fn(); + + // Act + + const result = tokenService.clearTokens(); + + // Assert + await expect(result).rejects.toThrow("User id not found. Cannot clear tokens."); + }); + }); + + describe("Two Factor Token methods", () => { + describe("setTwoFactorToken", () => { + it("should set the email and two factor token when there hasn't been a previous record (initializing the record)", async () => { + // Arrange + const email = "testUser@email.com"; + const twoFactorToken = "twoFactorTokenForTestUser"; + // Act + await tokenService.setTwoFactorToken(email, twoFactorToken); + // Assert + expect( + globalStateProvider.getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL).nextMock, + ).toHaveBeenCalledWith({ [email]: twoFactorToken }); + }); + + it("should set the email and two factor token when there is an initialized value already (updating the existing record)", async () => { + // Arrange + const email = "testUser@email.com"; + const twoFactorToken = "twoFactorTokenForTestUser"; + const initialTwoFactorTokenRecord: Record = { + otherUser: "otherUserTwoFactorToken", + }; + + globalStateProvider + .getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL) + .stateSubject.next(initialTwoFactorTokenRecord); + + // Act + await tokenService.setTwoFactorToken(email, twoFactorToken); + + // Assert + expect( + globalStateProvider.getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL).nextMock, + ).toHaveBeenCalledWith({ [email]: twoFactorToken, ...initialTwoFactorTokenRecord }); + }); + }); + + describe("getTwoFactorToken", () => { + it("should return the two factor token for the given email", async () => { + // Arrange + const email = "testUser"; + const twoFactorToken = "twoFactorTokenForTestUser"; + const initialTwoFactorTokenRecord: Record = { + [email]: twoFactorToken, + }; + + globalStateProvider + .getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL) + .stateSubject.next(initialTwoFactorTokenRecord); + + // Act + const result = await tokenService.getTwoFactorToken(email); + + // Assert + expect(result).toEqual(twoFactorToken); + }); + + it("should not return the two factor token for an email that doesn't exist", async () => { + // Arrange + const email = "testUser"; + const initialTwoFactorTokenRecord: Record = { + otherUser: "twoFactorTokenForOtherUser", + }; + + globalStateProvider + .getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL) + .stateSubject.next(initialTwoFactorTokenRecord); + + // Act + const result = await tokenService.getTwoFactorToken(email); + + // Assert + expect(result).toEqual(undefined); + }); + + it("should return null if there is no two factor token record", async () => { + // Arrange + globalStateProvider + .getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL) + .stateSubject.next(null); + + // Act + const result = await tokenService.getTwoFactorToken("testUser"); + + // Assert + expect(result).toEqual(null); + }); + }); + + describe("clearTwoFactorToken", () => { + it("should clear the two factor token for the given email when a record exists", async () => { + // Arrange + const email = "testUser"; + const twoFactorToken = "twoFactorTokenForTestUser"; + const initialTwoFactorTokenRecord: Record = { + [email]: twoFactorToken, + }; + + globalStateProvider + .getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL) + .stateSubject.next(initialTwoFactorTokenRecord); + + // Act + await tokenService.clearTwoFactorToken(email); + + // Assert + expect( + globalStateProvider.getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL).nextMock, + ).toHaveBeenCalledWith({}); + }); + + it("should initialize the record if it doesn't exist and delete the value", async () => { + // Arrange + const email = "testUser"; + + // Act + await tokenService.clearTwoFactorToken(email); + + // Assert + expect( + globalStateProvider.getFake(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL).nextMock, + ).toHaveBeenCalledWith({}); + }); + }); + }); + + // Helpers + function createTokenService(supportsSecureStorage: boolean) { + return new TokenService( + singleUserStateProvider, + globalStateProvider, + supportsSecureStorage, + secureStorageService, + ); + } +}); diff --git a/libs/common/src/auth/services/token.service.ts b/libs/common/src/auth/services/token.service.ts index b112c7b57d6..4e9722614ed 100644 --- a/libs/common/src/auth/services/token.service.ts +++ b/libs/common/src/auth/services/token.service.ts @@ -1,125 +1,629 @@ -import { StateService } from "../../platform/abstractions/state.service"; -import { Utils } from "../../platform/misc/utils"; +import { firstValueFrom } from "rxjs"; + +import { decodeJwtTokenToJson } from "@bitwarden/auth/common"; + +import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; +import { AbstractStorageService } from "../../platform/abstractions/storage.service"; +import { StorageLocation } from "../../platform/enums"; +import { StorageOptions } from "../../platform/models/domain/storage-options"; +import { + GlobalState, + GlobalStateProvider, + KeyDefinition, + SingleUserStateProvider, +} from "../../platform/state"; +import { UserId } from "../../types/guid"; import { TokenService as TokenServiceAbstraction } from "../abstractions/token.service"; -import { IdentityTokenResponse } from "../models/response/identity-token.response"; + +import { ACCOUNT_ACTIVE_ACCOUNT_ID } from "./account.service"; +import { + ACCESS_TOKEN_DISK, + ACCESS_TOKEN_MEMORY, + ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE, + API_KEY_CLIENT_ID_DISK, + API_KEY_CLIENT_ID_MEMORY, + API_KEY_CLIENT_SECRET_DISK, + API_KEY_CLIENT_SECRET_MEMORY, + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + REFRESH_TOKEN_DISK, + REFRESH_TOKEN_MEMORY, + REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE, +} from "./token.state"; + +export enum TokenStorageLocation { + Disk = "disk", + SecureStorage = "secureStorage", + Memory = "memory", +} + +/** + * Type representing the structure of a standard Bitwarden decoded access token. + * src: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 + * Note: all claims are technically optional so we must verify their existence before using them. + * Note 2: NumericDate is a number representing a date in seconds since the Unix epoch. + */ +export type DecodedAccessToken = { + /** Issuer - the issuer of the token, typically the URL of the authentication server */ + iss?: string; + + /** Not Before - a timestamp defining when the token starts being valid */ + nbf?: number; + + /** Issued At - a timestamp of when the token was issued */ + iat?: number; + + /** Expiration Time - a NumericDate timestamp of when the token will expire */ + exp?: number; + + /** Scope - the scope of the access request, such as the permissions the token grants */ + scope?: string[]; + + /** Authentication Method Reference - the methods used in the authentication */ + amr?: string[]; + + /** Client ID - the identifier for the client that requested the token */ + client_id?: string; + + /** Subject - the unique identifier for the user */ + sub?: string; + + /** Authentication Time - a timestamp of when the user authentication occurred */ + auth_time?: number; + + /** Identity Provider - the system or service that authenticated the user */ + idp?: string; + + /** Premium - a boolean flag indicating whether the account is premium */ + premium?: boolean; + + /** Email - the user's email address */ + email?: string; + + /** Email Verified - a boolean flag indicating whether the user's email address has been verified */ + email_verified?: boolean; + + /** + * Security Stamp - a unique identifier which invalidates the access token if it changes in the db + * (typically after critical account changes like a password change) + */ + sstamp?: string; + + /** Name - the name of the user */ + name?: string; + + /** Organization Owners - a list of organization owner identifiers */ + orgowner?: string[]; + + /** Device - the identifier of the device used */ + device?: string; + + /** JWT ID - a unique identifier for the JWT */ + jti?: string; +}; export class TokenService implements TokenServiceAbstraction { - static decodeToken(token: string): Promise { - if (token == null) { - throw new Error("Token not provided."); - } + private readonly accessTokenSecureStorageKey: string = "_accessToken"; - const parts = token.split("."); - if (parts.length !== 3) { - throw new Error("JWT must have 3 parts"); - } + private readonly refreshTokenSecureStorageKey: string = "_refreshToken"; - const decoded = Utils.fromUrlB64ToUtf8(parts[1]); - if (decoded == null) { - throw new Error("Cannot decode the token"); - } + private emailTwoFactorTokenRecordGlobalState: GlobalState>; - const decodedToken = JSON.parse(decoded); - return decodedToken; + private activeUserIdGlobalState: GlobalState; + + constructor( + // Note: we cannot use ActiveStateProvider because if we ever want to inject + // this service into the AccountService, we will make a circular dependency + private singleUserStateProvider: SingleUserStateProvider, + private globalStateProvider: GlobalStateProvider, + private readonly platformSupportsSecureStorage: boolean, + private secureStorageService: AbstractStorageService, + ) { + this.initializeState(); } - constructor(private stateService: StateService) {} + private initializeState(): void { + this.emailTwoFactorTokenRecordGlobalState = this.globalStateProvider.get( + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + ); + + this.activeUserIdGlobalState = this.globalStateProvider.get(ACCOUNT_ACTIVE_ACCOUNT_ID); + } async setTokens( accessToken: string, refreshToken: string, - clientIdClientSecret: [string, string], - ): Promise { - await this.setToken(accessToken); - await this.setRefreshToken(refreshToken); + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + clientIdClientSecret?: [string, string], + ): Promise { + if (!accessToken || !refreshToken) { + throw new Error("Access token and refresh token are required."); + } + + // get user id the access token + const userId: UserId = await this.getUserIdFromAccessToken(accessToken); + + if (!userId) { + throw new Error("User id not found. Cannot set tokens."); + } + + await this._setAccessToken(accessToken, vaultTimeoutAction, vaultTimeout, userId); + await this.setRefreshToken(refreshToken, vaultTimeoutAction, vaultTimeout, userId); if (clientIdClientSecret != null) { - await this.setClientId(clientIdClientSecret[0]); - await this.setClientSecret(clientIdClientSecret[1]); + await this.setClientId(clientIdClientSecret[0], vaultTimeoutAction, vaultTimeout, userId); + await this.setClientSecret(clientIdClientSecret[1], vaultTimeoutAction, vaultTimeout, userId); } } - async setClientId(clientId: string): Promise { - return await this.stateService.setApiKeyClientId(clientId); + /** + * Internal helper for set access token which always requires user id. + * This is useful because setTokens always will have a user id from the access token whereas + * the public setAccessToken method does not. + */ + private async _setAccessToken( + accessToken: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId: UserId, + ): Promise { + const storageLocation = await this.determineStorageLocation( + vaultTimeoutAction, + vaultTimeout, + true, + ); + + switch (storageLocation) { + case TokenStorageLocation.SecureStorage: + await this.saveStringToSecureStorage(userId, this.accessTokenSecureStorageKey, accessToken); + + // TODO: PM-6408 - https://bitwarden.atlassian.net/browse/PM-6408 + // 2024-02-20: Remove access token from memory and disk so that we migrate to secure storage over time. + // Remove these 2 calls to remove the access token from memory and disk after 3 releases. + + await this.singleUserStateProvider.get(userId, ACCESS_TOKEN_DISK).update((_) => null); + await this.singleUserStateProvider.get(userId, ACCESS_TOKEN_MEMORY).update((_) => null); + + // Set flag to indicate that the access token has been migrated to secure storage (don't remove this) + await this.setAccessTokenMigratedToSecureStorage(userId); + + return; + case TokenStorageLocation.Disk: + await this.singleUserStateProvider + .get(userId, ACCESS_TOKEN_DISK) + .update((_) => accessToken); + return; + case TokenStorageLocation.Memory: + await this.singleUserStateProvider + .get(userId, ACCESS_TOKEN_MEMORY) + .update((_) => accessToken); + return; + } } - async getClientId(): Promise { - return await this.stateService.getApiKeyClientId(); + async setAccessToken( + accessToken: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + ): Promise { + if (!accessToken) { + throw new Error("Access token is required."); + } + const userId: UserId = await this.getUserIdFromAccessToken(accessToken); + + // If we don't have a user id, we can't save the value + if (!userId) { + throw new Error("User id not found. Cannot save access token."); + } + + await this._setAccessToken(accessToken, vaultTimeoutAction, vaultTimeout, userId); } - async setClientSecret(clientSecret: string): Promise { - return await this.stateService.setApiKeyClientSecret(clientSecret); + async clearAccessToken(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + // If we don't have a user id, we can't clear the value + if (!userId) { + throw new Error("User id not found. Cannot clear access token."); + } + + // TODO: re-eval this once we get shared key definitions for vault timeout and vault timeout action data. + // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout + // but we can simply clear all locations to avoid the need to require those parameters + + if (this.platformSupportsSecureStorage) { + await this.secureStorageService.remove( + `${userId}${this.accessTokenSecureStorageKey}`, + this.getSecureStorageOptions(userId), + ); + } + + // Platform doesn't support secure storage, so use state provider implementation + await this.singleUserStateProvider.get(userId, ACCESS_TOKEN_DISK).update((_) => null); + await this.singleUserStateProvider.get(userId, ACCESS_TOKEN_MEMORY).update((_) => null); } - async getClientSecret(): Promise { - return await this.stateService.getApiKeyClientSecret(); + async getAccessToken(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + return undefined; + } + + const accessTokenMigratedToSecureStorage = + await this.getAccessTokenMigratedToSecureStorage(userId); + if (this.platformSupportsSecureStorage && accessTokenMigratedToSecureStorage) { + return await this.getStringFromSecureStorage(userId, this.accessTokenSecureStorageKey); + } + + // Try to get the access token from memory + const accessTokenMemory = await this.getStateValueByUserIdAndKeyDef( + userId, + ACCESS_TOKEN_MEMORY, + ); + + if (accessTokenMemory != null) { + return accessTokenMemory; + } + + // If memory is null, read from disk + return await this.getStateValueByUserIdAndKeyDef(userId, ACCESS_TOKEN_DISK); } - async setToken(token: string): Promise { - await this.stateService.setAccessToken(token); + private async getAccessTokenMigratedToSecureStorage(userId: UserId): Promise { + return await firstValueFrom( + this.singleUserStateProvider.get(userId, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE).state$, + ); } - async getToken(): Promise { - return await this.stateService.getAccessToken(); + private async setAccessTokenMigratedToSecureStorage(userId: UserId): Promise { + await this.singleUserStateProvider + .get(userId, ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .update((_) => true); } - async setRefreshToken(refreshToken: string): Promise { - return await this.stateService.setRefreshToken(refreshToken); + // Private because we only ever set the refresh token when also setting the access token + // and we need the user id from the access token to save to secure storage + private async setRefreshToken( + refreshToken: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId: UserId, + ): Promise { + // If we don't have a user id, we can't save the value + if (!userId) { + throw new Error("User id not found. Cannot save refresh token."); + } + + const storageLocation = await this.determineStorageLocation( + vaultTimeoutAction, + vaultTimeout, + true, + ); + + switch (storageLocation) { + case TokenStorageLocation.SecureStorage: + await this.saveStringToSecureStorage( + userId, + this.refreshTokenSecureStorageKey, + refreshToken, + ); + + // TODO: PM-6408 - https://bitwarden.atlassian.net/browse/PM-6408 + // 2024-02-20: Remove refresh token from memory and disk so that we migrate to secure storage over time. + // Remove these 2 calls to remove the refresh token from memory and disk after 3 releases. + await this.singleUserStateProvider.get(userId, REFRESH_TOKEN_DISK).update((_) => null); + await this.singleUserStateProvider.get(userId, REFRESH_TOKEN_MEMORY).update((_) => null); + + // Set flag to indicate that the refresh token has been migrated to secure storage (don't remove this) + await this.setRefreshTokenMigratedToSecureStorage(userId); + + return; + + case TokenStorageLocation.Disk: + await this.singleUserStateProvider + .get(userId, REFRESH_TOKEN_DISK) + .update((_) => refreshToken); + return; + + case TokenStorageLocation.Memory: + await this.singleUserStateProvider + .get(userId, REFRESH_TOKEN_MEMORY) + .update((_) => refreshToken); + return; + } } - async getRefreshToken(): Promise { - return await this.stateService.getRefreshToken(); + async getRefreshToken(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + return undefined; + } + + const refreshTokenMigratedToSecureStorage = + await this.getRefreshTokenMigratedToSecureStorage(userId); + if (this.platformSupportsSecureStorage && refreshTokenMigratedToSecureStorage) { + return await this.getStringFromSecureStorage(userId, this.refreshTokenSecureStorageKey); + } + + // pre-secure storage migration: + // Always read memory first b/c faster + const refreshTokenMemory = await this.getStateValueByUserIdAndKeyDef( + userId, + REFRESH_TOKEN_MEMORY, + ); + + if (refreshTokenMemory != null) { + return refreshTokenMemory; + } + + // if memory is null, read from disk + const refreshTokenDisk = await this.getStateValueByUserIdAndKeyDef(userId, REFRESH_TOKEN_DISK); + + if (refreshTokenDisk != null) { + return refreshTokenDisk; + } + + return null; } - async setTwoFactorToken(tokenResponse: IdentityTokenResponse): Promise { - return await this.stateService.setTwoFactorToken(tokenResponse.twoFactorToken); + private async clearRefreshToken(userId: UserId): Promise { + // If we don't have a user id, we can't clear the value + if (!userId) { + throw new Error("User id not found. Cannot clear refresh token."); + } + + // TODO: re-eval this once we get shared key definitions for vault timeout and vault timeout action data. + // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout + // but we can simply clear all locations to avoid the need to require those parameters + + if (this.platformSupportsSecureStorage) { + await this.secureStorageService.remove( + `${userId}${this.refreshTokenSecureStorageKey}`, + this.getSecureStorageOptions(userId), + ); + } + + // Platform doesn't support secure storage, so use state provider implementation + await this.singleUserStateProvider.get(userId, REFRESH_TOKEN_MEMORY).update((_) => null); + await this.singleUserStateProvider.get(userId, REFRESH_TOKEN_DISK).update((_) => null); } - async getTwoFactorToken(): Promise { - return await this.stateService.getTwoFactorToken(); + private async getRefreshTokenMigratedToSecureStorage(userId: UserId): Promise { + return await firstValueFrom( + this.singleUserStateProvider.get(userId, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE).state$, + ); } - async clearTwoFactorToken(): Promise { - return await this.stateService.setTwoFactorToken(null); + private async setRefreshTokenMigratedToSecureStorage(userId: UserId): Promise { + await this.singleUserStateProvider + .get(userId, REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE) + .update((_) => true); } - async clearToken(userId?: string): Promise { - await this.stateService.setAccessToken(null, { userId: userId }); - await this.stateService.setRefreshToken(null, { userId: userId }); - await this.stateService.setApiKeyClientId(null, { userId: userId }); - await this.stateService.setApiKeyClientSecret(null, { userId: userId }); + async setClientId( + clientId: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId?: UserId, + ): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + // If we don't have a user id, we can't save the value + if (!userId) { + throw new Error("User id not found. Cannot save client id."); + } + + const storageLocation = await this.determineStorageLocation( + vaultTimeoutAction, + vaultTimeout, + false, + ); + + if (storageLocation === TokenStorageLocation.Disk) { + await this.singleUserStateProvider + .get(userId, API_KEY_CLIENT_ID_DISK) + .update((_) => clientId); + } else if (storageLocation === TokenStorageLocation.Memory) { + await this.singleUserStateProvider + .get(userId, API_KEY_CLIENT_ID_MEMORY) + .update((_) => clientId); + } + } + + async getClientId(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + return undefined; + } + + // Always read memory first b/c faster + const apiKeyClientIdMemory = await this.getStateValueByUserIdAndKeyDef( + userId, + API_KEY_CLIENT_ID_MEMORY, + ); + + if (apiKeyClientIdMemory != null) { + return apiKeyClientIdMemory; + } + + // if memory is null, read from disk + return await this.getStateValueByUserIdAndKeyDef(userId, API_KEY_CLIENT_ID_DISK); + } + + private async clearClientId(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + // If we don't have a user id, we can't clear the value + if (!userId) { + throw new Error("User id not found. Cannot clear client id."); + } + + // TODO: re-eval this once we get shared key definitions for vault timeout and vault timeout action data. + // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout + // but we can simply clear both locations to avoid the need to require those parameters + + // Platform doesn't support secure storage, so use state provider implementation + await this.singleUserStateProvider.get(userId, API_KEY_CLIENT_ID_MEMORY).update((_) => null); + await this.singleUserStateProvider.get(userId, API_KEY_CLIENT_ID_DISK).update((_) => null); + } + + async setClientSecret( + clientSecret: string, + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + userId?: UserId, + ): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + throw new Error("User id not found. Cannot save client secret."); + } + + const storageLocation = await this.determineStorageLocation( + vaultTimeoutAction, + vaultTimeout, + false, + ); + + if (storageLocation === TokenStorageLocation.Disk) { + await this.singleUserStateProvider + .get(userId, API_KEY_CLIENT_SECRET_DISK) + .update((_) => clientSecret); + } else if (storageLocation === TokenStorageLocation.Memory) { + await this.singleUserStateProvider + .get(userId, API_KEY_CLIENT_SECRET_MEMORY) + .update((_) => clientSecret); + } + } + + async getClientSecret(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + return undefined; + } + + // Always read memory first b/c faster + const apiKeyClientSecretMemory = await this.getStateValueByUserIdAndKeyDef( + userId, + API_KEY_CLIENT_SECRET_MEMORY, + ); + + if (apiKeyClientSecretMemory != null) { + return apiKeyClientSecretMemory; + } + + // if memory is null, read from disk + return await this.getStateValueByUserIdAndKeyDef(userId, API_KEY_CLIENT_SECRET_DISK); + } + + private async clearClientSecret(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + // If we don't have a user id, we can't clear the value + if (!userId) { + throw new Error("User id not found. Cannot clear client secret."); + } + + // TODO: re-eval this once we get shared key definitions for vault timeout and vault timeout action data. + // we can't determine storage location w/out vaultTimeoutAction and vaultTimeout + // but we can simply clear both locations to avoid the need to require those parameters + + // Platform doesn't support secure storage, so use state provider implementation + await this.singleUserStateProvider + .get(userId, API_KEY_CLIENT_SECRET_MEMORY) + .update((_) => null); + await this.singleUserStateProvider.get(userId, API_KEY_CLIENT_SECRET_DISK).update((_) => null); + } + + async setTwoFactorToken(email: string, twoFactorToken: string): Promise { + await this.emailTwoFactorTokenRecordGlobalState.update((emailTwoFactorTokenRecord) => { + emailTwoFactorTokenRecord ??= {}; + + emailTwoFactorTokenRecord[email] = twoFactorToken; + return emailTwoFactorTokenRecord; + }); + } + + async getTwoFactorToken(email: string): Promise { + const emailTwoFactorTokenRecord: Record = await firstValueFrom( + this.emailTwoFactorTokenRecordGlobalState.state$, + ); + + if (!emailTwoFactorTokenRecord) { + return null; + } + + return emailTwoFactorTokenRecord[email]; + } + + async clearTwoFactorToken(email: string): Promise { + await this.emailTwoFactorTokenRecordGlobalState.update((emailTwoFactorTokenRecord) => { + emailTwoFactorTokenRecord ??= {}; + delete emailTwoFactorTokenRecord[email]; + return emailTwoFactorTokenRecord; + }); + } + + async clearTokens(userId?: UserId): Promise { + userId ??= await firstValueFrom(this.activeUserIdGlobalState.state$); + + if (!userId) { + throw new Error("User id not found. Cannot clear tokens."); + } + + await Promise.all([ + this.clearAccessToken(userId), + this.clearRefreshToken(userId), + this.clearClientId(userId), + this.clearClientSecret(userId), + ]); } // jwthelper methods // ref https://github.com/auth0/angular-jwt/blob/master/src/angularJwt/services/jwt.js - async decodeToken(token?: string): Promise { - token = token ?? (await this.stateService.getAccessToken()); + async decodeAccessToken(token?: string): Promise { + token = token ?? (await this.getAccessToken()); if (token == null) { - throw new Error("Token not found."); + throw new Error("Access token not found."); } - return TokenService.decodeToken(token); + return decodeJwtTokenToJson(token) as DecodedAccessToken; } - async getTokenExpirationDate(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.exp === "undefined") { + // TODO: PM-6678- tech debt - consider consolidating the return types of all these access + // token data retrieval methods to return null if something goes wrong instead of throwing an error. + + async getTokenExpirationDate(): Promise { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + // per RFC, exp claim is optional but if it exists, it should be a number + if (!decoded || typeof decoded.exp !== "number") { return null; } - const d = new Date(0); // The 0 here is the key, which sets the date to the epoch - d.setUTCSeconds(decoded.exp); - return d; + // The 0 in Date(0) is the key; it sets the date to the epoch + const expirationDate = new Date(0); + expirationDate.setUTCSeconds(decoded.exp); + return expirationDate; } async tokenSecondsRemaining(offsetSeconds = 0): Promise { - const d = await this.getTokenExpirationDate(); - if (d == null) { + const date = await this.getTokenExpirationDate(); + if (date == null) { return 0; } - const msRemaining = d.valueOf() - (new Date().valueOf() + offsetSeconds * 1000); + const msRemaining = date.valueOf() - (new Date().valueOf() + offsetSeconds * 1000); return Math.round(msRemaining / 1000); } @@ -128,54 +632,159 @@ export class TokenService implements TokenServiceAbstraction { return sRemaining < 60 * minutes; } - async getUserId(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.sub === "undefined") { + async getUserId(): Promise { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.sub !== "string") { throw new Error("No user id found"); } - return decoded.sub as string; + return decoded.sub as UserId; + } + + private async getUserIdFromAccessToken(accessToken: string): Promise { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(accessToken); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.sub !== "string") { + throw new Error("No user id found"); + } + + return decoded.sub as UserId; } async getEmail(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.email === "undefined") { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.email !== "string") { throw new Error("No email found"); } - return decoded.email as string; + return decoded.email; } async getEmailVerified(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.email_verified === "undefined") { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.email_verified !== "boolean") { throw new Error("No email verification found"); } - return decoded.email_verified as boolean; + return decoded.email_verified; } async getName(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.name === "undefined") { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.name !== "string") { return null; } - return decoded.name as string; + return decoded.name; } async getIssuer(): Promise { - const decoded = await this.decodeToken(); - if (typeof decoded.iss === "undefined") { + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } + + if (!decoded || typeof decoded.iss !== "string") { throw new Error("No issuer found"); } - return decoded.iss as string; + return decoded.iss; } async getIsExternal(): Promise { - const decoded = await this.decodeToken(); + let decoded: DecodedAccessToken; + try { + decoded = await this.decodeAccessToken(); + } catch (error) { + throw new Error("Failed to decode access token: " + error.message); + } return Array.isArray(decoded.amr) && decoded.amr.includes("external"); } + + private async getStateValueByUserIdAndKeyDef( + userId: UserId, + storageLocation: KeyDefinition, + ): Promise { + // read from single user state provider + return await firstValueFrom(this.singleUserStateProvider.get(userId, storageLocation).state$); + } + + private async determineStorageLocation( + vaultTimeoutAction: VaultTimeoutAction, + vaultTimeout: number | null, + useSecureStorage: boolean, + ): Promise { + if (vaultTimeoutAction === VaultTimeoutAction.LogOut && vaultTimeout != null) { + return TokenStorageLocation.Memory; + } else { + if (useSecureStorage && this.platformSupportsSecureStorage) { + return TokenStorageLocation.SecureStorage; + } + + return TokenStorageLocation.Disk; + } + } + + private async saveStringToSecureStorage( + userId: UserId, + storageKey: string, + value: string, + ): Promise { + await this.secureStorageService.save( + `${userId}${storageKey}`, + value, + this.getSecureStorageOptions(userId), + ); + } + + private async getStringFromSecureStorage( + userId: UserId, + storageKey: string, + ): Promise { + // If we have a user ID, read from secure storage. + return await this.secureStorageService.get( + `${userId}${storageKey}`, + this.getSecureStorageOptions(userId), + ); + } + + private getSecureStorageOptions(userId: UserId): StorageOptions { + return { + storageLocation: StorageLocation.Disk, + useSecureStorage: true, + userId: userId, + }; + } } diff --git a/libs/common/src/auth/services/token.state.spec.ts b/libs/common/src/auth/services/token.state.spec.ts new file mode 100644 index 00000000000..f4089a73fb4 --- /dev/null +++ b/libs/common/src/auth/services/token.state.spec.ts @@ -0,0 +1,64 @@ +import { KeyDefinition } from "../../platform/state"; + +import { + ACCESS_TOKEN_DISK, + ACCESS_TOKEN_MEMORY, + ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE, + API_KEY_CLIENT_ID_DISK, + API_KEY_CLIENT_ID_MEMORY, + API_KEY_CLIENT_SECRET_DISK, + API_KEY_CLIENT_SECRET_MEMORY, + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + REFRESH_TOKEN_DISK, + REFRESH_TOKEN_MEMORY, + REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE, +} from "./token.state"; + +describe.each([ + [ACCESS_TOKEN_DISK, "accessTokenDisk"], + [ACCESS_TOKEN_MEMORY, "accessTokenMemory"], + [ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE, true], + [REFRESH_TOKEN_DISK, "refreshTokenDisk"], + [REFRESH_TOKEN_MEMORY, "refreshTokenMemory"], + [REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE, true], + [EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, { user: "token" }], + [API_KEY_CLIENT_ID_DISK, "apiKeyClientIdDisk"], + [API_KEY_CLIENT_ID_MEMORY, "apiKeyClientIdMemory"], + [API_KEY_CLIENT_SECRET_DISK, "apiKeyClientSecretDisk"], + [API_KEY_CLIENT_SECRET_MEMORY, "apiKeyClientSecretMemory"], +])( + "deserializes state key definitions", + ( + keyDefinition: + | KeyDefinition + | KeyDefinition + | KeyDefinition>, + state: string | boolean | Record, + ) => { + function getTypeDescription(value: any): string { + if (isRecord(value)) { + return "Record"; + } else if (Array.isArray(value)) { + return "array"; + } else if (value === null) { + return "null"; + } + + // Fallback for primitive types + return typeof value; + } + + function isRecord(value: any): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); + } + + function testDeserialization(keyDefinition: KeyDefinition, state: T) { + const deserialized = keyDefinition.deserializer(JSON.parse(JSON.stringify(state))); + expect(deserialized).toEqual(state); + } + + it(`should deserialize state for KeyDefinition<${getTypeDescription(state)}>: "${keyDefinition.key}"`, () => { + testDeserialization(keyDefinition, state); + }); + }, +); diff --git a/libs/common/src/auth/services/token.state.ts b/libs/common/src/auth/services/token.state.ts new file mode 100644 index 00000000000..022f56f7aa5 --- /dev/null +++ b/libs/common/src/auth/services/token.state.ts @@ -0,0 +1,65 @@ +import { KeyDefinition, TOKEN_DISK, TOKEN_DISK_LOCAL, TOKEN_MEMORY } from "../../platform/state"; + +export const ACCESS_TOKEN_DISK = new KeyDefinition(TOKEN_DISK, "accessToken", { + deserializer: (accessToken) => accessToken, +}); + +export const ACCESS_TOKEN_MEMORY = new KeyDefinition(TOKEN_MEMORY, "accessToken", { + deserializer: (accessToken) => accessToken, +}); + +export const ACCESS_TOKEN_MIGRATED_TO_SECURE_STORAGE = new KeyDefinition( + TOKEN_DISK, + "accessTokenMigratedToSecureStorage", + { + deserializer: (accessTokenMigratedToSecureStorage) => accessTokenMigratedToSecureStorage, + }, +); + +export const REFRESH_TOKEN_DISK = new KeyDefinition(TOKEN_DISK, "refreshToken", { + deserializer: (refreshToken) => refreshToken, +}); + +export const REFRESH_TOKEN_MEMORY = new KeyDefinition(TOKEN_MEMORY, "refreshToken", { + deserializer: (refreshToken) => refreshToken, +}); + +export const REFRESH_TOKEN_MIGRATED_TO_SECURE_STORAGE = new KeyDefinition( + TOKEN_DISK, + "refreshTokenMigratedToSecureStorage", + { + deserializer: (refreshTokenMigratedToSecureStorage) => refreshTokenMigratedToSecureStorage, + }, +); + +export const EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL = KeyDefinition.record( + TOKEN_DISK_LOCAL, + "emailTwoFactorTokenRecord", + { + deserializer: (emailTwoFactorTokenRecord) => emailTwoFactorTokenRecord, + }, +); + +export const API_KEY_CLIENT_ID_DISK = new KeyDefinition(TOKEN_DISK, "apiKeyClientId", { + deserializer: (apiKeyClientId) => apiKeyClientId, +}); + +export const API_KEY_CLIENT_ID_MEMORY = new KeyDefinition(TOKEN_MEMORY, "apiKeyClientId", { + deserializer: (apiKeyClientId) => apiKeyClientId, +}); + +export const API_KEY_CLIENT_SECRET_DISK = new KeyDefinition( + TOKEN_DISK, + "apiKeyClientSecret", + { + deserializer: (apiKeyClientSecret) => apiKeyClientSecret, + }, +); + +export const API_KEY_CLIENT_SECRET_MEMORY = new KeyDefinition( + TOKEN_MEMORY, + "apiKeyClientSecret", + { + deserializer: (apiKeyClientSecret) => apiKeyClientSecret, + }, +); diff --git a/libs/common/src/autofill/services/domain-settings.service.ts b/libs/common/src/autofill/services/domain-settings.service.ts index 3131b9c50b5..6ef4d10c0a5 100644 --- a/libs/common/src/autofill/services/domain-settings.service.ts +++ b/libs/common/src/autofill/services/domain-settings.service.ts @@ -16,6 +16,10 @@ import { UserKeyDefinition, } from "../../platform/state"; +const SHOW_FAVICONS = new KeyDefinition(DOMAIN_SETTINGS_DISK, "showFavicons", { + deserializer: (value: boolean) => value ?? true, +}); + const NEVER_DOMAINS = new KeyDefinition(DOMAIN_SETTINGS_DISK, "neverDomains", { deserializer: (value: NeverDomains) => value ?? null, }); @@ -34,6 +38,8 @@ const DEFAULT_URI_MATCH_STRATEGY = new KeyDefinition( ); export abstract class DomainSettingsService { + showFavicons$: Observable; + setShowFavicons: (newValue: boolean) => Promise; neverDomains$: Observable; setNeverDomains: (newValue: NeverDomains) => Promise; equivalentDomains$: Observable; @@ -44,6 +50,9 @@ export abstract class DomainSettingsService { } export class DefaultDomainSettingsService implements DomainSettingsService { + private showFaviconsState: GlobalState; + readonly showFavicons$: Observable; + private neverDomainsState: GlobalState; readonly neverDomains$: Observable; @@ -54,6 +63,9 @@ export class DefaultDomainSettingsService implements DomainSettingsService { readonly defaultUriMatchStrategy$: Observable; constructor(private stateProvider: StateProvider) { + this.showFaviconsState = this.stateProvider.getGlobal(SHOW_FAVICONS); + this.showFavicons$ = this.showFaviconsState.state$.pipe(map((x) => x ?? true)); + this.neverDomainsState = this.stateProvider.getGlobal(NEVER_DOMAINS); this.neverDomains$ = this.neverDomainsState.state$.pipe(map((x) => x ?? null)); @@ -66,6 +78,10 @@ export class DefaultDomainSettingsService implements DomainSettingsService { ); } + async setShowFavicons(newValue: boolean): Promise { + await this.showFaviconsState.update(() => newValue); + } + async setNeverDomains(newValue: NeverDomains): Promise { await this.neverDomainsState.update(() => newValue); } diff --git a/libs/common/src/billing/abstractions/account/billing-account-profile-state.service.ts b/libs/common/src/billing/abstractions/account/billing-account-profile-state.service.ts new file mode 100644 index 00000000000..e07dec3cf90 --- /dev/null +++ b/libs/common/src/billing/abstractions/account/billing-account-profile-state.service.ts @@ -0,0 +1,36 @@ +import { Observable } from "rxjs"; + +export type BillingAccountProfile = { + hasPremiumPersonally: boolean; + hasPremiumFromAnyOrganization: boolean; +}; + +export abstract class BillingAccountProfileStateService { + /** + * Emits `true` when the active user's account has been granted premium from any of the + * organizations it is a member of. Otherwise, emits `false` + */ + hasPremiumFromAnyOrganization$: Observable; + + /** + * Emits `true` when the active user's account has an active premium subscription at the + * individual user level + */ + hasPremiumPersonally$: Observable; + + /** + * Emits `true` when either `hasPremiumPersonally` or `hasPremiumFromAnyOrganization` is `true` + */ + hasPremiumFromAnySource$: Observable; + + /** + * Sets the active user's premium status fields upon every full sync, either from their personal + * subscription to premium, or an organization they're a part of that grants them premium. + * @param hasPremiumPersonally + * @param hasPremiumFromAnyOrganization + */ + abstract setHasPremium( + hasPremiumPersonally: boolean, + hasPremiumFromAnyOrganization: boolean, + ): Promise; +} diff --git a/libs/common/src/billing/services/account/billing-account-profile-state.service.spec.ts b/libs/common/src/billing/services/account/billing-account-profile-state.service.spec.ts new file mode 100644 index 00000000000..4a2a94e9c60 --- /dev/null +++ b/libs/common/src/billing/services/account/billing-account-profile-state.service.spec.ts @@ -0,0 +1,165 @@ +import { firstValueFrom } from "rxjs"; + +import { + FakeAccountService, + FakeActiveUserStateProvider, + mockAccountServiceWith, + FakeActiveUserState, + trackEmissions, +} from "../../../../spec"; +import { UserId } from "../../../types/guid"; +import { BillingAccountProfile } from "../../abstractions/account/billing-account-profile-state.service"; + +import { + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + DefaultBillingAccountProfileStateService, +} from "./billing-account-profile-state.service"; + +describe("BillingAccountProfileStateService", () => { + let activeUserStateProvider: FakeActiveUserStateProvider; + let sut: DefaultBillingAccountProfileStateService; + let billingAccountProfileState: FakeActiveUserState; + let accountService: FakeAccountService; + + const userId = "fakeUserId" as UserId; + + beforeEach(() => { + accountService = mockAccountServiceWith(userId); + activeUserStateProvider = new FakeActiveUserStateProvider(accountService); + + sut = new DefaultBillingAccountProfileStateService(activeUserStateProvider); + + billingAccountProfileState = activeUserStateProvider.getFake( + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + ); + }); + + afterEach(() => { + return jest.resetAllMocks(); + }); + + describe("accountHasPremiumFromAnyOrganization$", () => { + it("should emit changes in hasPremiumFromAnyOrganization", async () => { + billingAccountProfileState.nextState({ + hasPremiumPersonally: false, + hasPremiumFromAnyOrganization: true, + }); + + expect(await firstValueFrom(sut.hasPremiumFromAnyOrganization$)).toBe(true); + }); + + it("should emit once when calling setHasPremium once", async () => { + const emissions = trackEmissions(sut.hasPremiumFromAnyOrganization$); + const startingEmissionCount = emissions.length; + + await sut.setHasPremium(true, true); + + const endingEmissionCount = emissions.length; + expect(endingEmissionCount - startingEmissionCount).toBe(1); + }); + }); + + describe("hasPremiumPersonally$", () => { + it("should emit changes in hasPremiumPersonally", async () => { + billingAccountProfileState.nextState({ + hasPremiumPersonally: true, + hasPremiumFromAnyOrganization: false, + }); + + expect(await firstValueFrom(sut.hasPremiumPersonally$)).toBe(true); + }); + + it("should emit once when calling setHasPremium once", async () => { + const emissions = trackEmissions(sut.hasPremiumPersonally$); + const startingEmissionCount = emissions.length; + + await sut.setHasPremium(true, true); + + const endingEmissionCount = emissions.length; + expect(endingEmissionCount - startingEmissionCount).toBe(1); + }); + }); + + describe("canAccessPremium$", () => { + it("should emit changes in hasPremiumPersonally", async () => { + billingAccountProfileState.nextState({ + hasPremiumPersonally: true, + hasPremiumFromAnyOrganization: false, + }); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(true); + }); + + it("should emit changes in hasPremiumFromAnyOrganization", async () => { + billingAccountProfileState.nextState({ + hasPremiumPersonally: false, + hasPremiumFromAnyOrganization: true, + }); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(true); + }); + + it("should emit changes in both hasPremiumPersonally and hasPremiumFromAnyOrganization", async () => { + billingAccountProfileState.nextState({ + hasPremiumPersonally: true, + hasPremiumFromAnyOrganization: true, + }); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(true); + }); + + it("should emit once when calling setHasPremium once", async () => { + const emissions = trackEmissions(sut.hasPremiumFromAnySource$); + const startingEmissionCount = emissions.length; + + await sut.setHasPremium(true, true); + + const endingEmissionCount = emissions.length; + expect(endingEmissionCount - startingEmissionCount).toBe(1); + }); + }); + + describe("setHasPremium", () => { + it("should have `hasPremiumPersonally$` emit `true` when passing `true` as an argument for hasPremiumPersonally", async () => { + await sut.setHasPremium(true, false); + + expect(await firstValueFrom(sut.hasPremiumPersonally$)).toBe(true); + }); + + it("should have `hasPremiumFromAnyOrganization$` emit `true` when passing `true` as an argument for hasPremiumFromAnyOrganization", async () => { + await sut.setHasPremium(false, true); + + expect(await firstValueFrom(sut.hasPremiumFromAnyOrganization$)).toBe(true); + }); + + it("should have `hasPremiumPersonally$` emit `false` when passing `false` as an argument for hasPremiumPersonally", async () => { + await sut.setHasPremium(false, false); + + expect(await firstValueFrom(sut.hasPremiumPersonally$)).toBe(false); + }); + + it("should have `hasPremiumFromAnyOrganization$` emit `false` when passing `false` as an argument for hasPremiumFromAnyOrganization", async () => { + await sut.setHasPremium(false, false); + + expect(await firstValueFrom(sut.hasPremiumFromAnyOrganization$)).toBe(false); + }); + + it("should have `canAccessPremium$` emit `true` when passing `true` as an argument for hasPremiumPersonally", async () => { + await sut.setHasPremium(true, false); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(true); + }); + + it("should have `canAccessPremium$` emit `true` when passing `true` as an argument for hasPremiumFromAnyOrganization", async () => { + await sut.setHasPremium(false, true); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(true); + }); + + it("should have `canAccessPremium$` emit `false` when passing `false` for all arguments", async () => { + await sut.setHasPremium(false, false); + + expect(await firstValueFrom(sut.hasPremiumFromAnySource$)).toBe(false); + }); + }); +}); diff --git a/libs/common/src/billing/services/account/billing-account-profile-state.service.ts b/libs/common/src/billing/services/account/billing-account-profile-state.service.ts new file mode 100644 index 00000000000..c6b6f104a8e --- /dev/null +++ b/libs/common/src/billing/services/account/billing-account-profile-state.service.ts @@ -0,0 +1,62 @@ +import { map, Observable } from "rxjs"; + +import { + ActiveUserState, + ActiveUserStateProvider, + BILLING_DISK, + KeyDefinition, +} from "../../../platform/state"; +import { + BillingAccountProfile, + BillingAccountProfileStateService, +} from "../../abstractions/account/billing-account-profile-state.service"; + +export const BILLING_ACCOUNT_PROFILE_KEY_DEFINITION = new KeyDefinition( + BILLING_DISK, + "accountProfile", + { + deserializer: (billingAccountProfile) => billingAccountProfile, + }, +); + +export class DefaultBillingAccountProfileStateService implements BillingAccountProfileStateService { + private billingAccountProfileState: ActiveUserState; + + hasPremiumFromAnyOrganization$: Observable; + hasPremiumPersonally$: Observable; + hasPremiumFromAnySource$: Observable; + + constructor(activeUserStateProvider: ActiveUserStateProvider) { + this.billingAccountProfileState = activeUserStateProvider.get( + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + ); + + this.hasPremiumFromAnyOrganization$ = this.billingAccountProfileState.state$.pipe( + map((billingAccountProfile) => !!billingAccountProfile?.hasPremiumFromAnyOrganization), + ); + + this.hasPremiumPersonally$ = this.billingAccountProfileState.state$.pipe( + map((billingAccountProfile) => !!billingAccountProfile?.hasPremiumPersonally), + ); + + this.hasPremiumFromAnySource$ = this.billingAccountProfileState.state$.pipe( + map( + (billingAccountProfile) => + billingAccountProfile?.hasPremiumFromAnyOrganization || + billingAccountProfile?.hasPremiumPersonally, + ), + ); + } + + async setHasPremium( + hasPremiumPersonally: boolean, + hasPremiumFromAnyOrganization: boolean, + ): Promise { + await this.billingAccountProfileState.update((billingAccountProfile) => { + return { + hasPremiumPersonally: hasPremiumPersonally, + hasPremiumFromAnyOrganization: hasPremiumFromAnyOrganization, + }; + }); + } +} diff --git a/libs/common/src/models/data/event.data.ts b/libs/common/src/models/data/event.data.ts index 2281f0258f7..e261e5fd3a1 100644 --- a/libs/common/src/models/data/event.data.ts +++ b/libs/common/src/models/data/event.data.ts @@ -1,3 +1,5 @@ +import { Jsonify } from "type-fest"; + import { EventType } from "../../enums"; export class EventData { @@ -5,4 +7,8 @@ export class EventData { cipherId: string; date: string; organizationId: string; + + static fromJSON(obj: Jsonify): EventData { + return Object.assign(new EventData(), obj); + } } diff --git a/libs/common/src/platform/abstractions/state.service.ts b/libs/common/src/platform/abstractions/state.service.ts index 3fc65e4acf1..3413afe1825 100644 --- a/libs/common/src/platform/abstractions/state.service.ts +++ b/libs/common/src/platform/abstractions/state.service.ts @@ -1,11 +1,9 @@ import { Observable } from "rxjs"; -import { OrganizationData } from "../../admin-console/models/data/organization.data"; import { AdminAuthRequestStorable } from "../../auth/models/domain/admin-auth-req-storable"; import { ForceSetPasswordReason } from "../../auth/models/domain/force-set-password-reason"; import { KdfConfig } from "../../auth/models/domain/kdf-config"; import { BiometricKey } from "../../auth/types/biometric-key"; -import { EventData } from "../../models/data/event.data"; import { WindowState } from "../../models/domain/window-state"; import { GeneratorOptions } from "../../tools/generator/generator-options"; import { GeneratedPasswordHistory, PasswordGeneratorOptions } from "../../tools/generator/password"; @@ -52,25 +50,15 @@ export abstract class StateService { clean: (options?: StorageOptions) => Promise; init: (initOptions?: InitOptions) => Promise; - getAccessToken: (options?: StorageOptions) => Promise; - setAccessToken: (value: string, options?: StorageOptions) => Promise; getAddEditCipherInfo: (options?: StorageOptions) => Promise; setAddEditCipherInfo: (value: AddEditCipherInfo, options?: StorageOptions) => Promise; getAlwaysShowDock: (options?: StorageOptions) => Promise; setAlwaysShowDock: (value: boolean, options?: StorageOptions) => Promise; - getApiKeyClientId: (options?: StorageOptions) => Promise; - setApiKeyClientId: (value: string, options?: StorageOptions) => Promise; - getApiKeyClientSecret: (options?: StorageOptions) => Promise; - setApiKeyClientSecret: (value: string, options?: StorageOptions) => Promise; + getAutoConfirmFingerPrints: (options?: StorageOptions) => Promise; setAutoConfirmFingerprints: (value: boolean, options?: StorageOptions) => Promise; getBiometricFingerprintValidated: (options?: StorageOptions) => Promise; setBiometricFingerprintValidated: (value: boolean, options?: StorageOptions) => Promise; - getCanAccessPremium: (options?: StorageOptions) => Promise; - getHasPremiumPersonally: (options?: StorageOptions) => Promise; - setHasPremiumPersonally: (value: boolean, options?: StorageOptions) => Promise; - setHasPremiumFromOrganization: (value: boolean, options?: StorageOptions) => Promise; - getHasPremiumFromOrganization: (options?: StorageOptions) => Promise; getConvertAccountToKeyConnector: (options?: StorageOptions) => Promise; setConvertAccountToKeyConnector: (value: boolean, options?: StorageOptions) => Promise; /** @@ -182,14 +170,6 @@ export abstract class StateService { * @deprecated Do not call this directly, use SendService */ setDecryptedSends: (value: SendView[], options?: StorageOptions) => Promise; - /** - * @deprecated Do not call this, use SettingsService - */ - getDisableFavicon: (options?: StorageOptions) => Promise; - /** - * @deprecated Do not call this, use SettingsService - */ - setDisableFavicon: (value: boolean, options?: StorageOptions) => Promise; getDisableGa: (options?: StorageOptions) => Promise; setDisableGa: (value: boolean, options?: StorageOptions) => Promise; getDuckDuckGoSharedKey: (options?: StorageOptions) => Promise; @@ -264,8 +244,6 @@ export abstract class StateService { * @deprecated Do not call this directly, use SendService */ setEncryptedSends: (value: { [id: string]: SendData }, options?: StorageOptions) => Promise; - getEventCollection: (options?: StorageOptions) => Promise; - setEventCollection: (value: EventData[], options?: StorageOptions) => Promise; getEverBeenUnlocked: (options?: StorageOptions) => Promise; setEverBeenUnlocked: (value: boolean, options?: StorageOptions) => Promise; getForceSetPasswordReason: (options?: StorageOptions) => Promise; @@ -301,17 +279,6 @@ export abstract class StateService { setOpenAtLogin: (value: boolean, options?: StorageOptions) => Promise; getOrganizationInvitation: (options?: StorageOptions) => Promise; setOrganizationInvitation: (value: any, options?: StorageOptions) => Promise; - /** - * @deprecated Do not call this directly, use OrganizationService - */ - getOrganizations: (options?: StorageOptions) => Promise<{ [id: string]: OrganizationData }>; - /** - * @deprecated Do not call this directly, use OrganizationService - */ - setOrganizations: ( - value: { [id: string]: OrganizationData }, - options?: StorageOptions, - ) => Promise; getPasswordGenerationOptions: (options?: StorageOptions) => Promise; setPasswordGenerationOptions: ( value: PasswordGeneratorOptions, @@ -332,14 +299,10 @@ export abstract class StateService { * Sets the user's Pin, encrypted by the user key */ setProtectedPin: (value: string, options?: StorageOptions) => Promise; - getRefreshToken: (options?: StorageOptions) => Promise; - setRefreshToken: (value: string, options?: StorageOptions) => Promise; getRememberedEmail: (options?: StorageOptions) => Promise; setRememberedEmail: (value: string, options?: StorageOptions) => Promise; getSecurityStamp: (options?: StorageOptions) => Promise; setSecurityStamp: (value: string, options?: StorageOptions) => Promise; - getTwoFactorToken: (options?: StorageOptions) => Promise; - setTwoFactorToken: (value: string, options?: StorageOptions) => Promise; getUserId: (options?: StorageOptions) => Promise; getUsesKeyConnector: (options?: StorageOptions) => Promise; setUsesKeyConnector: (value: boolean, options?: StorageOptions) => Promise; diff --git a/libs/common/src/platform/models/domain/account.ts b/libs/common/src/platform/models/domain/account.ts index 0c85307032d..07efb505a5e 100644 --- a/libs/common/src/platform/models/domain/account.ts +++ b/libs/common/src/platform/models/domain/account.ts @@ -1,12 +1,10 @@ import { Jsonify } from "type-fest"; -import { OrganizationData } from "../../../admin-console/models/data/organization.data"; import { AdminAuthRequestStorable } from "../../../auth/models/domain/admin-auth-req-storable"; import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason"; import { KeyConnectorUserDecryptionOption } from "../../../auth/models/domain/user-decryption-options/key-connector-user-decryption-option"; import { TrustedDeviceUserDecryptionOption } from "../../../auth/models/domain/user-decryption-options/trusted-device-user-decryption-option"; import { IdentityTokenResponse } from "../../../auth/models/response/identity-token.response"; -import { EventData } from "../../../models/data/event.data"; import { UriMatchStrategySetting } from "../../../models/domain/domain-service"; import { GeneratorOptions } from "../../../tools/generator/generator-options"; import { @@ -90,8 +88,6 @@ export class AccountData { GeneratedPasswordHistory[] > = new EncryptionPair(); addEditCipherInfo?: AddEditCipherInfo; - eventCollection?: EventData[]; - organizations?: { [id: string]: OrganizationData }; static fromJSON(obj: DeepJsonify): AccountData { if (obj == null) { @@ -112,7 +108,6 @@ export class AccountKeys { masterKeyEncryptedUserKey?: string; deviceKey?: ReturnType; publicKey?: Uint8Array; - apiKeyClientSecret?: string; /** @deprecated July 2023, left for migration purposes*/ cryptoMasterKey?: SymmetricCryptoKey; @@ -167,15 +162,12 @@ export class AccountKeys { } export class AccountProfile { - apiKeyClientId?: string; convertAccountToKeyConnector?: boolean; name?: string; email?: string; emailVerified?: boolean; everBeenUnlocked?: boolean; forceSetPasswordReason?: ForceSetPasswordReason; - hasPremiumPersonally?: boolean; - hasPremiumFromOrganization?: boolean; lastSync?: string; userId?: string; usesKeyConnector?: boolean; @@ -233,8 +225,6 @@ export class AccountSettings { } export class AccountTokens { - accessToken?: string; - refreshToken?: string; securityStamp?: string; static fromJSON(obj: Jsonify): AccountTokens { diff --git a/libs/common/src/platform/models/domain/global-state.ts b/libs/common/src/platform/models/domain/global-state.ts index 0b018aa36b5..b27bac3bd42 100644 --- a/libs/common/src/platform/models/domain/global-state.ts +++ b/libs/common/src/platform/models/domain/global-state.ts @@ -10,7 +10,6 @@ export class GlobalState { theme?: ThemeType = ThemeType.System; window?: WindowState = new WindowState(); twoFactorToken?: string; - disableFavicon?: boolean; biometricFingerprintValidated?: boolean; vaultTimeout?: number; vaultTimeoutAction?: string; diff --git a/libs/common/src/platform/services/state.service.ts b/libs/common/src/platform/services/state.service.ts index 08c5350d06d..31d69e868bf 100644 --- a/libs/common/src/platform/services/state.service.ts +++ b/libs/common/src/platform/services/state.service.ts @@ -1,15 +1,13 @@ import { BehaviorSubject, Observable, map } from "rxjs"; import { Jsonify, JsonValue } from "type-fest"; -import { OrganizationData } from "../../admin-console/models/data/organization.data"; import { AccountService } from "../../auth/abstractions/account.service"; +import { TokenService } from "../../auth/abstractions/token.service"; import { AuthenticationStatus } from "../../auth/enums/authentication-status"; import { AdminAuthRequestStorable } from "../../auth/models/domain/admin-auth-req-storable"; import { ForceSetPasswordReason } from "../../auth/models/domain/force-set-password-reason"; import { KdfConfig } from "../../auth/models/domain/kdf-config"; import { BiometricKey } from "../../auth/types/biometric-key"; -import { VaultTimeoutAction } from "../../enums/vault-timeout-action.enum"; -import { EventData } from "../../models/data/event.data"; import { WindowState } from "../../models/domain/window-state"; import { GeneratorOptions } from "../../tools/generator/generator-options"; import { GeneratedPasswordHistory, PasswordGeneratorOptions } from "../../tools/generator/password"; @@ -100,6 +98,7 @@ export class StateService< protected stateFactory: StateFactory, protected accountService: AccountService, protected environmentService: EnvironmentService, + protected tokenService: TokenService, private migrationRunner: MigrationRunner, protected useAccountCache: boolean = true, ) { @@ -190,7 +189,7 @@ export class StateService< // TODO: Temporary update to avoid routing all account status changes through account service for now. // The determination of state should be handled by the various services that control those values. - const token = await this.getAccessToken({ userId: userId }); + const token = await this.tokenService.getAccessToken(userId as UserId); const autoKey = await this.getUserKeyAutoUnlock({ userId: userId }); const accountStatus = token == null @@ -255,18 +254,6 @@ export class StateService< return currentUser as UserId; } - async getAccessToken(options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - return (await this.getAccount(options))?.tokens?.accessToken; - } - - async setAccessToken(value: string, options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - const account = await this.getAccount(options); - account.tokens.accessToken = value; - await this.saveAccount(account, options); - } - async getAddEditCipherInfo(options?: StorageOptions): Promise { const account = await this.getAccount( this.reconcileOptions(options, await this.defaultInMemoryOptions()), @@ -313,30 +300,6 @@ export class StateService< ); } - async getApiKeyClientId(options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - return (await this.getAccount(options))?.profile?.apiKeyClientId; - } - - async setApiKeyClientId(value: string, options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - const account = await this.getAccount(options); - account.profile.apiKeyClientId = value; - await this.saveAccount(account, options); - } - - async getApiKeyClientSecret(options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - return (await this.getAccount(options))?.keys?.apiKeyClientSecret; - } - - async setApiKeyClientSecret(value: string, options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - const account = await this.getAccount(options); - account.keys.apiKeyClientSecret = value; - await this.saveAccount(account, options); - } - async getAutoConfirmFingerPrints(options?: StorageOptions): Promise { return ( (await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))) @@ -373,72 +336,6 @@ export class StateService< ); } - async getCanAccessPremium(options?: StorageOptions): Promise { - if (!(await this.getIsAuthenticated(options))) { - return false; - } - - return ( - (await this.getHasPremiumPersonally(options)) || - (await this.getHasPremiumFromOrganization(options)) - ); - } - - async getHasPremiumPersonally(options?: StorageOptions): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - return account?.profile?.hasPremiumPersonally; - } - - async setHasPremiumPersonally(value: boolean, options?: StorageOptions): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - account.profile.hasPremiumPersonally = value; - await this.saveAccount( - account, - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - } - - async getHasPremiumFromOrganization(options?: StorageOptions): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - - if (account.profile?.hasPremiumFromOrganization) { - return true; - } - - // TODO: older server versions won't send the hasPremiumFromOrganization flag, so we're keeping the old logic - // for backwards compatibility. It can be removed after everyone has upgraded. - const organizations = await this.getOrganizations(options); - if (organizations == null) { - return false; - } - - for (const id of Object.keys(organizations)) { - const o = organizations[id]; - if (o.enabled && o.usersGetPremium && !o.isProviderUser) { - return true; - } - } - - return false; - } - - async setHasPremiumFromOrganization(value: boolean, options?: StorageOptions): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - account.profile.hasPremiumFromOrganization = value; - await this.saveAccount( - account, - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - } - async getConvertAccountToKeyConnector(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions())) @@ -813,27 +710,6 @@ export class StateService< ); } - async getDisableFavicon(options?: StorageOptions): Promise { - return ( - ( - await this.getGlobals( - this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()), - ) - )?.disableFavicon ?? false - ); - } - - async setDisableFavicon(value: boolean, options?: StorageOptions): Promise { - const globals = await this.getGlobals( - this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()), - ); - globals.disableFavicon = value; - await this.saveGlobals( - globals, - this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()), - ); - } - async getDisableGa(options?: StorageOptions): Promise { return ( (await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions()))) @@ -1278,24 +1154,6 @@ export class StateService< ); } - @withPrototypeForArrayMembers(EventData) - async getEventCollection(options?: StorageOptions): Promise { - return ( - await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions())) - )?.data?.eventCollection; - } - - async setEventCollection(value: EventData[], options?: StorageOptions): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - account.data.eventCollection = value; - await this.saveAccount( - account, - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - } - async getEverBeenUnlocked(options?: StorageOptions): Promise { return ( (await this.getAccount(this.reconcileOptions(options, await this.defaultInMemoryOptions()))) @@ -1356,7 +1214,10 @@ export class StateService< } async getIsAuthenticated(options?: StorageOptions): Promise { - return (await this.getAccessToken(options)) != null && (await this.getUserId(options)) != null; + return ( + (await this.tokenService.getAccessToken(options?.userId as UserId)) != null && + (await this.getUserId(options)) != null + ); } async getKdfConfig(options?: StorageOptions): Promise { @@ -1572,32 +1433,6 @@ export class StateService< ); } - /** - * @deprecated Do not call this directly, use OrganizationService - */ - async getOrganizations(options?: StorageOptions): Promise<{ [id: string]: OrganizationData }> { - return ( - await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions())) - )?.data?.organizations; - } - - /** - * @deprecated Do not call this directly, use OrganizationService - */ - async setOrganizations( - value: { [id: string]: OrganizationData }, - options?: StorageOptions, - ): Promise { - const account = await this.getAccount( - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - account.data.organizations = value; - await this.saveAccount( - account, - this.reconcileOptions(options, await this.defaultOnDiskOptions()), - ); - } - async getPasswordGenerationOptions(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())) @@ -1672,18 +1507,6 @@ export class StateService< ); } - async getRefreshToken(options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - return (await this.getAccount(options))?.tokens?.refreshToken; - } - - async setRefreshToken(value: string, options?: StorageOptions): Promise { - options = await this.getTimeoutBasedStorageOptions(options); - const account = await this.getAccount(options); - account.tokens.refreshToken = value; - await this.saveAccount(account, options); - } - async getRememberedEmail(options?: StorageOptions): Promise { return ( await this.getGlobals(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())) @@ -1718,23 +1541,6 @@ export class StateService< ); } - async getTwoFactorToken(options?: StorageOptions): Promise { - return ( - await this.getGlobals(this.reconcileOptions(options, await this.defaultOnDiskLocalOptions())) - )?.twoFactorToken; - } - - async setTwoFactorToken(value: string, options?: StorageOptions): Promise { - const globals = await this.getGlobals( - this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()), - ); - globals.twoFactorToken = value; - await this.saveGlobals( - globals, - this.reconcileOptions(options, await this.defaultOnDiskLocalOptions()), - ); - } - async getUserId(options?: StorageOptions): Promise { return ( await this.getAccount(this.reconcileOptions(options, await this.defaultOnDiskOptions())) @@ -2041,15 +1847,6 @@ export class StateService< await this.storageService.remove(keys.tempAccountSettings); } - if ( - account.settings.vaultTimeoutAction === VaultTimeoutAction.LogOut && - account.settings.vaultTimeout != null - ) { - account.tokens.accessToken = null; - account.tokens.refreshToken = null; - account.profile.apiKeyClientId = null; - account.keys.apiKeyClientSecret = null; - } await this.saveAccount( account, this.reconcileOptions( @@ -2250,7 +2047,7 @@ export class StateService< } protected async deAuthenticateAccount(userId: string): Promise { - await this.setAccessToken(null, { userId: userId }); + await this.tokenService.clearAccessToken(userId as UserId); await this.setLastActive(null, { userId: userId }); await this.updateState(async (state) => { state.authenticatedAccounts = state.authenticatedAccounts.filter((id) => id !== userId); @@ -2293,16 +2090,6 @@ export class StateService< return newActiveUser; } - private async getTimeoutBasedStorageOptions(options?: StorageOptions): Promise { - const timeoutAction = await this.getVaultTimeoutAction({ userId: options?.userId }); - const timeout = await this.getVaultTimeout({ userId: options?.userId }); - const defaultOptions = - timeoutAction === VaultTimeoutAction.LogOut && timeout != null - ? await this.defaultInMemoryOptions() - : await this.defaultOnDiskOptions(); - return this.reconcileOptions(options, defaultOptions); - } - protected async saveSecureStorageKey( key: string, value: T, diff --git a/libs/common/src/platform/state/implementations/default-state.provider.spec.ts b/libs/common/src/platform/state/implementations/default-state.provider.spec.ts index e7228192f12..3243b53d670 100644 --- a/libs/common/src/platform/state/implementations/default-state.provider.spec.ts +++ b/libs/common/src/platform/state/implementations/default-state.provider.spec.ts @@ -2,9 +2,9 @@ * need to update test environment so structuredClone works appropriately * @jest-environment ../shared/test.environment.ts */ -import { of } from "rxjs"; +import { Observable, of } from "rxjs"; -import { trackEmissions } from "../../../../spec"; +import { awaitAsync, trackEmissions } from "../../../../spec"; import { FakeAccountService, mockAccountServiceWith } from "../../../../spec/fake-account-service"; import { FakeActiveUserStateProvider, @@ -49,47 +49,111 @@ describe("DefaultStateProvider", () => { }); }); + describe.each([ + [ + "getUserState$", + (keyDefinition: KeyDefinition, userId?: UserId) => + sut.getUserState$(keyDefinition, userId), + ], + [ + "getUserStateOrDefault$", + (keyDefinition: KeyDefinition, userId?: UserId) => + sut.getUserStateOrDefault$(keyDefinition, { userId: userId }), + ], + ])( + "Shared behavior for %s", + ( + _testName: string, + methodUnderTest: ( + keyDefinition: KeyDefinition, + userId?: UserId, + ) => Observable, + ) => { + const accountInfo = { email: "email", name: "name", status: AuthenticationStatus.LoggedOut }; + const keyDefinition = new KeyDefinition(new StateDefinition("test", "disk"), "test", { + deserializer: (s) => s, + }); + + it("should follow the specified user if userId is provided", async () => { + const state = singleUserStateProvider.getFake(userId, keyDefinition); + state.nextState("value"); + const emissions = trackEmissions(methodUnderTest(keyDefinition, userId)); + + state.nextState("value2"); + state.nextState("value3"); + + expect(emissions).toEqual(["value", "value2", "value3"]); + }); + + it("should follow the current active user if no userId is provided", async () => { + accountService.activeAccountSubject.next({ id: userId, ...accountInfo }); + const state = singleUserStateProvider.getFake(userId, keyDefinition); + state.nextState("value"); + const emissions = trackEmissions(methodUnderTest(keyDefinition)); + + state.nextState("value2"); + state.nextState("value3"); + + expect(emissions).toEqual(["value", "value2", "value3"]); + }); + + it("should continue to follow the state of the user that was active when called, even if active user changes", async () => { + const state = singleUserStateProvider.getFake(userId, keyDefinition); + state.nextState("value"); + const emissions = trackEmissions(methodUnderTest(keyDefinition)); + + accountService.activeAccountSubject.next({ id: "newUserId" as UserId, ...accountInfo }); + const newUserEmissions = trackEmissions(sut.getUserState$(keyDefinition)); + state.nextState("value2"); + state.nextState("value3"); + + expect(emissions).toEqual(["value", "value2", "value3"]); + expect(newUserEmissions).toEqual([null]); + }); + }, + ); + describe("getUserState$", () => { const accountInfo = { email: "email", name: "name", status: AuthenticationStatus.LoggedOut }; const keyDefinition = new KeyDefinition(new StateDefinition("test", "disk"), "test", { deserializer: (s) => s, }); - it("should follow the specified user if userId is provided", async () => { + it("should not emit any values until a truthy user id is supplied", async () => { + accountService.activeAccountSubject.next(null); const state = singleUserStateProvider.getFake(userId, keyDefinition); - state.nextState("value"); - const emissions = trackEmissions(sut.getUserState$(keyDefinition, userId)); + state.stateSubject.next([userId, "value"]); - state.nextState("value2"); - state.nextState("value3"); + const emissions = trackEmissions(sut.getUserState$(keyDefinition)); - expect(emissions).toEqual(["value", "value2", "value3"]); - }); + await awaitAsync(); + + expect(emissions).toHaveLength(0); - it("should follow the current active user if no userId is provided", async () => { accountService.activeAccountSubject.next({ id: userId, ...accountInfo }); - const state = singleUserStateProvider.getFake(userId, keyDefinition); - state.nextState("value"); - const emissions = trackEmissions(sut.getUserState$(keyDefinition)); - state.nextState("value2"); - state.nextState("value3"); + await awaitAsync(); - expect(emissions).toEqual(["value", "value2", "value3"]); + expect(emissions).toEqual(["value"]); + }); + }); + + describe("getUserStateOrDefault$", () => { + const keyDefinition = new KeyDefinition(new StateDefinition("test", "disk"), "test", { + deserializer: (s) => s, }); - it("should continue to follow the state of the user that was active when called, even if active user changes", async () => { - const state = singleUserStateProvider.getFake(userId, keyDefinition); - state.nextState("value"); - const emissions = trackEmissions(sut.getUserState$(keyDefinition)); + it("should emit default value if no userId supplied and first active user id emission in falsy", async () => { + accountService.activeAccountSubject.next(null); - accountService.activeAccountSubject.next({ id: "newUserId" as UserId, ...accountInfo }); - const newUserEmissions = trackEmissions(sut.getUserState$(keyDefinition)); - state.nextState("value2"); - state.nextState("value3"); + const emissions = trackEmissions( + sut.getUserStateOrDefault$(keyDefinition, { + userId: undefined, + defaultValue: "I'm default!", + }), + ); - expect(emissions).toEqual(["value", "value2", "value3"]); - expect(newUserEmissions).toEqual([null]); + expect(emissions).toEqual(["I'm default!"]); }); }); diff --git a/libs/common/src/platform/state/implementations/default-state.provider.ts b/libs/common/src/platform/state/implementations/default-state.provider.ts index 6a35e0fabb2..1413d26ce51 100644 --- a/libs/common/src/platform/state/implementations/default-state.provider.ts +++ b/libs/common/src/platform/state/implementations/default-state.provider.ts @@ -1,4 +1,4 @@ -import { Observable, switchMap, take } from "rxjs"; +import { Observable, filter, of, switchMap, take } from "rxjs"; import { UserId } from "../../../types/guid"; import { DerivedStateDependencies } from "../../../types/state"; @@ -30,12 +30,30 @@ export class DefaultStateProvider implements StateProvider { return this.getUser(userId, keyDefinition).state$; } else { return this.activeUserId$.pipe( + filter((userId) => userId != null), // Filter out null-ish user ids since we can't get state for a null user id take(1), switchMap((userId) => this.getUser(userId, keyDefinition).state$), ); } } + getUserStateOrDefault$( + keyDefinition: KeyDefinition | UserKeyDefinition, + config: { userId: UserId | undefined; defaultValue?: T }, + ): Observable { + const { userId, defaultValue = null } = config; + if (userId) { + return this.getUser(userId, keyDefinition).state$; + } else { + return this.activeUserId$.pipe( + take(1), + switchMap((userId) => + userId != null ? this.getUser(userId, keyDefinition).state$ : of(defaultValue), + ), + ); + } + } + async setUserState( keyDefinition: KeyDefinition | UserKeyDefinition, value: T, diff --git a/libs/common/src/platform/state/state-definitions.ts b/libs/common/src/platform/state/state-definitions.ts index 86b8dd051cb..8115555b2ed 100644 --- a/libs/common/src/platform/state/state-definitions.ts +++ b/libs/common/src/platform/state/state-definitions.ts @@ -23,11 +23,19 @@ export const ORGANIZATIONS_DISK = new StateDefinition("organizations", "disk"); export const POLICIES_DISK = new StateDefinition("policies", "disk"); export const PROVIDERS_DISK = new StateDefinition("providers", "disk"); +// Billing +export const BILLING_DISK = new StateDefinition("billing", "disk"); + // Auth export const ACCOUNT_MEMORY = new StateDefinition("account", "memory"); export const AVATAR_DISK = new StateDefinition("avatar", "disk", { web: "disk-local" }); export const SSO_DISK = new StateDefinition("ssoLogin", "disk"); +export const TOKEN_DISK = new StateDefinition("token", "disk"); +export const TOKEN_DISK_LOCAL = new StateDefinition("tokenDiskLocal", "disk", { + web: "disk-local", +}); +export const TOKEN_MEMORY = new StateDefinition("token", "memory"); export const LOGIN_STRATEGY_MEMORY = new StateDefinition("loginStrategy", "memory"); // Autofill @@ -38,15 +46,11 @@ export const USER_NOTIFICATION_SETTINGS_DISK = new StateDefinition( "disk", ); -// Billing - export const DOMAIN_SETTINGS_DISK = new StateDefinition("domainSettings", "disk"); - export const AUTOFILL_SETTINGS_DISK = new StateDefinition("autofillSettings", "disk"); export const AUTOFILL_SETTINGS_DISK_LOCAL = new StateDefinition("autofillSettingsLocal", "disk", { web: "disk-local", }); -export const BILLING_DISK = new StateDefinition("billing", "disk"); // Components @@ -77,6 +81,7 @@ export const SM_ONBOARDING_DISK = new StateDefinition("smOnboarding", "disk", { export const GENERATOR_DISK = new StateDefinition("generator", "disk"); export const GENERATOR_MEMORY = new StateDefinition("generator", "memory"); +export const EVENT_COLLECTION_DISK = new StateDefinition("eventCollection", "disk"); // Vault diff --git a/libs/common/src/platform/state/state.provider.ts b/libs/common/src/platform/state/state.provider.ts index f2b008eb996..ddbb6a7c875 100644 --- a/libs/common/src/platform/state/state.provider.ts +++ b/libs/common/src/platform/state/state.provider.ts @@ -24,8 +24,11 @@ export abstract class StateProvider { /** * Gets a state observable for a given key and userId. * - * @remarks If userId is falsy the observable returned will point to the currently active user _and not update if the active user changes_. + * @remarks If userId is falsy the observable returned will attempt to point to the currently active user _and not update if the active user changes_. * This is different to how `getActive` works and more similar to `getUser` for whatever user happens to be active at the time of the call. + * If no user happens to be active at the time this method is called with a falsy userId then this observable will not emit a value until + * a user becomes active. If you are not confident a user is active at the time this method is called, you may want to pipe a call to `timeout` + * or instead call {@link getUserStateOrDefault$} and supply a value you would rather have given in the case of no passed in userId and no active user. * * @note consider converting your {@link KeyDefinition} to a {@link UserKeyDefinition} for additional features. * @@ -37,14 +40,49 @@ export abstract class StateProvider { /** * Gets a state observable for a given key and userId. * - * @remarks If userId is falsy the observable returned will point to the currently active user _and not update if the active user changes_. + * @remarks If userId is falsy the observable returned will attempt to point to the currently active user _and not update if the active user changes_. * This is different to how `getActive` works and more similar to `getUser` for whatever user happens to be active at the time of the call. + * If no user happens to be active at the time this method is called with a falsy userId then this observable will not emit a value until + * a user becomes active. If you are not confident a user is active at the time this method is called, you may want to pipe a call to `timeout` + * or instead call {@link getUserStateOrDefault$} and supply a value you would rather have given in the case of no passed in userId and no active user. * * @param keyDefinition - The key definition for the state you want to get. * @param userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned. */ abstract getUserState$(keyDefinition: UserKeyDefinition, userId?: UserId): Observable; + /** + * Gets a state observable for a given key and userId + * + * @remarks If userId is falsy the observable return will first attempt to point to the currently active user but will not follow subsequent active user changes, + * if there is no immediately available active user, then it will fallback to returning a default value in an observable that immediately completes. + * + * @note consider converting your {@link KeyDefinition} to a {@link UserKeyDefinition} for additional features. + * + * @param keyDefinition - The key definition for the state you want to get. + * @param config.userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned. + * @param config.defaultValue - The default value that should be wrapped in an observable if no active user is immediately available and no truthy userId is passed in. + */ + abstract getUserStateOrDefault$( + keyDefinition: KeyDefinition, + config: { userId: UserId | undefined; defaultValue?: T }, + ): Observable; + + /** + * Gets a state observable for a given key and userId + * + * @remarks If userId is falsy the observable return will first attempt to point to the currently active user but will not follow subsequent active user changes, + * if there is no immediately available active user, then it will fallback to returning a default value in an observable that immediately completes. + * + * @param keyDefinition - The key definition for the state you want to get. + * @param config.userId - The userId for which you want the state for. If not provided, the state for the currently active user will be returned. + * @param config.defaultValue - The default value that should be wrapped in an observable if no active user is immediately available and no truthy userId is passed in. + */ + abstract getUserStateOrDefault$( + keyDefinition: UserKeyDefinition, + config: { userId: UserId | undefined; defaultValue?: T }, + ): Observable; + /** * Sets the state for a given key and userId. * diff --git a/libs/common/src/platform/state/user-key-definition.ts b/libs/common/src/platform/state/user-key-definition.ts index 99e3039e1eb..3405b388375 100644 --- a/libs/common/src/platform/state/user-key-definition.ts +++ b/libs/common/src/platform/state/user-key-definition.ts @@ -138,7 +138,9 @@ export class UserKeyDefinition { buildKey(userId: UserId) { if (!Utils.isGuid(userId)) { - throw new Error("You cannot build a user key without a valid UserId"); + throw new Error( + `You cannot build a user key without a valid UserId, building for key ${this.fullName}`, + ); } return `user_${userId}_${this.stateDefinition.name}_${this.key}` as StorageKey; } diff --git a/libs/common/src/services/api.service.ts b/libs/common/src/services/api.service.ts index 336191f3abb..869d45ebff1 100644 --- a/libs/common/src/services/api.service.ts +++ b/libs/common/src/services/api.service.ts @@ -93,6 +93,7 @@ import { SubscriptionResponse } from "../billing/models/response/subscription.re import { TaxInfoResponse } from "../billing/models/response/tax-info.response"; import { TaxRateResponse } from "../billing/models/response/tax-rate.response"; import { DeviceType } from "../enums"; +import { VaultTimeoutAction } from "../enums/vault-timeout-action.enum"; import { CollectionBulkDeleteRequest } from "../models/request/collection-bulk-delete.request"; import { DeleteRecoverRequest } from "../models/request/delete-recover.request"; import { EventRequest } from "../models/request/event.request"; @@ -116,6 +117,7 @@ import { UserKeyResponse } from "../models/response/user-key.response"; import { AppIdService } from "../platform/abstractions/app-id.service"; import { EnvironmentService } from "../platform/abstractions/environment.service"; import { PlatformUtilsService } from "../platform/abstractions/platform-utils.service"; +import { StateService } from "../platform/abstractions/state.service"; import { Utils } from "../platform/misc/utils"; import { AttachmentRequest } from "../vault/models/request/attachment.request"; import { CipherBulkDeleteRequest } from "../vault/models/request/cipher-bulk-delete.request"; @@ -154,6 +156,7 @@ export class ApiService implements ApiServiceAbstraction { private platformUtilsService: PlatformUtilsService, private environmentService: EnvironmentService, private appIdService: AppIdService, + private stateService: StateService, private logoutCallback: (expired: boolean) => Promise, private customUserAgent: string = null, ) { @@ -224,7 +227,6 @@ export class ApiService implements ApiServiceAbstraction { responseJson.TwoFactorProviders2 && Object.keys(responseJson.TwoFactorProviders2).length ) { - await this.tokenService.clearTwoFactorToken(); return new IdentityTwoFactorResponse(responseJson); } else if ( response.status === 400 && @@ -1578,10 +1580,10 @@ export class ApiService implements ApiServiceAbstraction { // Helpers async getActiveBearerToken(): Promise { - let accessToken = await this.tokenService.getToken(); + let accessToken = await this.tokenService.getAccessToken(); if (await this.tokenService.tokenNeedsRefresh()) { await this.doAuthRefresh(); - accessToken = await this.tokenService.getToken(); + accessToken = await this.tokenService.getAccessToken(); } return accessToken; } @@ -1749,7 +1751,7 @@ export class ApiService implements ApiServiceAbstraction { headers.set("User-Agent", this.customUserAgent); } - const decodedToken = await this.tokenService.decodeToken(); + const decodedToken = await this.tokenService.decodeAccessToken(); const response = await this.fetch( new Request(this.environmentService.getIdentityUrl() + "/connect/token", { body: this.qsStringify({ @@ -1767,10 +1769,15 @@ export class ApiService implements ApiServiceAbstraction { if (response.status === 200) { const responseJson = await response.json(); const tokenResponse = new IdentityTokenResponse(responseJson); + + const vaultTimeoutAction = await this.stateService.getVaultTimeoutAction(); + const vaultTimeout = await this.stateService.getVaultTimeout(); + await this.tokenService.setTokens( tokenResponse.accessToken, tokenResponse.refreshToken, - null, + vaultTimeoutAction as VaultTimeoutAction, + vaultTimeout, ); } else { const error = await this.handleError(response, true, true); @@ -1796,7 +1803,14 @@ export class ApiService implements ApiServiceAbstraction { throw new Error("Invalid response received when refreshing api token"); } - await this.tokenService.setToken(response.accessToken); + const vaultTimeoutAction = await this.stateService.getVaultTimeoutAction(); + const vaultTimeout = await this.stateService.getVaultTimeout(); + + await this.tokenService.setAccessToken( + response.accessToken, + vaultTimeoutAction as VaultTimeoutAction, + vaultTimeout, + ); } async send( diff --git a/libs/common/src/services/event/event-collection.service.ts b/libs/common/src/services/event/event-collection.service.ts index f2447ba24eb..2d2b5530626 100644 --- a/libs/common/src/services/event/event-collection.service.ts +++ b/libs/common/src/services/event/event-collection.service.ts @@ -1,61 +1,105 @@ +import { firstValueFrom, map, from, zip } from "rxjs"; + import { EventCollectionService as EventCollectionServiceAbstraction } from "../../abstractions/event/event-collection.service"; import { EventUploadService } from "../../abstractions/event/event-upload.service"; import { OrganizationService } from "../../admin-console/abstractions/organization/organization.service.abstraction"; +import { AccountService } from "../../auth/abstractions/account.service"; +import { AuthenticationStatus } from "../../auth/enums/authentication-status"; import { EventType } from "../../enums"; import { EventData } from "../../models/data/event.data"; -import { StateService } from "../../platform/abstractions/state.service"; +import { StateProvider } from "../../platform/state"; import { CipherService } from "../../vault/abstractions/cipher.service"; +import { EVENT_COLLECTION } from "./key-definitions"; + export class EventCollectionService implements EventCollectionServiceAbstraction { constructor( private cipherService: CipherService, - private stateService: StateService, + private stateProvider: StateProvider, private organizationService: OrganizationService, private eventUploadService: EventUploadService, + private accountService: AccountService, ) {} + /** Adds an event to the active user's event collection + * @param eventType the event type to be added + * @param cipherId if provided the id of the cipher involved in the event + * @param uploadImmediately in some cases the recorded events should be uploaded right after being added + * @param organizationId the organizationId involved in the event. If the cipherId is not provided an organizationId is required + */ async collect( eventType: EventType, cipherId: string = null, uploadImmediately = false, organizationId: string = null, ): Promise { - const authed = await this.stateService.getIsAuthenticated(); - if (!authed) { + const userId = await firstValueFrom(this.stateProvider.activeUserId$); + const eventStore = this.stateProvider.getUser(userId, EVENT_COLLECTION); + + if (!(await this.shouldUpdate(cipherId, organizationId))) { return; } - const organizations = await this.organizationService.getAll(); - if (organizations == null) { - return; - } - const orgIds = new Set(organizations.filter((o) => o.useEvents).map((o) => o.id)); - if (orgIds.size === 0) { - return; - } - if (cipherId != null) { - const cipher = await this.cipherService.get(cipherId); - if (cipher == null || cipher.organizationId == null || !orgIds.has(cipher.organizationId)) { - return; - } - } - if (organizationId != null) { - if (!orgIds.has(organizationId)) { - return; - } - } - let eventCollection = await this.stateService.getEventCollection(); - if (eventCollection == null) { - eventCollection = []; - } + const event = new EventData(); event.type = eventType; event.cipherId = cipherId; event.date = new Date().toISOString(); event.organizationId = organizationId; - eventCollection.push(event); - await this.stateService.setEventCollection(eventCollection); + + await eventStore.update((events) => { + events = events ?? []; + events.push(event); + return events; + }); + if (uploadImmediately) { await this.eventUploadService.uploadEvents(); } } + + /** Verifies if the event collection should be updated for the provided information + * @param cipherId the cipher for the event + * @param organizationId the organization for the event + */ + private async shouldUpdate( + cipherId: string = null, + organizationId: string = null, + ): Promise { + const orgIds$ = this.organizationService.organizations$.pipe( + map((orgs) => orgs?.filter((o) => o.useEvents)?.map((x) => x.id) ?? []), + ); + + const cipher$ = from(this.cipherService.get(cipherId)); + + const [accountInfo, orgIds, cipher] = await firstValueFrom( + zip(this.accountService.activeAccount$, orgIds$, cipher$), + ); + + // The user must be authorized + if (accountInfo.status != AuthenticationStatus.Unlocked) { + return false; + } + + // User must have organizations assigned to them + if (orgIds == null || orgIds.length == 0) { + return false; + } + + // If the cipher is null there must be an organization id provided + if (cipher == null && organizationId == null) { + return false; + } + + // If the cipher is present it must be in the user's org list + if (cipher != null && !orgIds.includes(cipher?.organizationId)) { + return false; + } + + // If the organization id is provided it must be in the user's org list + if (organizationId != null && !orgIds.includes(organizationId)) { + return false; + } + + return true; + } } diff --git a/libs/common/src/services/event/event-upload.service.ts b/libs/common/src/services/event/event-upload.service.ts index 75c7a7a1936..4ee4300c39c 100644 --- a/libs/common/src/services/event/event-upload.service.ts +++ b/libs/common/src/services/event/event-upload.service.ts @@ -1,15 +1,24 @@ +import { firstValueFrom, map } from "rxjs"; + import { ApiService } from "../../abstractions/api.service"; import { EventUploadService as EventUploadServiceAbstraction } from "../../abstractions/event/event-upload.service"; +import { AccountService } from "../../auth/abstractions/account.service"; +import { AuthenticationStatus } from "../../auth/enums/authentication-status"; +import { EventData } from "../../models/data/event.data"; import { EventRequest } from "../../models/request/event.request"; import { LogService } from "../../platform/abstractions/log.service"; -import { StateService } from "../../platform/abstractions/state.service"; +import { StateProvider } from "../../platform/state"; +import { UserId } from "../../types/guid"; + +import { EVENT_COLLECTION } from "./key-definitions"; export class EventUploadService implements EventUploadServiceAbstraction { private inited = false; constructor( private apiService: ApiService, - private stateService: StateService, + private stateProvider: StateProvider, private logService: LogService, + private accountService: AccountService, ) {} init(checkOnInterval: boolean) { @@ -26,12 +35,26 @@ export class EventUploadService implements EventUploadServiceAbstraction { } } - async uploadEvents(userId?: string): Promise { - const authed = await this.stateService.getIsAuthenticated({ userId: userId }); - if (!authed) { + /** Upload the event collection from state. + * @param userId upload events for provided user. If not active user will be used. + */ + async uploadEvents(userId?: UserId): Promise { + if (!userId) { + userId = await firstValueFrom(this.stateProvider.activeUserId$); + } + + // Get the auth status from the provided user or the active user + const userAuth$ = this.accountService.accounts$.pipe( + map((accounts) => accounts[userId]?.status === AuthenticationStatus.Unlocked), + ); + + const isAuthenticated = await firstValueFrom(userAuth$); + if (!isAuthenticated) { return; } - const eventCollection = await this.stateService.getEventCollection({ userId: userId }); + + const eventCollection = await this.takeEvents(userId); + if (eventCollection == null || eventCollection.length === 0) { return; } @@ -45,15 +68,23 @@ export class EventUploadService implements EventUploadServiceAbstraction { }); try { await this.apiService.postEventsCollect(request); - // FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.clearEvents(userId); } catch (e) { this.logService.error(e); + // Add the events back to state if there was an error and they were not uploaded. + await this.stateProvider.setUserState(EVENT_COLLECTION, eventCollection, userId); } } - private async clearEvents(userId?: string): Promise { - await this.stateService.setEventCollection(null, { userId: userId }); + /** Return user's events and then clear them from state + * @param userId the user to grab and clear events for + */ + private async takeEvents(userId: UserId): Promise { + let taken = null; + await this.stateProvider.getUser(userId, EVENT_COLLECTION).update((current) => { + taken = current ?? []; + return []; + }); + + return taken; } } diff --git a/libs/common/src/services/event/key-definitions.ts b/libs/common/src/services/event/key-definitions.ts new file mode 100644 index 00000000000..1059d24b724 --- /dev/null +++ b/libs/common/src/services/event/key-definitions.ts @@ -0,0 +1,10 @@ +import { EventData } from "../../models/data/event.data"; +import { KeyDefinition, EVENT_COLLECTION_DISK } from "../../platform/state"; + +export const EVENT_COLLECTION: KeyDefinition = KeyDefinition.array( + EVENT_COLLECTION_DISK, + "events", + { + deserializer: (s) => EventData.fromJSON(s), + }, +); diff --git a/libs/common/src/services/settings.service.ts b/libs/common/src/services/settings.service.ts deleted file mode 100644 index 9a4d04a147d..00000000000 --- a/libs/common/src/services/settings.service.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { BehaviorSubject, concatMap } from "rxjs"; - -import { SettingsService as SettingsServiceAbstraction } from "../abstractions/settings.service"; -import { StateService } from "../platform/abstractions/state.service"; -import { Utils } from "../platform/misc/utils"; - -export class SettingsService implements SettingsServiceAbstraction { - protected _disableFavicon = new BehaviorSubject(null); - - disableFavicon$ = this._disableFavicon.asObservable(); - - constructor(private stateService: StateService) { - this.stateService.activeAccountUnlocked$ - .pipe( - concatMap(async (unlocked) => { - if (Utils.global.bitwardenContainerService == null) { - return; - } - - if (!unlocked) { - return; - } - - const disableFavicon = await this.stateService.getDisableFavicon(); - - this._disableFavicon.next(disableFavicon); - }), - ) - .subscribe(); - } - - async setDisableFavicon(value: boolean) { - this._disableFavicon.next(value); - await this.stateService.setDisableFavicon(value); - } - - getDisableFavicon(): boolean { - return this._disableFavicon.getValue(); - } -} diff --git a/libs/common/src/services/vault-timeout/vault-timeout-settings.service.ts b/libs/common/src/services/vault-timeout/vault-timeout-settings.service.ts index 0d0eb508cb2..e8897d82b7d 100644 --- a/libs/common/src/services/vault-timeout/vault-timeout-settings.service.ts +++ b/libs/common/src/services/vault-timeout/vault-timeout-settings.service.ts @@ -29,7 +29,7 @@ export class VaultTimeoutSettingsService implements VaultTimeoutSettingsServiceA async setVaultTimeoutOptions(timeout: number, action: VaultTimeoutAction): Promise { // We swap these tokens from being on disk for lock actions, and in memory for logout actions // Get them here to set them to their new location after changing the timeout action and clearing if needed - const token = await this.tokenService.getToken(); + const accessToken = await this.tokenService.getAccessToken(); const refreshToken = await this.tokenService.getRefreshToken(); const clientId = await this.tokenService.getClientId(); const clientSecret = await this.tokenService.getClientSecret(); @@ -37,21 +37,22 @@ export class VaultTimeoutSettingsService implements VaultTimeoutSettingsServiceA await this.stateService.setVaultTimeout(timeout); const currentAction = await this.stateService.getVaultTimeoutAction(); + if ( (timeout != null || timeout === 0) && action === VaultTimeoutAction.LogOut && action !== currentAction ) { // if we have a vault timeout and the action is log out, reset tokens - await this.tokenService.clearToken(); + await this.tokenService.clearTokens(); } await this.stateService.setVaultTimeoutAction(action); - await this.tokenService.setToken(token); - await this.tokenService.setRefreshToken(refreshToken); - await this.tokenService.setClientId(clientId); - await this.tokenService.setClientSecret(clientSecret); + await this.tokenService.setTokens(accessToken, refreshToken, action, timeout, [ + clientId, + clientSecret, + ]); await this.cryptoService.refreshAdditionalKeys(); } diff --git a/libs/common/src/state-migrations/migrate.ts b/libs/common/src/state-migrations/migrate.ts index 77a35ccb871..000f08b392b 100644 --- a/libs/common/src/state-migrations/migrate.ts +++ b/libs/common/src/state-migrations/migrate.ts @@ -24,7 +24,6 @@ import { RevertLastSyncMigrator } from "./migrations/26-revert-move-last-sync-to import { BadgeSettingsMigrator } from "./migrations/27-move-badge-settings-to-state-providers"; import { MoveBiometricUnlockToStateProviders } from "./migrations/28-move-biometric-unlock-to-state-providers"; import { UserNotificationSettingsKeyMigrator } from "./migrations/29-move-user-notification-settings-to-state-provider"; -import { FixPremiumMigrator } from "./migrations/3-fix-premium"; import { PolicyMigrator } from "./migrations/30-move-policy-state-to-state-provider"; import { EnableContextMenuMigrator } from "./migrations/31-move-enable-context-menu-to-autofill-settings-state-provider"; import { PreferredLanguageMigrator } from "./migrations/32-move-preferred-language"; @@ -33,7 +32,12 @@ import { DomainSettingsMigrator } from "./migrations/34-move-domain-settings-to- import { MoveThemeToStateProviderMigrator } from "./migrations/35-move-theme-to-state-providers"; import { VaultSettingsKeyMigrator } from "./migrations/36-move-show-card-and-identity-to-state-provider"; import { AvatarColorMigrator } from "./migrations/37-move-avatar-color-to-state-providers"; +import { TokenServiceStateProviderMigrator } from "./migrations/38-migrate-token-svc-to-state-provider"; +import { MoveBillingAccountProfileMigrator } from "./migrations/39-move-billing-account-profile-to-state-providers"; import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked"; +import { OrganizationMigrator } from "./migrations/40-move-organization-state-to-state-provider"; +import { EventCollectionMigrator } from "./migrations/41-move-event-collection-to-state-provider"; +import { EnableFaviconMigrator } from "./migrations/42-move-enable-favicon-to-domain-settings-state-provider"; import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys"; import { RemoveLegacyEtmKeyMigrator } from "./migrations/6-remove-legacy-etm-key"; import { MoveBiometricAutoPromptToAccount } from "./migrations/7-move-biometric-auto-prompt-to-account"; @@ -41,14 +45,13 @@ import { MoveStateVersionMigrator } from "./migrations/8-move-state-version"; import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-settings-to-global"; import { MinVersionMigrator } from "./migrations/min-version"; -export const MIN_VERSION = 2; -export const CURRENT_VERSION = 37; +export const MIN_VERSION = 3; +export const CURRENT_VERSION = 42; export type MinVersion = typeof MIN_VERSION; export function createMigrationBuilder() { return MigrationBuilder.create() .with(MinVersionMigrator) - .with(FixPremiumMigrator, 2, 3) .with(RemoveEverBeenUnlockedMigrator, 3, 4) .with(AddKeyTypeToOrgKeysMigrator, 4, 5) .with(RemoveLegacyEtmKeyMigrator, 5, 6) @@ -82,7 +85,12 @@ export function createMigrationBuilder() { .with(DomainSettingsMigrator, 33, 34) .with(MoveThemeToStateProviderMigrator, 34, 35) .with(VaultSettingsKeyMigrator, 35, 36) - .with(AvatarColorMigrator, 36, CURRENT_VERSION); + .with(AvatarColorMigrator, 36, 37) + .with(TokenServiceStateProviderMigrator, 37, 38) + .with(MoveBillingAccountProfileMigrator, 38, 39) + .with(OrganizationMigrator, 39, 40) + .with(EventCollectionMigrator, 40, 41) + .with(EnableFaviconMigrator, 41, 42); } export async function currentVersion( diff --git a/libs/common/src/state-migrations/migrations/3-fix-premium.spec.ts b/libs/common/src/state-migrations/migrations/3-fix-premium.spec.ts deleted file mode 100644 index 1ef910d4569..00000000000 --- a/libs/common/src/state-migrations/migrations/3-fix-premium.spec.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { MockProxy } from "jest-mock-extended"; - -// eslint-disable-next-line import/no-restricted-paths -- Used for testing migration, which requires import -import { TokenService } from "../../auth/services/token.service"; -import { MigrationHelper } from "../migration-helper"; -import { mockMigrationHelper } from "../migration-helper.spec"; - -import { FixPremiumMigrator } from "./3-fix-premium"; - -function migrateExampleJSON() { - return { - global: { - stateVersion: 2, - otherStuff: "otherStuff1", - }, - authenticatedAccounts: [ - "c493ed01-4e08-4e88-abc7-332f380ca760", - "23e61a5f-2ece-4f5e-b499-f0bc489482a9", - ], - "c493ed01-4e08-4e88-abc7-332f380ca760": { - profile: { - otherStuff: "otherStuff2", - hasPremiumPersonally: null as boolean, - }, - tokens: { - otherStuff: "otherStuff3", - accessToken: "accessToken", - }, - otherStuff: "otherStuff4", - }, - "23e61a5f-2ece-4f5e-b499-f0bc489482a9": { - profile: { - otherStuff: "otherStuff5", - hasPremiumPersonally: true, - }, - tokens: { - otherStuff: "otherStuff6", - accessToken: "accessToken", - }, - otherStuff: "otherStuff7", - }, - otherStuff: "otherStuff8", - }; -} - -jest.mock("../../auth/services/token.service", () => ({ - TokenService: { - decodeToken: jest.fn(), - }, -})); - -describe("FixPremiumMigrator", () => { - let helper: MockProxy; - let sut: FixPremiumMigrator; - const decodeTokenSpy = TokenService.decodeToken as jest.Mock; - - beforeEach(() => { - helper = mockMigrationHelper(migrateExampleJSON()); - sut = new FixPremiumMigrator(2, 3); - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - describe("migrate", () => { - it("should migrate hasPremiumPersonally", async () => { - decodeTokenSpy.mockResolvedValueOnce({ premium: true }); - await sut.migrate(helper); - - expect(helper.set).toHaveBeenCalledTimes(1); - expect(helper.set).toHaveBeenCalledWith("c493ed01-4e08-4e88-abc7-332f380ca760", { - profile: { - otherStuff: "otherStuff2", - hasPremiumPersonally: true, - }, - tokens: { - otherStuff: "otherStuff3", - accessToken: "accessToken", - }, - otherStuff: "otherStuff4", - }); - }); - - it("should not migrate if decode throws", async () => { - decodeTokenSpy.mockRejectedValueOnce(new Error("test")); - await sut.migrate(helper); - - expect(helper.set).not.toHaveBeenCalled(); - }); - - it("should not migrate if decode returns null", async () => { - decodeTokenSpy.mockResolvedValueOnce(null); - await sut.migrate(helper); - - expect(helper.set).not.toHaveBeenCalled(); - }); - }); - - describe("updateVersion", () => { - it("should update version", async () => { - await sut.updateVersion(helper, "up"); - - expect(helper.set).toHaveBeenCalledTimes(1); - expect(helper.set).toHaveBeenCalledWith("global", { - stateVersion: 3, - otherStuff: "otherStuff1", - }); - }); - }); -}); diff --git a/libs/common/src/state-migrations/migrations/3-fix-premium.ts b/libs/common/src/state-migrations/migrations/3-fix-premium.ts deleted file mode 100644 index b6c69a99168..00000000000 --- a/libs/common/src/state-migrations/migrations/3-fix-premium.ts +++ /dev/null @@ -1,48 +0,0 @@ -// eslint-disable-next-line import/no-restricted-paths -- Used for token decoding, which are valid for days. We want the latest -import { TokenService } from "../../auth/services/token.service"; -import { MigrationHelper } from "../migration-helper"; -import { Migrator, IRREVERSIBLE, Direction } from "../migrator"; - -type ExpectedAccountType = { - profile?: { hasPremiumPersonally?: boolean }; - tokens?: { accessToken?: string }; -}; - -export class FixPremiumMigrator extends Migrator<2, 3> { - async migrate(helper: MigrationHelper): Promise { - const accounts = await helper.getAccounts(); - - async function fixPremium(userId: string, account: ExpectedAccountType) { - if (account?.profile?.hasPremiumPersonally === null && account.tokens?.accessToken != null) { - let decodedToken: { premium: boolean }; - try { - decodedToken = await TokenService.decodeToken(account.tokens.accessToken); - } catch { - return; - } - - if (decodedToken?.premium == null) { - return; - } - - account.profile.hasPremiumPersonally = decodedToken?.premium; - return helper.set(userId, account); - } - } - - await Promise.all(accounts.map(({ userId, account }) => fixPremium(userId, account))); - } - - rollback(helper: MigrationHelper): Promise { - throw IRREVERSIBLE; - } - - // Override is necessary because default implementation assumes `stateVersion` at the root, but for this version - // it is nested inside a global object. - override async updateVersion(helper: MigrationHelper, direction: Direction): Promise { - const endVersion = direction === "up" ? this.toVersion : this.fromVersion; - helper.currentVersion = endVersion; - const global: Record = (await helper.get("global")) || {}; - await helper.set("global", { ...global, stateVersion: endVersion }); - } -} diff --git a/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.spec.ts new file mode 100644 index 00000000000..a5243c261a5 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.spec.ts @@ -0,0 +1,258 @@ +import { MockProxy, any } from "jest-mock-extended"; + +import { MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + ACCESS_TOKEN_DISK, + REFRESH_TOKEN_DISK, + API_KEY_CLIENT_ID_DISK, + API_KEY_CLIENT_SECRET_DISK, + TokenServiceStateProviderMigrator, +} from "./38-migrate-token-svc-to-state-provider"; + +// Represents data in state service pre-migration +function preMigrationJson() { + return { + global: { + twoFactorToken: "twoFactorToken", + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user1", "user2", "user3"], + user1: { + tokens: { + accessToken: "accessToken", + refreshToken: "refreshToken", + otherStuff: "overStuff2", + }, + profile: { + apiKeyClientId: "apiKeyClientId", + email: "user1Email", + otherStuff: "overStuff3", + }, + keys: { + apiKeyClientSecret: "apiKeyClientSecret", + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }, + user2: { + tokens: { + // no tokens to migrate + otherStuff: "overStuff2", + }, + profile: { + // no apiKeyClientId to migrate + otherStuff: "overStuff3", + email: "user2Email", + }, + keys: { + // no apiKeyClientSecret to migrate + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +function rollbackJSON() { + return { + // User specific state provider data + // use pattern user_{userId}_{stateDefinitionName}_{keyDefinitionKey} for user data + + // User1 migrated data + user_user1_token_accessToken: "accessToken", + user_user1_token_refreshToken: "refreshToken", + user_user1_token_apiKeyClientId: "apiKeyClientId", + user_user1_token_apiKeyClientSecret: "apiKeyClientSecret", + + // User2 migrated data + user_user2_token_accessToken: null as any, + user_user2_token_refreshToken: null as any, + user_user2_token_apiKeyClientId: null as any, + user_user2_token_apiKeyClientSecret: null as any, + + // Global state provider data + // use pattern global_{stateDefinitionName}_{keyDefinitionKey} for global data + global_tokenDiskLocal_emailTwoFactorTokenRecord: { + user1Email: "twoFactorToken", + user2Email: "twoFactorToken", + }, + + global: { + // no longer has twoFactorToken + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user1", "user2", "user3"], + user1: { + tokens: { + otherStuff: "overStuff2", + }, + profile: { + email: "user1Email", + otherStuff: "overStuff3", + }, + keys: { + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }, + user2: { + tokens: { + otherStuff: "overStuff2", + }, + profile: { + email: "user2Email", + otherStuff: "overStuff3", + }, + keys: { + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +describe("TokenServiceStateProviderMigrator", () => { + let helper: MockProxy; + let sut: TokenServiceStateProviderMigrator; + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(preMigrationJson(), 37); + sut = new TokenServiceStateProviderMigrator(37, 38); + }); + + it("should remove state service data from all accounts that have it", async () => { + await sut.migrate(helper); + + expect(helper.set).toHaveBeenCalledWith("user1", { + tokens: { + otherStuff: "overStuff2", + }, + profile: { + email: "user1Email", + otherStuff: "overStuff3", + }, + keys: { + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }); + + expect(helper.set).toHaveBeenCalledTimes(2); + expect(helper.set).not.toHaveBeenCalledWith("user2", any()); + expect(helper.set).not.toHaveBeenCalledWith("user3", any()); + }); + + it("should migrate data to state providers for defined accounts that have the data", async () => { + await sut.migrate(helper); + + // Two factor Token Migration + expect(helper.setToGlobal).toHaveBeenLastCalledWith( + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + { + user1Email: "twoFactorToken", + user2Email: "twoFactorToken", + }, + ); + expect(helper.setToGlobal).toHaveBeenCalledTimes(1); + + expect(helper.setToUser).toHaveBeenCalledWith("user1", ACCESS_TOKEN_DISK, "accessToken"); + expect(helper.setToUser).toHaveBeenCalledWith("user1", REFRESH_TOKEN_DISK, "refreshToken"); + expect(helper.setToUser).toHaveBeenCalledWith( + "user1", + API_KEY_CLIENT_ID_DISK, + "apiKeyClientId", + ); + expect(helper.setToUser).toHaveBeenCalledWith( + "user1", + API_KEY_CLIENT_SECRET_DISK, + "apiKeyClientSecret", + ); + + expect(helper.setToUser).not.toHaveBeenCalledWith("user2", ACCESS_TOKEN_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user2", REFRESH_TOKEN_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user2", API_KEY_CLIENT_ID_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user2", API_KEY_CLIENT_SECRET_DISK, any()); + + // Expect that we didn't migrate anything to user 3 + + expect(helper.setToUser).not.toHaveBeenCalledWith("user3", ACCESS_TOKEN_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user3", REFRESH_TOKEN_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user3", API_KEY_CLIENT_ID_DISK, any()); + expect(helper.setToUser).not.toHaveBeenCalledWith("user3", API_KEY_CLIENT_SECRET_DISK, any()); + }); + }); + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 38); + sut = new TokenServiceStateProviderMigrator(37, 38); + }); + + it("should null out newly migrated entries in state provider framework", async () => { + await sut.rollback(helper); + + expect(helper.setToGlobal).toHaveBeenCalledWith( + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + null, + ); + + expect(helper.setToUser).toHaveBeenCalledWith("user1", ACCESS_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user1", REFRESH_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user1", API_KEY_CLIENT_ID_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user1", API_KEY_CLIENT_SECRET_DISK, null); + + expect(helper.setToUser).toHaveBeenCalledWith("user2", ACCESS_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user2", REFRESH_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user2", API_KEY_CLIENT_ID_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user2", API_KEY_CLIENT_SECRET_DISK, null); + + expect(helper.setToUser).toHaveBeenCalledWith("user3", ACCESS_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user3", REFRESH_TOKEN_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user3", API_KEY_CLIENT_ID_DISK, null); + expect(helper.setToUser).toHaveBeenCalledWith("user3", API_KEY_CLIENT_SECRET_DISK, null); + }); + + it("should add back data to all accounts that had migrated data (only user 1)", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalledWith("user1", { + tokens: { + accessToken: "accessToken", + refreshToken: "refreshToken", + otherStuff: "overStuff2", + }, + profile: { + apiKeyClientId: "apiKeyClientId", + email: "user1Email", + otherStuff: "overStuff3", + }, + keys: { + apiKeyClientSecret: "apiKeyClientSecret", + otherStuff: "overStuff4", + }, + otherStuff: "otherStuff5", + }); + }); + + it("should add back the global twoFactorToken", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalledWith("global", { + twoFactorToken: "twoFactorToken", + otherStuff: "otherStuff1", + }); + }); + + it("should not add data back if data wasn't migrated or acct doesn't exist", async () => { + await sut.rollback(helper); + + // no data to add back for user2 (acct exists but no migrated data) and user3 (no acct) + expect(helper.set).not.toHaveBeenCalledWith("user2", any()); + expect(helper.set).not.toHaveBeenCalledWith("user3", any()); + }); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.ts b/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.ts new file mode 100644 index 00000000000..17753d21879 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/38-migrate-token-svc-to-state-provider.ts @@ -0,0 +1,231 @@ +import { KeyDefinitionLike, MigrationHelper, StateDefinitionLike } from "../migration-helper"; +import { Migrator } from "../migrator"; + +// Types to represent data as it is stored in JSON +type ExpectedAccountType = { + tokens?: { + accessToken?: string; + refreshToken?: string; + }; + profile?: { + apiKeyClientId?: string; + email?: string; + }; + keys?: { + apiKeyClientSecret?: string; + }; +}; + +type ExpectedGlobalType = { + twoFactorToken?: string; +}; + +export const EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL: KeyDefinitionLike = { + key: "emailTwoFactorTokenRecord", + stateDefinition: { + name: "tokenDiskLocal", + }, +}; + +const TOKEN_STATE_DEF_LIKE: StateDefinitionLike = { + name: "token", +}; + +export const ACCESS_TOKEN_DISK: KeyDefinitionLike = { + key: "accessToken", // matches KeyDefinition.key + stateDefinition: TOKEN_STATE_DEF_LIKE, +}; + +export const REFRESH_TOKEN_DISK: KeyDefinitionLike = { + key: "refreshToken", + stateDefinition: TOKEN_STATE_DEF_LIKE, +}; + +export const API_KEY_CLIENT_ID_DISK: KeyDefinitionLike = { + key: "apiKeyClientId", + stateDefinition: TOKEN_STATE_DEF_LIKE, +}; + +export const API_KEY_CLIENT_SECRET_DISK: KeyDefinitionLike = { + key: "apiKeyClientSecret", + stateDefinition: TOKEN_STATE_DEF_LIKE, +}; + +export class TokenServiceStateProviderMigrator extends Migrator<37, 38> { + async migrate(helper: MigrationHelper): Promise { + // Move global data + const globalData = await helper.get("global"); + + // Create new global record for 2FA token that we can accumulate data in + const emailTwoFactorTokenRecord = {}; + + const accounts = await helper.getAccounts(); + async function migrateAccount( + userId: string, + account: ExpectedAccountType | undefined, + globalTwoFactorToken: string | undefined, + emailTwoFactorTokenRecord: Record, + ): Promise { + let updatedAccount = false; + + // migrate 2FA token from global to user state + // Due to the existing implmentation, n users on the same device share the same global state value for 2FA token. + // So, we will just migrate it to all users to keep it valid for whichever was the user that set it previously. + // Note: don't bother migrating 2FA Token if user account or email is undefined + const email = account?.profile?.email; + if (globalTwoFactorToken != undefined && account != undefined && email != undefined) { + emailTwoFactorTokenRecord[email] = globalTwoFactorToken; + // Note: don't set updatedAccount to true here as we aren't updating + // the legacy user state, just migrating a global state to a new user state + } + + // Migrate access token + const existingAccessToken = account?.tokens?.accessToken; + + if (existingAccessToken != null) { + // Only migrate data that exists + await helper.setToUser(userId, ACCESS_TOKEN_DISK, existingAccessToken); + delete account.tokens.accessToken; + updatedAccount = true; + } + + // Migrate refresh token + const existingRefreshToken = account?.tokens?.refreshToken; + + if (existingRefreshToken != null) { + await helper.setToUser(userId, REFRESH_TOKEN_DISK, existingRefreshToken); + delete account.tokens.refreshToken; + updatedAccount = true; + } + + // Migrate API key client id + const existingApiKeyClientId = account?.profile?.apiKeyClientId; + + if (existingApiKeyClientId != null) { + await helper.setToUser(userId, API_KEY_CLIENT_ID_DISK, existingApiKeyClientId); + delete account.profile.apiKeyClientId; + updatedAccount = true; + } + + // Migrate API key client secret + const existingApiKeyClientSecret = account?.keys?.apiKeyClientSecret; + if (existingApiKeyClientSecret != null) { + await helper.setToUser(userId, API_KEY_CLIENT_SECRET_DISK, existingApiKeyClientSecret); + delete account.keys.apiKeyClientSecret; + updatedAccount = true; + } + + if (updatedAccount) { + // Save the migrated account only if it was updated + await helper.set(userId, account); + } + } + + await Promise.all([ + ...accounts.map(({ userId, account }) => + migrateAccount(userId, account, globalData?.twoFactorToken, emailTwoFactorTokenRecord), + ), + ]); + + // Save the global 2FA token record + await helper.setToGlobal(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, emailTwoFactorTokenRecord); + + // Delete global data + delete globalData?.twoFactorToken; + await helper.set("global", globalData); + } + + async rollback(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + + // Since we migrated the global 2FA token to all users, we need to rollback the 2FA token for all users + // but we only need to set it to the global state once + + // Go through accounts and find the first user that has a non-null email and 2FA token + let migratedTwoFactorToken: string | null = null; + for (const { account } of accounts) { + const email = account?.profile?.email; + if (email == null) { + continue; + } + const emailTwoFactorTokenRecord: Record = await helper.getFromGlobal( + EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, + ); + + migratedTwoFactorToken = emailTwoFactorTokenRecord[email]; + + if (migratedTwoFactorToken != null) { + break; + } + } + + if (migratedTwoFactorToken != null) { + let legacyGlobal = await helper.get("global"); + if (!legacyGlobal) { + legacyGlobal = {}; + } + legacyGlobal.twoFactorToken = migratedTwoFactorToken; + await helper.set("global", legacyGlobal); + } + + // delete global 2FA token record + await helper.setToGlobal(EMAIL_TWO_FACTOR_TOKEN_RECORD_DISK_LOCAL, null); + + async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise { + let updatedLegacyAccount = false; + + // Rollback access token + const migratedAccessToken = await helper.getFromUser(userId, ACCESS_TOKEN_DISK); + + if (account?.tokens && migratedAccessToken != null) { + account.tokens.accessToken = migratedAccessToken; + updatedLegacyAccount = true; + } + + await helper.setToUser(userId, ACCESS_TOKEN_DISK, null); + + // Rollback refresh token + const migratedRefreshToken = await helper.getFromUser(userId, REFRESH_TOKEN_DISK); + + if (account?.tokens && migratedRefreshToken != null) { + account.tokens.refreshToken = migratedRefreshToken; + updatedLegacyAccount = true; + } + + await helper.setToUser(userId, REFRESH_TOKEN_DISK, null); + + // Rollback API key client id + + const migratedApiKeyClientId = await helper.getFromUser( + userId, + API_KEY_CLIENT_ID_DISK, + ); + + if (account?.profile && migratedApiKeyClientId != null) { + account.profile.apiKeyClientId = migratedApiKeyClientId; + updatedLegacyAccount = true; + } + + await helper.setToUser(userId, API_KEY_CLIENT_ID_DISK, null); + + // Rollback API key client secret + const migratedApiKeyClientSecret = await helper.getFromUser( + userId, + API_KEY_CLIENT_SECRET_DISK, + ); + + if (account?.keys && migratedApiKeyClientSecret != null) { + account.keys.apiKeyClientSecret = migratedApiKeyClientSecret; + updatedLegacyAccount = true; + } + + await helper.setToUser(userId, API_KEY_CLIENT_SECRET_DISK, null); + + if (updatedLegacyAccount) { + await helper.set(userId, account); + } + } + + await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]); + } +} diff --git a/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.spec.ts b/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.spec.ts new file mode 100644 index 00000000000..8616dda81de --- /dev/null +++ b/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.spec.ts @@ -0,0 +1,126 @@ +import { MockProxy, any } from "jest-mock-extended"; + +import { MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + MoveBillingAccountProfileMigrator, +} from "./39-move-billing-account-profile-to-state-providers"; + +const exampleJSON = () => ({ + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2", "user-3"], + "user-1": { + profile: { + hasPremiumPersonally: true, + hasPremiumFromOrganization: false, + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + otherStuff: "otherStuff4", + }, +}); + +const rollbackJSON = () => ({ + "user_user-1_billing_accountProfile": { + hasPremiumPersonally: true, + hasPremiumFromOrganization: false, + }, + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2", "user-3"], + "user-1": { + profile: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + otherStuff: "otherStuff4", + }, +}); + +describe("MoveBillingAccountProfileToStateProviders migrator", () => { + let helper: MockProxy; + let sut: MoveBillingAccountProfileMigrator; + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(exampleJSON(), 39); + sut = new MoveBillingAccountProfileMigrator(38, 39); + }); + + it("removes from all accounts", async () => { + await sut.migrate(helper); + expect(helper.set).toHaveBeenCalledTimes(1); + expect(helper.set).toHaveBeenCalledWith("user-1", { + profile: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it("sets hasPremiumPersonally value for account that have it", async () => { + await sut.migrate(helper); + + expect(helper.setToUser).toHaveBeenCalledWith( + "user-1", + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + { hasPremiumFromOrganization: false, hasPremiumPersonally: true }, + ); + }); + + it("should not call extra setToUser", async () => { + await sut.migrate(helper); + + expect(helper.setToUser).toHaveBeenCalledTimes(1); + }); + }); + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 39); + sut = new MoveBillingAccountProfileMigrator(38, 39); + }); + + it("nulls out new values", async () => { + await sut.rollback(helper); + + expect(helper.setToUser).toHaveBeenCalledWith( + "user-1", + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + null, + ); + }); + + it("adds explicit value back to accounts", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalledTimes(1); + expect(helper.set).toHaveBeenCalledWith("user-1", { + profile: { + hasPremiumPersonally: true, + hasPremiumFromOrganization: false, + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it.each(["user-2", "user-3"])( + "does not restore values when accounts are not present", + async (userId) => { + await sut.rollback(helper); + + expect(helper.set).not.toHaveBeenCalledWith(userId, any()); + }, + ); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.ts b/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.ts new file mode 100644 index 00000000000..b6c0e531ef5 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/39-move-billing-account-profile-to-state-providers.ts @@ -0,0 +1,67 @@ +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { Migrator } from "../migrator"; + +type ExpectedAccountType = { + profile?: { + hasPremiumPersonally?: boolean; + hasPremiumFromOrganization?: boolean; + }; +}; + +type ExpectedBillingAccountProfileType = { + hasPremiumPersonally: boolean; + hasPremiumFromOrganization: boolean; +}; + +export const BILLING_ACCOUNT_PROFILE_KEY_DEFINITION: KeyDefinitionLike = { + key: "accountProfile", + stateDefinition: { + name: "billing", + }, +}; + +export class MoveBillingAccountProfileMigrator extends Migrator<38, 39> { + async migrate(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + + const migrateAccount = async (userId: string, account: ExpectedAccountType): Promise => { + const hasPremiumPersonally = account?.profile?.hasPremiumPersonally; + const hasPremiumFromOrganization = account?.profile?.hasPremiumFromOrganization; + + if (hasPremiumPersonally != null || hasPremiumFromOrganization != null) { + await helper.setToUser(userId, BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, { + hasPremiumPersonally: hasPremiumPersonally, + hasPremiumFromOrganization: hasPremiumFromOrganization, + }); + + delete account?.profile?.hasPremiumPersonally; + delete account?.profile?.hasPremiumFromOrganization; + await helper.set(userId, account); + } + }; + + await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]); + } + + async rollback(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + const rollbackAccount = async (userId: string, account: ExpectedAccountType): Promise => { + const value = await helper.getFromUser( + userId, + BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, + ); + + if (account && value) { + account.profile = Object.assign(account.profile ?? {}, { + hasPremiumPersonally: value?.hasPremiumPersonally, + hasPremiumFromOrganization: value?.hasPremiumFromOrganization, + }); + await helper.set(userId, account); + } + + await helper.setToUser(userId, BILLING_ACCOUNT_PROFILE_KEY_DEFINITION, null); + }; + + await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]); + } +} diff --git a/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.spec.ts new file mode 100644 index 00000000000..94078e8153a --- /dev/null +++ b/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.spec.ts @@ -0,0 +1,183 @@ +import { any, MockProxy } from "jest-mock-extended"; + +import { MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { OrganizationMigrator } from "./40-move-organization-state-to-state-provider"; + +const testDate = new Date(); +function exampleOrganization1() { + return JSON.stringify({ + id: "id", + name: "name", + status: 0, + type: 0, + enabled: false, + usePolicies: false, + useGroups: false, + useDirectory: false, + useEvents: false, + useTotp: false, + use2fa: false, + useApi: false, + useSso: false, + useKeyConnector: false, + useScim: false, + useCustomPermissions: false, + useResetPassword: false, + useSecretsManager: false, + usePasswordManager: false, + useActivateAutofillPolicy: false, + selfHost: false, + usersGetPremium: false, + seats: 0, + maxCollections: 0, + ssoBound: false, + identifier: "identifier", + resetPasswordEnrolled: false, + userId: "userId", + hasPublicAndPrivateKeys: false, + providerId: "providerId", + providerName: "providerName", + isProviderUser: false, + isMember: false, + familySponsorshipFriendlyName: "fsfn", + familySponsorshipAvailable: false, + planProductType: 0, + keyConnectorEnabled: false, + keyConnectorUrl: "kcu", + accessSecretsManager: false, + limitCollectionCreationDeletion: false, + allowAdminAccessToAllCollectionItems: false, + flexibleCollections: false, + familySponsorshipLastSyncDate: testDate, + }); +} + +function exampleJSON() { + return { + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + data: { + organizations: { + "organization-id-1": exampleOrganization1(), + "organization-id-2": { + // ... + }, + }, + otherStuff: "overStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + data: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +function rollbackJSON() { + return { + "user_user-1_organizations_organizations": { + "organization-id-1": exampleOrganization1(), + "organization-id-2": { + // ... + }, + }, + "user_user-2_organizations_organizations": null as any, + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + data: { + otherStuff: "overStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + data: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +describe("OrganizationMigrator", () => { + let helper: MockProxy; + let sut: OrganizationMigrator; + const keyDefinitionLike = { + key: "organizations", + stateDefinition: { + name: "organizations", + }, + }; + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(exampleJSON(), 40); + sut = new OrganizationMigrator(39, 40); + }); + + it("should remove organizations from all accounts", async () => { + await sut.migrate(helper); + expect(helper.set).toHaveBeenCalledWith("user-1", { + data: { + otherStuff: "overStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it("should set organizations value for each account", async () => { + await sut.migrate(helper); + + expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, { + "organization-id-1": exampleOrganization1(), + "organization-id-2": { + // ... + }, + }); + }); + }); + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 40); + sut = new OrganizationMigrator(39, 40); + }); + + it.each(["user-1", "user-2"])("should null out new values", async (userId) => { + await sut.rollback(helper); + expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null); + }); + + it("should add explicit value back to accounts", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalledWith("user-1", { + data: { + organizations: { + "organization-id-1": exampleOrganization1(), + "organization-id-2": { + // ... + }, + }, + otherStuff: "overStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it("should not try to restore values to missing accounts", async () => { + await sut.rollback(helper); + expect(helper.set).not.toHaveBeenCalledWith("user-3", any()); + }); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.ts b/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.ts new file mode 100644 index 00000000000..1dfb019942a --- /dev/null +++ b/libs/common/src/state-migrations/migrations/40-move-organization-state-to-state-provider.ts @@ -0,0 +1,148 @@ +import { Jsonify } from "type-fest"; + +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { Migrator } from "../migrator"; + +// Local declarations of `OrganizationData` and the types of it's properties. +// Duplicated to remain frozen in time when migration occurs. +enum OrganizationUserStatusType { + Invited = 0, + Accepted = 1, + Confirmed = 2, + Revoked = -1, +} + +enum OrganizationUserType { + Owner = 0, + Admin = 1, + User = 2, + Manager = 3, + Custom = 4, +} + +type PermissionsApi = { + accessEventLogs: boolean; + accessImportExport: boolean; + accessReports: boolean; + createNewCollections: boolean; + editAnyCollection: boolean; + deleteAnyCollection: boolean; + editAssignedCollections: boolean; + deleteAssignedCollections: boolean; + manageCiphers: boolean; + manageGroups: boolean; + manageSso: boolean; + managePolicies: boolean; + manageUsers: boolean; + manageResetPassword: boolean; + manageScim: boolean; +}; + +enum ProviderType { + Msp = 0, + Reseller = 1, +} + +enum ProductType { + Free = 0, + Families = 1, + Teams = 2, + Enterprise = 3, + TeamsStarter = 4, +} + +type OrganizationData = { + id: string; + name: string; + status: OrganizationUserStatusType; + type: OrganizationUserType; + enabled: boolean; + usePolicies: boolean; + useGroups: boolean; + useDirectory: boolean; + useEvents: boolean; + useTotp: boolean; + use2fa: boolean; + useApi: boolean; + useSso: boolean; + useKeyConnector: boolean; + useScim: boolean; + useCustomPermissions: boolean; + useResetPassword: boolean; + useSecretsManager: boolean; + usePasswordManager: boolean; + useActivateAutofillPolicy: boolean; + selfHost: boolean; + usersGetPremium: boolean; + seats: number; + maxCollections: number; + maxStorageGb?: number; + ssoBound: boolean; + identifier: string; + permissions: PermissionsApi; + resetPasswordEnrolled: boolean; + userId: string; + hasPublicAndPrivateKeys: boolean; + providerId: string; + providerName: string; + providerType?: ProviderType; + isProviderUser: boolean; + isMember: boolean; + familySponsorshipFriendlyName: string; + familySponsorshipAvailable: boolean; + planProductType: ProductType; + keyConnectorEnabled: boolean; + keyConnectorUrl: string; + familySponsorshipLastSyncDate?: Date; + familySponsorshipValidUntil?: Date; + familySponsorshipToDelete?: boolean; + accessSecretsManager: boolean; + limitCollectionCreationDeletion: boolean; + allowAdminAccessToAllCollectionItems: boolean; + flexibleCollections: boolean; +}; + +type ExpectedAccountType = { + data?: { + organizations?: Record>; + }; +}; + +const USER_ORGANIZATIONS: KeyDefinitionLike = { + key: "organizations", + stateDefinition: { + name: "organizations", + }, +}; + +export class OrganizationMigrator extends Migrator<39, 40> { + async migrate(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + async function migrateAccount(userId: string, account: ExpectedAccountType): Promise { + const value = account?.data?.organizations; + if (value != null) { + await helper.setToUser(userId, USER_ORGANIZATIONS, value); + delete account.data.organizations; + await helper.set(userId, account); + } + } + + await Promise.all(accounts.map(({ userId, account }) => migrateAccount(userId, account))); + } + + async rollback(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise { + const value = await helper.getFromUser(userId, USER_ORGANIZATIONS); + if (account) { + account.data = Object.assign(account.data ?? {}, { + organizations: value, + }); + await helper.set(userId, account); + } + await helper.setToUser(userId, USER_ORGANIZATIONS, null); + } + + await Promise.all(accounts.map(({ userId, account }) => rollbackAccount(userId, account))); + } +} diff --git a/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.spec.ts new file mode 100644 index 00000000000..d88100ebe2c --- /dev/null +++ b/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.spec.ts @@ -0,0 +1,168 @@ +import { MockProxy, any } from "jest-mock-extended"; + +import { MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { EventCollectionMigrator } from "./41-move-event-collection-to-state-provider"; + +function exampleJSON() { + return { + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + data: { + eventCollection: [ + { + type: 1107, + cipherId: "5154f91d-c469-4d23-aefa-b12a0140d684", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T21:59:50.169Z", + }, + { + type: 1107, + cipherId: "ed4661bd-412c-4b05-89a2-b12a01697a2c", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T22:02:06.089Z", + }, + ], + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + data: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +function rollbackJSON() { + return { + "user_user-1_eventCollection_eventCollection": [ + { + type: 1107, + cipherId: "5154f91d-c469-4d23-aefa-b12a0140d684", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T21:59:50.169Z", + }, + { + type: 1107, + cipherId: "ed4661bd-412c-4b05-89a2-b12a01697a2c", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T22:02:06.089Z", + }, + ], + "user_user-2_eventCollection_data": null as any, + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + data: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + data: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +describe("EventCollectionMigrator", () => { + let helper: MockProxy; + let sut: EventCollectionMigrator; + const keyDefinitionLike = { + stateDefinition: { + name: "eventCollection", + }, + key: "eventCollection", + }; + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(exampleJSON(), 40); + sut = new EventCollectionMigrator(40, 41); + }); + + it("should remove event collections from all accounts", async () => { + await sut.migrate(helper); + expect(helper.set).toHaveBeenCalledWith("user-1", { + data: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it("should set event collections for each account", async () => { + await sut.migrate(helper); + + expect(helper.setToUser).toHaveBeenCalledWith("user-1", keyDefinitionLike, [ + { + type: 1107, + cipherId: "5154f91d-c469-4d23-aefa-b12a0140d684", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T21:59:50.169Z", + }, + { + type: 1107, + cipherId: "ed4661bd-412c-4b05-89a2-b12a01697a2c", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T22:02:06.089Z", + }, + ]); + }); + }); + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 41); + sut = new EventCollectionMigrator(40, 41); + }); + + it.each(["user-1", "user-2"])("should null out new values", async (userId) => { + await sut.rollback(helper); + expect(helper.setToUser).toHaveBeenCalledWith(userId, keyDefinitionLike, null); + }); + + it("should add event collection values back to accounts", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalled(); + expect(helper.set).toHaveBeenCalledWith("user-1", { + data: { + eventCollection: [ + { + type: 1107, + cipherId: "5154f91d-c469-4d23-aefa-b12a0140d684", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T21:59:50.169Z", + }, + { + type: 1107, + cipherId: "ed4661bd-412c-4b05-89a2-b12a01697a2c", + organizationId: "278d5f91-835b-459a-a229-b11e01336d6d", + date: "2024-03-05T22:02:06.089Z", + }, + ], + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }); + }); + + it("should not try to restore values to missing accounts", async () => { + await sut.rollback(helper); + + expect(helper.set).not.toHaveBeenCalledWith("user-3", any()); + }); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.ts b/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.ts new file mode 100644 index 00000000000..f2f4d94d195 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/41-move-event-collection-to-state-provider.ts @@ -0,0 +1,49 @@ +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { Migrator } from "../migrator"; + +type ExpectedAccountState = { + data?: { + eventCollection?: []; + }; +}; + +const EVENT_COLLECTION: KeyDefinitionLike = { + stateDefinition: { + name: "eventCollection", + }, + key: "eventCollection", +}; + +export class EventCollectionMigrator extends Migrator<40, 41> { + async migrate(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + + async function migrateAccount(userId: string, account: ExpectedAccountState): Promise { + const value = account?.data?.eventCollection; + if (value != null) { + await helper.setToUser(userId, EVENT_COLLECTION, value); + delete account.data.eventCollection; + await helper.set(userId, account); + } + } + + await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]); + } + + async rollback(helper: MigrationHelper): Promise { + const accounts = await helper.getAccounts(); + + async function rollbackAccount(userId: string, account: ExpectedAccountState): Promise { + const value = await helper.getFromUser(userId, EVENT_COLLECTION); + if (account) { + account.data = Object.assign(account.data ?? {}, { + eventCollection: value, + }); + + await helper.set(userId, account); + } + await helper.setToUser(userId, EVENT_COLLECTION, null); + } + await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]); + } +} diff --git a/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.spec.ts b/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.spec.ts new file mode 100644 index 00000000000..ddb3bd99072 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.spec.ts @@ -0,0 +1,108 @@ +import { MockProxy } from "jest-mock-extended"; + +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { mockMigrationHelper } from "../migration-helper.spec"; + +import { EnableFaviconMigrator } from "./42-move-enable-favicon-to-domain-settings-state-provider"; + +function exampleJSON() { + return { + global: { + otherStuff: "otherStuff1", + disableFavicon: true, + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + settings: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + settings: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +function rollbackJSON() { + return { + global_domainSettings_showFavicons: false, + global: { + otherStuff: "otherStuff1", + }, + authenticatedAccounts: ["user-1", "user-2"], + "user-1": { + settings: { + otherStuff: "otherStuff2", + }, + otherStuff: "otherStuff3", + }, + "user-2": { + settings: { + otherStuff: "otherStuff4", + }, + otherStuff: "otherStuff5", + }, + }; +} + +const showFaviconsKeyDefinition: KeyDefinitionLike = { + stateDefinition: { + name: "domainSettings", + }, + key: "showFavicons", +}; + +describe("EnableFaviconMigrator", () => { + let helper: MockProxy; + let sut: EnableFaviconMigrator; + + describe("migrate", () => { + beforeEach(() => { + helper = mockMigrationHelper(exampleJSON(), 41); + sut = new EnableFaviconMigrator(41, 42); + }); + + it("should remove global disableFavicon", async () => { + await sut.migrate(helper); + expect(helper.set).toHaveBeenCalledTimes(1); + expect(helper.set).toHaveBeenCalledWith("global", { + otherStuff: "otherStuff1", + }); + }); + + it("should set global showFavicons", async () => { + await sut.migrate(helper); + + expect(helper.setToGlobal).toHaveBeenCalledTimes(1); + expect(helper.setToGlobal).toHaveBeenCalledWith(showFaviconsKeyDefinition, false); + }); + }); + + describe("rollback", () => { + beforeEach(() => { + helper = mockMigrationHelper(rollbackJSON(), 42); + sut = new EnableFaviconMigrator(41, 42); + }); + + it("should null global showFavicons", async () => { + await sut.rollback(helper); + + expect(helper.setToGlobal).toHaveBeenCalledTimes(1); + expect(helper.setToGlobal).toHaveBeenCalledWith(showFaviconsKeyDefinition, null); + }); + + it("should add global disableFavicon back", async () => { + await sut.rollback(helper); + + expect(helper.set).toHaveBeenCalledTimes(1); + expect(helper.set).toHaveBeenCalledWith("global", { + disableFavicon: true, + otherStuff: "otherStuff1", + }); + }); + }); +}); diff --git a/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.ts b/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.ts new file mode 100644 index 00000000000..7da988ad8f6 --- /dev/null +++ b/libs/common/src/state-migrations/migrations/42-move-enable-favicon-to-domain-settings-state-provider.ts @@ -0,0 +1,45 @@ +import { KeyDefinitionLike, MigrationHelper } from "../migration-helper"; +import { Migrator } from "../migrator"; + +type ExpectedGlobalState = { + disableFavicon?: boolean; +}; + +const ShowFaviconDefinition: KeyDefinitionLike = { + stateDefinition: { + name: "domainSettings", + }, + key: "showFavicons", +}; + +export class EnableFaviconMigrator extends Migrator<41, 42> { + async migrate(helper: MigrationHelper): Promise { + // global state ("disableFavicon" -> "showFavicons") + const globalState = await helper.get("global"); + + if (globalState?.disableFavicon != null) { + await helper.setToGlobal(ShowFaviconDefinition, !globalState.disableFavicon); + + // delete `disableFavicon` from state global + delete globalState.disableFavicon; + + await helper.set("global", globalState); + } + } + + async rollback(helper: MigrationHelper): Promise { + // global state ("showFavicons" -> "disableFavicon") + const globalState = (await helper.get("global")) || {}; + const showFavicons: boolean = await helper.getFromGlobal(ShowFaviconDefinition); + + if (showFavicons != null) { + await helper.set("global", { + ...globalState, + disableFavicon: !showFavicons, + }); + + // remove the global state provider framework key for `showFavicons` + await helper.setToGlobal(ShowFaviconDefinition, null); + } + } +} diff --git a/libs/common/src/vault/icon/build-cipher-icon.ts b/libs/common/src/vault/icon/build-cipher-icon.ts index 80bc5cbe3ae..9e6e671f44d 100644 --- a/libs/common/src/vault/icon/build-cipher-icon.ts +++ b/libs/common/src/vault/icon/build-cipher-icon.ts @@ -2,12 +2,7 @@ import { Utils } from "../../platform/misc/utils"; import { CipherType } from "../enums/cipher-type"; import { CipherView } from "../models/view/cipher.view"; -export function buildCipherIcon( - iconsServerUrl: string, - cipher: CipherView, - isFaviconDisabled: boolean, -) { - const imageEnabled = !isFaviconDisabled; +export function buildCipherIcon(iconsServerUrl: string, cipher: CipherView, showFavicon: boolean) { let icon; let image; let fallbackImage = ""; @@ -38,17 +33,17 @@ export function buildCipherIcon( icon = "bwi-apple"; image = null; } else if ( - imageEnabled && + showFavicon && hostnameUri.indexOf("://") === -1 && hostnameUri.indexOf(".") > -1 ) { hostnameUri = `http://${hostnameUri}`; isWebsite = true; - } else if (imageEnabled) { + } else if (showFavicon) { isWebsite = hostnameUri.indexOf("http") === 0 && hostnameUri.indexOf(".") > -1; } - if (imageEnabled && isWebsite) { + if (showFavicon && isWebsite) { try { image = `${iconsServerUrl}/${Utils.getHostname(hostnameUri)}/icon.png`; fallbackImage = "images/bwi-globe.png"; @@ -65,7 +60,7 @@ export function buildCipherIcon( break; case CipherType.Card: icon = "bwi-credit-card"; - if (imageEnabled && cipher.card.brand in cardIcons) { + if (showFavicon && cipher.card.brand in cardIcons) { icon = `credit-card-icon ${cardIcons[cipher.card.brand]}`; } break; @@ -77,7 +72,7 @@ export function buildCipherIcon( } return { - imageEnabled, + imageEnabled: showFavicon, image, fallbackImage, icon, diff --git a/libs/common/src/vault/services/fido2/fido2-client.service.spec.ts b/libs/common/src/vault/services/fido2/fido2-client.service.spec.ts index dd90c53cb1a..2f76c5043af 100644 --- a/libs/common/src/vault/services/fido2/fido2-client.service.spec.ts +++ b/libs/common/src/vault/services/fido2/fido2-client.service.spec.ts @@ -5,7 +5,6 @@ import { AuthService } from "../../../auth/abstractions/auth.service"; import { AuthenticationStatus } from "../../../auth/enums/authentication-status"; import { DomainSettingsService } from "../../../autofill/services/domain-settings.service"; import { ConfigServiceAbstraction } from "../../../platform/abstractions/config/config.service.abstraction"; -import { StateService } from "../../../platform/abstractions/state.service"; import { Utils } from "../../../platform/misc/utils"; import { Fido2AuthenticatorError, @@ -33,7 +32,6 @@ describe("FidoAuthenticatorService", () => { let authenticator!: MockProxy; let configService!: MockProxy; let authService!: MockProxy; - let stateService!: MockProxy; let vaultSettingsService: MockProxy; let domainSettingsService: MockProxy; let client!: Fido2ClientService; @@ -43,7 +41,6 @@ describe("FidoAuthenticatorService", () => { authenticator = mock(); configService = mock(); authService = mock(); - stateService = mock(); vaultSettingsService = mock(); domainSettingsService = mock(); @@ -51,7 +48,6 @@ describe("FidoAuthenticatorService", () => { authenticator, configService, authService, - stateService, vaultSettingsService, domainSettingsService, ); diff --git a/libs/common/src/vault/services/fido2/fido2-client.service.ts b/libs/common/src/vault/services/fido2/fido2-client.service.ts index 257700453e4..c725b226372 100644 --- a/libs/common/src/vault/services/fido2/fido2-client.service.ts +++ b/libs/common/src/vault/services/fido2/fido2-client.service.ts @@ -6,7 +6,6 @@ import { AuthenticationStatus } from "../../../auth/enums/authentication-status" import { DomainSettingsService } from "../../../autofill/services/domain-settings.service"; import { ConfigServiceAbstraction } from "../../../platform/abstractions/config/config.service.abstraction"; import { LogService } from "../../../platform/abstractions/log.service"; -import { StateService } from "../../../platform/abstractions/state.service"; import { Utils } from "../../../platform/misc/utils"; import { Fido2AuthenticatorError, @@ -43,7 +42,6 @@ export class Fido2ClientService implements Fido2ClientServiceAbstraction { private authenticator: Fido2AuthenticatorService, private configService: ConfigServiceAbstraction, private authService: AuthService, - private stateService: StateService, private vaultSettingsService: VaultSettingsService, private domainSettingsService: DomainSettingsService, private logService?: LogService, diff --git a/libs/common/src/vault/services/sync/sync.service.ts b/libs/common/src/vault/services/sync/sync.service.ts index 1b46bf43294..1b3e63d0012 100644 --- a/libs/common/src/vault/services/sync/sync.service.ts +++ b/libs/common/src/vault/services/sync/sync.service.ts @@ -11,6 +11,7 @@ import { AvatarService } from "../../../auth/abstractions/avatar.service"; import { KeyConnectorService } from "../../../auth/abstractions/key-connector.service"; import { ForceSetPasswordReason } from "../../../auth/models/domain/force-set-password-reason"; import { DomainSettingsService } from "../../../autofill/services/domain-settings.service"; +import { BillingAccountProfileStateService } from "../../../billing/abstractions/account/billing-account-profile-state.service"; import { DomainsResponse } from "../../../models/response/domains.response"; import { SyncCipherNotification, @@ -62,6 +63,7 @@ export class SyncService implements SyncServiceAbstraction { private sendApiService: SendApiService, private avatarService: AvatarService, private logoutCallback: (expired: boolean) => Promise, + private billingAccountProfileStateService: BillingAccountProfileStateService, ) {} async getLastSync(): Promise { @@ -314,8 +316,11 @@ export class SyncService implements SyncServiceAbstraction { await this.avatarService.setAvatarColor(response.avatarColor); await this.stateService.setSecurityStamp(response.securityStamp); await this.stateService.setEmailVerified(response.emailVerified); - await this.stateService.setHasPremiumPersonally(response.premiumPersonally); - await this.stateService.setHasPremiumFromOrganization(response.premiumFromOrganization); + + await this.billingAccountProfileStateService.setHasPremium( + response.premiumPersonally, + response.premiumFromOrganization, + ); await this.keyConnectorService.setUsesKeyConnector(response.usesKeyConnector); await this.setForceSetPasswordReasonIfNeeded(response); diff --git a/libs/importer/src/components/import.component.html b/libs/importer/src/components/import.component.html index 6c24b80f92d..836a1d9a1a1 100644 --- a/libs/importer/src/components/import.component.html +++ b/libs/importer/src/components/import.component.html @@ -37,7 +37,7 @@ @@ -46,7 +46,7 @@ diff --git a/libs/importer/src/components/import.component.ts b/libs/importer/src/components/import.component.ts index 796f02c7761..dfbe8d8c5e9 100644 --- a/libs/importer/src/components/import.component.ts +++ b/libs/importer/src/components/import.component.ts @@ -207,7 +207,7 @@ export class ImportComponent implements OnInit, OnDestroy { this.setImportOptions(); await this.initializeOrganizations(); - if (this.organizationId && this.canAccessImportExport(this.organizationId)) { + if (this.organizationId && (await this.canAccessImportExport(this.organizationId))) { this.handleOrganizationImportInit(); } else { this.handleImportInit(); @@ -359,7 +359,7 @@ export class ImportComponent implements OnInit, OnDestroy { importContents, this.organizationId, this.formGroup.controls.targetSelector.value, - this.canAccessImportExport(this.organizationId) && this._isFromAC, + (await this.canAccessImportExport(this.organizationId)) && this._isFromAC, ); //No errors, display success message @@ -379,11 +379,11 @@ export class ImportComponent implements OnInit, OnDestroy { } } - private canAccessImportExport(organizationId?: string): boolean { + private async canAccessImportExport(organizationId?: string): Promise { if (!organizationId) { return false; } - return this.organizationService.get(this.organizationId)?.canAccessImportExport; + return (await this.organizationService.get(this.organizationId))?.canAccessImportExport; } getFormatInstructionTitle() { diff --git a/libs/importer/src/components/lastpass/lastpass-direct-import.service.ts b/libs/importer/src/components/lastpass/lastpass-direct-import.service.ts index 4b002061e06..7d77bbbc868 100644 --- a/libs/importer/src/components/lastpass/lastpass-direct-import.service.ts +++ b/libs/importer/src/components/lastpass/lastpass-direct-import.service.ts @@ -2,7 +2,6 @@ import { Injectable, NgZone } from "@angular/core"; import { OidcClient } from "oidc-client-ts"; import { Subject, firstValueFrom } from "rxjs"; -import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; import { ClientType } from "@bitwarden/common/enums"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; @@ -32,7 +31,6 @@ export class LastPassDirectImportService { ssoImportCallback$ = this._ssoImportCallback$.asObservable(); constructor( - private tokenService: TokenService, private cryptoFunctionService: CryptoFunctionService, private environmentService: EnvironmentService, private appIdService: AppIdService, @@ -44,7 +42,7 @@ export class LastPassDirectImportService { private dialogService: DialogService, private i18nService: I18nService, ) { - this.vault = new Vault(this.cryptoFunctionService, this.tokenService); + this.vault = new Vault(this.cryptoFunctionService); /** TODO: remove this in favor of dedicated service */ this.broadcasterService.subscribe("LastPassDirectImportService", (message: any) => { diff --git a/libs/importer/src/importers/lastpass/access/vault.ts b/libs/importer/src/importers/lastpass/access/vault.ts index 814390f5c80..13b8b62c108 100644 --- a/libs/importer/src/importers/lastpass/access/vault.ts +++ b/libs/importer/src/importers/lastpass/access/vault.ts @@ -1,6 +1,6 @@ import * as papa from "papaparse"; -import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { decodeJwtTokenToJson } from "@bitwarden/auth/common"; import { HttpStatusCode } from "@bitwarden/common/enums"; import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; @@ -24,10 +24,7 @@ export class Vault { private client: Client; private cryptoUtils: CryptoUtils; - constructor( - private cryptoFunctionService: CryptoFunctionService, - private tokenService: TokenService, - ) { + constructor(private cryptoFunctionService: CryptoFunctionService) { this.cryptoUtils = new CryptoUtils(cryptoFunctionService); const parser = new Parser(cryptoFunctionService, this.cryptoUtils); this.client = new Client(parser, this.cryptoUtils); @@ -212,7 +209,7 @@ export class Vault { } private async getK1FromAccessToken(federatedUser: FederatedUserContext, b64: boolean) { - const decodedAccessToken = await this.tokenService.decodeToken(federatedUser.accessToken); + const decodedAccessToken = decodeJwtTokenToJson(federatedUser.accessToken); const k1 = decodedAccessToken?.LastPassK1 as string; if (k1 != null) { return b64 ? Utils.fromB64ToArray(k1) : Utils.fromByteStringToArray(k1); diff --git a/libs/importer/src/services/import.service.abstraction.ts b/libs/importer/src/services/import.service.abstraction.ts index 95208c9b99f..dc77e76390e 100644 --- a/libs/importer/src/services/import.service.abstraction.ts +++ b/libs/importer/src/services/import.service.abstraction.ts @@ -1,3 +1,6 @@ +import { CollectionView } from "@bitwarden/common/vault/models/view/collection.view"; +import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; + import { Importer } from "../importers/importer"; import { ImportOption, ImportType } from "../models/import-options"; import { ImportResult } from "../models/import-result"; @@ -10,7 +13,7 @@ export abstract class ImportServiceAbstraction { importer: Importer, fileContents: string, organizationId?: string, - selectedImportTarget?: string, + selectedImportTarget?: FolderView | CollectionView, canAccessImportExport?: boolean, ) => Promise; getImporter: ( diff --git a/libs/importer/src/services/import.service.spec.ts b/libs/importer/src/services/import.service.spec.ts index 6a2e0a339c7..a95b74d792c 100644 --- a/libs/importer/src/services/import.service.spec.ts +++ b/libs/importer/src/services/import.service.spec.ts @@ -86,7 +86,7 @@ describe("ImportService", () => { }); it("empty importTarget does nothing", async () => { - await importService["setImportTarget"](importResult, null, ""); + await importService["setImportTarget"](importResult, null, null); expect(importResult.folders.length).toBe(0); }); @@ -99,9 +99,9 @@ describe("ImportService", () => { Promise.resolve([mockImportTargetFolder]), ); - await importService["setImportTarget"](importResult, null, "myImportTarget"); + await importService["setImportTarget"](importResult, null, mockImportTargetFolder); expect(importResult.folders.length).toBe(1); - expect(importResult.folders[0].name).toBe("myImportTarget"); + expect(importResult.folders[0]).toBe(mockImportTargetFolder); }); const mockFolder1 = new FolderView(); @@ -119,16 +119,18 @@ describe("ImportService", () => { mockFolder2, ]); - const myImportTarget = "myImportTarget"; - importResult.folders.push(mockFolder1); importResult.folders.push(mockFolder2); - await importService["setImportTarget"](importResult, null, myImportTarget); + await importService["setImportTarget"](importResult, null, mockImportTargetFolder); expect(importResult.folders.length).toBe(3); - expect(importResult.folders[0].name).toBe(myImportTarget); - expect(importResult.folders[1].name).toBe(`${myImportTarget}/${mockFolder1.name}`); - expect(importResult.folders[2].name).toBe(`${myImportTarget}/${mockFolder2.name}`); + expect(importResult.folders[0]).toBe(mockImportTargetFolder); + expect(importResult.folders[1].name).toBe( + `${mockImportTargetFolder.name}/${mockFolder1.name}`, + ); + expect(importResult.folders[2].name).toBe( + `${mockImportTargetFolder.name}/${mockFolder2.name}`, + ); }); const mockImportTargetCollection = new CollectionView(); @@ -152,9 +154,13 @@ describe("ImportService", () => { mockCollection1, ]); - await importService["setImportTarget"](importResult, organizationId, "myImportTarget"); + await importService["setImportTarget"]( + importResult, + organizationId, + mockImportTargetCollection, + ); expect(importResult.collections.length).toBe(1); - expect(importResult.collections[0].name).toBe("myImportTarget"); + expect(importResult.collections[0]).toBe(mockImportTargetCollection); }); it("passing importTarget sets it as new root for all existing collections", async () => { @@ -164,16 +170,42 @@ describe("ImportService", () => { mockCollection2, ]); - const myImportTarget = "myImportTarget"; - importResult.collections.push(mockCollection1); importResult.collections.push(mockCollection2); - await importService["setImportTarget"](importResult, organizationId, myImportTarget); + await importService["setImportTarget"]( + importResult, + organizationId, + mockImportTargetCollection, + ); expect(importResult.collections.length).toBe(3); - expect(importResult.collections[0].name).toBe(myImportTarget); - expect(importResult.collections[1].name).toBe(`${myImportTarget}/${mockCollection1.name}`); - expect(importResult.collections[2].name).toBe(`${myImportTarget}/${mockCollection2.name}`); + expect(importResult.collections[0]).toBe(mockImportTargetCollection); + expect(importResult.collections[1].name).toBe( + `${mockImportTargetCollection.name}/${mockCollection1.name}`, + ); + expect(importResult.collections[2].name).toBe( + `${mockImportTargetCollection.name}/${mockCollection2.name}`, + ); + }); + + it("passing importTarget as null on setImportTarget with organizationId throws error", async () => { + const setImportTargetMethod = importService["setImportTarget"]( + null, + organizationId, + new Object() as FolderView, + ); + + await expect(setImportTargetMethod).rejects.toThrow("Error assigning target collection"); + }); + + it("passing importTarget as null on setImportTarget throws error", async () => { + const setImportTargetMethod = importService["setImportTarget"]( + null, + "", + new Object() as CollectionView, + ); + + await expect(setImportTargetMethod).rejects.toThrow("Error assigning target folder"); }); }); }); diff --git a/libs/importer/src/services/import.service.ts b/libs/importer/src/services/import.service.ts index e26b768ab67..a6fd233dcf6 100644 --- a/libs/importer/src/services/import.service.ts +++ b/libs/importer/src/services/import.service.ts @@ -110,7 +110,7 @@ export class ImportService implements ImportServiceAbstraction { importer: Importer, fileContents: string, organizationId: string = null, - selectedImportTarget: string = null, + selectedImportTarget: FolderView | CollectionView = null, canAccessImportExport: boolean, ): Promise { let importResult: ImportResult; @@ -147,11 +147,7 @@ export class ImportService implements ImportServiceAbstraction { } } - if ( - organizationId && - Utils.isNullOrWhitespace(selectedImportTarget) && - !canAccessImportExport - ) { + if (organizationId && !selectedImportTarget && !canAccessImportExport) { const hasUnassignedCollections = importResult.collectionRelationships.length < importResult.ciphers.length; if (hasUnassignedCollections) { @@ -428,29 +424,30 @@ export class ImportService implements ImportServiceAbstraction { private async setImportTarget( importResult: ImportResult, organizationId: string, - importTarget: string, + importTarget: FolderView | CollectionView, ) { - if (Utils.isNullOrWhitespace(importTarget)) { + if (!importTarget) { return; } if (organizationId) { - const collectionViews: CollectionView[] = await this.collectionService.getAllDecrypted(); - const targetCollection = collectionViews.find((c) => c.id === importTarget); + if (!(importTarget instanceof CollectionView)) { + throw new Error("Error assigning target collection"); + } const noCollectionRelationShips: [number, number][] = []; importResult.ciphers.forEach((c, index) => { if (!Array.isArray(c.collectionIds) || c.collectionIds.length == 0) { - c.collectionIds = [targetCollection.id]; + c.collectionIds = [importTarget.id]; noCollectionRelationShips.push([index, 0]); } }); const collections: CollectionView[] = [...importResult.collections]; - importResult.collections = [targetCollection]; + importResult.collections = [importTarget as CollectionView]; collections.map((x) => { const f = new CollectionView(); - f.name = `${targetCollection.name}/${x.name}`; + f.name = `${importTarget.name}/${x.name}`; importResult.collections.push(f); }); @@ -463,21 +460,22 @@ export class ImportService implements ImportServiceAbstraction { return; } - const folderViews = await this.folderService.getAllDecryptedFromState(); - const targetFolder = folderViews.find((f) => f.id === importTarget); + if (!(importTarget instanceof FolderView)) { + throw new Error("Error assigning target folder"); + } const noFolderRelationShips: [number, number][] = []; importResult.ciphers.forEach((c, index) => { if (Utils.isNullOrEmpty(c.folderId)) { - c.folderId = targetFolder.id; + c.folderId = importTarget.id; noFolderRelationShips.push([index, 0]); } }); const folders: FolderView[] = [...importResult.folders]; - importResult.folders = [targetFolder]; + importResult.folders = [importTarget as FolderView]; folders.map((x) => { - const newFolderName = `${targetFolder.name}/${x.name}`; + const newFolderName = `${importTarget.name}/${x.name}`; const f = new FolderView(); f.name = newFolderName; importResult.folders.push(f); diff --git a/package-lock.json b/package-lock.json index 468ae14ac71..53f31340b33 100644 --- a/package-lock.json +++ b/package-lock.json @@ -127,11 +127,11 @@ "copy-webpack-plugin": "11.0.0", "cross-env": "7.0.3", "css-loader": "6.8.1", - "electron": "28.2.6", + "electron": "28.2.7", "electron-builder": "24.13.3", "electron-log": "5.0.1", "electron-reload": "2.0.0-alpha.1", - "electron-store": "8.1.0", + "electron-store": "8.2.0", "electron-updater": "6.1.8", "eslint": "8.57.0", "eslint-config-prettier": "9.1.0", @@ -161,7 +161,7 @@ "postcss": "8.4.35", "postcss-loader": "7.3.4", "prettier": "3.2.2", - "prettier-plugin-tailwindcss": "0.5.11", + "prettier-plugin-tailwindcss": "0.5.12", "process": "0.11.10", "react": "18.2.0", "react-dom": "18.2.0", @@ -17853,9 +17853,9 @@ } }, "node_modules/electron": { - "version": "28.2.6", - "resolved": "https://registry.npmjs.org/electron/-/electron-28.2.6.tgz", - "integrity": "sha512-RuhbW+ifvh3DqnVlHCcCKhKIFOxTktq1GN1gkIkEZ8y5LEZfcjOkxB2s6Fd1S6MzsMZbiJti+ZJG5hXS4SDVLQ==", + "version": "28.2.7", + "resolved": "https://registry.npmjs.org/electron/-/electron-28.2.7.tgz", + "integrity": "sha512-iEBTYNFuZtLpAS+8ql0ATUWBPAC9uMYqwNJtMLqlT3/zOzHj6aYpwoJILwWgIuTAx+/yTYgARS46Nr/RazxTpg==", "dev": true, "hasInstallScript": true, "dependencies": { @@ -17969,9 +17969,9 @@ } }, "node_modules/electron-store": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/electron-store/-/electron-store-8.1.0.tgz", - "integrity": "sha512-2clHg/juMjOH0GT9cQ6qtmIvK183B39ZXR0bUoPwKwYHJsEF3quqyDzMFUAu+0OP8ijmN2CbPRAelhNbWUbzwA==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/electron-store/-/electron-store-8.2.0.tgz", + "integrity": "sha512-ukLL5Bevdil6oieAOXz3CMy+OgaItMiVBg701MNlG6W5RaC0AHN7rvlqTCmeb6O7jP0Qa1KKYTE0xV0xbhF4Hw==", "dev": true, "dependencies": { "conf": "^10.2.0", @@ -31209,9 +31209,9 @@ } }, "node_modules/prettier-plugin-tailwindcss": { - "version": "0.5.11", - "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.5.11.tgz", - "integrity": "sha512-AvI/DNyMctyyxGOjyePgi/gqj5hJYClZ1avtQvLlqMT3uDZkRbi4HhGUpok3DRzv9z7Lti85Kdj3s3/1CeNI0w==", + "version": "0.5.12", + "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.5.12.tgz", + "integrity": "sha512-o74kiDBVE73oHW+pdkFSluHBL3cYEvru5YgEqNkBMFF7Cjv+w1vI565lTlfoJT4VLWDe0FMtZ7FkE/7a4pMXSQ==", "dev": true, "engines": { "node": ">=14.21.3" @@ -31229,6 +31229,7 @@ "prettier-plugin-marko": "*", "prettier-plugin-organize-attributes": "*", "prettier-plugin-organize-imports": "*", + "prettier-plugin-sort-imports": "*", "prettier-plugin-style-order": "*", "prettier-plugin-svelte": "*" }, @@ -31266,6 +31267,9 @@ "prettier-plugin-organize-imports": { "optional": true }, + "prettier-plugin-sort-imports": { + "optional": true + }, "prettier-plugin-style-order": { "optional": true }, diff --git a/package.json b/package.json index 0eabb1da8bf..ec0f1d64747 100644 --- a/package.json +++ b/package.json @@ -88,11 +88,11 @@ "copy-webpack-plugin": "11.0.0", "cross-env": "7.0.3", "css-loader": "6.8.1", - "electron": "28.2.6", + "electron": "28.2.7", "electron-builder": "24.13.3", "electron-log": "5.0.1", "electron-reload": "2.0.0-alpha.1", - "electron-store": "8.1.0", + "electron-store": "8.2.0", "electron-updater": "6.1.8", "eslint": "8.57.0", "eslint-config-prettier": "9.1.0", @@ -122,7 +122,7 @@ "postcss": "8.4.35", "postcss-loader": "7.3.4", "prettier": "3.2.2", - "prettier-plugin-tailwindcss": "0.5.11", + "prettier-plugin-tailwindcss": "0.5.12", "process": "0.11.10", "react": "18.2.0", "react-dom": "18.2.0",