Skip to content

Commit 51a45c7

Browse files
committed
Fix Keychain.getAll, change scope implementation
1 parent 7050a2e commit 51a45c7

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

Tool/Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ let package = Package(
7777
dependencies: ["Configs"]
7878
),
7979

80+
.testTarget(
81+
name: "KeychainTests",
82+
dependencies: ["Keychain"]
83+
),
84+
8085
.target(
8186
name: "Toast",
8287
dependencies: [.product(

Tool/Sources/Keychain/Keychain.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct Keychain: KeychainType {
3535
let service = keychainService
3636
let accessGroup = keychainAccessGroup
3737
let scope: String
38-
38+
3939
public static var apiKey: Keychain {
4040
Keychain(scope: "apiKey")
4141
}
@@ -61,7 +61,6 @@ public struct Keychain: KeychainType {
6161
}
6262

6363
func set(_ value: String, key: String) throws {
64-
let key = scopeKey(key)
6564
let query = query(key).merging([
6665
kSecValueData as String: value.data(using: .utf8) ?? Data(),
6766
], uniquingKeysWith: { _, b in b })
@@ -80,18 +79,18 @@ public struct Keychain: KeychainType {
8079
if scope.isEmpty {
8180
return key
8281
}
83-
return "\(scope).\(key)"
82+
return "\(scope)::\(key)"
8483
}
85-
84+
8685
func escapeScope(_ key: String) -> String? {
8786
if scope.isEmpty {
8887
return key
8988
}
90-
if !key.hasPrefix("\(scope).") { return nil }
91-
return key.replacingOccurrences(of: "\(scope).", with: "")
89+
if !key.hasPrefix("\(scope)::") { return nil }
90+
return key.replacingOccurrences(of: "\(scope)::", with: "")
9291
}
9392

94-
public func getAll() throws -> [String : String] {
93+
public func getAll() throws -> [String: String] {
9594
let query = [
9695
kSecClass as String: kSecClassGenericPassword as String,
9796
kSecAttrService as String: service,
@@ -109,18 +108,17 @@ public struct Keychain: KeychainType {
109108

110109
var dict = [String: String]()
111110
for item in items {
112-
guard let keyData = item[kSecAttrAccount as String] as? Data,
113-
let key = String(data: keyData, encoding: .utf8),
114-
let valueData = item[kSecValueData as String] as? Data,
115-
let value = String(data: valueData, encoding: .utf8),
111+
guard let key = item[kSecAttrAccount as String] as? String,
116112
let escapedKey = escapeScope(key)
117-
else {
118-
continue
119-
}
113+
else { continue }
114+
guard let valueData = item[kSecValueData as String] as? Data,
115+
let value = String(data: valueData, encoding: .utf8)
116+
else { continue }
120117
dict[escapedKey] = value
121118
}
119+
return dict
122120
}
123-
121+
124122
return [:]
125123
}
126124

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
import XCTest
3+
4+
@testable import Keychain
5+
6+
class KeychainTests: XCTestCase {
7+
func test_scope_key() {
8+
let keychain = Keychain(scope: "scope")
9+
XCTAssertEqual(keychain.scopeKey("key"), "scope::key")
10+
}
11+
12+
func test_escape_scope() {
13+
let keychain = Keychain(scope: "scope")
14+
XCTAssertEqual(keychain.escapeScope("scope::key"), "key")
15+
}
16+
}

0 commit comments

Comments
 (0)