diff --git a/common/src/services/auth.service.ts b/common/src/services/auth.service.ts index 3c733d38..391ae758 100644 --- a/common/src/services/auth.service.ts +++ b/common/src/services/auth.service.ts @@ -88,7 +88,7 @@ export class AuthService implements AuthServiceAbstraction { 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) { return result; @@ -133,7 +133,7 @@ export class AuthService implements AuthServiceAbstraction { 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) { return result; @@ -186,7 +186,7 @@ export class AuthService implements AuthServiceAbstraction { 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) { return result; @@ -197,6 +197,9 @@ export class AuthService implements AuthServiceAbstraction { return result; } + await this.stateService.setApiKeyClientId(clientId); + await this.stateService.setApiKeyClientSecret(clientSecret); + const tokenResponse = response as IdentityTokenResponse; if (tokenResponse.apiUseKeyConnector) { const keyConnectorUrl = this.environmentService.getKeyConnectorUrl(); @@ -263,8 +266,6 @@ export class AuthService implements AuthServiceAbstraction { private async processTokenResponse( response: IdentityTokenResponse | IdentityTwoFactorResponse | IdentityCaptchaResponse, code: string, - clientId: string, - clientSecret: string, ): Promise { this.clearState(); const result = new AuthResult(); @@ -284,23 +285,21 @@ export class AuthService implements AuthServiceAbstraction { result.resetMasterPassword = tokenResponse.resetMasterPassword; result.forcePasswordReset = tokenResponse.forcePasswordReset; - this.saveAccountInformation(tokenResponse, clientId, clientSecret); + this.saveAccountInformation(tokenResponse); if (tokenResponse.twoFactorToken != null) { await this.tokenService.setTwoFactorToken(tokenResponse.twoFactorToken); } - if (this.setCryptoKeys) { - if (!this.isNewSsoUser(code, tokenResponse.key)) { - await this.cryptoService.setEncKey(tokenResponse.key); + if (this.setCryptoKeys && !this.isNewSsoUser(code, tokenResponse.key)) { + await this.cryptoService.setEncKey(tokenResponse.key); - // User doesn't have a key pair yet (old account), let's generate one for them - if (tokenResponse.privateKey == null) { - const newKeyPair = await this.createKeyPair(); - await this.cryptoService.setEncPrivateKey(newKeyPair); - } else { - await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey); - } + // User doesn't have a key pair yet (old account), let's generate one for them + if (tokenResponse.privateKey == null) { + const newKeyPair = await this.createKeyPair(); + await this.cryptoService.setEncPrivateKey(newKeyPair); + } else { + await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey); } } @@ -337,11 +336,7 @@ export class AuthService implements AuthServiceAbstraction { return twoFactor; } - private async saveAccountInformation( - tokenResponse: IdentityTokenResponse, - clientId: string, - clientSecret: string - ) { + private async saveAccountInformation(tokenResponse: IdentityTokenResponse) { const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken); await this.stateService.addAccount({ profile: { @@ -349,8 +344,6 @@ export class AuthService implements AuthServiceAbstraction { ...{ userId: accountInformation.sub, email: accountInformation.email, - apiKeyClientId: clientId, - apiKeyClientSecret: clientSecret, hasPremiumPersonally: accountInformation.premium, kdfIterations: tokenResponse.kdfIterations, kdfType: tokenResponse.kdf, diff --git a/spec/common/services/auth.service.spec.ts b/spec/common/services/auth.service.spec.ts index 3ef68ece..d31d8df5 100644 --- a/spec/common/services/auth.service.spec.ts +++ b/spec/common/services/auth.service.spec.ts @@ -136,8 +136,6 @@ describe("Cipher Service", () => { ...{ userId: userId, email: email, - apiKeyClientId: null, - apiKeyClientSecret: null, hasPremiumPersonally: false, kdfIterations: kdfIterations, kdfType: kdf, @@ -539,30 +537,11 @@ describe("Cipher Service", () => { }) ); + // Sets local environment: - stateService.received(1).addAccount({ - profile: { - ...new AccountProfile(), - ...{ - 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"); + stateService.received(1).setApiKeyClientId(apiClientId); + stateService.received(1).setApiKeyClientSecret(apiClientSecret); + commonSuccessAssertions(); cryptoService.received(1).setEncKey(encKey); cryptoService.received(1).setEncPrivateKey(privateKey);