mirror of
https://github.com/maxgoedjen/secretive.git
synced 2025-01-08 12:37:07 +00:00
Backport mutex
This commit is contained in:
parent
576e625b8f
commit
9b02afb20c
@ -27,18 +27,26 @@ let package = Package(
|
|||||||
.library(
|
.library(
|
||||||
name: "Brief",
|
name: "Brief",
|
||||||
targets: ["Brief"]),
|
targets: ["Brief"]),
|
||||||
|
.library(
|
||||||
|
name: "Backports",
|
||||||
|
targets: ["Backports"]),
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
name: "SecretKit",
|
name: "Backports",
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
|
.target(
|
||||||
|
name: "SecretKit",
|
||||||
|
dependencies: ["Backports"],
|
||||||
|
swiftSettings: swiftSettings
|
||||||
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "SecretKitTests",
|
name: "SecretKitTests",
|
||||||
dependencies: ["SecretKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
|
dependencies: ["Backports", "SecretKit", "SecureEnclaveSecretKit", "SmartCardSecretKit"],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@ -48,12 +56,12 @@ let package = Package(
|
|||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
name: "SmartCardSecretKit",
|
name: "SmartCardSecretKit",
|
||||||
dependencies: ["SecretKit"],
|
dependencies: ["Backports", "SecretKit"],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
name: "SecretAgentKit",
|
name: "SecretAgentKit",
|
||||||
dependencies: ["SecretKit", "SecretAgentKitHeaders"],
|
dependencies: ["Backports", "SecretKit", "SecretAgentKitHeaders"],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
.systemLibrary(
|
.systemLibrary(
|
||||||
@ -65,7 +73,7 @@ let package = Package(
|
|||||||
,
|
,
|
||||||
.target(
|
.target(
|
||||||
name: "Brief",
|
name: "Brief",
|
||||||
dependencies: []
|
dependencies: ["Backports"]
|
||||||
),
|
),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "BriefTests",
|
name: "BriefTests",
|
||||||
|
30
Sources/Packages/Sources/Backports/_Mutex.swift
Normal file
30
Sources/Packages/Sources/Backports/_Mutex.swift
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
#if canImport(Synchronization)
|
||||||
|
import Synchronization
|
||||||
|
public typealias _Mutex = Mutex
|
||||||
|
#else
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
public final class _Mutex<Value: ~Copyable>: @unchecked Sendable {
|
||||||
|
|
||||||
|
private var value: Value
|
||||||
|
private var lock = OSAllocatedUnfairLock()
|
||||||
|
|
||||||
|
public init(_ value: consuming sending Value) {
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public borrowing func withLock<Result, E>(_ body: (inout sending Value) throws(E) -> sending Result) throws(E) -> sending Result where E : Error, Result : ~Copyable {
|
||||||
|
lock.lock()
|
||||||
|
defer {
|
||||||
|
lock.unlock()
|
||||||
|
}
|
||||||
|
return try body(&value)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Observation
|
import Observation
|
||||||
import Synchronization
|
import Synchronization
|
||||||
|
import Backports
|
||||||
|
|
||||||
/// A concrete implementation of ``UpdaterProtocol`` which considers the current release and OS version.
|
/// A concrete implementation of ``UpdaterProtocol`` which considers the current release and OS version.
|
||||||
@Observable public final class Updater: UpdaterProtocol, ObservableObject, Sendable {
|
@Observable public final class Updater: UpdaterProtocol, ObservableObject, Sendable {
|
||||||
@ -8,7 +9,7 @@ import Synchronization
|
|||||||
public var update: Release? {
|
public var update: Release? {
|
||||||
_update.withLock { $0 }
|
_update.withLock { $0 }
|
||||||
}
|
}
|
||||||
private let _update: Mutex<Release?> = .init(nil)
|
private let _update: _Mutex<Release?> = .init(nil)
|
||||||
public let testBuild: Bool
|
public let testBuild: Bool
|
||||||
|
|
||||||
/// The current OS version.
|
/// The current OS version.
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import OSLog
|
import OSLog
|
||||||
import Synchronization
|
import Synchronization
|
||||||
|
import Backports
|
||||||
|
|
||||||
/// Manages storage and lookup for OpenSSH certificates.
|
/// Manages storage and lookup for OpenSSH certificates.
|
||||||
public final class OpenSSHCertificateHandler: Sendable {
|
public final class OpenSSHCertificateHandler: Sendable {
|
||||||
@ -8,7 +9,7 @@ public final class OpenSSHCertificateHandler: Sendable {
|
|||||||
private let publicKeyFileStoreController = PublicKeyFileStoreController(homeDirectory: NSHomeDirectory())
|
private let publicKeyFileStoreController = PublicKeyFileStoreController(homeDirectory: NSHomeDirectory())
|
||||||
private let logger = Logger(subsystem: "com.maxgoedjen.secretive.secretagent", category: "OpenSSHCertificateHandler")
|
private let logger = Logger(subsystem: "com.maxgoedjen.secretive.secretagent", category: "OpenSSHCertificateHandler")
|
||||||
private let writer = OpenSSHKeyWriter()
|
private let writer = OpenSSHKeyWriter()
|
||||||
private let keyBlobsAndNames: Mutex<[AnySecret: (Data, Data)]> = .init([:])
|
private let keyBlobsAndNames: _Mutex<[AnySecret: (Data, Data)]> = .init([:])
|
||||||
|
|
||||||
/// Initializes an OpenSSHCertificateHandler.
|
/// Initializes an OpenSSHCertificateHandler.
|
||||||
public init() {
|
public init() {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Observation
|
import Observation
|
||||||
import Synchronization
|
import Synchronization
|
||||||
|
import Backports
|
||||||
|
|
||||||
/// A "Store Store," which holds a list of type-erased stores.
|
/// A "Store Store," which holds a list of type-erased stores.
|
||||||
@Observable public final class SecretStoreList: Sendable {
|
@Observable public final class SecretStoreList: Sendable {
|
||||||
@ -9,13 +10,13 @@ import Synchronization
|
|||||||
public var stores: [AnySecretStore] {
|
public var stores: [AnySecretStore] {
|
||||||
__stores.withLock { $0 }
|
__stores.withLock { $0 }
|
||||||
}
|
}
|
||||||
private let __stores: Mutex<[AnySecretStore]> = .init([])
|
private let __stores: _Mutex<[AnySecretStore]> = .init([])
|
||||||
|
|
||||||
/// A modifiable store, if one is available.
|
/// A modifiable store, if one is available.
|
||||||
public var modifiableStore: AnySecretStoreModifiable? {
|
public var modifiableStore: AnySecretStoreModifiable? {
|
||||||
__modifiableStore.withLock { $0 }
|
__modifiableStore.withLock { $0 }
|
||||||
}
|
}
|
||||||
private let __modifiableStore: Mutex<AnySecretStoreModifiable?> = .init(nil)
|
private let __modifiableStore: _Mutex<AnySecretStoreModifiable?> = .init(nil)
|
||||||
|
|
||||||
/// Initializes a SecretStoreList.
|
/// Initializes a SecretStoreList.
|
||||||
public init() {
|
public init() {
|
||||||
|
@ -5,6 +5,7 @@ import CryptoKit
|
|||||||
import LocalAuthentication
|
import LocalAuthentication
|
||||||
import SecretKit
|
import SecretKit
|
||||||
import Synchronization
|
import Synchronization
|
||||||
|
import Backports
|
||||||
|
|
||||||
extension SecureEnclave {
|
extension SecureEnclave {
|
||||||
|
|
||||||
@ -19,9 +20,9 @@ extension SecureEnclave {
|
|||||||
public var secrets: [Secret] {
|
public var secrets: [Secret] {
|
||||||
_secrets.withLock { $0 }
|
_secrets.withLock { $0 }
|
||||||
}
|
}
|
||||||
private let _secrets: Mutex<[Secret]> = .init([])
|
private let _secrets: _Mutex<[Secret]> = .init([])
|
||||||
|
|
||||||
private let persistedAuthenticationContexts: Mutex<[Secret: PersistentAuthenticationContext]> = .init([:])
|
private let persistedAuthenticationContexts: _Mutex<[Secret: PersistentAuthenticationContext]> = .init([:])
|
||||||
|
|
||||||
/// Initializes a Store.
|
/// Initializes a Store.
|
||||||
public init() {
|
public init() {
|
||||||
@ -105,7 +106,7 @@ extension SecureEnclave {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func sign(data: Data, with secret: Secret, for provenance: SigningRequestProvenance) throws -> Data {
|
public func sign(data: Data, with secret: Secret, for provenance: SigningRequestProvenance) throws -> Data {
|
||||||
let context: Mutex<LAContext>
|
let context: _Mutex<LAContext>
|
||||||
// if let existing = persistedAuthenticationContexts.withLock({ $0 })[secret], existing.valid {
|
// if let existing = persistedAuthenticationContexts.withLock({ $0 })[secret], existing.valid {
|
||||||
// context = existing.context
|
// context = existing.context
|
||||||
// } else {
|
// } else {
|
||||||
|
@ -5,6 +5,7 @@ import Security
|
|||||||
import CryptoTokenKit
|
import CryptoTokenKit
|
||||||
import LocalAuthentication
|
import LocalAuthentication
|
||||||
import SecretKit
|
import SecretKit
|
||||||
|
import Backports
|
||||||
|
|
||||||
extension SmartCard {
|
extension SmartCard {
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ extension SmartCard {
|
|||||||
/// An implementation of Store backed by a Smart Card.
|
/// An implementation of Store backed by a Smart Card.
|
||||||
@Observable public final class Store: SecretStore {
|
@Observable public final class Store: SecretStore {
|
||||||
|
|
||||||
private let state: Mutex<State> = .init(.init())
|
private let state: _Mutex<State> = .init(.init())
|
||||||
public var isAvailable: Bool {
|
public var isAvailable: Bool {
|
||||||
state.withLock { $0.isAvailable }
|
state.withLock { $0.isAvailable }
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import SecretKit
|
|||||||
import SecretAgentKit
|
import SecretAgentKit
|
||||||
import Brief
|
import Brief
|
||||||
import Synchronization
|
import Synchronization
|
||||||
|
import Backports
|
||||||
|
|
||||||
final class Notifier: Sendable {
|
final class Notifier: Sendable {
|
||||||
|
|
||||||
@ -151,7 +152,7 @@ final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate, Se
|
|||||||
fileprivate var pendingPersistableSecrets: [String: AnySecret] = [:]
|
fileprivate var pendingPersistableSecrets: [String: AnySecret] = [:]
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate let state: Mutex<State> = .init(.init())
|
fileprivate let state: _Mutex<State> = .init(.init())
|
||||||
|
|
||||||
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification?) {
|
||||||
|
|
||||||
|
@ -2,13 +2,14 @@ import Foundation
|
|||||||
import Synchronization
|
import Synchronization
|
||||||
import Observation
|
import Observation
|
||||||
import Brief
|
import Brief
|
||||||
|
import Backports
|
||||||
|
|
||||||
@Observable class PreviewUpdater: UpdaterProtocol {
|
@Observable class PreviewUpdater: UpdaterProtocol {
|
||||||
|
|
||||||
var update: Release? {
|
var update: Release? {
|
||||||
_update.withLock { $0 }
|
_update.withLock { $0 }
|
||||||
}
|
}
|
||||||
let _update: Mutex<Release?> = .init(nil)
|
let _update: _Mutex<Release?> = .init(nil)
|
||||||
|
|
||||||
let testBuild = false
|
let testBuild = false
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user