diff --git a/Sources/KeychainSwift.swift b/Sources/KeychainSwift.swift index e7779c9..33d9a54 100644 --- a/Sources/KeychainSwift.swift +++ b/Sources/KeychainSwift.swift @@ -141,8 +141,8 @@ open class KeychainSwift { - returns: The text value from the keychain. Returns nil if unable to read the item. */ - open func get(_ key: String) -> String? { - if let data = getData(key) { + open func get(_ key: String, withAccess access: KeychainSwiftAccessOptions? = nil) -> String? { + if let data = getData(key, withAccess: access) { if let currentString = String(data: data, encoding: .utf8) { return currentString @@ -163,7 +163,7 @@ open class KeychainSwift { - returns: The text value from the keychain. Returns nil if unable to read the item. */ - open func getData(_ key: String, asReference: Bool = false) -> Data? { + open func getData(_ key: String, asReference: Bool = false, withAccess access: KeychainSwiftAccessOptions? = nil) -> Data? { // The lock prevents the code to be run simlultaneously // from multiple threads which may result in crashing lock.lock() @@ -174,9 +174,13 @@ open class KeychainSwift { var query: [String: Any] = [ KeychainSwiftConstants.klass : kSecClassGenericPassword, KeychainSwiftConstants.attrAccount : prefixedKey, - KeychainSwiftConstants.matchLimit : kSecMatchLimitOne + KeychainSwiftConstants.matchLimit : kSecMatchLimitOne, ] + if let access = access { + query[KeychainSwiftConstants.accessible] = access.value + } + if asReference { query[KeychainSwiftConstants.returnReference] = kCFBooleanTrue } else { @@ -208,8 +212,8 @@ open class KeychainSwift { - returns: The boolean value from the keychain. Returns nil if unable to read the item. */ - open func getBool(_ key: String) -> Bool? { - guard let data = getData(key) else { return nil } + open func getBool(_ key: String, withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool? { + guard let data = getData(key, withAccess: access) else { return nil } guard let firstBit = data.first else { return nil } return firstBit == 1 } diff --git a/Tests/KeychainSwiftTests/KeychainSwiftTests.swift b/Tests/KeychainSwiftTests/KeychainSwiftTests.swift index 3cea865..672a12d 100644 --- a/Tests/KeychainSwiftTests/KeychainSwiftTests.swift +++ b/Tests/KeychainSwiftTests/KeychainSwiftTests.swift @@ -78,6 +78,20 @@ class KeychainSwiftTests: XCTestCase { func testGet_returnNilWhenValueNotSet() { XCTAssert(obj.get("key 1") == nil) } + + func testGet_usesAccessibleWhenUnlockedByDefault() { + obj.set("hello :)", forKey: "key 1") + XCTAssertTrue(obj.get("key 1") != nil) + let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String + XCTAssert(accessValue == nil) + } + + func testGet_withAccessOption() { + obj.set("hello :)", forKey: "key 1", withAccess: .accessibleAfterFirstUnlock) + XCTAssertTrue(obj.get("key 1", withAccess: .accessibleAfterFirstUnlock) != nil) + let accessValue = obj.lastQueryParameters?[KeychainSwiftConstants.accessible] as? String + XCTAssertEqual(KeychainSwiftAccessOptions.accessibleAfterFirstUnlock.value, accessValue!) + } // MARK: - Get bool // -----------------------