Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Sources/Keychain/Keychain+Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ extension DependencyValues {
}

public enum KeychainDependencyKey: DependencyKey {
public static let liveValue: any Keychain = ValetKeychain()
}

extension KeychainDependencyKey: TestDependencyKey {
public static let testValue: any Keychain = InMemoryKeychain()
public static var liveValue: any Keychain { ValetKeychain() }
public static var testValue: any Keychain { InMemoryKeychain() }
}
2 changes: 0 additions & 2 deletions Sources/Keychain/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import Foundation

public protocol Keychain: Sendable {

@inlinable
func load<T>(key: String) throws -> T where T: Decodable

@inlinable
func save<T>(key: String, value: T) throws where T: Encodable

func delete(key: String) throws
Expand Down
1 change: 0 additions & 1 deletion Sources/Keychain/KeychainConfiguration+Dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Dependencies
import Foundation
import XCTestDynamicOverlay

extension DependencyValues {

Expand Down
18 changes: 6 additions & 12 deletions Sources/Keychain/Keychains/InMemoryKeychain.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import Dependencies
import Foundation
import os
import Valet

public final class InMemoryKeychain {

@usableFromInline
static let data = OSAllocatedUnfairLock(initialState: [String: Data]())
let data = OSAllocatedUnfairLock(initialState: [String: Data]())

init() {}

@usableFromInline
let encoder = JSONEncoder()

@usableFromInline
let decoder = JSONDecoder()

}
Expand All @@ -22,26 +18,24 @@ public final class InMemoryKeychain {

extension InMemoryKeychain: Keychain {

@inlinable
public func load<T>(key: String) throws -> T where T: Decodable {
guard let data = InMemoryKeychain.data.withLock({ $0[key] }) else {
guard let data = data.withLock({ $0[key] }) else {
throw KeychainError.itemNotFound
}
return try decoder.decode(T.self, from: data)
}

@inlinable
public func save<T>(key: String, value: T) throws where T: Encodable {
let data = try encoder.encode(value)
InMemoryKeychain.data.withLock { $0[key] = data }
let encoded = try encoder.encode(value)
data.withLock { $0[key] = encoded }
}

public func delete(key: String) throws {
_ = InMemoryKeychain.data.withLock { $0.removeValue(forKey: key) }
_ = data.withLock { $0.removeValue(forKey: key) }
}

public func allKeys() throws -> Set<String> {
InMemoryKeychain.data.withLock { Set($0.keys) }
data.withLock { Set($0.keys) }
}

}
2 changes: 0 additions & 2 deletions Sources/Keychain/Keychains/ValetKeychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ struct ValetKeychain {

extension ValetKeychain: Keychain {

@inlinable
func load<T>(key: String) throws -> T where T: Decodable {
let data = try valet().object(forKey: key)
return try decoder.decode(T.self, from: data)
}

@inlinable
func save<T>(key: String, value: T) throws where T: Encodable {
let data = try encoder.encode(value)
try valet().setObject(data, forKey: key)
Expand Down
Loading