mirror of
https://github.com/bitwarden/jslib
synced 2025-12-10 13:23:15 +00:00
Move api specific stateService calls to entry method
This commit is contained in:
@@ -88,7 +88,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
|
|
||||||
const response = await this.apiService.postIdentityToken(tokenRequest);
|
const response = await this.apiService.postIdentityToken(tokenRequest);
|
||||||
|
|
||||||
const result = await this.processTokenResponse(response, null, null, null);
|
const result = await this.processTokenResponse(response, null);
|
||||||
|
|
||||||
if (!!result.captchaSiteKey) {
|
if (!!result.captchaSiteKey) {
|
||||||
return result;
|
return result;
|
||||||
@@ -133,7 +133,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
|
|
||||||
const response = await this.apiService.postIdentityToken(tokenRequest);
|
const response = await this.apiService.postIdentityToken(tokenRequest);
|
||||||
|
|
||||||
const result = await this.processTokenResponse(response, code, null, null);
|
const result = await this.processTokenResponse(response, code);
|
||||||
|
|
||||||
if (!!result.captchaSiteKey) {
|
if (!!result.captchaSiteKey) {
|
||||||
return result;
|
return result;
|
||||||
@@ -186,7 +186,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
|
|
||||||
const response = await this.apiService.postIdentityToken(tokenRequest);
|
const response = await this.apiService.postIdentityToken(tokenRequest);
|
||||||
|
|
||||||
const result = await this.processTokenResponse(response, null, clientId, clientSecret);
|
const result = await this.processTokenResponse(response, null);
|
||||||
|
|
||||||
if (!!result.captchaSiteKey) {
|
if (!!result.captchaSiteKey) {
|
||||||
return result;
|
return result;
|
||||||
@@ -197,6 +197,9 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.stateService.setApiKeyClientId(clientId);
|
||||||
|
await this.stateService.setApiKeyClientSecret(clientSecret);
|
||||||
|
|
||||||
const tokenResponse = response as IdentityTokenResponse;
|
const tokenResponse = response as IdentityTokenResponse;
|
||||||
if (tokenResponse.apiUseKeyConnector) {
|
if (tokenResponse.apiUseKeyConnector) {
|
||||||
const keyConnectorUrl = this.environmentService.getKeyConnectorUrl();
|
const keyConnectorUrl = this.environmentService.getKeyConnectorUrl();
|
||||||
@@ -263,8 +266,6 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
private async processTokenResponse(
|
private async processTokenResponse(
|
||||||
response: IdentityTokenResponse | IdentityTwoFactorResponse | IdentityCaptchaResponse,
|
response: IdentityTokenResponse | IdentityTwoFactorResponse | IdentityCaptchaResponse,
|
||||||
code: string,
|
code: string,
|
||||||
clientId: string,
|
|
||||||
clientSecret: string,
|
|
||||||
): Promise<AuthResult> {
|
): Promise<AuthResult> {
|
||||||
this.clearState();
|
this.clearState();
|
||||||
const result = new AuthResult();
|
const result = new AuthResult();
|
||||||
@@ -284,23 +285,21 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
result.resetMasterPassword = tokenResponse.resetMasterPassword;
|
result.resetMasterPassword = tokenResponse.resetMasterPassword;
|
||||||
result.forcePasswordReset = tokenResponse.forcePasswordReset;
|
result.forcePasswordReset = tokenResponse.forcePasswordReset;
|
||||||
|
|
||||||
this.saveAccountInformation(tokenResponse, clientId, clientSecret);
|
this.saveAccountInformation(tokenResponse);
|
||||||
|
|
||||||
if (tokenResponse.twoFactorToken != null) {
|
if (tokenResponse.twoFactorToken != null) {
|
||||||
await this.tokenService.setTwoFactorToken(tokenResponse.twoFactorToken);
|
await this.tokenService.setTwoFactorToken(tokenResponse.twoFactorToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.setCryptoKeys) {
|
if (this.setCryptoKeys && !this.isNewSsoUser(code, tokenResponse.key)) {
|
||||||
if (!this.isNewSsoUser(code, tokenResponse.key)) {
|
await this.cryptoService.setEncKey(tokenResponse.key);
|
||||||
await this.cryptoService.setEncKey(tokenResponse.key);
|
|
||||||
|
|
||||||
// User doesn't have a key pair yet (old account), let's generate one for them
|
// User doesn't have a key pair yet (old account), let's generate one for them
|
||||||
if (tokenResponse.privateKey == null) {
|
if (tokenResponse.privateKey == null) {
|
||||||
const newKeyPair = await this.createKeyPair();
|
const newKeyPair = await this.createKeyPair();
|
||||||
await this.cryptoService.setEncPrivateKey(newKeyPair);
|
await this.cryptoService.setEncPrivateKey(newKeyPair);
|
||||||
} else {
|
} else {
|
||||||
await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey);
|
await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,11 +336,7 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
return twoFactor;
|
return twoFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async saveAccountInformation(
|
private async saveAccountInformation(tokenResponse: IdentityTokenResponse) {
|
||||||
tokenResponse: IdentityTokenResponse,
|
|
||||||
clientId: string,
|
|
||||||
clientSecret: string
|
|
||||||
) {
|
|
||||||
const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken);
|
const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken);
|
||||||
await this.stateService.addAccount({
|
await this.stateService.addAccount({
|
||||||
profile: {
|
profile: {
|
||||||
@@ -349,8 +344,6 @@ export class AuthService implements AuthServiceAbstraction {
|
|||||||
...{
|
...{
|
||||||
userId: accountInformation.sub,
|
userId: accountInformation.sub,
|
||||||
email: accountInformation.email,
|
email: accountInformation.email,
|
||||||
apiKeyClientId: clientId,
|
|
||||||
apiKeyClientSecret: clientSecret,
|
|
||||||
hasPremiumPersonally: accountInformation.premium,
|
hasPremiumPersonally: accountInformation.premium,
|
||||||
kdfIterations: tokenResponse.kdfIterations,
|
kdfIterations: tokenResponse.kdfIterations,
|
||||||
kdfType: tokenResponse.kdf,
|
kdfType: tokenResponse.kdf,
|
||||||
|
|||||||
@@ -136,8 +136,6 @@ describe("Cipher Service", () => {
|
|||||||
...{
|
...{
|
||||||
userId: userId,
|
userId: userId,
|
||||||
email: email,
|
email: email,
|
||||||
apiKeyClientId: null,
|
|
||||||
apiKeyClientSecret: null,
|
|
||||||
hasPremiumPersonally: false,
|
hasPremiumPersonally: false,
|
||||||
kdfIterations: kdfIterations,
|
kdfIterations: kdfIterations,
|
||||||
kdfType: kdf,
|
kdfType: kdf,
|
||||||
@@ -539,30 +537,11 @@ describe("Cipher Service", () => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// Sets local environment:
|
// Sets local environment:
|
||||||
stateService.received(1).addAccount({
|
stateService.received(1).setApiKeyClientId(apiClientId);
|
||||||
profile: {
|
stateService.received(1).setApiKeyClientSecret(apiClientSecret);
|
||||||
...new AccountProfile(),
|
commonSuccessAssertions();
|
||||||
...{
|
|
||||||
userId: userId,
|
|
||||||
email: email,
|
|
||||||
apiKeyClientId: apiClientId,
|
|
||||||
apiKeyClientSecret: apiClientSecret,
|
|
||||||
hasPremiumPersonally: false,
|
|
||||||
kdfIterations: kdfIterations,
|
|
||||||
kdfType: kdf,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
tokens: {
|
|
||||||
...new AccountTokens(),
|
|
||||||
...{
|
|
||||||
accessToken: accessToken,
|
|
||||||
refreshToken: refreshToken,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
stateService.received(1).setBiometricLocked(false);
|
|
||||||
messagingService.received(1).send("loggedIn");
|
|
||||||
|
|
||||||
cryptoService.received(1).setEncKey(encKey);
|
cryptoService.received(1).setEncKey(encKey);
|
||||||
cryptoService.received(1).setEncPrivateKey(privateKey);
|
cryptoService.received(1).setEncPrivateKey(privateKey);
|
||||||
|
|||||||
Reference in New Issue
Block a user