|
| 1 | +// |
| 2 | +// KeychainStorage.swift |
| 3 | +// Core |
| 4 | +// |
| 5 | +// Created by NDGL on 2026-02-06. |
| 6 | +// Copyright © 2026 NDGL-iOS. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import Security |
| 11 | + |
| 12 | +public protocol KeychainStorageProtocol { |
| 13 | + func save(_ value: String, forKey key: String) -> Bool |
| 14 | + func load(forKey key: String) -> String? |
| 15 | + func delete(forKey key: String) -> Bool |
| 16 | + func clear() -> Bool |
| 17 | +} |
| 18 | + |
| 19 | +public final class KeychainStorage: KeychainStorageProtocol { |
| 20 | + |
| 21 | + private let service: String |
| 22 | + |
| 23 | + public init(service: String = Bundle.main.bundleIdentifier ?? "com.ndgl.app") { |
| 24 | + self.service = service |
| 25 | + } |
| 26 | + |
| 27 | + @discardableResult |
| 28 | + public func save(_ value: String, forKey key: String) -> Bool { |
| 29 | + guard let data = value.data(using: .utf8) else { return false } |
| 30 | + |
| 31 | + // 기존 항목 삭제 |
| 32 | + delete(forKey: key) |
| 33 | + |
| 34 | + let query: [String: Any] = [ |
| 35 | + kSecClass as String: kSecClassGenericPassword, |
| 36 | + kSecAttrService as String: service, |
| 37 | + kSecAttrAccount as String: key, |
| 38 | + kSecValueData as String: data, |
| 39 | + kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly |
| 40 | + ] |
| 41 | + |
| 42 | + let status = SecItemAdd(query as CFDictionary, nil) |
| 43 | + return status == errSecSuccess |
| 44 | + } |
| 45 | + |
| 46 | + public func load(forKey key: String) -> String? { |
| 47 | + let query: [String: Any] = [ |
| 48 | + kSecClass as String: kSecClassGenericPassword, |
| 49 | + kSecAttrService as String: service, |
| 50 | + kSecAttrAccount as String: key, |
| 51 | + kSecReturnData as String: true, |
| 52 | + kSecMatchLimit as String: kSecMatchLimitOne |
| 53 | + ] |
| 54 | + |
| 55 | + var result: AnyObject? |
| 56 | + let status = SecItemCopyMatching(query as CFDictionary, &result) |
| 57 | + |
| 58 | + guard status == errSecSuccess, |
| 59 | + let data = result as? Data, |
| 60 | + let value = String(data: data, encoding: .utf8) else { |
| 61 | + return nil |
| 62 | + } |
| 63 | + |
| 64 | + return value |
| 65 | + } |
| 66 | + |
| 67 | + @discardableResult |
| 68 | + public func delete(forKey key: String) -> Bool { |
| 69 | + let query: [String: Any] = [ |
| 70 | + kSecClass as String: kSecClassGenericPassword, |
| 71 | + kSecAttrService as String: service, |
| 72 | + kSecAttrAccount as String: key |
| 73 | + ] |
| 74 | + |
| 75 | + let status = SecItemDelete(query as CFDictionary) |
| 76 | + return status == errSecSuccess || status == errSecItemNotFound |
| 77 | + } |
| 78 | + |
| 79 | + @discardableResult |
| 80 | + public func clear() -> Bool { |
| 81 | + let query: [String: Any] = [ |
| 82 | + kSecClass as String: kSecClassGenericPassword, |
| 83 | + kSecAttrService as String: service |
| 84 | + ] |
| 85 | + |
| 86 | + let status = SecItemDelete(query as CFDictionary) |
| 87 | + return status == errSecSuccess || status == errSecItemNotFound |
| 88 | + } |
| 89 | +} |
0 commit comments