mirror of
https://github.com/bitwarden/jslib
synced 2026-01-04 09:33:14 +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:
@@ -1,7 +1,6 @@
|
||||
import { TwoFactorProviderType } from "../enums/twoFactorProviderType";
|
||||
|
||||
import { AuthResult } from "../models/domain/authResult";
|
||||
import { SymmetricCryptoKey } from "../models/domain/symmetricCryptoKey";
|
||||
import { TwoFactorData } from '../models/request/identityToken/tokenRequest';
|
||||
|
||||
export abstract class AuthService {
|
||||
email: string;
|
||||
@@ -12,41 +11,17 @@ export abstract class AuthService {
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
|
||||
logIn: (email: string, masterPassword: string, captchaToken?: string) => Promise<AuthResult>;
|
||||
logIn: (email: string, masterPassword: string, twoFactor: TwoFactorData, captchaToken?: string) => Promise<AuthResult>;
|
||||
logInSso: (
|
||||
code: string,
|
||||
codeVerifier: string,
|
||||
redirectUrl: string,
|
||||
twoFactor: TwoFactorData,
|
||||
orgId: string
|
||||
) => Promise<AuthResult>;
|
||||
logInApiKey: (clientId: string, clientSecret: string) => Promise<AuthResult>;
|
||||
logInApiKey: (clientId: string, clientSecret: string, twoFactor: TwoFactorData) => Promise<AuthResult>;
|
||||
logInTwoFactor: (
|
||||
twoFactorProvider: TwoFactorProviderType,
|
||||
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
|
||||
twoFactor: TwoFactorData
|
||||
) => Promise<AuthResult>;
|
||||
logOut: (callback: Function) => void;
|
||||
makePreloginKey: (masterPassword: string, email: string) => Promise<SymmetricCryptoKey>;
|
||||
|
||||
@@ -65,7 +65,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
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();
|
||||
const key = await this.makePreloginKey(masterPassword, email);
|
||||
const hashedPassword = await this.cryptoService.hashPassword(masterPassword, key);
|
||||
@@ -84,9 +84,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
null,
|
||||
null,
|
||||
key,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
twoFactor,
|
||||
captchaToken,
|
||||
null
|
||||
);
|
||||
@@ -96,6 +94,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
code: string,
|
||||
codeVerifier: string,
|
||||
redirectUrl: string,
|
||||
twoFactor: TwoFactorData,
|
||||
orgId: string
|
||||
): Promise<AuthResult> {
|
||||
this.twoFactorService.clearSelectedProvider();
|
||||
@@ -109,15 +108,13 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
twoFactor,
|
||||
null,
|
||||
orgId
|
||||
);
|
||||
}
|
||||
|
||||
async logInApiKey(clientId: string, clientSecret: string): Promise<AuthResult> {
|
||||
async logInApiKey(clientId: string, clientSecret: string, twoFactor: TwoFactorData): Promise<AuthResult> {
|
||||
this.twoFactorService.clearSelectedProvider();
|
||||
return await this.logInHelper(
|
||||
null,
|
||||
@@ -129,18 +126,14 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
clientId,
|
||||
clientSecret,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
twoFactor,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
async logInTwoFactor(
|
||||
twoFactorProvider: TwoFactorProviderType,
|
||||
twoFactorToken: string,
|
||||
remember?: boolean
|
||||
twoFactor: TwoFactorData,
|
||||
): Promise<AuthResult> {
|
||||
return await this.logInHelper(
|
||||
this.email,
|
||||
@@ -152,101 +145,12 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
this.clientId,
|
||||
this.clientSecret,
|
||||
this.key,
|
||||
twoFactorProvider,
|
||||
twoFactorToken,
|
||||
remember,
|
||||
twoFactor,
|
||||
this.captchaToken,
|
||||
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) {
|
||||
callback();
|
||||
this.messagingService.send("loggedOut");
|
||||
@@ -292,9 +196,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
clientId: string,
|
||||
clientSecret: string,
|
||||
key: SymmetricCryptoKey,
|
||||
twoFactorProvider?: TwoFactorProviderType,
|
||||
twoFactorToken?: string,
|
||||
remember?: boolean,
|
||||
twoFactor: TwoFactorData,
|
||||
captchaToken?: string,
|
||||
orgId?: string
|
||||
): Promise<AuthResult> {
|
||||
@@ -306,9 +208,7 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
redirectUrl,
|
||||
clientId,
|
||||
clientSecret,
|
||||
twoFactorToken,
|
||||
twoFactorProvider,
|
||||
remember,
|
||||
twoFactor,
|
||||
captchaToken
|
||||
);
|
||||
|
||||
@@ -400,28 +300,27 @@ export class AuthService implements AuthServiceAbstraction {
|
||||
redirectUrl: string,
|
||||
clientId: string,
|
||||
clientSecret: string,
|
||||
twoFactorToken: string,
|
||||
twoFactorProvider: TwoFactorProviderType,
|
||||
remember: boolean,
|
||||
twoFactor: TwoFactorData,
|
||||
captchaToken: string
|
||||
) {
|
||||
const deviceRequest = await this.createDeviceRequest();
|
||||
const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
|
||||
|
||||
const twoFactor: TwoFactorData = {
|
||||
token: null,
|
||||
provider: null,
|
||||
remember: false,
|
||||
};
|
||||
|
||||
if (twoFactorToken != null && twoFactorProvider != null) {
|
||||
twoFactor.token = twoFactorToken;
|
||||
twoFactor.provider = twoFactorProvider;
|
||||
twoFactor.remember = remember;
|
||||
} else if (storedTwoFactorToken != null) {
|
||||
twoFactor.token = storedTwoFactorToken;
|
||||
twoFactor.provider = TwoFactorProviderType.Remember;
|
||||
}
|
||||
if (twoFactor == null) {
|
||||
const storedTwoFactorToken = await this.tokenService.getTwoFactorToken(email);
|
||||
if (storedTwoFactorToken != null) {
|
||||
twoFactor = {
|
||||
token: storedTwoFactorToken,
|
||||
provider: TwoFactorProviderType.Remember,
|
||||
remember: false,
|
||||
}
|
||||
} else {
|
||||
twoFactor = {
|
||||
token: null,
|
||||
provider: null,
|
||||
remember: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (email != null && hashedPassword != null) {
|
||||
return new PasswordTokenRequest(
|
||||
|
||||
@@ -190,7 +190,7 @@ describe("Cipher Service", () => {
|
||||
const expected = newAuthResponse();
|
||||
|
||||
// Act
|
||||
const result = await authService.logIn(email, masterPassword);
|
||||
const result = await authService.logIn(email, masterPassword, null);
|
||||
|
||||
// Assert
|
||||
// Api call:
|
||||
@@ -240,7 +240,7 @@ describe("Cipher Service", () => {
|
||||
expected.captchaSiteKey = siteKey;
|
||||
|
||||
// Act
|
||||
const result = await authService.logIn(email, masterPassword);
|
||||
const result = await authService.logIn(email, masterPassword, null);
|
||||
|
||||
// Assertions
|
||||
stateService.didNotReceive().addAccount(Arg.any());
|
||||
@@ -274,7 +274,7 @@ describe("Cipher Service", () => {
|
||||
);
|
||||
|
||||
// Act
|
||||
const result = await authService.logIn(email, masterPassword);
|
||||
const result = await authService.logIn(email, masterPassword, null);
|
||||
|
||||
// Assertions
|
||||
commonSuccessAssertions();
|
||||
@@ -293,7 +293,7 @@ describe("Cipher Service", () => {
|
||||
tokenService.getTwoFactorToken(email).resolves(null);
|
||||
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
|
||||
|
||||
const result = await authService.logIn(email, masterPassword);
|
||||
const result = await authService.logIn(email, masterPassword, null);
|
||||
|
||||
commonSuccessAssertions();
|
||||
apiService.received(1).postAccountKeys(Arg.any());
|
||||
@@ -317,7 +317,7 @@ describe("Cipher Service", () => {
|
||||
expected.twoFactorProviders = twoFactorProviders;
|
||||
expected.captchaSiteKey = undefined;
|
||||
|
||||
const result = await authService.logIn(email, masterPassword);
|
||||
const result = await authService.logIn(email, masterPassword, null);
|
||||
|
||||
stateService.didNotReceive().addAccount(Arg.any());
|
||||
messagingService.didNotReceive().send(Arg.any());
|
||||
@@ -332,7 +332,7 @@ describe("Cipher Service", () => {
|
||||
authService.masterPasswordHash = hashedPassword;
|
||||
authService.localMasterPasswordHash = localHashedPassword;
|
||||
|
||||
await authService.logInTwoFactor(twoFactorProviderType, twoFactorToken, twoFactorRemember);
|
||||
await authService.logInTwoFactor({ provider: twoFactorProviderType, token: twoFactorToken, remember: twoFactorRemember });
|
||||
|
||||
apiService.received(1).postIdentityToken(
|
||||
Arg.is((actual) => {
|
||||
@@ -359,7 +359,7 @@ describe("Cipher Service", () => {
|
||||
tokenService.getTwoFactorToken(null).resolves(null);
|
||||
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
|
||||
// Api call:
|
||||
@@ -405,7 +405,7 @@ describe("Cipher Service", () => {
|
||||
tokenService.getTwoFactorToken(null).resolves(null);
|
||||
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
|
||||
cryptoService.didNotReceive().setEncPrivateKey(privateKey);
|
||||
@@ -419,7 +419,7 @@ describe("Cipher Service", () => {
|
||||
|
||||
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();
|
||||
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
|
||||
@@ -453,7 +453,7 @@ describe("Cipher Service", () => {
|
||||
|
||||
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();
|
||||
cryptoService.received(1).setKey(preloginKey);
|
||||
@@ -482,7 +482,7 @@ describe("Cipher Service", () => {
|
||||
const tokenResponse = newTokenResponse();
|
||||
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(
|
||||
Arg.is((actual) => {
|
||||
|
||||
Reference in New Issue
Block a user