1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-23 21:31:29 +01:00

Get clientType via service.

This commit is contained in:
Alec Rippberger 2024-10-09 22:31:43 -05:00
parent 982da467b4
commit 3868dcbc60
No known key found for this signature in database
GPG Key ID: 9DD8DA583B28154A
8 changed files with 205 additions and 64 deletions

View File

@ -1,8 +1,15 @@
import { TestBed } from "@angular/core/testing";
import { MockProxy, mock } from "jest-mock-extended";
import { DefaultLoginComponentService } from "@bitwarden/auth/angular";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { flagEnabled } from "../../../platform/flags";
import { BrowserPlatformUtilsService } from "../../../platform/services/platform-utils/browser-platform-utils.service";
import { ExtensionLoginComponentService } from "./extension-login-component.service";
@ -12,12 +19,38 @@ jest.mock("../../../platform/flags", () => ({
describe("ExtensionLoginComponentService", () => {
let service: ExtensionLoginComponentService;
let cryptoFunctionService: MockProxy<CryptoFunctionService>;
let environmentService: MockProxy<EnvironmentService>;
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let platformUtilsService: MockProxy<BrowserPlatformUtilsService>;
let ssoLoginService: MockProxy<SsoLoginServiceAbstraction>;
beforeEach(() => {
cryptoFunctionService = mock<CryptoFunctionService>();
environmentService = mock<EnvironmentService>();
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
platformUtilsService = mock<BrowserPlatformUtilsService>();
ssoLoginService = mock<SsoLoginServiceAbstraction>();
TestBed.configureTestingModule({
providers: [
ExtensionLoginComponentService,
{ provide: DefaultLoginComponentService, useClass: ExtensionLoginComponentService },
{
provide: ExtensionLoginComponentService,
useFactory: () =>
new ExtensionLoginComponentService(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
),
},
{ provide: DefaultLoginComponentService, useExisting: ExtensionLoginComponentService },
{ provide: CryptoFunctionService, useValue: cryptoFunctionService },
{ provide: EnvironmentService, useValue: environmentService },
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: PlatformUtilsService, useValue: platformUtilsService },
{ provide: SsoLoginServiceAbstraction, useValue: ssoLoginService },
],
});
service = TestBed.inject(ExtensionLoginComponentService);

View File

@ -1,13 +1,36 @@
import { Injectable } from "@angular/core";
import { DefaultLoginComponentService, LoginComponentService } from "@bitwarden/auth/angular";
import { ClientType } from "@bitwarden/common/enums";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { flagEnabled } from "../../../platform/flags";
@Injectable()
export class ExtensionLoginComponentService
extends DefaultLoginComponentService
implements LoginComponentService
{
clientType = ClientType.Browser;
constructor(
cryptoFunctionService: CryptoFunctionService,
environmentService: EnvironmentService,
passwordGenerationService: PasswordGenerationServiceAbstraction,
platformUtilsService: PlatformUtilsService,
ssoLoginService: SsoLoginServiceAbstraction,
) {
super(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
);
this.clientType = this.platformUtilsService.getClientType();
}
isLoginViaAuthRequestSupported(): boolean {
return flagEnabled("showPasswordless");
}

View File

@ -83,7 +83,7 @@ import { MemoryStorageService as MemoryStorageServiceForStateProviders } from "@
import { ThemeStateService } from "@bitwarden/common/platform/theming/theme-state.service";
import { VaultTimeoutStringType } from "@bitwarden/common/types/vault-timeout.type";
import { CipherService as CipherServiceAbstraction } from "@bitwarden/common/vault/abstractions/cipher.service";
import { DialogService } from "@bitwarden/components";
import { DialogService, ToastService } from "@bitwarden/components";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { BiometricStateService, BiometricsService } from "@bitwarden/key-management";
@ -329,6 +329,8 @@ const safeProviders: SafeProvider[] = [
PasswordGenerationServiceAbstraction,
PlatformUtilsServiceAbstraction,
SsoLoginServiceAbstraction,
I18nServiceAbstraction,
ToastService,
],
}),
safeProvider({

View File

@ -1,13 +1,17 @@
import { TestBed } from "@angular/core/testing";
import { MockProxy } from "jest-mock-extended";
import { MockProxy, mock } from "jest-mock-extended";
import { DefaultLoginComponentService } from "@bitwarden/auth/angular";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { ToastService } from "@bitwarden/components";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { ElectronPlatformUtilsService } from "../../platform/services/electron-platform-utils.service";
import { DesktopLoginComponentService } from "./desktop-login-component.service";
(global as any).ipc = {
@ -23,22 +27,46 @@ import { DesktopLoginComponentService } from "./desktop-login-component.service"
describe("DesktopLoginComponentService", () => {
let service: DesktopLoginComponentService;
let cryptoFunctionService: MockProxy<CryptoFunctionService>;
let environmentService: MockProxy<EnvironmentService>;
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let platformUtilsService: MockProxy<ElectronPlatformUtilsService>;
let ssoLoginService: MockProxy<SsoLoginServiceAbstraction>;
let i18nService: MockProxy<I18nService>;
let toastService: MockProxy<ToastService>;
let ssoLoginService: MockProxy<SsoLoginServiceAbstraction>;
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let cryptoFunctionService: MockProxy<CryptoFunctionService>;
beforeEach(() => {
cryptoFunctionService = mock<CryptoFunctionService>();
environmentService = mock<EnvironmentService>();
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
platformUtilsService = mock<ElectronPlatformUtilsService>();
ssoLoginService = mock<SsoLoginServiceAbstraction>();
i18nService = mock<I18nService>();
toastService = mock<ToastService>();
TestBed.configureTestingModule({
providers: [
DesktopLoginComponentService,
{ provide: DefaultLoginComponentService, useClass: DesktopLoginComponentService },
{
provide: DesktopLoginComponentService,
useFactory: () =>
new DesktopLoginComponentService(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
i18nService,
toastService,
),
},
{ provide: DefaultLoginComponentService, useExisting: DesktopLoginComponentService },
{ provide: CryptoFunctionService, useValue: cryptoFunctionService },
{ provide: EnvironmentService, useValue: environmentService },
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: PlatformUtilsService, useValue: platformUtilsService },
{ provide: SsoLoginServiceAbstraction, useValue: ssoLoginService },
{ provide: I18nService, useValue: i18nService },
{ provide: ToastService, useValue: toastService },
{ provide: SsoLoginServiceAbstraction, useValue: ssoLoginService },
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: CryptoFunctionService, useValue: cryptoFunctionService },
],
});

View File

@ -1,18 +1,38 @@
import { inject } from "@angular/core";
import { Injectable } from "@angular/core";
import { DefaultLoginComponentService, LoginComponentService } from "@bitwarden/auth/angular";
import { ClientType } from "@bitwarden/common/enums";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { ToastService } from "@bitwarden/components";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
@Injectable()
export class DesktopLoginComponentService
extends DefaultLoginComponentService
implements LoginComponentService
{
i18nService = inject(I18nService);
toastService = inject(ToastService);
clientType = ClientType.Desktop;
constructor(
protected cryptoFunctionService: CryptoFunctionService,
protected environmentService: EnvironmentService,
protected passwordGenerationService: PasswordGenerationServiceAbstraction,
protected platformUtilsService: PlatformUtilsService,
protected ssoLoginService: SsoLoginServiceAbstraction,
protected i18nService: I18nService,
protected toastService: ToastService,
) {
super(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
);
this.clientType = this.platformUtilsService.getClientType();
}
override async launchSsoBrowserWindow(email: string, clientId: "desktop"): Promise<void | null> {
if (!ipc.platform.isAppImage && !ipc.platform.isSnapStore && !ipc.platform.isDev) {

View File

@ -1,10 +1,16 @@
import { TestBed } from "@angular/core/testing";
import { UrlTree } from "@angular/router";
import { MockProxy, mock } from "jest-mock-extended";
import { DefaultLoginComponentService } from "@bitwarden/auth/angular";
import { PolicyApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy-api.service.abstraction";
import { InternalPolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { RouterService } from "../../../../../../../../apps/web/src/app/core";
import { flagEnabled } from "../../../../../utils/flags";
@ -18,37 +24,43 @@ jest.mock("../../../../../utils/flags", () => ({
describe("WebLoginComponentService", () => {
let service: WebLoginComponentService;
let acceptOrganizationInviteService: MockProxy<AcceptOrganizationInviteService>;
let logService: MockProxy<LogService>;
let policyApiService: MockProxy<PolicyApiServiceAbstraction>;
let internalPolicyService: MockProxy<InternalPolicyService>;
let routerService: MockProxy<RouterService>;
let cryptoFunctionService: MockProxy<CryptoFunctionService>;
let environmentService: MockProxy<EnvironmentService>;
let passwordGenerationService: MockProxy<PasswordGenerationServiceAbstraction>;
let platformUtilsService: MockProxy<PlatformUtilsService>;
let ssoLoginService: MockProxy<SsoLoginServiceAbstraction>;
beforeEach(() => {
acceptOrganizationInviteService = mock<AcceptOrganizationInviteService>();
logService = mock<LogService>();
policyApiService = mock<PolicyApiServiceAbstraction>();
internalPolicyService = mock<InternalPolicyService>();
routerService = mock<RouterService>();
cryptoFunctionService = mock<CryptoFunctionService>();
environmentService = mock<EnvironmentService>();
passwordGenerationService = mock<PasswordGenerationServiceAbstraction>();
platformUtilsService = mock<PlatformUtilsService>();
ssoLoginService = mock<SsoLoginServiceAbstraction>();
TestBed.configureTestingModule({
providers: [
WebLoginComponentService,
{ provide: DefaultLoginComponentService, useClass: WebLoginComponentService },
{
provide: AcceptOrganizationInviteService,
useValue: {
getOrganizationInvite: jest.fn(),
},
},
{
provide: LogService,
useValue: {
error: jest.fn(),
},
},
{
provide: PolicyApiServiceAbstraction,
useValue: {
getPoliciesByToken: jest.fn(),
},
},
{ provide: InternalPolicyService, useValue: {} },
{
provide: RouterService,
useValue: {
setPreviousUrl: jest.fn(),
},
},
{ provide: AcceptOrganizationInviteService, useValue: acceptOrganizationInviteService },
{ provide: LogService, useValue: logService },
{ provide: PolicyApiServiceAbstraction, useValue: policyApiService },
{ provide: InternalPolicyService, useValue: internalPolicyService },
{ provide: RouterService, useValue: routerService },
{ provide: CryptoFunctionService, useValue: cryptoFunctionService },
{ provide: EnvironmentService, useValue: environmentService },
{ provide: PasswordGenerationServiceAbstraction, useValue: passwordGenerationService },
{ provide: PlatformUtilsService, useValue: platformUtilsService },
{ provide: SsoLoginServiceAbstraction, useValue: ssoLoginService },
],
});
service = TestBed.inject(WebLoginComponentService);
@ -72,22 +84,19 @@ describe("WebLoginComponentService", () => {
it("sets the previous URL", () => {
const route = { toString: () => "test-url" } as UrlTree;
const routerServiceSpy = jest.spyOn(service.routerService, "setPreviousUrl");
service.setPreviousUrl(route);
expect(routerServiceSpy).toHaveBeenCalledWith("test-url");
expect(routerService.setPreviousUrl).toHaveBeenCalledWith("test-url");
});
it("returns undefined if organization invite is null", async () => {
jest
.spyOn(service.acceptOrganizationInviteService, "getOrganizationInvite")
.mockResolvedValue(null);
acceptOrganizationInviteService.getOrganizationInvite.mockResolvedValue(null);
const result = await service.getOrgPolicies();
expect(result).toBeUndefined();
});
it("logs an error if getPoliciesByToken throws an error", async () => {
const error = new Error("Test error");
jest.spyOn(service.acceptOrganizationInviteService, "getOrganizationInvite").mockResolvedValue({
acceptOrganizationInviteService.getOrganizationInvite.mockResolvedValue({
organizationId: "org-id",
token: "token",
email: "email",
@ -97,9 +106,8 @@ describe("WebLoginComponentService", () => {
orgUserHasExistingUser: false,
organizationName: "org-name",
});
jest.spyOn(service.policyApiService, "getPoliciesByToken").mockRejectedValue(error);
const logServiceSpy = jest.spyOn(service.logService, "error");
policyApiService.getPoliciesByToken.mockRejectedValue(error);
await service.getOrgPolicies();
expect(logServiceSpy).toHaveBeenCalledWith(error);
expect(logService.error).toHaveBeenCalledWith(error);
});
});

View File

@ -1,5 +1,5 @@
import { inject } from "@angular/core";
import { Router, UrlTree } from "@angular/router";
import { Injectable } from "@angular/core";
import { UrlTree } from "@angular/router";
import { firstValueFrom } from "rxjs";
import {
@ -10,24 +10,43 @@ import {
import { PolicyApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy-api.service.abstraction";
import { InternalPolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { Policy } from "@bitwarden/common/admin-console/models/domain/policy";
import { ClientType } from "@bitwarden/common/enums";
import { SsoLoginServiceAbstraction } from "@bitwarden/common/auth/abstractions/sso-login.service.abstraction";
import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { PasswordGenerationServiceAbstraction } from "@bitwarden/generator-legacy";
import { flagEnabled } from "../../../../../utils/flags";
import { RouterService } from "../../../../core/router.service";
import { AcceptOrganizationInviteService } from "../../../organization-invite/accept-organization.service";
@Injectable()
export class WebLoginComponentService
extends DefaultLoginComponentService
implements LoginComponentService
{
acceptOrganizationInviteService = inject(AcceptOrganizationInviteService);
logService = inject(LogService);
policyApiService = inject(PolicyApiServiceAbstraction);
policyService = inject(InternalPolicyService);
router = inject(Router);
routerService = inject(RouterService);
clientType = ClientType.Web;
constructor(
protected acceptOrganizationInviteService: AcceptOrganizationInviteService,
protected logService: LogService,
protected policyApiService: PolicyApiServiceAbstraction,
protected policyService: InternalPolicyService,
protected routerService: RouterService,
cryptoFunctionService: CryptoFunctionService,
environmentService: EnvironmentService,
passwordGenerationService: PasswordGenerationServiceAbstraction,
platformUtilsService: PlatformUtilsService,
ssoLoginService: SsoLoginServiceAbstraction,
) {
super(
cryptoFunctionService,
environmentService,
passwordGenerationService,
platformUtilsService,
ssoLoginService,
);
this.clientType = this.platformUtilsService.getClientType();
}
isLoginViaAuthRequestSupported(): boolean {
return flagEnabled("showPasswordless");

View File

@ -38,7 +38,10 @@ import {
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
import { PolicyApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy-api.service.abstraction";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import {
InternalPolicyService,
PolicyService,
} from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { AccountApiService as AccountApiServiceAbstraction } from "@bitwarden/common/auth/abstractions/account-api.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
@ -257,6 +260,11 @@ const safeProviders: SafeProvider[] = [
provide: LoginComponentService,
useClass: WebLoginComponentService,
deps: [
AcceptOrganizationInviteService,
LogService,
PolicyApiServiceAbstraction,
InternalPolicyService,
RouterService,
CryptoFunctionServiceAbstraction,
EnvironmentService,
PasswordGenerationServiceAbstraction,