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 keychain sharing support #184

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions Sources/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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,
]
}
}
Expand Down