Skip to content

Commit 0e113bf

Browse files
[BITAU-121] Add accountDomain and accountEmail to AuthenticatorBridge items (#1017)
1 parent 621d901 commit 0e113bf

9 files changed

+95
-47
lines changed

AuthenticatorBridgeKit/AuthenticatorBridgeItemDataModel.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import Foundation
66
public struct AuthenticatorBridgeItemDataModel: Codable, Equatable {
77
// MARK: Properties
88

9+
/// The domain of the Bitwarden account that owns this item. (e.g. https://vault.bitwarden.com)
10+
public let accountDomain: String?
11+
12+
/// The email of the Bitwarden account that owns this item.
13+
public let accountEmail: String?
14+
915
/// Bool indicating if this item is a favorite.
1016
public let favorite: Bool
1117

@@ -18,6 +24,6 @@ public struct AuthenticatorBridgeItemDataModel: Codable, Equatable {
1824
/// The TOTP key used to generate codes.
1925
public let totpKey: String?
2026

21-
/// The username of the Bitwarden account that owns this iteam.
27+
/// The username of the item.
2228
public let username: String?
2329
}

AuthenticatorBridgeKit/AuthenticatorBridgeItemDataView.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import Foundation
66
public struct AuthenticatorBridgeItemDataView: Codable, Equatable {
77
// MARK: Properties
88

9+
/// The domain of the Bitwarden account that owns this item. (e.g. https://vault.bitwarden.com)
10+
public let accountDomain: String?
11+
12+
/// The email of the Bitwarden account that owns this item.
13+
public let accountEmail: String?
14+
915
/// Bool indicating if this item is a favorite.
1016
public let favorite: Bool
1117

@@ -18,19 +24,29 @@ public struct AuthenticatorBridgeItemDataView: Codable, Equatable {
1824
/// The TOTP key used to generate codes.
1925
public let totpKey: String?
2026

21-
/// The username of the Bitwarden account that owns this iteam.
27+
/// The username of the item.
2228
public let username: String?
2329

2430
/// Initialize an `AuthenticatorBridgeItemDataView` with the values provided.
2531
///
2632
/// - Parameters:
33+
/// - accountDomain: The domain of the Bitwarden account that owns this item.
34+
/// - accountEmail: The email of the Bitwarden account that owns this item
2735
/// - favorite: Bool indicating if this item is a favorite.
2836
/// - id: The unique id of the item.
2937
/// - name: The name of the item.
3038
/// - totpKey: The TOTP key used to generate codes.
31-
/// - username: The username of the Bitwarden account that owns this item.
39+
/// - username: The username of the item.
3240
///
33-
public init(favorite: Bool, id: String, name: String, totpKey: String?, username: String?) {
41+
public init(accountDomain: String?,
42+
accountEmail: String?,
43+
favorite: Bool,
44+
id: String,
45+
name: String,
46+
totpKey: String?,
47+
username: String?) {
48+
self.accountDomain = accountDomain
49+
self.accountEmail = accountEmail
3450
self.favorite = favorite
3551
self.id = id
3652
self.name = name

AuthenticatorBridgeKit/SharedCryptographyService.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class DefaultAuthenticatorCryptographyService: SharedCryptographyService
6262

6363
return items.map { item in
6464
AuthenticatorBridgeItemDataView(
65+
accountDomain: (try? decrypt(item.accountDomain, withKey: symmetricKey)) ?? "",
66+
accountEmail: (try? decrypt(item.accountEmail, withKey: symmetricKey)) ?? "",
6567
favorite: item.favorite,
6668
id: item.id,
6769
name: (try? decrypt(item.name, withKey: symmetricKey)) ?? "",
@@ -79,6 +81,8 @@ public class DefaultAuthenticatorCryptographyService: SharedCryptographyService
7981

8082
return items.map { item in
8183
AuthenticatorBridgeItemDataModel(
84+
accountDomain: encrypt(item.accountDomain, withKey: symmetricKey) ?? "",
85+
accountEmail: encrypt(item.accountEmail, withKey: symmetricKey) ?? "",
8286
favorite: item.favorite,
8387
id: item.id,
8488
name: encrypt(item.name, withKey: symmetricKey) ?? "",

AuthenticatorBridgeKit/Tests/AuthenticatorBridgeItemDataTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ final class AuthenticatorBridgeItemDataTests: AuthenticatorBridgeKitTestCase {
4949
context: dataStore.persistentContainer.viewContext,
5050
userId: "userId",
5151
authenticatorItem: AuthenticatorBridgeItemDataModel(
52-
favorite: true, id: "is", name: "name", totpKey: "TOTP Key", username: "username"
52+
accountDomain: "https://vault.example.com",
53+
accountEmail: "[email protected]",
54+
favorite: true,
55+
id: "is",
56+
name: "name",
57+
totpKey: "TOTP Key",
58+
username: "username"
5359
)
5460
)
5561

AuthenticatorBridgeKit/Tests/SharedCryptographyServiceTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ final class SharedCryptographyServiceTests: AuthenticatorBridgeKitTestCase {
7171

7272
// Encrypted values should not remain equal, unless they were `nil`
7373
XCTAssertNotEqual(item.name, encryptedItem.name)
74+
if item.accountDomain != nil {
75+
XCTAssertNotEqual(item.accountDomain, encryptedItem.accountDomain)
76+
} else {
77+
XCTAssertNil(encryptedItem.accountDomain)
78+
}
79+
if item.accountEmail != nil {
80+
XCTAssertNotEqual(item.accountEmail, encryptedItem.accountEmail)
81+
} else {
82+
XCTAssertNil(encryptedItem.accountEmail)
83+
}
7484
if item.totpKey != nil {
7585
XCTAssertNotEqual(item.totpKey, encryptedItem.totpKey)
7686
} else {

AuthenticatorBridgeKit/Tests/TestHelpers/Fixtures/AuthenticatorBridgeItemDataView+Fixtures.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import Foundation
44

55
extension AuthenticatorBridgeItemDataView {
66
static func fixture(
7+
accountDomain: String? = "",
8+
accountEmail: String? = "",
79
favorite: Bool = false,
810
id: String = UUID().uuidString,
911
name: String = "Name",
1012
totpKey: String? = nil,
1113
username: String? = nil
1214
) -> AuthenticatorBridgeItemDataView {
1315
AuthenticatorBridgeItemDataView(
16+
accountDomain: accountDomain,
17+
accountEmail: accountEmail,
1418
favorite: favorite,
1519
id: id,
1620
name: name,
@@ -23,9 +27,12 @@ extension AuthenticatorBridgeItemDataView {
2327
[
2428
AuthenticatorBridgeItemDataView.fixture(),
2529
AuthenticatorBridgeItemDataView.fixture(favorite: true),
30+
AuthenticatorBridgeItemDataView.fixture(accountDomain: "https://vault.example.com"),
31+
AuthenticatorBridgeItemDataView.fixture(accountEmail: "[email protected]"),
2632
AuthenticatorBridgeItemDataView.fixture(totpKey: "TOTP Key"),
2733
AuthenticatorBridgeItemDataView.fixture(username: "Username"),
2834
AuthenticatorBridgeItemDataView.fixture(totpKey: "TOTP Key", username: "Username"),
35+
AuthenticatorBridgeItemDataView.fixture(accountEmail: ""),
2936
AuthenticatorBridgeItemDataView.fixture(totpKey: ""),
3037
AuthenticatorBridgeItemDataView.fixture(username: ""),
3138
AuthenticatorBridgeItemDataView.fixture(totpKey: "", username: ""),

AuthenticatorBridgeKit/Tests/TestHelpers/MockSharedCryptographyService.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,14 @@ class MockSharedCryptographyService: SharedCryptographyService {
77
var decryptCalled = false
88
var encryptCalled = false
99

10-
func decryptAuthenticatorItemDatas(
11-
_ items: [AuthenticatorBridgeKit.AuthenticatorBridgeItemData]
12-
) async throws -> [AuthenticatorBridgeKit.AuthenticatorBridgeItemDataView] {
13-
decryptCalled = true
14-
15-
return items.compactMap { item in
16-
guard let model = item.model else { return nil }
17-
18-
return AuthenticatorBridgeItemDataView(
19-
favorite: model.favorite,
20-
id: model.id,
21-
name: model.name,
22-
totpKey: model.totpKey,
23-
username: model.username
24-
)
25-
}
26-
}
27-
2810
func decryptAuthenticatorItems(
2911
_ items: [AuthenticatorBridgeItemDataModel]
3012
) async throws -> [AuthenticatorBridgeItemDataView] {
3113
decryptCalled = true
3214
return items.map { model in
3315
AuthenticatorBridgeItemDataView(
16+
accountDomain: model.accountDomain,
17+
accountEmail: model.accountEmail,
3418
favorite: model.favorite,
3519
id: model.id,
3620
name: model.name,
@@ -46,6 +30,8 @@ class MockSharedCryptographyService: SharedCryptographyService {
4630
encryptCalled = true
4731
return items.map { view in
4832
AuthenticatorBridgeItemDataModel(
33+
accountDomain: view.accountDomain,
34+
accountEmail: view.accountEmail,
4935
favorite: view.favorite,
5036
id: view.id,
5137
name: view.name,

BitwardenShared/Core/Platform/Services/AuthenticatorSyncService.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,17 @@ actor DefaultAuthenticatorSyncService: NSObject, AuthenticatorSyncService {
160160
let decryptedCiphers = try await totpCiphers.asyncMap { cipher in
161161
try await self.clientService.vault(for: userId).ciphers().decrypt(cipher: cipher)
162162
}
163-
let account = try await stateService.getActiveAccount()
164-
let username = account.profile.email
163+
let account = try? await stateService.getAccount(userId: userId)
165164

166165
return decryptedCiphers.map { cipher in
167166
AuthenticatorBridgeItemDataView(
167+
accountDomain: account?.settings.environmentUrls?.webVaultHost,
168+
accountEmail: account?.profile.email,
168169
favorite: false,
169170
id: cipher.id ?? UUID().uuidString,
170171
name: cipher.name,
171172
totpKey: cipher.login?.totp,
172-
username: username
173+
username: cipher.login?.username
173174
)
174175
}
175176
}

0 commit comments

Comments
 (0)