From c43bd0e1447787bdd9322608a7e0fb9ed088ba4c Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Thu, 16 Dec 2021 11:46:28 +1000 Subject: [PATCH] Extract createKeyPair to own method --- common/src/services/auth.service.ts | 63 ++++++++++++++++------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/common/src/services/auth.service.ts b/common/src/services/auth.service.ts index 5bb8ba05..3e9d610d 100644 --- a/common/src/services/auth.service.ts +++ b/common/src/services/auth.service.ts @@ -365,31 +365,41 @@ export class AuthService implements AuthServiceAbstraction { await this.apiService.postSetKeyConnectorKey(setPasswordRequest); } -private async saveAccountInformation(tokenResponse: IdentityTokenResponse, clientId: string, clientSecret: string) { - const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken); - await this.stateService.addAccount({ - profile: { - ...new AccountProfile(), - ...{ - userId: accountInformation.sub, - email: accountInformation.email, - apiKeyClientId: clientId, - apiKeyClientSecret: clientSecret, - hasPremiumPersonally: accountInformation.premium, - kdfIterations: tokenResponse.kdfIterations, - kdfType: tokenResponse.kdf, + private async saveAccountInformation(tokenResponse: IdentityTokenResponse, clientId: string, clientSecret: string) { + const accountInformation = await this.tokenService.decodeToken(tokenResponse.accessToken); + await this.stateService.addAccount({ + profile: { + ...new AccountProfile(), + ...{ + userId: accountInformation.sub, + email: accountInformation.email, + apiKeyClientId: clientId, + apiKeyClientSecret: clientSecret, + hasPremiumPersonally: accountInformation.premium, + kdfIterations: tokenResponse.kdfIterations, + kdfType: tokenResponse.kdf, + }, }, - }, - tokens: { - ...new AccountTokens(), - ...{ - accessToken: tokenResponse.accessToken, - refreshToken: tokenResponse.refreshToken, + tokens: { + ...new AccountTokens(), + ...{ + accessToken: tokenResponse.accessToken, + refreshToken: tokenResponse.refreshToken, + }, }, - }, }); } + private async createKeyPair() { + try { + const keyPair = await this.cryptoService.makeKeyPair(); + await this.apiService.postAccountKeys(new KeysRequest(keyPair[0], keyPair[1].encryptedString)); + return keyPair[1].encryptedString; + } catch (e) { + this.logService.error(e); + } + } + private async logInHelper(email: string, hashedPassword: string, localHashedPassword: string, code: string, codeVerifier: string, redirectUrl: string, clientId: string, clientSecret: string, key: SymmetricCryptoKey, twoFactorProvider?: TwoFactorProviderType, twoFactorToken?: string, remember?: boolean, captchaToken?: string, @@ -449,16 +459,11 @@ private async saveAccountInformation(tokenResponse: IdentityTokenResponse, clien // User doesn't have a key pair yet (old account), let's generate one for them if (tokenResponse.privateKey == null) { - try { - const keyPair = await this.cryptoService.makeKeyPair(); - await this.apiService.postAccountKeys(new KeysRequest(keyPair[0], keyPair[1].encryptedString)); - tokenResponse.privateKey = keyPair[1].encryptedString; - } catch (e) { - this.logService.error(e); - } + const newKeyPair = await this.createKeyPair(); + await this.cryptoService.setEncPrivateKey(newKeyPair); + } else { + await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey); } - - await this.cryptoService.setEncPrivateKey(tokenResponse.privateKey); } else if (tokenResponse.keyConnectorUrl != null) { await this.convertNewUserToKeyConnector(tokenResponse, orgId); }