SHARE
TWEET

Untitled

a guest Sep 4th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  KeychainUtil.swift
  3. //
  4. //  Created by Rodrigo Busata on 10/07/18.
  5. //
  6.  
  7. import Foundation
  8.  
  9. struct KeychainUtil {
  10.    
  11.     private static let server = Bundle.main.bundleIdentifier!
  12.    
  13.    
  14.     static func saveCredentials(username: String, password: String) {
  15.         let password = password.data(using: String.Encoding.utf8)!
  16.        
  17.         if self.getCredentials(byUsernamre: username) == nil {
  18.             let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
  19.                                         kSecAttrServer as String: self.server,
  20.                                         kSecAttrAccount as String: username,
  21.                                         kSecValueData as String: password]
  22.            
  23.             let status = SecItemAdd(query as CFDictionary, nil)
  24.             if status != errSecSuccess {
  25.                 print("Failed on saveCredentials() INSERT")
  26.             }
  27.            
  28.         } else {
  29.             let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
  30.                                         kSecAttrServer as String: self.server,
  31.                                         kSecAttrAccount as String: username]
  32.            
  33.             let attributes: [String: Any] = [kSecValueData as String: password]
  34.            
  35.             let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
  36.             if status != errSecSuccess {
  37.                 print("Failed on saveCredentials() UPDATE")
  38.             }
  39.         }
  40.     }
  41.    
  42.    
  43.     static func getCredentials(byUsernamre username: String) -> String? {
  44.         let query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
  45.                                     kSecAttrServer as String: self.server,
  46.                                     kSecAttrAccount as String: username,
  47.                                     kSecMatchLimit as String: kSecMatchLimitOne,
  48.                                     kSecReturnAttributes as String: true,
  49.                                     kSecReturnData as String: true]
  50.        
  51.         var item: CFTypeRef?
  52.         let status = SecItemCopyMatching(query as CFDictionary, &item)
  53.         if status == errSecItemNotFound {
  54.             return nil
  55.         }
  56.        
  57.         if status != errSecSuccess {
  58.             print("Failed on getCredentials()")
  59.             return nil
  60.         }
  61.        
  62.         guard let existingItem = item as? [String : Any],
  63.             let passwordData = existingItem[kSecValueData as String] as? Data,
  64.             let password = String(data: passwordData, encoding: String.Encoding.utf8)
  65.             else {
  66.                 return nil
  67.         }
  68.        
  69.         return password
  70.     }
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top