Skip to content

Commit

Permalink
PM-9532: pt2. separate vault unlock logic and fail out on error durin…
Browse files Browse the repository at this point in the history
…g login. (#3609)
  • Loading branch information
dseverns-livefront authored Aug 6, 2024
1 parent a090000 commit 18cd66a
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 149 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.x8bit.bitwarden.data.auth.repository.model

import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockError
import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult

/**
* Helper function to map a [VaultUnlockError] to a [LoginResult.Error] with
* the necessary `message` if applicable.
*/
fun VaultUnlockError.toLoginErrorResult(): LoginResult.Error = when (this) {
is VaultUnlockResult.AuthenticationError -> LoginResult.Error(this.message)
VaultUnlockResult.GenericError,
VaultUnlockResult.InvalidStateError,
-> LoginResult.Error(errorMessage = null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class VaultSdkSourceImpl(
InitializeCryptoResult.Success
} catch (exception: BitwardenException) {
// The only truly expected error from the SDK is an incorrect key/password.
InitializeCryptoResult.AuthenticationError
InitializeCryptoResult.AuthenticationError(message = exception.message)
}
}

Expand All @@ -150,7 +150,9 @@ class VaultSdkSourceImpl(
InitializeCryptoResult.Success
} catch (exception: BitwardenException) {
// The only truly expected error from the SDK is for incorrect keys.
InitializeCryptoResult.AuthenticationError
InitializeCryptoResult.AuthenticationError(
message = exception.message,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ sealed class InitializeCryptoResult {
/**
* Incorrect password or key(s) provided.
*/
data object AuthenticationError : InitializeCryptoResult()
data class AuthenticationError(
val message: String? = null,
) : InitializeCryptoResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ sealed class VaultUnlockResult {
/**
* Incorrect password provided.
*/
data object AuthenticationError : VaultUnlockResult()
data class AuthenticationError(
val message: String? = null,
) : VaultUnlockResult(), VaultUnlockError

/**
* Unable to access user state information.
*/
data object InvalidStateError : VaultUnlockResult()
data object InvalidStateError : VaultUnlockResult(), VaultUnlockError

/**
* Generic error thrown by Bitwarden SDK.
*/
data object GenericError : VaultUnlockResult()
data object GenericError : VaultUnlockResult(), VaultUnlockError
}

/**
* Sealed interface to denote that a [VaultUnlockResult] is an error result.
*/
sealed interface VaultUnlockError
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import com.x8bit.bitwarden.data.vault.repository.model.VaultUnlockResult
*/
fun InitializeCryptoResult.toVaultUnlockResult(): VaultUnlockResult =
when (this) {
InitializeCryptoResult.AuthenticationError -> VaultUnlockResult.AuthenticationError
is InitializeCryptoResult.AuthenticationError -> {
VaultUnlockResult.AuthenticationError(
message = this.message,
)
}

InitializeCryptoResult.Success -> VaultUnlockResult.Success
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.x8bit.bitwarden.ui.platform.components.scaffold.BitwardenScaffold
import com.x8bit.bitwarden.ui.platform.components.text.BitwardenClickableText
import com.x8bit.bitwarden.ui.platform.components.toggle.BitwardenSwitch
import com.x8bit.bitwarden.ui.platform.components.util.rememberVectorPainter
import com.x8bit.bitwarden.ui.platform.theme.BitwardenTheme

/**
* The top level composable for the Reset Password screen.
Expand Down Expand Up @@ -230,26 +231,28 @@ private fun TrustedDeviceDialogs(
@Preview
@Composable
private fun TrustedDeviceScaffold_preview() {
TrustedDeviceScaffold(
state = TrustedDeviceState(
dialogState = null,
isRemembered = false,
emailAddress = "[email protected]",
environmentLabel = "vault.bitwarden.pw",
showContinueButton = false,
showOtherDeviceButton = true,
showRequestAdminButton = true,
showMasterPasswordButton = true,
),
handlers = TrustedDeviceHandlers(
onBackClick = {},
onDismissDialog = {},
onRememberToggle = {},
onContinueClick = {},
onApproveWithAdminClick = {},
onApproveWithDeviceClick = {},
onApproveWithPasswordClick = {},
onNotYouButtonClick = {},
),
)
BitwardenTheme {
TrustedDeviceScaffold(
state = TrustedDeviceState(
dialogState = null,
isRemembered = false,
emailAddress = "[email protected]",
environmentLabel = "vault.bitwarden.pw",
showContinueButton = false,
showOtherDeviceButton = true,
showRequestAdminButton = true,
showMasterPasswordButton = true,
),
handlers = TrustedDeviceHandlers(
onBackClick = {},
onDismissDialog = {},
onRememberToggle = {},
onContinueClick = {},
onApproveWithAdminClick = {},
onApproveWithDeviceClick = {},
onApproveWithPasswordClick = {},
onNotYouButtonClick = {},
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class VaultUnlockViewModel @Inject constructor(
action.vaultUnlockResult is VaultUnlockResult.Success

when (action.vaultUnlockResult) {
VaultUnlockResult.AuthenticationError -> {
is VaultUnlockResult.AuthenticationError -> {
mutableStateFlow.update {
it.copy(
dialog = VaultUnlockState.VaultUnlockDialog.Error(
Expand Down
Loading

0 comments on commit 18cd66a

Please sign in to comment.