From 9caea70ea2dbfdb591ef08a3d33def7f591f1276 Mon Sep 17 00:00:00 2001 From: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Date: Mon, 7 Feb 2022 07:33:19 +1000 Subject: [PATCH] [authService refactor] Fix browser by not using instanceof (#647) * use authenticationType enum instead of instanceof --- common/src/enums/authenticationType.ts | 5 +++++ common/src/models/domain/logInCredentials.ts | 8 ++++++++ common/src/services/auth.service.ts | 14 +++++++------- 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 common/src/enums/authenticationType.ts diff --git a/common/src/enums/authenticationType.ts b/common/src/enums/authenticationType.ts new file mode 100644 index 0000000000..ed7375c808 --- /dev/null +++ b/common/src/enums/authenticationType.ts @@ -0,0 +1,5 @@ +export enum AuthenticationType { + Password = 0, + Sso = 1, + Api = 2, +} diff --git a/common/src/models/domain/logInCredentials.ts b/common/src/models/domain/logInCredentials.ts index d53da02ee0..91bb9a7697 100644 --- a/common/src/models/domain/logInCredentials.ts +++ b/common/src/models/domain/logInCredentials.ts @@ -1,6 +1,10 @@ +import { AuthenticationType } from "../../enums/authenticationType"; + import { TokenRequestTwoFactor } from "../request/identityToken/tokenRequest"; export class PasswordLogInCredentials { + readonly type = AuthenticationType.Password; + constructor( public email: string, public masterPassword: string, @@ -10,6 +14,8 @@ export class PasswordLogInCredentials { } export class SsoLogInCredentials { + readonly type = AuthenticationType.Sso; + constructor( public code: string, public codeVerifier: string, @@ -20,5 +26,7 @@ export class SsoLogInCredentials { } export class ApiLogInCredentials { + readonly type = AuthenticationType.Api; + constructor(public clientId: string, public clientSecret: string) {} } diff --git a/common/src/services/auth.service.ts b/common/src/services/auth.service.ts index 93f85c4d66..3bd4f34de3 100644 --- a/common/src/services/auth.service.ts +++ b/common/src/services/auth.service.ts @@ -28,6 +28,8 @@ import { StateService } from "../abstractions/state.service"; import { TokenService } from "../abstractions/token.service"; import { TwoFactorService } from "../abstractions/twoFactor.service"; +import { AuthenticationType } from "../enums/authenticationType"; + export class AuthService implements AuthServiceAbstraction { get email(): string { return this.logInStrategy instanceof PasswordLogInStrategy ? this.logInStrategy.email : null; @@ -60,10 +62,9 @@ export class AuthService implements AuthServiceAbstraction { ): Promise { this.clearState(); - let result: AuthResult; let strategy: ApiLogInStrategy | PasswordLogInStrategy | SsoLogInStrategy; - if (credentials instanceof PasswordLogInCredentials) { + if (credentials.type === AuthenticationType.Password) { strategy = new PasswordLogInStrategy( this.cryptoService, this.apiService, @@ -76,8 +77,7 @@ export class AuthService implements AuthServiceAbstraction { this.twoFactorService, this ); - result = await strategy.logIn(credentials); - } else if (credentials instanceof SsoLogInCredentials) { + } else if (credentials.type === AuthenticationType.Sso) { strategy = new SsoLogInStrategy( this.cryptoService, this.apiService, @@ -90,8 +90,7 @@ export class AuthService implements AuthServiceAbstraction { this.twoFactorService, this.keyConnectorService ); - result = await strategy.logIn(credentials); - } else if (credentials instanceof ApiLogInCredentials) { + } else if (credentials.type === AuthenticationType.Api) { strategy = new ApiLogInStrategy( this.cryptoService, this.apiService, @@ -105,9 +104,10 @@ export class AuthService implements AuthServiceAbstraction { this.environmentService, this.keyConnectorService ); - result = await strategy.logIn(credentials); } + const result = await strategy.logIn(credentials as any); + if (result?.requiresTwoFactor) { this.saveState(strategy); }