Implement keychain groups

This commit is contained in:
Maurice Parker committed 2019-09-22 16:00:06 -05:00
1 parent 0e12f0192a
commit 20fa5d9e48
8 files changed
+47 -11

No files matched your search

+8
View File
@@ -72,6 +72,14 @@ public final class AccountManager: UnreadCountProvider {
return CombinedRefreshProgress(downloadProgressArray: downloadProgressArray)
}
public convenience init() {
let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as! String
let accountsURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)
let accountsFolder = accountsURL!.appendingPathComponent("Accounts").absoluteString
let accountsFolderPath = accountsFolder.suffix(from: accountsFolder.index(accountsFolder.startIndex, offsetBy: 7))
self.init(accountsFolder: String(accountsFolderPath))
}
public init(accountsFolder: String) {
self.accountsFolder = accountsFolder
@@ -10,6 +10,15 @@ import Foundation
public struct CredentialsManager {
private static var keychainGroup: String? = {
guard let appGroup = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as? String else {
return nil
}
let appIdentifierPrefix = Bundle.main.object(forInfoDictionaryKey: "AppIdentifierPrefix") as! String
let appGroupSuffix = appGroup.suffix(appGroup.count - 6)
return "\(appIdentifierPrefix)\(appGroupSuffix)"
}()
public static func storeCredentials(_ credentials: Credentials, server: String) throws {
var query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
@@ -20,6 +29,10 @@ public struct CredentialsManager {
query[kSecAttrSecurityDomain as String] = credentials.type.rawValue
}
if let securityGroup = keychainGroup {
query[kSecAttrAccessGroup as String] = securityGroup
}
let secretData = credentials.secret.data(using: String.Encoding.utf8)!
let attributes: [String: Any] = [kSecValueData as String: secretData]
let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
@@ -59,6 +72,10 @@ public struct CredentialsManager {
query[kSecAttrSecurityDomain as String] = type.rawValue
}
if let securityGroup = keychainGroup {
query[kSecAttrAccessGroup as String] = securityGroup
}
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
@@ -93,6 +110,10 @@ public struct CredentialsManager {
query[kSecAttrSecurityDomain as String] = type.rawValue
}
if let securityGroup = keychainGroup {
query[kSecAttrAccessGroup as String] = securityGroup
}
let status = SecItemDelete(query as CFDictionary)
guard status == errSecSuccess || status == errSecItemNotFound else {
throw CredentialsError.unhandledError(status: status)