Skip to content

Enable updating an item’s accessible attribute #6

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 1 addition & 9 deletions Sources/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ public final class Keychain {

public func store<T: KeychainStorable>(_ storable: T, service: String = defaultService, accessGroup: String? = defaultAccessGroup) throws {
let newData = try JSONEncoder().encode(storable)
var query = self.query(for: storable, service: service, accessGroup: accessGroup)
var query = self.query(forAccount: storable.account, service: service, accessGroup: accessGroup)
let existingData = try data(forAccount: storable.account, service: service, accessGroup: accessGroup)
var status = noErr
let newAttributes: [String: Any] = [Constants.valueData: newData, Constants.accessible: storable.accessible.rawValue]
if existingData != nil {
try data(forAccount: storable.account, service: service, accessGroup: accessGroup)
status = securityItemManager.update(withQuery: query, attributesToUpdate: newAttributes)
} else {
query.merge(newAttributes) { $1 }
Expand Down Expand Up @@ -148,15 +147,8 @@ public final class Keychain {
return query
}

func query(for storable: KeychainStorable, service: String, accessGroup: String?) -> [String: Any] {
var query = self.query(forAccount: storable.account, service: service, accessGroup: accessGroup)
query[Constants.accessible] = storable.accessible.rawValue
return query
}

// MARK: - Data

@discardableResult
func data(forAccount account: String, service: String, accessGroup: String?) throws -> Data? {
var query = self.query(forAccount: account, service: service, accessGroup: accessGroup)
query[Constants.matchLimit] = Constants.matchLimitOne
Expand Down
5 changes: 5 additions & 0 deletions Tests/KeychainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ final class MockSecurityItemManager: SecurityItemManaging {
}

struct Credential: KeychainStorable {
static var accessible: Keychain.AccessibleOption = .whenUnlocked
var accessible: Keychain.AccessibleOption { Self.accessible }

let email: String
let password: String
let pin: Int
Expand Down Expand Up @@ -109,6 +112,7 @@ class KeychainTests: XCTestCase {
}

func cleanup() {
Credential.accessible = .whenUnlocked
Keychain.resetDefaults()
do {
let credentials = [credential, credentialTwo]
Expand Down Expand Up @@ -168,6 +172,7 @@ class KeychainTests: XCTestCase {
XCTAssertEqual(retrievedValue?.password, credential.password)
XCTAssertEqual(retrievedValue?.pin, credential.pin)
XCTAssertEqual(retrievedValue?.dob, credential.dob)
Credential.accessible = .afterFirstUnlock
XCTAssertNoThrow(try keychain.store(updatedCredential))
let updatedValue: Credential? = try! keychain.retrieveValue(forAccount: Email.test)
XCTAssertNotNil(updatedValue)
Expand Down