1
0
mirror of https://github.com/bitwarden/jslib synced 2026-01-03 09:03:13 +00:00

Deprecate LogIn...Complete methods

Add TwoFactorData to main LogIn methods and handle null instead of
having duplicative methods
This commit is contained in:
Thomas Rittson
2021-12-17 20:32:09 +10:00
parent 33f5ac822d
commit 8999793882
3 changed files with 43 additions and 169 deletions

View File

@@ -1,7 +1,6 @@
import { TwoFactorProviderType } from "../enums/twoFactorProviderType";
import { AuthResult } from "../models/domain/authResult"; import { AuthResult } from "../models/domain/authResult";
import { SymmetricCryptoKey } from "../models/domain/symmetricCryptoKey"; import { SymmetricCryptoKey } from "../models/domain/symmetricCryptoKey";
import { TwoFactorData } from '../models/request/identityToken/tokenRequest';
export abstract class AuthService { export abstract class AuthService {
email: string; email: string;
@@ -12,41 +11,17 @@ export abstract class AuthService {
clientId: string; clientId: string;
clientSecret: string; clientSecret: string;
logIn: (email: string, masterPassword: string, captchaToken?: string) => Promise<AuthResult>; logIn: (email: string, masterPassword: string, twoFactor: TwoFactorData, captchaToken?: string) => Promise<AuthResult>;
logInSso: ( logInSso: (
code: string, code: string,
codeVerifier: string, codeVerifier: string,
redirectUrl: string, redirectUrl: string,
twoFactor: TwoFactorData,
orgId: string orgId: string
) => Promise<AuthResult>; ) => Promise<AuthResult>;
logInApiKey: (clientId: string, clientSecret: string) => Promise<AuthResult>; logInApiKey: (clientId: string, clientSecret: string, twoFactor: TwoFactorData) => Promise<AuthResult>;
logInTwoFactor: ( logInTwoFactor: (
twoFactorProvider: TwoFactorProviderType, twoFactor: TwoFactorData
twoFactorToken: string,
remember?: boolean
) => Promise<AuthResult>;
logInComplete: (
email: string,
masterPassword: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean,
captchaToken?: string
) => Promise<AuthResult>;
logInSsoComplete: (
code: string,
codeVerifier: string,
redirectUrl: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean
) => Promise<AuthResult>;
logInApiKeyComplete: (
clientId: string,
clientSecret: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean
) => Promise<AuthResult>; ) => Promise<AuthResult>;
logOut: (callback: Function) => void; logOut: (callback: Function) => void;
makePreloginKey: (masterPassword: string, email: string) => Promise<SymmetricCryptoKey>; makePreloginKey: (masterPassword: string, email: string) => Promise<SymmetricCryptoKey>;

View File

@@ -65,7 +65,7 @@ export class AuthService implements AuthServiceAbstraction {
private setCryptoKeys = true private setCryptoKeys = true
) {} ) {}
async logIn(email: string, masterPassword: string, captchaToken?: string): Promise<AuthResult> { async logIn(email: string, masterPassword: string, twoFactor: TwoFactorData, captchaToken?: string): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider(); this.twoFactorService.clearSelectedProvider();
const key = await this.makePreloginKey(masterPassword, email); const key = await this.makePreloginKey(masterPassword, email);
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key); const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
@@ -84,9 +84,7 @@ export class AuthService implements AuthServiceAbstraction {
null, null,
null, null,
key, key,
null, twoFactor,
null,
null,
captchaToken, captchaToken,
null null
); );
@@ -96,6 +94,7 @@ export class AuthService implements AuthServiceAbstraction {
code: string, code: string,
codeVerifier: string, codeVerifier: string,
redirectUrl: string, redirectUrl: string,
twoFactor: TwoFactorData,
orgId: string orgId: string
): Promise<AuthResult> { ): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider(); this.twoFactorService.clearSelectedProvider();
@@ -109,15 +108,13 @@ export class AuthService implements AuthServiceAbstraction {
null, null,
null, null,
null, null,
null, twoFactor,
null,
null,
null, null,
orgId orgId
); );
} }
async logInApiKey(clientId: string, clientSecret: string): Promise<AuthResult> { async logInApiKey(clientId: string, clientSecret: string, twoFactor: TwoFactorData): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider(); this.twoFactorService.clearSelectedProvider();
return await this.logInHelper( return await this.logInHelper(
null, null,
@@ -129,18 +126,14 @@ export class AuthService implements AuthServiceAbstraction {
clientId, clientId,
clientSecret, clientSecret,
null, null,
null, twoFactor,
null,
null,
null, null,
null null
); );
} }
async logInTwoFactor( async logInTwoFactor(
twoFactorProvider: TwoFactorProviderType, twoFactor: TwoFactorData,
twoFactorToken: string,
remember?: boolean
): Promise<AuthResult> { ): Promise<AuthResult> {
return await this.logInHelper( return await this.logInHelper(
this.email, this.email,
@@ -152,101 +145,12 @@ export class AuthService implements AuthServiceAbstraction {
this.clientId, this.clientId,
this.clientSecret, this.clientSecret,
this.key, this.key,
twoFactorProvider, twoFactor,
twoFactorToken,
remember,
this.captchaToken, this.captchaToken,
null null
); );
} }
async logInComplete(
email: string,
masterPassword: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean,
captchaToken?: string
): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider();
const key = await this.makePreloginKey(masterPassword, email);
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
const localHashedPassword = await this.cryptoService.hashPassword(
masterPassword,
key,
HashPurpose.LocalAuthorization
);
return await this.logInHelper(
email,
hashedPassword,
localHashedPassword,
null,
null,
null,
null,
null,
key,
twoFactorProvider,
twoFactorToken,
remember,
captchaToken,
null
);
}
async logInSsoComplete(
code: string,
codeVerifier: string,
redirectUrl: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean
): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider();
return await this.logInHelper(
null,
null,
null,
code,
codeVerifier,
redirectUrl,
null,
null,
null,
twoFactorProvider,
twoFactorToken,
remember,
null,
null
);
}
async logInApiKeyComplete(
clientId: string,
clientSecret: string,
twoFactorProvider: TwoFactorProviderType,
twoFactorToken: string,
remember?: boolean
): Promise<AuthResult> {
this.twoFactorService.clearSelectedProvider();
return await this.logInHelper(
null,
null,
null,
null,
null,
null,
clientId,
clientSecret,
null,
twoFactorProvider,
twoFactorToken,
remember,
null,
null
);
}
logOut(callback: Function) { logOut(callback: Function) {
callback(); callback();
this.messagingService.send("loggedOut"); this.messagingService.send("loggedOut");
@@ -292,9 +196,7 @@ export class AuthService implements AuthServiceAbstraction {
clientId: string, clientId: string,
clientSecret: string, clientSecret: string,
key: SymmetricCryptoKey, key: SymmetricCryptoKey,
twoFactorProvider?: TwoFactorProviderType, twoFactor: TwoFactorData,
twoFactorToken?: string,
remember?: boolean,
captchaToken?: string, captchaToken?: string,
orgId?: string orgId?: string
): Promise<AuthResult> { ): Promise<AuthResult> {
@@ -306,9 +208,7 @@ export class AuthService implements AuthServiceAbstraction {
redirectUrl, redirectUrl,
clientId, clientId,
clientSecret, clientSecret,
twoFactorToken, twoFactor,
twoFactorProvider,
remember,
captchaToken captchaToken
); );
@@ -400,28 +300,27 @@ export class AuthService implements AuthServiceAbstraction {
redirectUrl: string, redirectUrl: string,
clientId: string, clientId: string,
clientSecret: string, clientSecret: string,
twoFactorToken: string, twoFactor: TwoFactorData,
twoFactorProvider: TwoFactorProviderType,
remember: boolean,
captchaToken: string captchaToken: string
) { ) {
const deviceRequest = await this.createDeviceRequest(); const deviceRequest = await this.createDeviceRequest();
const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
const twoFactor: TwoFactorData = { if (twoFactor == null) {
token: null, const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
provider: null, if (storedTwoFactorToken != null) {
remember: false, twoFactor = {
}; token: storedTwoFactorToken,
provider: TwoFactorProviderType.Remember,
if (twoFactorToken != null && twoFactorProvider != null) { remember: false,
twoFactor.token = twoFactorToken; }
twoFactor.provider = twoFactorProvider; } else {
twoFactor.remember = remember; twoFactor = {
} else if (storedTwoFactorToken != null) { token: null,
twoFactor.token = storedTwoFactorToken; provider: null,
twoFactor.provider = TwoFactorProviderType.Remember; remember: false,
} };
}
}
if (email != null && hashedPassword != null) { if (email != null && hashedPassword != null) {
return new PasswordTokenRequest( return new PasswordTokenRequest(

View File

@@ -190,7 +190,7 @@ describe("Cipher Service", () => {
const expected = newAuthResponse(); const expected = newAuthResponse();
// Act // Act
const result = await authService.logIn(email, masterPassword); const result = await authService.logIn(email, masterPassword, null);
// Assert // Assert
// Api call: // Api call:
@@ -240,7 +240,7 @@ describe("Cipher Service", () => {
expected.captchaSiteKey = siteKey; expected.captchaSiteKey = siteKey;
// Act // Act
const result = await authService.logIn(email, masterPassword); const result = await authService.logIn(email, masterPassword, null);
// Assertions // Assertions
stateService.didNotReceive().addAccount(Arg.any()); stateService.didNotReceive().addAccount(Arg.any());
@@ -274,7 +274,7 @@ describe("Cipher Service", () => {
); );
// Act // Act
const result = await authService.logIn(email, masterPassword); const result = await authService.logIn(email, masterPassword, null);
// Assertions // Assertions
commonSuccessAssertions(); commonSuccessAssertions();
@@ -293,7 +293,7 @@ describe("Cipher Service", () => {
tokenService.getTwoFactorToken(email).resolves(null); tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logIn(email, masterPassword); const result = await authService.logIn(email, masterPassword, null);
commonSuccessAssertions(); commonSuccessAssertions();
apiService.received(1).postAccountKeys(Arg.any()); apiService.received(1).postAccountKeys(Arg.any());
@@ -317,7 +317,7 @@ describe("Cipher Service", () => {
expected.twoFactorProviders = twoFactorProviders; expected.twoFactorProviders = twoFactorProviders;
expected.captchaSiteKey = undefined; expected.captchaSiteKey = undefined;
const result = await authService.logIn(email, masterPassword); const result = await authService.logIn(email, masterPassword, null);
stateService.didNotReceive().addAccount(Arg.any()); stateService.didNotReceive().addAccount(Arg.any());
messagingService.didNotReceive().send(Arg.any()); messagingService.didNotReceive().send(Arg.any());
@@ -332,7 +332,7 @@ describe("Cipher Service", () => {
authService.masterPasswordHash = hashedPassword; authService.masterPasswordHash = hashedPassword;
authService.localMasterPasswordHash = localHashedPassword; authService.localMasterPasswordHash = localHashedPassword;
await authService.logInTwoFactor(twoFactorProviderType, twoFactorToken, twoFactorRemember); await authService.logInTwoFactor({ provider: twoFactorProviderType, token: twoFactorToken, remember: twoFactorRemember });
apiService.received(1).postIdentityToken( apiService.received(1).postIdentityToken(
Arg.is((actual) => { Arg.is((actual) => {
@@ -359,7 +359,7 @@ describe("Cipher Service", () => {
tokenService.getTwoFactorToken(null).resolves(null); tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId); const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
// Assert // Assert
// Api call: // Api call:
@@ -405,7 +405,7 @@ describe("Cipher Service", () => {
tokenService.getTwoFactorToken(null).resolves(null); tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId); const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
// Assert // Assert
cryptoService.didNotReceive().setEncPrivateKey(privateKey); cryptoService.didNotReceive().setEncPrivateKey(privateKey);
@@ -419,7 +419,7 @@ describe("Cipher Service", () => {
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId); const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
commonSuccessAssertions(); commonSuccessAssertions();
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl); keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
@@ -453,7 +453,7 @@ describe("Cipher Service", () => {
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId); const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
commonSuccessAssertions(); commonSuccessAssertions();
cryptoService.received(1).setKey(preloginKey); cryptoService.received(1).setKey(preloginKey);
@@ -482,7 +482,7 @@ describe("Cipher Service", () => {
const tokenResponse = newTokenResponse(); const tokenResponse = newTokenResponse();
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse); apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInApiKey(apiClientId, apiClientSecret); const result = await authService.logInApiKey(apiClientId, apiClientSecret, null);
apiService.received(1).postIdentityToken( apiService.received(1).postIdentityToken(
Arg.is((actual) => { Arg.is((actual) => {