Guest User

Untitled

a guest
Apr 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. class PasswordManager {
  2. static let shared = PasswordManager() // singleton instance
  3.  
  4. private lazy var protectionSpace: URLProtectionSpace = {
  5. return URLProtectionSpace(host: "somehost.com",
  6. port: 0,
  7. protocol: "http",
  8. realm: nil,
  9. authenticationMethod: nil)
  10. }()
  11.  
  12. private init() { }
  13.  
  14. func password(for userID: String) -> String? {
  15. guard let credentials = URLCredentialStorage.shared.credentials(for: protectionSpace) else { return nil }
  16. return credentials[userID]?.password
  17. }
  18.  
  19. func set(password: String, for userID: String) {
  20. let credential = URLCredential(user: userID, password: password, persistence: .permanent)
  21. URLCredentialStorage.shared.set(credential, for: protectionSpace)
  22. }
  23.  
  24. func clear(for userID: String) {
  25. if let password = password(for: userID) {
  26. let credential = URLCredential(user: userID, password: password, persistence: .permanent)
  27. URLCredentialStorage.shared.remove(credential, for: protectionSpace)
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment