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
57 changes: 0 additions & 57 deletions Sources/Keychain/Keychain+Sharing.swift

This file was deleted.

2 changes: 2 additions & 0 deletions Sources/Keychain/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public protocol Keychain: Sendable {

func delete(key: String) throws

func allKeys() throws -> Set<String>

}
22 changes: 18 additions & 4 deletions Sources/Keychain/Keychains/InMemoryKeychain.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import Dependencies
import Foundation
import os
import Valet

public final class InMemoryKeychain {

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

init() {}

private let encoder = JSONEncoder()
private let decoder = JSONDecoder()
@usableFromInline
let encoder = JSONEncoder()

@usableFromInline
let decoder = JSONDecoder()

}

// MARK: Keychain

extension InMemoryKeychain: Keychain {

@inlinable
public func load<T>(key: String) throws -> T where T: Decodable {
return try decoder.decode(T.self, from: InMemoryKeychain.data.withLock { $0[key] } ?? Data())
guard let data = InMemoryKeychain.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 }
Expand All @@ -30,4 +40,8 @@ extension InMemoryKeychain: Keychain {
_ = InMemoryKeychain.data.withLock { $0.removeValue(forKey: key) }
}

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

}
4 changes: 4 additions & 0 deletions Sources/Keychain/Keychains/ValetKeychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ extension ValetKeychain: Keychain {
try valet().removeObject(forKey: key)
}

func allKeys() throws -> Set<String> {
try valet().allKeys()
}

}

extension KeychainConfiguration.Accessibility {
Expand Down
4 changes: 2 additions & 2 deletions Tests/KeychainTests/KeychainTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@testable import Keychain
import Dependencies
@testable import Keychain
import XCTest

class KeychainTests: XCTestCase {
Expand All @@ -13,7 +13,7 @@ class KeychainTests: XCTestCase {
try keychain.save(key: "key", value: "test")
XCTAssertEqual(try keychain.load(key: "key"), "test")
try keychain.delete(key: "key")
XCTAssertEqual(try? keychain.load(key: "key"), Optional<String>.none)
XCTAssertThrowsError(try { let _: String = try keychain.load(key: "key") }())
}
}

Expand Down