From cd25e2b2801299d673cae42565e191b28a4695cb Mon Sep 17 00:00:00 2001 From: David Perez Date: Wed, 19 Jun 2024 12:36:00 -0500 Subject: [PATCH] BIT-2430: Allow lowercase TOTP secrets (#1469) --- .../feature/qrcodescan/QrCodeScanViewModel.kt | 2 +- .../qrcodescan/QrCodeScanViewModelTest.kt | 24 +++++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModel.kt index af2d93eb2..0e48b1f54 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModel.kt @@ -165,6 +165,6 @@ sealed class QrCodeScanAction { * Checks if a string is using base32 digits. */ private fun String.isBase32(): Boolean { - val regex = ("^[A-Z2-7]+=*$").toRegex() + val regex = ("^[A-Za-z2-7]+=*$").toRegex() return regex.matches(this) } diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModelTest.kt index adcfba25f..0648ce37c 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/vault/feature/qrcodescan/QrCodeScanViewModelTest.kt @@ -81,7 +81,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { setupMockUri() val validCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP&algorithm=sha256&digits=8&period=60" + "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP&algorithm=sha256&digits=8&period=60" val viewModel = createViewModel() val result = TotpCodeResult.Success(validCode) @@ -99,8 +99,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { queryParameterNames = setOf(SECRET), ) - val validCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP" + val validCode = "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP" val viewModel = createViewModel() val result = TotpCodeResult.Success(validCode) @@ -119,8 +118,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { val viewModel = createViewModel() val result = TotpCodeResult.CodeScanningError - val invalidCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP&algorithm=sha224" + val invalidCode = "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP&algorithm=sha224" viewModel.eventFlow.test { viewModel.trySendAction(QrCodeScanAction.QrCodeScanReceive(invalidCode)) @@ -136,8 +134,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { val viewModel = createViewModel() val result = TotpCodeResult.CodeScanningError - val invalidCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP&digits=11" + val invalidCode = "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP&digits=11" viewModel.eventFlow.test { viewModel.trySendAction(QrCodeScanAction.QrCodeScanReceive(invalidCode)) @@ -153,8 +150,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { val viewModel = createViewModel() val result = TotpCodeResult.CodeScanningError - val invalidCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP&period=0" + val invalidCode = "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP&period=0" viewModel.eventFlow.test { viewModel.trySendAction(QrCodeScanAction.QrCodeScanReceive(invalidCode)) @@ -169,8 +165,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { val viewModel = createViewModel() val result = TotpCodeResult.CodeScanningError - val invalidCode = - "nototpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP" + val invalidCode = "nototpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP" viewModel.eventFlow.test { viewModel.trySendAction(QrCodeScanAction.QrCodeScanReceive(invalidCode)) @@ -182,12 +177,11 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { @Test fun `QrCodeScan should emit failure result with non base32 secret`() = runTest { - setupMockUri(secret = "JBSWY3DPEHPK3PXP1") + setupMockUri(secret = "JBSWY3dpeHPK3PXP1") val viewModel = createViewModel() val result = TotpCodeResult.CodeScanningError - val invalidCode = - "otpauth://totp/Test:me?secret=JBSWY3DPEHPK3PXP1" + val invalidCode = "otpauth://totp/Test:me?secret=JBSWY3dpeHPK3PXP1" viewModel.eventFlow.test { viewModel.trySendAction(QrCodeScanAction.QrCodeScanReceive(invalidCode)) @@ -244,7 +238,7 @@ class QrCodeScanViewModelTest : BaseViewModelTest() { } private fun setupMockUri( - secret: String? = "JBSWY3DPEHPK3PXP", + secret: String? = "JBSWY3dpeHPK3PXP", algorithm: String = "SHA256", digits: String = "8", period: String = "60",