Files
e-party-iOS/yana/Utils/Security/KeychainManager.swift
edwinQQQ f9f3dec53f feat: 更新Podfile和Podfile.lock,移除Alamofire依赖并添加API认证机制文档
- 注释掉Podfile中的Alamofire依赖,更新Podfile.lock以反映更改。
- 在yana/APIs/API-README.md中新增自动认证Header机制的详细文档,描述其工作原理、实现细节及最佳实践。
- 在yana/yanaApp.swift中将print语句替换为debugInfo以增强调试信息的输出。
- 在API相关文件中实现用户认证状态检查和相关header的自动添加逻辑,提升API请求的安全性和用户体验。
- 更新多个文件中的日志输出,确保在DEBUG模式下提供详细的调试信息。
2025-07-11 16:53:46 +08:00

362 lines
11 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
import Security
/// Keychain
///
/// UserDefaults
/// Codable
///
///
/// - iOS Keychain
/// - Codable
/// -
/// - 线
/// - 访
final class KeychainManager {
// MARK: -
static let shared = KeychainManager()
private init() {}
// MARK: -
private let service: String = {
return Bundle.main.bundleIdentifier ?? "com.yana.app"
}()
private let accessGroup: String? = nil // App Group
// MARK: -
enum KeychainError: Error, LocalizedError {
case dataConversionFailed
case encodingFailed(Error)
case decodingFailed(Error)
case keychainOperationFailed(OSStatus)
case itemNotFound
case duplicateItem
case invalidParameters
var errorDescription: String? {
switch self {
case .dataConversionFailed:
return "数据转换失败"
case .encodingFailed(let error):
return "编码失败: \(error.localizedDescription)"
case .decodingFailed(let error):
return "解码失败: \(error.localizedDescription)"
case .keychainOperationFailed(let status):
return "Keychain 操作失败: \(status)"
case .itemNotFound:
return "未找到指定项目"
case .duplicateItem:
return "项目已存在"
case .invalidParameters:
return "无效参数"
}
}
}
// MARK: - 访
enum AccessLevel {
case whenUnlocked // 访
case whenUnlockedThisDeviceOnly // 访
case afterFirstUnlock // 访
case afterFirstUnlockThisDeviceOnly // 访
var attribute: CFString {
switch self {
case .whenUnlocked:
return kSecAttrAccessibleWhenUnlocked
case .whenUnlockedThisDeviceOnly:
return kSecAttrAccessibleWhenUnlockedThisDeviceOnly
case .afterFirstUnlock:
return kSecAttrAccessibleAfterFirstUnlock
case .afterFirstUnlockThisDeviceOnly:
return kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
}
}
}
// MARK: -
/// Codable Keychain
/// - Parameters:
/// - object: Codable
/// - key:
/// - accessLevel: 访访
/// - Throws: KeychainError
func store<T: Codable>(_ object: T, forKey key: String, accessLevel: AccessLevel = .whenUnlockedThisDeviceOnly) throws {
// 1. Data
let data: Data
do {
data = try JSONEncoder().encode(object)
} catch {
throw KeychainError.encodingFailed(error)
}
// 2.
var query = baseQuery(forKey: key)
query[kSecValueData] = data
query[kSecAttrAccessible] = accessLevel.attribute
// 3.
SecItemDelete(query as CFDictionary)
// 4.
let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else {
throw KeychainError.keychainOperationFailed(status)
}
debugInfo("🔐 Keychain 存储成功: \(key)")
}
/// Keychain Codable
/// - Parameters:
/// - type:
/// - key:
/// - Returns: nil
/// - Throws: KeychainError
func retrieve<T: Codable>(_ type: T.Type, forKey key: String) throws -> T? {
// 1.
var query = baseQuery(forKey: key)
query[kSecReturnData] = true
query[kSecMatchLimit] = kSecMatchLimitOne
// 2.
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
// 3.
switch status {
case errSecSuccess:
guard let data = result as? Data else {
throw KeychainError.dataConversionFailed
}
// 4.
do {
let object = try JSONDecoder().decode(type, from: data)
debugInfo("🔐 Keychain 读取成功: \(key)")
return object
} catch {
throw KeychainError.decodingFailed(error)
}
case errSecItemNotFound:
return nil
default:
throw KeychainError.keychainOperationFailed(status)
}
}
/// Keychain
/// - Parameters:
/// - object:
/// - key:
/// - Throws: KeychainError
func update<T: Codable>(_ object: T, forKey key: String) throws {
// 1.
let data: Data
do {
data = try JSONEncoder().encode(object)
} catch {
throw KeychainError.encodingFailed(error)
}
// 2.
let query = baseQuery(forKey: key)
let updateAttributes: [CFString: Any] = [
kSecValueData: data
]
// 3.
let status = SecItemUpdate(query as CFDictionary, updateAttributes as CFDictionary)
switch status {
case errSecSuccess:
debugInfo("🔐 Keychain 更新成功: \(key)")
case errSecItemNotFound:
//
try store(object, forKey: key)
default:
throw KeychainError.keychainOperationFailed(status)
}
}
/// Keychain
/// - Parameter key:
/// - Throws: KeychainError
func delete(forKey key: String) throws {
let query = baseQuery(forKey: key)
let status = SecItemDelete(query as CFDictionary)
switch status {
case errSecSuccess:
debugInfo("🔐 Keychain 删除成功: \(key)")
case errSecItemNotFound:
//
break
default:
throw KeychainError.keychainOperationFailed(status)
}
}
/// Keychain
/// - Parameter key:
/// - Returns:
func exists(forKey key: String) -> Bool {
var query = baseQuery(forKey: key)
query[kSecReturnData] = false
query[kSecMatchLimit] = kSecMatchLimitOne
let status = SecItemCopyMatching(query as CFDictionary, nil)
return status == errSecSuccess
}
/// Keychain
/// - Throws: KeychainError
func clearAll() throws {
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service
]
let status = SecItemDelete(query as CFDictionary)
switch status {
case errSecSuccess, errSecItemNotFound:
debugInfo("🔐 Keychain 清除完成")
default:
throw KeychainError.keychainOperationFailed(status)
}
}
// MARK: -
///
/// - Parameter key:
/// - Returns:
private func baseQuery(forKey key: String) -> [CFString: Any] {
var query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecAttrAccount: key
]
if let accessGroup = accessGroup {
query[kSecAttrAccessGroup] = accessGroup
}
return query
}
}
// MARK: - 便
extension KeychainManager {
/// Keychain
/// - Parameters:
/// - string:
/// - key:
/// - accessLevel: 访
/// - Throws: KeychainError
func storeString(_ string: String, forKey key: String, accessLevel: AccessLevel = .whenUnlockedThisDeviceOnly) throws {
try store(string, forKey: key, accessLevel: accessLevel)
}
/// Keychain
/// - Parameter key:
/// - Returns:
/// - Throws: KeychainError
func retrieveString(forKey key: String) throws -> String? {
return try retrieve(String.self, forKey: key)
}
/// Keychain
/// - Parameters:
/// - data:
/// - key:
/// - accessLevel: 访
/// - Throws: KeychainError
func storeData(_ data: Data, forKey key: String, accessLevel: AccessLevel = .whenUnlockedThisDeviceOnly) throws {
var query = baseQuery(forKey: key)
query[kSecValueData] = data
query[kSecAttrAccessible] = accessLevel.attribute
SecItemDelete(query as CFDictionary)
let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else {
throw KeychainError.keychainOperationFailed(status)
}
}
/// Keychain
/// - Parameter key:
/// - Returns:
/// - Throws: KeychainError
func retrieveData(forKey key: String) throws -> Data? {
var query = baseQuery(forKey: key)
query[kSecReturnData] = true
query[kSecMatchLimit] = kSecMatchLimitOne
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
switch status {
case errSecSuccess:
return result as? Data
case errSecItemNotFound:
return nil
default:
throw KeychainError.keychainOperationFailed(status)
}
}
}
// MARK: -
#if DEBUG
extension KeychainManager {
///
/// - Returns:
func debugListAllKeys() -> [String] {
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecReturnAttributes: true,
kSecMatchLimit: kSecMatchLimitAll
]
var result: AnyObject?
let status = SecItemCopyMatching(query as CFDictionary, &result)
guard status == errSecSuccess,
let items = result as? [[CFString: Any]] else {
return []
}
return items.compactMap { item in
item[kSecAttrAccount] as? String
}
}
///
func debugPrintAllKeys() {
let keys = debugListAllKeys()
debugInfo("🔐 Keychain 中存储的键:")
for key in keys {
debugInfo(" - \(key)")
}
}
}
#endif