mirror of
https://github.com/bitwarden/jslib
synced 2025-12-19 09:43:28 +00:00
Update clients to use new authService interface
This commit is contained in:
@@ -93,7 +93,7 @@ export class LoginComponent extends CaptchaProtectedComponent implements OnInit
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.formPromise = this.authService.logIn(this.email, this.masterPassword, this.captchaToken);
|
this.formPromise = this.authService.logIn(this.email, this.masterPassword, null, this.captchaToken);
|
||||||
const response = await this.formPromise;
|
const response = await this.formPromise;
|
||||||
if (this.rememberEmail) {
|
if (this.rememberEmail) {
|
||||||
await this.stateService.setRememberedEmail(this.email);
|
await this.stateService.setRememberedEmail(this.email);
|
||||||
|
|||||||
@@ -193,9 +193,11 @@ export class TwoFactorComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
async doSubmit() {
|
async doSubmit() {
|
||||||
this.formPromise = this.authService.logInTwoFactor(
|
this.formPromise = this.authService.logInTwoFactor(
|
||||||
this.selectedProviderType,
|
{
|
||||||
this.token,
|
provider: this.selectedProviderType,
|
||||||
this.remember
|
token: this.token,
|
||||||
|
remember: this.remember
|
||||||
|
}
|
||||||
);
|
);
|
||||||
const response: AuthResult = await this.formPromise;
|
const response: AuthResult = await this.formPromise;
|
||||||
const disableFavicon = await this.stateService.getDisableFavicon();
|
const disableFavicon = await this.stateService.getDisableFavicon();
|
||||||
|
|||||||
@@ -11,15 +11,15 @@ export abstract class AuthService {
|
|||||||
clientId: string;
|
clientId: string;
|
||||||
clientSecret: string;
|
clientSecret: string;
|
||||||
|
|
||||||
logIn: (email: string, masterPassword: string, twoFactor: TwoFactorData, 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
|
twoFactor?: TwoFactorData,
|
||||||
) => Promise<AuthResult>;
|
) => Promise<AuthResult>;
|
||||||
logInApiKey: (clientId: string, clientSecret: string, twoFactor: TwoFactorData) => Promise<AuthResult>;
|
logInApiKey: (clientId: string, clientSecret: string, twoFactor?: TwoFactorData) => Promise<AuthResult>;
|
||||||
logInTwoFactor: (
|
logInTwoFactor: (
|
||||||
twoFactor: TwoFactorData
|
twoFactor: TwoFactorData
|
||||||
) => Promise<AuthResult>;
|
) => Promise<AuthResult>;
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
private setCryptoKeys = true
|
private setCryptoKeys = true
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async logIn(email: string, masterPassword: string, twoFactor: TwoFactorData, 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);
|
||||||
@@ -94,8 +94,8 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
code: string,
|
code: string,
|
||||||
codeVerifier: string,
|
codeVerifier: string,
|
||||||
redirectUrl: string,
|
redirectUrl: string,
|
||||||
twoFactor: TwoFactorData,
|
orgId: string,
|
||||||
orgId: string
|
twoFactor?: TwoFactorData,
|
||||||
): Promise<AuthResult> {
|
): Promise<AuthResult> {
|
||||||
this.twoFactorService.clearSelectedProvider();
|
this.twoFactorService.clearSelectedProvider();
|
||||||
return await this.logInHelper(
|
return await this.logInHelper(
|
||||||
@@ -114,7 +114,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async logInApiKey(clientId: string, clientSecret: string, twoFactor: TwoFactorData): 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,
|
||||||
|
|||||||
@@ -156,46 +156,38 @@ export class LoginCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let response: AuthResult = null;
|
let response: AuthResult = null;
|
||||||
if (twoFactorToken != null && twoFactorMethod != null) {
|
|
||||||
if (clientId != null && clientSecret != null) {
|
if (clientId != null && clientSecret != null) {
|
||||||
response = await this.authService.logInApiKeyComplete(
|
response = await this.authService.logInApiKey(
|
||||||
clientId,
|
clientId,
|
||||||
clientSecret,
|
clientSecret,
|
||||||
twoFactorMethod,
|
{
|
||||||
twoFactorToken,
|
provider: twoFactorMethod,
|
||||||
false
|
token: twoFactorToken,
|
||||||
);
|
remember: false
|
||||||
} else if (ssoCode != null && ssoCodeVerifier != null) {
|
|
||||||
response = await this.authService.logInSsoComplete(
|
|
||||||
ssoCode,
|
|
||||||
ssoCodeVerifier,
|
|
||||||
this.ssoRedirectUri,
|
|
||||||
twoFactorMethod,
|
|
||||||
twoFactorToken,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
response = await this.authService.logInComplete(
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
twoFactorMethod,
|
|
||||||
twoFactorToken,
|
|
||||||
false,
|
|
||||||
this.clientSecret
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
);
|
||||||
if (clientId != null && clientSecret != null) {
|
|
||||||
response = await this.authService.logInApiKey(clientId, clientSecret);
|
|
||||||
} else if (ssoCode != null && ssoCodeVerifier != null) {
|
} else if (ssoCode != null && ssoCodeVerifier != null) {
|
||||||
response = await this.authService.logInSso(
|
response = await this.authService.logInSso(
|
||||||
ssoCode,
|
ssoCode,
|
||||||
ssoCodeVerifier,
|
ssoCodeVerifier,
|
||||||
this.ssoRedirectUri,
|
this.ssoRedirectUri,
|
||||||
orgIdentifier
|
orgIdentifier,
|
||||||
|
{
|
||||||
|
provider: twoFactorMethod,
|
||||||
|
token: twoFactorToken,
|
||||||
|
remember: false
|
||||||
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
response = await this.authService.logIn(email, password);
|
response = await this.authService.logIn(
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
{
|
||||||
|
provider: twoFactorMethod,
|
||||||
|
token: twoFactorToken,
|
||||||
|
remember: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (response.captchaSiteKey) {
|
if (response.captchaSiteKey) {
|
||||||
const badCaptcha = Response.badRequest(
|
const badCaptcha = Response.badRequest(
|
||||||
@@ -210,12 +202,14 @@ export class LoginCommand {
|
|||||||
return badCaptcha;
|
return badCaptcha;
|
||||||
}
|
}
|
||||||
|
|
||||||
const secondResponse = await this.authService.logInComplete(
|
const secondResponse = await this.authService.logIn(
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
twoFactorMethod,
|
{
|
||||||
twoFactorToken,
|
provider: twoFactorMethod,
|
||||||
false,
|
token: twoFactorToken,
|
||||||
|
remember: false,
|
||||||
|
},
|
||||||
captchaClientSecret
|
captchaClientSecret
|
||||||
);
|
);
|
||||||
response = secondResponse;
|
response = secondResponse;
|
||||||
@@ -298,12 +292,11 @@ export class LoginCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response = await this.authService.logInTwoFactor(
|
response = await this.authService.logInTwoFactor({
|
||||||
selectedProvider.type,
|
provider: selectedProvider.type,
|
||||||
twoFactorToken,
|
token: twoFactorToken,
|
||||||
false
|
remember: false,
|
||||||
);
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.twoFactor) {
|
if (response.twoFactor) {
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ describe("Cipher Service", () => {
|
|||||||
const expected = newAuthResponse();
|
const expected = newAuthResponse();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
const result = await authService.logIn(email, masterPassword, null);
|
const result = await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
// 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, null);
|
const result = await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
// 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, null);
|
const result = await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
// 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, null);
|
const result = await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
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, null);
|
const result = await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
stateService.didNotReceive().addAccount(Arg.any());
|
stateService.didNotReceive().addAccount(Arg.any());
|
||||||
messagingService.didNotReceive().send(Arg.any());
|
messagingService.didNotReceive().send(Arg.any());
|
||||||
@@ -331,7 +331,7 @@ describe("Cipher Service", () => {
|
|||||||
|
|
||||||
tokenService.getTwoFactorToken(email).resolves(twoFactorToken);
|
tokenService.getTwoFactorToken(email).resolves(twoFactorToken);
|
||||||
|
|
||||||
await authService.logIn(email, masterPassword, null);
|
await authService.logIn(email, masterPassword);
|
||||||
|
|
||||||
apiService.received(1).postIdentityToken(
|
apiService.received(1).postIdentityToken(
|
||||||
Arg.is((actual) => {
|
Arg.is((actual) => {
|
||||||
@@ -406,7 +406,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, null, ssoOrgId);
|
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
// Api call:
|
// Api call:
|
||||||
@@ -452,7 +452,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, null, ssoOrgId);
|
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
cryptoService.didNotReceive().setEncPrivateKey(privateKey);
|
cryptoService.didNotReceive().setEncPrivateKey(privateKey);
|
||||||
@@ -466,7 +466,7 @@ describe("Cipher Service", () => {
|
|||||||
|
|
||||||
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
|
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
|
||||||
|
|
||||||
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
|
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
|
||||||
|
|
||||||
commonSuccessAssertions();
|
commonSuccessAssertions();
|
||||||
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
|
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
|
||||||
@@ -500,7 +500,7 @@ describe("Cipher Service", () => {
|
|||||||
|
|
||||||
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
|
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
|
||||||
|
|
||||||
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, null, ssoOrgId);
|
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
|
||||||
|
|
||||||
commonSuccessAssertions();
|
commonSuccessAssertions();
|
||||||
cryptoService.received(1).setKey(preloginKey);
|
cryptoService.received(1).setKey(preloginKey);
|
||||||
@@ -529,7 +529,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, null);
|
const result = await authService.logInApiKey(apiClientId, apiClientSecret);
|
||||||
|
|
||||||
apiService.received(1).postIdentityToken(
|
apiService.received(1).postIdentityToken(
|
||||||
Arg.is((actual) => {
|
Arg.is((actual) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user