askrobots / network-computer-ios
Public
iPhone client for network-computer: see and drive a remote Linux/Mac desktop over WebRTC, phone as trackpad and keyboard, desktop on a TV via AirPlay.
Keychain.swift · 1.4 KB · 32 lines
| 1 | import Foundation |
| 2 | import Security |
| 3 | |
| 4 | /// Minimal Keychain wrapper. Passwords and PINs live here, never in UserDefaults or code. |
| 5 | enum Keychain { |
| 6 | private static let service = "com.askrobots.networkcomputer" |
| 7 | |
| 8 | static func set(_ value: String, for key: String) { |
| 9 | let data = Data(value.utf8) |
| 10 | let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, |
| 11 | kSecAttrService as String: service, |
| 12 | kSecAttrAccount as String: key] |
| 13 | SecItemDelete(query as CFDictionary) |
| 14 | if value.isEmpty { return } |
| 15 | var add = query |
| 16 | add[kSecValueData as String] = data |
| 17 | add[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly |
| 18 | SecItemAdd(add as CFDictionary, nil) |
| 19 | } |
| 20 | |
| 21 | static func get(_ key: String) -> String { |
| 22 | let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, |
| 23 | kSecAttrService as String: service, |
| 24 | kSecAttrAccount as String: key, |
| 25 | kSecReturnData as String: true, |
| 26 | kSecMatchLimit as String: kSecMatchLimitOne] |
| 27 | var out: AnyObject? |
| 28 | guard SecItemCopyMatching(query as CFDictionary, &out) == errSecSuccess, |
| 29 | let data = out as? Data else { return "" } |
| 30 | return String(decoding: data, as: UTF8.self) |
| 31 | } |
| 32 | } |