Skip to content

Instantly share code, notes, and snippets.

@cjwirth
Forked from erica/credential.swift
Created December 21, 2015 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjwirth/104264ad9318284c0c78 to your computer and use it in GitHub Desktop.
Save cjwirth/104264ad9318284c0c78 to your computer and use it in GitHub Desktop.
import Foundation
public class CredentialHelper {
// MARK: Init
private var host: String
private var protectionSpace: NSURLProtectionSpace
public init(host: String) {
self.host = host
protectionSpace = NSURLProtectionSpace(host: host, port: 0, `protocol`: "http", realm: nil, authenticationMethod: nil)
}
// MARK: Store credentials
private func storeCredential(key: String, value: String) {
let credential = NSURLCredential(user:key, password: value, persistence: .Permanent)
NSURLCredentialStorage.sharedCredentialStorage().setCredential(credential, forProtectionSpace: protectionSpace)
}
private func storeDefaultCredential(key: String, value: String) {
let credential = NSURLCredential(user:key, password: value, persistence: .Permanent)
NSURLCredentialStorage.sharedCredentialStorage().setDefaultCredential(credential, forProtectionSpace: protectionSpace)
}
// MARK: Retrieve credentials
private var credentials: [NSObject:AnyObject] {
if let dict = NSURLCredentialStorage.sharedCredentialStorage().credentialsForProtectionSpace(protectionSpace) {
return dict
}
return [NSObject:AnyObject]()
}
public var credentialCount: Int {
return credentials.count
}
private func credentialForKey(key: String) -> NSURLCredential? {
if let credential: NSURLCredential = credentials[key] as? NSURLCredential {
return credential
}
return nil
}
private func valueForKey(key: String) -> String? {
if let credential = credentialForKey(key) {
return credential.password
}
return nil
}
// MARK: Edit credentials
public func removeCredential(key: String) {
if let credential: NSURLCredential = credentials[key] as? NSURLCredential {
NSURLCredentialStorage.sharedCredentialStorage().removeCredential(credential, forProtectionSpace: protectionSpace)
}
}
public func removeAllCredentials() {
for key in credentials.keys {
if let key = key as? String {
removeCredential(key)
}
}
}
// MARK: Default credentials
private var defaultCredential: NSURLCredential? {
return NSURLCredentialStorage.sharedCredentialStorage().defaultCredentialForProtectionSpace(protectionSpace)
}
private func setDefaultCredential(key: String) {
if let value = self[key] {
let credential = NSURLCredential(user:key, password: value, persistence: .Permanent)
NSURLCredentialStorage.sharedCredentialStorage().setDefaultCredential(credential, forProtectionSpace: protectionSpace)
} else {
print("Credential not found. Default credential is unchanged")
}
}
public var defaultKey: NSString? {
get {return defaultCredential?.user}
set (newValue) {
if let newValue = newValue {
setDefaultCredential(newValue as String)
} else {
print("Nil key. Default credential is unchanged")
}
}
}
// I strongly considered letting assignment to nil remove the entire credential here
public var defaultValue: NSString? {
get {return defaultCredential?.password}
set (newValue) {
if let newValue = newValue {
if let defaultKey = defaultKey {
storeCredential(defaultKey as String, value: newValue as String)
}
} else {
print("Nil value. Default credential is unchanged")
}
}
}
// MARK: Dictionary access -- preferred entry point for assignment and retrieval
public subscript (key: String) -> String? {
get {return valueForKey(key)}
set(newValue) {
if let newValue = newValue {
storeCredential(key, value: newValue)
} else {
removeCredential(key)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment