Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Access options to the get parameters with a default option set. #123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions Sources/KeychainSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/KeychainSwiftTests/KeychainSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -----------------------
Expand Down