diff --git a/Sources/Keychain.swift b/Sources/Keychain.swift index ff1637b8..9a64515f 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, ] } }