From a6afb57f25c1f6bab69418c97d5c802b9e92c853 Mon Sep 17 00:00:00 2001 From: Andrew Reed Date: Mon, 13 Aug 2018 10:57:42 +0100 Subject: [PATCH 1/2] added keychain group support --- Sources/Keychain.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/Keychain.swift b/Sources/Keychain.swift index ff1637b8..c0085bc2 100644 --- a/Sources/Keychain.swift +++ b/Sources/Keychain.swift @@ -63,8 +63,8 @@ public final class Keychain { /// /// - throws: A `Keychain.Error` if the token was not added successfully. /// - returns: The new persistent token. - public func add(_ token: Token) throws -> PersistentToken { - let attributes = try token.keychainAttributes() + public func add(_ token: Token, keychainGroupName: String = "") throws -> PersistentToken { + let attributes = try token.keychainAttributes(keychainGroupName: keychainGroupName) let persistentRef = try addKeychainItem(withAttributes: attributes) return PersistentToken(token: token, identifier: persistentRef) } @@ -76,8 +76,8 @@ public final class Keychain { /// /// - throws: A `Keychain.Error` if the update did not succeed. /// - returns: The updated persistent token. - public func update(_ persistentToken: PersistentToken, with token: Token) throws -> PersistentToken { - let attributes = try token.keychainAttributes() + public func update(_ persistentToken: PersistentToken, with token: Token, keychainGroupName: String = "") throws -> PersistentToken { + let attributes = try token.keychainAttributes(keychainGroupName: keychainGroupName) try updateKeychainItem(forPersistentRef: persistentToken.identifier, withAttributes: attributes) return PersistentToken(token: token, identifier: persistentToken.identifier) @@ -114,7 +114,7 @@ private let kOTPService = "me.mattrubin.onetimepassword.token" private let urlStringEncoding = String.Encoding.utf8 private extension Token { - func keychainAttributes() throws -> [String: AnyObject] { + func keychainAttributes(keychainGroupName: String) throws -> [String: AnyObject] { let url = try self.toURL() guard let data = url.absoluteString.data(using: urlStringEncoding) else { throw Keychain.Error.tokenSerializationFailure @@ -123,6 +123,7 @@ private extension Token { kSecAttrGeneric as String: data as NSData, kSecValueData as String: generator.secret as NSData, kSecAttrService as String: kOTPService as NSString, + kSecAttrAccessGroup as String: keychainGroupName as AnyObject ] } } From a326c3cb7ea4007834f564d33a15b781293afb15 Mon Sep 17 00:00:00 2001 From: Andrew Reed Date: Mon, 13 Aug 2018 11:01:45 +0100 Subject: [PATCH 2/2] added comma for lint --- Sources/Keychain.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Keychain.swift b/Sources/Keychain.swift index c0085bc2..9a64515f 100644 --- a/Sources/Keychain.swift +++ b/Sources/Keychain.swift @@ -123,7 +123,7 @@ private extension Token { kSecAttrGeneric as String: data as NSData, kSecValueData as String: generator.secret as NSData, kSecAttrService as String: kOTPService as NSString, - kSecAttrAccessGroup as String: keychainGroupName as AnyObject + kSecAttrAccessGroup as String: keychainGroupName as AnyObject, ] } }