xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Utilities / SigningInfo.swift
SigningInfo.swift88 lines · 2.6 KiBRaw
| 1 | // |
| 2 | // SigningInfo.swift |
| 3 | // XKit |
| 4 | // |
| 5 | // Created by Kabir Oberai on 12/10/19. |
| 6 | // Copyright © 2019 Kabir Oberai. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import Foundation |
| 10 | import ConcurrencyExtras |
| 11 | import Dependencies |
| 12 | |
| 13 | public struct SigningInfo: Codable, Sendable { |
| 14 | public let privateKey: PrivateKey |
| 15 | public let certificate: Certificate |
| 16 | } |
| 17 | |
| 18 | public protocol SigningInfoManager: Sendable { |
| 19 | func info(forIdentityID identityID: String) throws -> SigningInfo? |
| 20 | func setInfo(_ info: SigningInfo?, forIdentityID identityID: String) throws |
| 21 | } |
| 22 | |
| 23 | public enum SigningInfoManagerDependencyKey: DependencyKey { |
| 24 | public static let testValue: SigningInfoManager = UnimplementedSigningInfoManager() |
| 25 | public static let liveValue: SigningInfoManager = KeyValueSigningInfoManager() |
| 26 | } |
| 27 | |
| 28 | extension DependencyValues { |
| 29 | public var signingInfoManager: SigningInfoManager { |
| 30 | get { self[SigningInfoManagerDependencyKey.self] } |
| 31 | set { self[SigningInfoManagerDependencyKey.self] = newValue } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | extension SigningInfoManager { |
| 36 | subscript(identityID: String) -> SigningInfo? { |
| 37 | get { |
| 38 | try? info(forIdentityID: identityID) |
| 39 | } |
| 40 | nonmutating set { |
| 41 | try? setInfo(newValue, forIdentityID: identityID) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private struct UnimplementedSigningInfoManager: SigningInfoManager { |
| 47 | func info(forIdentityID identityID: String) throws -> SigningInfo? { |
| 48 | unimplemented(placeholder: nil) |
| 49 | } |
| 50 | |
| 51 | func setInfo(_ info: SigningInfo?, forIdentityID identityID: String) throws { |
| 52 | unimplemented() |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public final class MemoryBackedSigningInfoManager: SigningInfoManager { |
| 57 | private let infos = LockIsolated<[String: SigningInfo]>([:]) |
| 58 | |
| 59 | public init() {} |
| 60 | |
| 61 | public func info(forIdentityID identityID: String) throws -> SigningInfo? { |
| 62 | infos[identityID] |
| 63 | } |
| 64 | |
| 65 | public func setInfo(_ info: SigningInfo?, forIdentityID identityID: String) throws { |
| 66 | infos.withValue { |
| 67 | $0[identityID] = info |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public struct KeyValueSigningInfoManager: SigningInfoManager { |
| 73 | private let encoder = PropertyListEncoder() |
| 74 | private let decoder = PropertyListDecoder() |
| 75 | |
| 76 | @Dependency(\.keyValueStorage) var storage |
| 77 | |
| 78 | public func info(forIdentityID identityID: String) throws -> SigningInfo? { |
| 79 | guard let data = try storage.data(forKey: identityID) |
| 80 | else { return nil } |
| 81 | return try decoder.decode(SigningInfo.self, from: data) |
| 82 | } |
| 83 | |
| 84 | public func setInfo(_ info: SigningInfo?, forIdentityID identityID: String) throws { |
| 85 | let data = try encoder.encode(info) |
| 86 | try storage.setData(data, forKey: identityID) |
| 87 | } |
| 88 | } |