Moved keychain credentials out of Resolver

This commit is contained in:
Mikhail Churbanov 2018-07-24 22:25:59 +03:00
parent 6bf8011e12
commit fb4e8171a9
1 changed files with 25 additions and 23 deletions

View File

@ -21,13 +21,37 @@ public struct Credentials {
public let password: String
}
extension Credentials {
public static func get(for host: String) -> Credentials? {
let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
kSecAttrServer as String: host,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnAttributes as String: true,
kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
guard status == errSecSuccess else { return nil }
guard let existingItem = item as? [String : Any],
let passwordData = existingItem[kSecValueData as String] as? Data,
let password = String(data: passwordData, encoding: .utf8),
let account = existingItem[kSecAttrAccount as String] as? String
else {
return nil
}
return Credentials(username: account, password: password)
}
}
public struct Proxy {
public let type: ProxyType
public let host: String
public let port: UInt32
public var credentials: Credentials? {
return ProxyResolver.getCredentials(for: host)
return Credentials.get(for: host)
}
}
@ -284,26 +308,4 @@ public final class ProxyResolver {
return urlComponents.url
}
class func getCredentials(for host: String) -> Credentials? {
let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
kSecAttrServer as String: host,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnAttributes as String: true,
kSecReturnData as String: true]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
guard status == errSecSuccess else { return nil }
guard let existingItem = item as? [String : Any],
let passwordData = existingItem[kSecValueData as String] as? Data,
let password = String(data: passwordData, encoding: .utf8),
let account = existingItem[kSecAttrAccount as String] as? String
else {
return nil
}
return Credentials(username: account, password: password)
}
}