Skip to content

Commit 3ba69ac

Browse files
PM-9094 - TOTP not shown when viewing an item (#735)
1 parent b2fe8b5 commit 3ba69ac

24 files changed

+145
-195
lines changed

BitwardenShared/Core/Vault/Models/Domain/Fixtures/VaultListItem+Fixtures.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension VaultListTOTP {
4949
static func fixture(
5050
id: String = "123",
5151
loginView: BitwardenSdk.LoginView = .fixture(
52-
totp: .base32Key
52+
totp: .standardTotpKey
5353
),
5454
requiresMasterPassword: Bool = false,
5555
timeProvider: TimeProvider,
@@ -71,7 +71,7 @@ extension VaultListTOTP {
7171
static func fixture(
7272
id: String = "123",
7373
loginView: BitwardenSdk.LoginView = .fixture(
74-
totp: .base32Key
74+
totp: .standardTotpKey
7575
),
7676
requiresMasterPassword: Bool = false,
7777
totpCode: TOTPCodeModel = .init(

BitwardenShared/Core/Vault/Repositories/TestHelpers/MockVaultRepository.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class MockVaultRepository: VaultRepository {
6060
var refreshedTOTPTime: Date?
6161
var refreshedTOTPCodes: [VaultListItem] = []
6262
var refreshTOTPCodeResult: Result<LoginTOTPState, Error> = .success(
63-
LoginTOTPState(
64-
authKeyModel: TOTPKeyModel(authenticatorKey: .base32Key)!
65-
)
63+
LoginTOTPState(authKeyModel: TOTPKeyModel(authenticatorKey: .standardTotpKey))
6664
)
6765
var refreshedTOTPKeyConfig: TOTPKeyModel?
6866

BitwardenShared/Core/Vault/Repositories/VaultRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ extension DefaultVaultRepository: VaultRepository {
12441244
return cipher.type == .secureNote
12451245
case .totp:
12461246
return cipher.type == .login
1247-
&& TOTPKey(cipher.login?.totp ?? "") != nil
1247+
&& cipher.login?.totp != nil
12481248
case .trash:
12491249
return cipher.deletedDate != nil
12501250
}

BitwardenShared/Core/Vault/Repositories/VaultRepositoryTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
406406
/// `refreshTOTPCode(:)` rethrows errors.
407407
func test_refreshTOTPCode_error() async throws {
408408
clientService.mockVault.generateTOTPCodeResult = .failure(BitwardenTestError.example)
409-
let keyModel = try XCTUnwrap(TOTPKeyModel(authenticatorKey: .base32Key))
409+
let keyModel = TOTPKeyModel(authenticatorKey: .standardTotpKey)
410410
await assertAsyncThrows(error: BitwardenTestError.example) {
411411
_ = try await subject.refreshTOTPCode(for: keyModel)
412412
}
@@ -416,7 +416,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
416416
func test_refreshTOTPCode_success() async throws {
417417
let newCode = "999232"
418418
clientService.mockVault.generateTOTPCodeResult = .success(newCode)
419-
let keyModel = try XCTUnwrap(TOTPKeyModel(authenticatorKey: .base32Key))
419+
let keyModel = TOTPKeyModel(authenticatorKey: .standardTotpKey)
420420
let update = try await subject.refreshTOTPCode(for: keyModel)
421421
XCTAssertEqual(
422422
update,
@@ -467,7 +467,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
467467
clientService.mockVault.generateTOTPCodeResult = .success(newCode)
468468
let totpModel = VaultListTOTP(
469469
id: "123",
470-
loginView: .fixture(totp: .base32Key),
470+
loginView: .fixture(totp: .standardTotpKey),
471471
requiresMasterPassword: false,
472472
totpCode: .init(
473473
code: "123456",
@@ -675,7 +675,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
675675
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
676676
.fixture(
677677
id: "5",
678-
login: .fixture(totp: .base32Key),
678+
login: .fixture(totp: .standardTotpKey),
679679
name: "one time cafefe",
680680
type: .login
681681
),
@@ -711,7 +711,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
711711
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
712712
.fixture(
713713
id: "5",
714-
login: .fixture(totp: .base32Key),
714+
login: .fixture(totp: .standardTotpKey),
715715
name: "one time cafefe",
716716
type: .login
717717
),
@@ -752,7 +752,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
752752
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
753753
.fixture(
754754
id: "5",
755-
login: .fixture(totp: .base32Key),
755+
login: .fixture(totp: .standardTotpKey),
756756
name: "one time cafefe",
757757
type: .login
758758
),
@@ -797,7 +797,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
797797
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
798798
.fixture(
799799
id: "5",
800-
login: .fixture(totp: .base32Key),
800+
login: .fixture(totp: .standardTotpKey),
801801
name: "one time cafefe",
802802
type: .login
803803
),
@@ -838,7 +838,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
838838
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
839839
.fixture(
840840
id: "6",
841-
login: .fixture(totp: .base32Key),
841+
login: .fixture(totp: .standardTotpKey),
842842
name: "one time cafefe",
843843
type: .login
844844
),
@@ -889,7 +889,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
889889
.fixture(id: "5", name: "Café thoughts", type: .secureNote),
890890
.fixture(
891891
id: "6",
892-
login: .fixture(totp: .base32Key),
892+
login: .fixture(totp: .standardTotpKey),
893893
name: "one time cafefe",
894894
type: .login
895895
),
@@ -927,7 +927,7 @@ class VaultRepositoryTests: BitwardenTestCase { // swiftlint:disable:this type_b
927927
.fixture(id: "5", name: "Café thoughts", type: .login),
928928
.fixture(
929929
id: "6",
930-
login: .fixture(totp: .base32Key),
930+
login: .fixture(totp: .standardTotpKey),
931931
name: "one time cafefe",
932932
type: .login
933933
),

BitwardenShared/Core/Vault/Services/TOTP/OTPAuthModel.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public struct OTPAuthModel: Equatable, Hashable, Sendable {
1818
/// The provider or service the account is associated with.
1919
let issuer: String?
2020

21-
/// The Base32-encoded key used for generating the OTP.
22-
let keyB32: String
21+
/// The key used for generating the OTP.
22+
let key: String
2323

2424
/// The time period in seconds for which the OTP is valid.
2525
let period: Int
@@ -36,7 +36,7 @@ public struct OTPAuthModel: Equatable, Hashable, Sendable {
3636
/// - algorithm: The hashing algorithm used for generating the OTP.
3737
/// - digits: The number of digits in the OTP.
3838
/// - issuer: The provider or service the account is associated with.
39-
/// - keyB32: The Base32-encoded key.
39+
/// - key: The totp key.
4040
/// - period: The time period in seconds for which the OTP is valid.
4141
/// - uri: The unparsed key URI.
4242
///
@@ -45,15 +45,15 @@ public struct OTPAuthModel: Equatable, Hashable, Sendable {
4545
algorithm: TOTPCryptoHashAlgorithm,
4646
digits: Int,
4747
issuer: String? = nil,
48-
keyB32: String,
48+
key: String,
4949
period: Int,
5050
uri: String
5151
) {
5252
self.accountName = accountName
5353
self.algorithm = algorithm
5454
self.digits = digits
5555
self.issuer = issuer
56-
self.keyB32 = keyB32
56+
self.key = key
5757
self.period = period
5858
self.uri = uri
5959
}
@@ -92,7 +92,7 @@ public struct OTPAuthModel: Equatable, Hashable, Sendable {
9292
algorithm: algorithm,
9393
digits: digits,
9494
issuer: issuer,
95-
keyB32: secret,
95+
key: secret,
9696
period: period,
9797
uri: otpAuthKey
9898
)

BitwardenShared/Core/Vault/Services/TOTP/OTPAuthModelTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class OTPAuthModelTests: BitwardenTestCase {
99

1010
/// Tests that a malformed string does not create a model.
1111
func test_init_otpAuthKey_failure_base32() {
12-
let subject = OTPAuthModel(otpAuthKey: .base32Key)
12+
let subject = OTPAuthModel(otpAuthKey: .standardTotpKey)
1313
XCTAssertNil(subject)
1414
}
1515

@@ -43,7 +43,7 @@ class OTPAuthModelTests: BitwardenTestCase {
4343
algorithm: .sha256,
4444
digits: 6,
4545
issuer: "Example",
46-
keyB32: "JBSWY3DPEHPK3PXP",
46+
key: "JBSWY3DPEHPK3PXP",
4747
period: 30,
4848
uri: .otpAuthUriKeyComplete
4949
)
@@ -61,7 +61,7 @@ class OTPAuthModelTests: BitwardenTestCase {
6161
algorithm: .sha1,
6262
digits: 6,
6363
issuer: "Bitwarden",
64-
keyB32: "JBSWY3DPEHPK3PXP",
64+
key: "JBSWY3DPEHPK3PXP",
6565
period: 30,
6666
uri: key
6767
)
@@ -78,7 +78,7 @@ class OTPAuthModelTests: BitwardenTestCase {
7878
algorithm: .sha1,
7979
digits: 6,
8080
issuer: nil,
81-
keyB32: "JBSWY3DPEHPK3PXP",
81+
key: "JBSWY3DPEHPK3PXP",
8282
period: 30,
8383
uri: .otpAuthUriKeyMinimum
8484
)
@@ -95,7 +95,7 @@ class OTPAuthModelTests: BitwardenTestCase {
9595
algorithm: .sha1,
9696
digits: 6,
9797
issuer: "Example",
98-
keyB32: "JBSWY3DPEHPK3PXP",
98+
key: "JBSWY3DPEHPK3PXP",
9999
period: 30,
100100
uri: .otpAuthUriKeyPartial
101101
)
@@ -113,7 +113,7 @@ class OTPAuthModelTests: BitwardenTestCase {
113113
algorithm: .sha1,
114114
digits: 6,
115115
issuer: "ACME Co",
116-
keyB32: "JBSWY3DPEHPK3PXP",
116+
key: "JBSWY3DPEHPK3PXP",
117117
period: 30,
118118
uri: key
119119
)
@@ -131,7 +131,7 @@ class OTPAuthModelTests: BitwardenTestCase {
131131
algorithm: .sha1,
132132
digits: 6,
133133
issuer: "Bitwarden",
134-
keyB32: "JBSWY3DPEHPK3PXP",
134+
key: "JBSWY3DPEHPK3PXP",
135135
period: 30,
136136
uri: key
137137
)

BitwardenShared/Core/Vault/Services/TOTP/TOTPCodeConfigTests.swift

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,41 @@ import XCTest
77
final class TOTPCodeConfigTests: BitwardenTestCase {
88
// MARK: Tests
99

10-
/// Tests that a malformed string does not create a model.
11-
func test_init_totpCodeConfig_failure_incompletePrefix() {
12-
let subject = TOTPKeyModel(
13-
authenticatorKey: "totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP"
14-
)
15-
XCTAssertNil(subject)
10+
/// Tests that a standard key string creates the model.
11+
func test_init_totpCodeConfig_standard() {
12+
let subject = TOTPKeyModel(authenticatorKey: .standardTotpKey)
13+
XCTAssertEqual(subject.key, .standardTotpKey)
1614
}
1715

18-
/// Tests that a malformed string does not create a model.
19-
func test_init_totpCodeConfig_failure_noSecret() {
20-
let subject = TOTPKeyModel(
21-
authenticatorKey: "otpauth://totp/Example:[email protected]?issuer=Example&algorithm=SHA256&digits=6&period=30" // swiftlint:disable:this line_length
22-
)
23-
XCTAssertNil(subject)
24-
}
25-
26-
/// Tests that a base32 string creates the model.
27-
func test_init_totpCodeConfig_base32() {
28-
let subject = TOTPKeyModel(
29-
authenticatorKey: .base32Key
30-
)
31-
XCTAssertNotNil(subject)
32-
XCTAssertEqual(subject?.base32Key, .base32Key)
16+
/// Tests that a key string with spaces creates the model.
17+
func test_init_totpCodeConfig_spacesPresent() {
18+
let subject = TOTPKeyModel(authenticatorKey: .standardTotpKeyWithSpaces)
19+
XCTAssertEqual(subject.key, .standardTotpKeyWithSpaces)
3320
}
3421

3522
/// Tests that an otp auth string creates the model.
3623
func test_init_totpCodeConfig_success_full() {
37-
let subject = TOTPKeyModel(
38-
authenticatorKey: .otpAuthUriKeyComplete
39-
)
40-
XCTAssertNotNil(subject)
41-
XCTAssertEqual(
42-
subject?.base32Key,
43-
.base32Key
44-
)
24+
let subject = TOTPKeyModel(authenticatorKey: .otpAuthUriKeyComplete)
25+
XCTAssertEqual(subject.key, .standardTotpKey)
4526
}
4627

4728
/// Tests that an otp auth string creates the model.
4829
func test_init_totpCodeConfig_success_partial() {
49-
let subject = TOTPKeyModel(
50-
authenticatorKey: .otpAuthUriKeyPartial
51-
)
52-
XCTAssertNotNil(subject)
53-
XCTAssertEqual(
54-
subject?.base32Key,
55-
.base32Key
56-
)
30+
let subject = TOTPKeyModel(authenticatorKey: .otpAuthUriKeyPartial)
31+
XCTAssertEqual(subject.key, .standardTotpKey)
5732
}
5833

5934
/// Tests that an otp auth string creates the model.
6035
func test_init_totpCodeConfig_success_sha512() {
61-
let subject = TOTPKeyModel(
62-
authenticatorKey: .otpAuthUriKeySHA512
63-
)
64-
XCTAssertNotNil(subject)
65-
XCTAssertEqual(
66-
subject?.base32Key,
67-
.base32Key
68-
)
36+
let subject = TOTPKeyModel(authenticatorKey: .otpAuthUriKeySHA512)
37+
XCTAssertEqual(subject.key, .standardTotpKey)
6938
}
7039

7140
/// Tests that a steam string creates the model.
7241
func test_init_totpCodeConfig_success_steam() {
7342
let subject = TOTPKeyModel(authenticatorKey: .steamUriKey)
74-
XCTAssertNotNil(subject)
75-
XCTAssertEqual(subject?.base32Key, .base32Key)
76-
XCTAssertEqual(subject?.digits, 5)
77-
XCTAssertEqual(subject?.algorithm, .sha1)
43+
XCTAssertEqual(subject.key, .standardTotpKey)
44+
XCTAssertEqual(subject.digits, 5)
45+
XCTAssertEqual(subject.algorithm, .sha1)
7846
}
7947
}

0 commit comments

Comments
 (0)