Simplify common login helper methods (#3805)

This commit is contained in:
David Perez 2024-08-22 11:22:07 -05:00 committed by GitHub
parent 0d6aeee870
commit 91f039ecb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,6 +7,7 @@ import com.bitwarden.crypto.HashPurpose
import com.bitwarden.crypto.Kdf import com.bitwarden.crypto.Kdf
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson.Profile
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountTokensJson import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountTokensJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.ForcePasswordResetReason import com.x8bit.bitwarden.data.auth.datasource.disk.model.ForcePasswordResetReason
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
@ -1475,7 +1476,7 @@ class AuthRepositoryImpl(
if (isDeviceUnlockAvailable) { if (isDeviceUnlockAvailable) {
unlockVaultWithTdeOnLoginSuccess( unlockVaultWithTdeOnLoginSuccess(
loginResponse = loginResponse, loginResponse = loginResponse,
userStateJson = userStateJson, profile = profile,
deviceData = deviceData, deviceData = deviceData,
) )
} else if (keyConnectorUrl != null && orgIdentifier != null) { } else if (keyConnectorUrl != null && orgIdentifier != null) {
@ -1488,7 +1489,7 @@ class AuthRepositoryImpl(
} else { } else {
unlockVaultWithPasswordOnLoginSuccess( unlockVaultWithPasswordOnLoginSuccess(
loginResponse = loginResponse, loginResponse = loginResponse,
userStateJson = userStateJson, profile = profile,
password = password, password = password,
) )
} }
@ -1663,7 +1664,7 @@ class AuthRepositoryImpl(
*/ */
private suspend fun unlockVaultWithPasswordOnLoginSuccess( private suspend fun unlockVaultWithPasswordOnLoginSuccess(
loginResponse: GetTokenResponseJson.Success, loginResponse: GetTokenResponseJson.Success,
userStateJson: UserStateJson, profile: Profile,
password: String?, password: String?,
): VaultUnlockResult? { ): VaultUnlockResult? {
// Attempt to unlock the vault with password if possible. // Attempt to unlock the vault with password if possible.
@ -1671,7 +1672,7 @@ class AuthRepositoryImpl(
val privateKey = loginResponse.privateKey ?: return null val privateKey = loginResponse.privateKey ?: return null
val key = loginResponse.key ?: return null val key = loginResponse.key ?: return null
return unlockVault( return unlockVault(
accountProfile = userStateJson.activeAccount.profile, accountProfile = profile,
privateKey = privateKey, privateKey = privateKey,
initUserCryptoMethod = InitUserCryptoMethod.Password( initUserCryptoMethod = InitUserCryptoMethod.Password(
password = masterPassword, password = masterPassword,
@ -1685,7 +1686,7 @@ class AuthRepositoryImpl(
*/ */
private suspend fun unlockVaultWithTdeOnLoginSuccess( private suspend fun unlockVaultWithTdeOnLoginSuccess(
loginResponse: GetTokenResponseJson.Success, loginResponse: GetTokenResponseJson.Success,
userStateJson: UserStateJson, profile: Profile,
deviceData: DeviceDataModel?, deviceData: DeviceDataModel?,
): VaultUnlockResult? { ): VaultUnlockResult? {
// Attempt to unlock the vault with auth request if possible. // Attempt to unlock the vault with auth request if possible.
@ -1693,7 +1694,7 @@ class AuthRepositoryImpl(
if (loginResponse.privateKey != null && loginResponse.key != null) { if (loginResponse.privateKey != null && loginResponse.key != null) {
deviceData?.let { model -> deviceData?.let { model ->
return unlockVault( return unlockVault(
accountProfile = userStateJson.activeAccount.profile, accountProfile = profile,
privateKey = loginResponse.privateKey, privateKey = loginResponse.privateKey,
initUserCryptoMethod = InitUserCryptoMethod.AuthRequest( initUserCryptoMethod = InitUserCryptoMethod.AuthRequest(
requestPrivateKey = model.privateKey, requestPrivateKey = model.privateKey,
@ -1722,7 +1723,7 @@ class AuthRepositoryImpl(
loginResponse.privateKey?.let { privateKey -> loginResponse.privateKey?.let { privateKey ->
unlockVaultWithTrustedDeviceUserDecryptionOptionsAndStoreKeys( unlockVaultWithTrustedDeviceUserDecryptionOptionsAndStoreKeys(
options = options, options = options,
userStateJson = userStateJson, profile = profile,
privateKey = privateKey, privateKey = privateKey,
) )
} }
@ -1735,11 +1736,11 @@ class AuthRepositoryImpl(
*/ */
private suspend fun unlockVaultWithTrustedDeviceUserDecryptionOptionsAndStoreKeys( private suspend fun unlockVaultWithTrustedDeviceUserDecryptionOptionsAndStoreKeys(
options: TrustedDeviceUserDecryptionOptionsJson, options: TrustedDeviceUserDecryptionOptionsJson,
userStateJson: UserStateJson, profile: Profile,
privateKey: String, privateKey: String,
): VaultUnlockResult? { ): VaultUnlockResult? {
var vaultUnlockResult: VaultUnlockResult? = null var vaultUnlockResult: VaultUnlockResult? = null
val userId = userStateJson.activeUserId val userId = profile.userId
val deviceKey = authDiskSource.getDeviceKey(userId = userId) val deviceKey = authDiskSource.getDeviceKey(userId = userId)
if (deviceKey == null) { if (deviceKey == null) {
// A null device key means this device is not trusted. // A null device key means this device is not trusted.
@ -1753,7 +1754,7 @@ class AuthRepositoryImpl(
// For approved requests the key will always be present. // For approved requests the key will always be present.
val userKey = requireNotNull(request.key) val userKey = requireNotNull(request.key)
vaultUnlockResult = unlockVault( vaultUnlockResult = unlockVault(
accountProfile = userStateJson.activeAccount.profile, accountProfile = profile,
privateKey = privateKey, privateKey = privateKey,
initUserCryptoMethod = InitUserCryptoMethod.AuthRequest( initUserCryptoMethod = InitUserCryptoMethod.AuthRequest(
requestPrivateKey = pendingRequest.requestPrivateKey, requestPrivateKey = pendingRequest.requestPrivateKey,
@ -1780,7 +1781,7 @@ class AuthRepositoryImpl(
} }
vaultUnlockResult = unlockVault( vaultUnlockResult = unlockVault(
accountProfile = userStateJson.activeAccount.profile, accountProfile = profile,
privateKey = privateKey, privateKey = privateKey,
initUserCryptoMethod = InitUserCryptoMethod.DeviceKey( initUserCryptoMethod = InitUserCryptoMethod.DeviceKey(
deviceKey = deviceKey, deviceKey = deviceKey,