1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 07:43:35 +00:00

[PM-5255] Create login strategy service (#7750)

* refactor login strategies into own service

* create login service factory

* replaces instances of authService with loginStrategyService

* replace more instances of authService

* move logout back to auth service

* add browser dependencies

* fix desktop dependencies

* fix cli dependencies

* fix lint and test files

* fix anonymous hub deps

* fix webauthn-login service deps

* add loginstrategyservice to bg

* move login strategy service and models to auth folder

* revert changes to tsconfig

* use alias for imports

* fix path

---------

Co-authored-by: rr-bw <102181210+rr-bw@users.noreply.github.com>
This commit is contained in:
Jake Fink
2024-02-05 14:26:41 -05:00
committed by GitHub
parent 568f3ecb2a
commit 816bcf4f39
56 changed files with 1002 additions and 850 deletions

View File

@@ -5,6 +5,12 @@ import * as inquirer from "inquirer";
import Separator from "inquirer/lib/objects/separator";
import { firstValueFrom } from "rxjs";
import {
LoginStrategyServiceAbstraction,
PasswordLoginCredentials,
SsoLoginCredentials,
UserApiLoginCredentials,
} from "@bitwarden/auth/common";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { PolicyApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/policy/policy-api.service.abstraction";
@@ -15,11 +21,6 @@ import { TwoFactorService } from "@bitwarden/common/auth/abstractions/two-factor
import { TwoFactorProviderType } from "@bitwarden/common/auth/enums/two-factor-provider-type";
import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result";
import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/force-set-password-reason";
import {
PasswordLoginCredentials,
SsoLoginCredentials,
UserApiLoginCredentials,
} from "@bitwarden/common/auth/models/domain/login-credentials";
import { TokenTwoFactorRequest } from "@bitwarden/common/auth/models/request/identity-token/token-two-factor.request";
import { PasswordRequest } from "@bitwarden/common/auth/models/request/password.request";
import { TwoFactorEmailRequest } from "@bitwarden/common/auth/models/request/two-factor-email.request";
@@ -50,6 +51,7 @@ export class LoginCommand {
private options: OptionValues;
constructor(
protected loginStrategyService: LoginStrategyServiceAbstraction,
protected authService: AuthService,
protected apiService: ApiService,
protected cryptoFunctionService: CryptoFunctionService,
@@ -178,7 +180,7 @@ export class LoginCommand {
return Response.error("Invalid API Key; Organization API Key currently not supported");
}
try {
response = await this.authService.logIn(
response = await this.loginStrategyService.logIn(
new UserApiLoginCredentials(clientId, clientSecret),
);
} catch (e) {
@@ -195,7 +197,7 @@ export class LoginCommand {
throw e;
}
} else if (ssoCode != null && ssoCodeVerifier != null) {
response = await this.authService.logIn(
response = await this.loginStrategyService.logIn(
new SsoLoginCredentials(
ssoCode,
ssoCodeVerifier,
@@ -205,7 +207,7 @@ export class LoginCommand {
),
);
} else {
response = await this.authService.logIn(
response = await this.loginStrategyService.logIn(
new PasswordLoginCredentials(email, password, null, twoFactor),
);
}
@@ -271,8 +273,8 @@ export class LoginCommand {
selectedProvider.type === TwoFactorProviderType.Email
) {
const emailReq = new TwoFactorEmailRequest();
emailReq.email = this.authService.email;
emailReq.masterPasswordHash = this.authService.masterPasswordHash;
emailReq.email = this.loginStrategyService.email;
emailReq.masterPasswordHash = this.loginStrategyService.masterPasswordHash;
await this.apiService.postTwoFactorEmail(emailReq);
}
@@ -292,7 +294,7 @@ export class LoginCommand {
}
}
response = await this.authService.logInTwoFactor(
response = await this.loginStrategyService.logInTwoFactor(
new TokenTwoFactorRequest(selectedProvider.type, twoFactorToken),
null,
);
@@ -604,9 +606,9 @@ export class LoginCommand {
if (credentials != null) {
credentials.captchaToken = captchaClientSecret;
credentials.twoFactor = twoFactorRequest;
authResultResponse = await this.authService.logIn(credentials);
authResultResponse = await this.loginStrategyService.logIn(credentials);
} else {
authResultResponse = await this.authService.logInTwoFactor(
authResultResponse = await this.loginStrategyService.logInTwoFactor(
twoFactorRequest,
captchaClientSecret,
);

View File

@@ -4,7 +4,12 @@ import * as path from "path";
import { program } from "commander";
import * as jsdom from "jsdom";
import { PinCryptoServiceAbstraction, PinCryptoService } from "@bitwarden/auth/common";
import {
LoginStrategyService,
LoginStrategyServiceAbstraction,
PinCryptoService,
PinCryptoServiceAbstraction,
} from "@bitwarden/auth/common";
import { EventCollectionService as EventCollectionServiceAbstraction } from "@bitwarden/common/abstractions/event/event-collection.service";
import { EventUploadService as EventUploadServiceAbstraction } from "@bitwarden/common/abstractions/event/event-upload.service";
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
@@ -191,6 +196,7 @@ export class Main {
activeUserStateProvider: ActiveUserStateProvider;
derivedStateProvider: DerivedStateProvider;
stateProvider: StateProvider;
loginStrategyService: LoginStrategyServiceAbstraction;
constructor() {
let p = null;
@@ -393,7 +399,7 @@ export class Main {
this.authRequestCryptoService = new AuthRequestCryptoServiceImplementation(this.cryptoService);
this.authService = new AuthService(
this.loginStrategyService = new LoginStrategyService(
this.cryptoService,
this.apiService,
this.tokenService,
@@ -413,6 +419,13 @@ export class Main {
this.authRequestCryptoService,
);
this.authService = new AuthService(
this.messagingService,
this.cryptoService,
this.apiService,
this.stateService,
);
this.configApiService = new ConfigApiService(this.apiService, this.authService);
this.configService = new CliConfigService(

View File

@@ -139,6 +139,7 @@ export class Program {
if (!options.check) {
await this.exitIfAuthed();
const command = new LoginCommand(
this.main.loginStrategyService,
this.main.authService,
this.main.apiService,
this.main.cryptoFunctionService,