Skip to content

Commit 1f9952f

Browse files
authored
Allow explicitly set identifiers on all systems (#348)
1 parent cf949b2 commit 1f9952f

File tree

4 files changed

+10
-33
lines changed

4 files changed

+10
-33
lines changed

Sources/Valet/Internal/Service.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ import Foundation
2020
enum Service: CustomStringConvertible, Equatable, Sendable {
2121
case standard(Identifier, Configuration)
2222
case sharedGroup(SharedGroupIdentifier, Identifier?, Configuration)
23-
24-
#if os(macOS)
2523
case standardOverride(service: Identifier, Configuration)
2624
case sharedGroupOverride(service: SharedGroupIdentifier, Configuration)
27-
#endif
2825

2926
// MARK: Equatable
3027

@@ -77,14 +74,12 @@ enum Service: CustomStringConvertible, Equatable, Sendable {
7774
baseQuery[kSecAttrAccessGroup as String] = groupIdentifier.description
7875
configuration = desiredConfiguration
7976

80-
#if os(macOS)
8177
case let .standardOverride(_, desiredConfiguration):
8278
configuration = desiredConfiguration
8379

8480
case let .sharedGroupOverride(identifier, desiredConfiguration):
8581
baseQuery[kSecAttrAccessGroup as String] = identifier.description
8682
configuration = desiredConfiguration
87-
#endif
8883
}
8984

9085
switch configuration {
@@ -113,12 +108,10 @@ enum Service: CustomStringConvertible, Equatable, Sendable {
113108
service = Service.standard(with: configuration, identifier: identifier, accessibilityDescription: configuration.accessibility.description)
114109
case let .sharedGroup(groupIdentifier, identifier, configuration):
115110
service = Service.sharedGroup(with: configuration, groupIdentifier: groupIdentifier, identifier: identifier, accessibilityDescription: configuration.accessibility.description)
116-
#if os(macOS)
117111
case let .standardOverride(identifier, _):
118112
service = identifier.description
119113
case let .sharedGroupOverride(identifier, _):
120114
service = identifier.groupIdentifier
121-
#endif
122115
}
123116

124117
switch self {
@@ -136,11 +129,9 @@ enum Service: CustomStringConvertible, Equatable, Sendable {
136129

137130
return service
138131

139-
#if os(macOS)
140132
case .standardOverride,
141133
.sharedGroupOverride:
142134
return service
143-
#endif
144135
}
145136
}
146137
}

Sources/Valet/SecureEnclave.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,12 @@ public final class SecureEnclave: Sendable {
3131
// To avoid prompting the user for Touch ID or passcode, create a Valet with our identifier and accessibility and ask it if it can access the keychain.
3232
let noPromptValet: Valet
3333
switch service {
34-
#if os(macOS)
3534
case let .standardOverride(identifier, _):
3635
noPromptValet = .valet(with: identifier, accessibility: .whenPasscodeSetThisDeviceOnly)
37-
#endif
3836
case let .standard(identifier, _):
3937
noPromptValet = .valet(with: identifier, accessibility: .whenPasscodeSetThisDeviceOnly)
40-
#if os(macOS)
4138
case let .sharedGroupOverride(identifier, _):
4239
noPromptValet = .sharedGroupValet(withExplicitlySet: identifier, accessibility: .whenPasscodeSetThisDeviceOnly)
43-
#endif
4440
case let .sharedGroup(groupIdentifier, identifier, _):
4541
noPromptValet = .sharedGroupValet(with: groupIdentifier, identifier: identifier, accessibility: .whenPasscodeSetThisDeviceOnly)
4642
}

Sources/Valet/SecureEnclaveValet.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ extension Service {
431431
.standard(identifier, .singlePromptSecureEnclave(accessControl))
432432
case let .sharedGroup(sharedGroupIdentifier, identifier, _):
433433
.sharedGroup(sharedGroupIdentifier, identifier, .singlePromptSecureEnclave(accessControl))
434+
case let .standardOverride(identifier, _):
435+
.standard(identifier, .singlePromptSecureEnclave(accessControl))
436+
case let .sharedGroupOverride(sharedGroupIdentifier, _):
437+
.sharedGroupOverride(service: sharedGroupIdentifier, .singlePromptSecureEnclave(accessControl))
434438
}
435439
}
436440
}

Sources/Valet/Valet.swift

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public final class Valet: NSObject, Sendable {
5858
findOrCreate(groupIdentifier, identifier: identifier, configuration: .iCloud(accessibility))
5959
}
6060

61-
#if os(macOS)
62-
/// Creates a Valet with an explicitly set kSecAttrService.
61+
/// Creates a Valet with an explicitly set kSecAttrService. This API is intended for use with macOS applications where service identifiers can be user-facing.
6362
/// - Parameters:
6463
/// - identifier: A non-empty string that uniquely identifies a Valet. Must be unique relative to other Valet identifiers.
6564
/// - accessibility: The desired accessibility for the Valet.
@@ -70,7 +69,7 @@ public final class Valet: NSObject, Sendable {
7069
findOrCreate(explicitlySet: identifier, configuration: .valet(accessibility))
7170
}
7271

73-
/// Creates an iCloud Valet with an explicitly set kSecAttrService.
72+
/// Creates an iCloud Valet with an explicitly set kSecAttrService. This API is intended for use with macOS applications where service identifiers can be user-facing.
7473
/// - Parameters:
7574
/// - identifier: A non-empty string that uniquely identifies a Valet. Must be unique relative to other Valet identifiers.
7675
/// - accessibility: The desired accessibility for the Valet.
@@ -81,7 +80,7 @@ public final class Valet: NSObject, Sendable {
8180
findOrCreate(explicitlySet: identifier, configuration: .iCloud(accessibility))
8281
}
8382

84-
/// Creates a shared-access-group Valet with an explicitly set kSecAttrService.
83+
/// Creates a shared-access-group Valet with an explicitly set kSecAttrService. This API is intended for use with macOS applications where service identifiers can be user-facing.
8584
/// - Parameters:
8685
/// - identifier: The identifier for the Valet's shared access group. Must correspond with the value for keychain-access-groups in your Entitlements file. Must be unique relative to other Valet identifiers.
8786
/// - accessibility: The desired accessibility for the Valet.
@@ -92,7 +91,7 @@ public final class Valet: NSObject, Sendable {
9291
findOrCreate(explicitlySet: identifier, configuration: .valet(accessibility))
9392
}
9493

95-
/// Creates an iCloud-shared-access-group Valet with an explicitly set kSecAttrService.
94+
/// Creates an iCloud-shared-access-group Valet with an explicitly set kSecAttrService. This API is intended for use with macOS applications where service identifiers can be user-facing.
9695
/// - Parameters:
9796
/// - identifier: The identifier for the Valet's shared access group. Must correspond with the value for keychain-access-groups in your Entitlements file. Must be unique relative to other Valet identifiers.
9897
/// - accessibility: The desired accessibility for the Valet.
@@ -102,8 +101,7 @@ public final class Valet: NSObject, Sendable {
102101
public class func iCloudSharedGroupValet(withExplicitlySet identifier: SharedGroupIdentifier, accessibility: CloudAccessibility) -> Valet {
103102
findOrCreate(explicitlySet: identifier, configuration: .iCloud(accessibility))
104103
}
105-
#endif
106-
104+
107105
// MARK: Equatable
108106

109107
/// - Returns: `true` if lhs and rhs both read from and write to the same sandbox within the keychain.
@@ -144,7 +142,6 @@ public final class Valet: NSObject, Sendable {
144142
}
145143

146144

147-
#if os(macOS)
148145
private class func findOrCreate(explicitlySet identifier: Identifier, configuration: Configuration) -> Valet {
149146
let service: Service = .standardOverride(service: identifier, configuration)
150147
let key = service.description + configuration.description + configuration.accessibility.description + identifier.description
@@ -171,8 +168,7 @@ public final class Valet: NSObject, Sendable {
171168
}
172169
}
173170

174-
#endif
175-
171+
176172
// MARK: Initialization
177173

178174
@available(*, unavailable)
@@ -201,7 +197,6 @@ public final class Valet: NSObject, Sendable {
201197
accessibility = configuration.accessibility
202198
}
203199

204-
#if os(macOS)
205200
private init(overrideIdentifier: Identifier, configuration: Configuration) {
206201
self.identifier = overrideIdentifier
207202
self.configuration = configuration
@@ -215,7 +210,6 @@ public final class Valet: NSObject, Sendable {
215210
service = .sharedGroupOverride(service: identifier, configuration)
216211
accessibility = configuration.accessibility
217212
}
218-
#endif
219213

220214
// MARK: CustomStringConvertible
221215

@@ -482,12 +476,10 @@ public final class Valet: NSObject, Sendable {
482476
serviceAttribute = Service.sharedGroup(with: configuration, groupIdentifier: sharedGroupIdentifier, identifier: identifier, accessibilityDescription: accessibilityDescription)
483477
case .standard:
484478
serviceAttribute = Service.standard(with: configuration, identifier: identifier, accessibilityDescription: accessibilityDescription)
485-
#if os(macOS)
486479
case let .sharedGroupOverride(sharedGroupIdentifier, _):
487480
serviceAttribute = sharedGroupIdentifier.description
488481
case .standardOverride:
489482
serviceAttribute = identifier.description
490-
#endif
491483
}
492484
keychainQuery[kSecAttrService as String] = serviceAttribute
493485
try migrateObjects(matching: keychainQuery, removeOnCompletion: removeOnCompletion)
@@ -517,12 +509,10 @@ public final class Valet: NSObject, Sendable {
517509
serviceAttribute = Service.sharedGroup(with: configuration, groupIdentifier: groupIdentifier, identifier: identifier, accessibilityDescription: accessibilityDescription)
518510
case .standard:
519511
serviceAttribute = Service.standard(with: configuration, identifier: identifier, accessibilityDescription: accessibilityDescription)
520-
#if os(macOS)
521512
case .sharedGroupOverride:
522513
serviceAttribute = Service.sharedGroup(with: configuration, explicitlySetIdentifier: identifier, accessibilityDescription: accessibilityDescription)
523514
case .standardOverride:
524515
serviceAttribute = Service.standard(with: configuration, identifier: identifier, accessibilityDescription: accessibilityDescription)
525-
#endif
526516
}
527517
keychainQuery[kSecAttrService as String] = serviceAttribute
528518
try migrateObjects(matching: keychainQuery, removeOnCompletion: removeOnCompletion)
@@ -648,7 +638,6 @@ extension Valet {
648638
return iCloudSharedGroupValet(with: identifier, accessibility: accessibility)
649639
}
650640

651-
#if os(macOS)
652641
/// Creates a Valet with an explicitly set kSecAttrService.
653642
/// - Parameters:
654643
/// - identifier: A non-empty string that uniquely identifies a Valet. Must be unique relative to other Valet identifiers.
@@ -716,7 +705,6 @@ extension Valet {
716705
}
717706
return findOrCreate(explicitlySet: identifier, configuration: .iCloud(accessibility))
718707
}
719-
#endif
720708

721709
// MARK: Public Methods
722710

@@ -809,7 +797,6 @@ extension Valet {
809797
}
810798
}
811799

812-
#if os(macOS)
813800
class func permutations(withExplictlySet identifier: Identifier, shared: Bool = false) -> [Valet] {
814801
Accessibility.allCases.map { accessibility in
815802
.valet(withExplicitlySet: identifier, accessibility: accessibility)
@@ -833,6 +820,5 @@ extension Valet {
833820
.iCloudSharedGroupValet(withExplicitlySet: identifier, accessibility: cloudAccessibility)
834821
}
835822
}
836-
#endif
837823

838824
}

0 commit comments

Comments
 (0)