OZKeychainStorageAdapter
@available(iOS 13.0, macOS 10.15, *)
public final actor OZKeychainStorageAdapter : OZStorageAdapter
Persistent OZStorageAdapter backed by the iOS / macOS Keychain Services API.
Stores credential and session payloads as kSecClassGenericPassword items
using the Security framework’s SecItem* primitives. Each entry is a UTF-8
JSON document keyed by a stable account name:
cred_<credentialId>for individual stored credentials,credential_indexfor the JSON-encoded list of known credential IDs,session_currentfor the active session.
All items are written with kSecAttrAccessibleAfterFirstUnlock so they
survive app restarts but become inaccessible until the device is unlocked
after a reboot.
Thread safety is provided by Swift Concurrency actor isolation, so all
operations serialize even when invoked from multiple tasks.
Example:
let storage = OZKeychainStorageAdapter()
try await storage.save(credential: credential)
let loaded = try await storage.get(credentialId: credential.credentialId)
Important
On iOS Simulator and unsigned macOS test binaries, Keychain access requires thekeychain-access-groups entitlement to be configured.
-
Default Keychain service name used as
kSecAttrServicefor every item.Consumers can override this via the initializer to scope the adapter to a different service identifier — useful when multiple isolated stores must coexist within the same app (for example, separate test scopes or per-tenant scoping).
Declaration
Swift
public static let defaultServiceName: String -
Initializes a new
OZKeychainStorageAdapter.Declaration
Swift
public init(serviceName: String = OZKeychainStorageAdapter.defaultServiceName)Parameters
serviceNameKeychain service identifier (
kSecAttrService). Defaults toOZKeychainStorageAdapter.defaultServiceName. -
save(credential:Asynchronous) Declaration
Swift
public func save(credential: OZStoredCredential) async throws -
get(credentialId:Asynchronous) Declaration
Swift
public func get(credentialId: String) async throws -> OZStoredCredential? -
getByContract(contractId:Asynchronous) Declaration
Swift
public func getByContract(contractId: String) async throws -> [OZStoredCredential] -
getAll()AsynchronousDeclaration
Swift
public func getAll() async throws -> [OZStoredCredential] -
delete(credentialId:Asynchronous) Declaration
Swift
public func delete(credentialId: String) async throws -
update(credentialId:Asynchronousupdates: ) Declaration
Swift
public func update(credentialId: String, updates: OZStoredCredentialUpdate) async throws -
clear()AsynchronousDeclaration
Swift
public func clear() async throws -
saveSession(_:Asynchronous) Declaration
Swift
public func saveSession(_ session: OZStoredSession) async throws -
getSession()AsynchronousDeclaration
Swift
public func getSession() async throws -> OZStoredSession? -
clearSession()AsynchronousDeclaration
Swift
public func clearSession() async throws
View on GitHub
Install in Dash