From eceee581c95b20a9aca823a067863787d3724202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Bispo?= Date: Mon, 24 Jul 2023 22:23:17 +0100 Subject: [PATCH] [PM-2297] Refactor auth service key connector code --- src/Core/Services/AuthService.cs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Core/Services/AuthService.cs b/src/Core/Services/AuthService.cs index d05c081a7..d11009d2d 100644 --- a/src/Core/Services/AuthService.cs +++ b/src/Core/Services/AuthService.cs @@ -482,14 +482,9 @@ namespace Bit.Core.Services if (code == null || tokenResponse.Key != null) { - if (tokenResponse.KeyConnectorUrl != null) - { - await _keyConnectorService.GetAndSetKey(tokenResponse.KeyConnectorUrl); - } - + var decryptOptions = await _stateService.GetAccountDecryptionOptions(); await _cryptoService.SetMasterKeyEncryptedUserKeyAsync(tokenResponse.Key); - var decryptOptions = await _stateService.GetAccountDecryptionOptions(); if (decryptOptions?.TrustedDeviceOption != null) { var key = await _deviceTrustCryptoService.DecryptUserKeyWithDeviceKeyAsync(decryptOptions?.TrustedDeviceOption.EncryptedPrivateKey, decryptOptions?.TrustedDeviceOption.EncryptedUserKey); @@ -498,12 +493,16 @@ namespace Bit.Core.Services await _cryptoService.SetUserKeyAsync(key); } } - else if (masterKey != null && - (!string.IsNullOrEmpty(tokenResponse.KeyConnectorUrl) || !string.IsNullOrEmpty(decryptOptions?.KeyConnectorOption?.KeyConnectorUrl))) + else if (!string.IsNullOrEmpty(tokenResponse.KeyConnectorUrl) || !string.IsNullOrEmpty(decryptOptions?.KeyConnectorOption?.KeyConnectorUrl)) { - await _cryptoService.SetMasterKeyAsync(masterKey); - var userKey = await _cryptoService.DecryptUserKeyWithMasterKeyAsync(masterKey); - await _cryptoService.SetUserKeyAsync(userKey); + + await _cryptoService.SetMasterKeyEncryptedUserKeyAsync(tokenResponse.Key); + if (masterKey != null) + { + await _cryptoService.SetMasterKeyAsync(masterKey); + var userKey = await _cryptoService.DecryptUserKeyWithMasterKeyAsync(masterKey); + await _cryptoService.SetUserKeyAsync(userKey); + } } // User doesn't have a key pair yet (old account), let's generate one for them.