subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

Keychain.swift (1234B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 import Security
      4 
      5 /// Cloud password, kept in the device keychain.
      6 enum Keychain {
      7     private static func query(_ account: String) -> [String: Any] {
      8         [kSecClass as String: kSecClassGenericPassword,
      9          kSecAttrService as String: Brand.keychainService,
     10          kSecAttrAccount as String: account]
     11     }
     12 
     13     static func save(password: String, account: String) {
     14         SecItemDelete(query(account) as CFDictionary)
     15         var item = query(account)
     16         item[kSecValueData as String] = Data(password.utf8)
     17         item[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
     18         SecItemAdd(item as CFDictionary, nil)
     19     }
     20 
     21     static func password(account: String) -> String? {
     22         var item = query(account)
     23         item[kSecReturnData as String] = true
     24         item[kSecMatchLimit as String] = kSecMatchLimitOne
     25         var result: AnyObject?
     26         guard SecItemCopyMatching(item as CFDictionary, &result) == errSecSuccess, let data = result as? Data else { return nil }
     27         return String(decoding: data, as: UTF8.self)
     28     }
     29 
     30     static func delete(account: String) {
     31         SecItemDelete(query(account) as CFDictionary)
     32     }
     33 }