mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-28 03:21:40 +01:00
[authService refactor] Fix browser by not using instanceof (#647)
* use authenticationType enum instead of instanceof
This commit is contained in:
parent
380a7c7ee5
commit
9caea70ea2
5
common/src/enums/authenticationType.ts
Normal file
5
common/src/enums/authenticationType.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export enum AuthenticationType {
|
||||||
|
Password = 0,
|
||||||
|
Sso = 1,
|
||||||
|
Api = 2,
|
||||||
|
}
|
@ -1,6 +1,10 @@
|
|||||||
|
import { AuthenticationType } from "../../enums/authenticationType";
|
||||||
|
|
||||||
import { TokenRequestTwoFactor } from "../request/identityToken/tokenRequest";
|
import { TokenRequestTwoFactor } from "../request/identityToken/tokenRequest";
|
||||||
|
|
||||||
export class PasswordLogInCredentials {
|
export class PasswordLogInCredentials {
|
||||||
|
readonly type = AuthenticationType.Password;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public email: string,
|
public email: string,
|
||||||
public masterPassword: string,
|
public masterPassword: string,
|
||||||
@ -10,6 +14,8 @@ export class PasswordLogInCredentials {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class SsoLogInCredentials {
|
export class SsoLogInCredentials {
|
||||||
|
readonly type = AuthenticationType.Sso;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public code: string,
|
public code: string,
|
||||||
public codeVerifier: string,
|
public codeVerifier: string,
|
||||||
@ -20,5 +26,7 @@ export class SsoLogInCredentials {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class ApiLogInCredentials {
|
export class ApiLogInCredentials {
|
||||||
|
readonly type = AuthenticationType.Api;
|
||||||
|
|
||||||
constructor(public clientId: string, public clientSecret: string) {}
|
constructor(public clientId: string, public clientSecret: string) {}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,8 @@ import { StateService } from "../abstractions/state.service";
|
|||||||
import { TokenService } from "../abstractions/token.service";
|
import { TokenService } from "../abstractions/token.service";
|
||||||
import { TwoFactorService } from "../abstractions/twoFactor.service";
|
import { TwoFactorService } from "../abstractions/twoFactor.service";
|
||||||
|
|
||||||
|
import { AuthenticationType } from "../enums/authenticationType";
|
||||||
|
|
||||||
export class AuthService implements AuthServiceAbstraction {
|
export class AuthService implements AuthServiceAbstraction {
|
||||||
get email(): string {
|
get email(): string {
|
||||||
return this.logInStrategy instanceof PasswordLogInStrategy ? this.logInStrategy.email : null;
|
return this.logInStrategy instanceof PasswordLogInStrategy ? this.logInStrategy.email : null;
|
||||||
@ -60,10 +62,9 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
): Promise<AuthResult> {
|
): Promise<AuthResult> {
|
||||||
this.clearState();
|
this.clearState();
|
||||||
|
|
||||||
let result: AuthResult;
|
|
||||||
let strategy: ApiLogInStrategy | PasswordLogInStrategy | SsoLogInStrategy;
|
let strategy: ApiLogInStrategy | PasswordLogInStrategy | SsoLogInStrategy;
|
||||||
|
|
||||||
if (credentials instanceof PasswordLogInCredentials) {
|
if (credentials.type === AuthenticationType.Password) {
|
||||||
strategy = new PasswordLogInStrategy(
|
strategy = new PasswordLogInStrategy(
|
||||||
this.cryptoService,
|
this.cryptoService,
|
||||||
this.apiService,
|
this.apiService,
|
||||||
@ -76,8 +77,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
this.twoFactorService,
|
this.twoFactorService,
|
||||||
this
|
this
|
||||||
);
|
);
|
||||||
result = await strategy.logIn(credentials);
|
} else if (credentials.type === AuthenticationType.Sso) {
|
||||||
} else if (credentials instanceof SsoLogInCredentials) {
|
|
||||||
strategy = new SsoLogInStrategy(
|
strategy = new SsoLogInStrategy(
|
||||||
this.cryptoService,
|
this.cryptoService,
|
||||||
this.apiService,
|
this.apiService,
|
||||||
@ -90,8 +90,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
this.twoFactorService,
|
this.twoFactorService,
|
||||||
this.keyConnectorService
|
this.keyConnectorService
|
||||||
);
|
);
|
||||||
result = await strategy.logIn(credentials);
|
} else if (credentials.type === AuthenticationType.Api) {
|
||||||
} else if (credentials instanceof ApiLogInCredentials) {
|
|
||||||
strategy = new ApiLogInStrategy(
|
strategy = new ApiLogInStrategy(
|
||||||
this.cryptoService,
|
this.cryptoService,
|
||||||
this.apiService,
|
this.apiService,
|
||||||
@ -105,9 +104,10 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
this.environmentService,
|
this.environmentService,
|
||||||
this.keyConnectorService
|
this.keyConnectorService
|
||||||
);
|
);
|
||||||
result = await strategy.logIn(credentials);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const result = await strategy.logIn(credentials as any);
|
||||||
|
|
||||||
if (result?.requiresTwoFactor) {
|
if (result?.requiresTwoFactor) {
|
||||||
this.saveState(strategy);
|
this.saveState(strategy);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user