Skip to content

Commit 5680858

Browse files
committed
Improves type-checking for SecItemQuery
1 parent 1830586 commit 5680858

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

Sources/SwiftSecurity/Keychain/SecItemStore/SecItemQuery.swift

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import Foundation
99
import Security
1010

11+
/// A structure that defines an item query.
1112
public struct SecItemQuery<Value> where Value: SecItem {
1213
private(set) var rawValue: [String: Any]
1314

@@ -49,7 +50,10 @@ public extension SecItemQuery {
4950
/// - synchronizable: A value indicating whether the item synchronizes through iCloud.
5051
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
5152
/// - Returns: ``SecItemQuery<GenericPassword>``.
52-
static func credential(for service: String, synchronizable: Bool? = nil) -> SecItemQuery<GenericPassword> {
53+
static func credential(
54+
for service: String,
55+
synchronizable: Bool? = nil
56+
) -> SecItemQuery<GenericPassword> where Value == GenericPassword {
5357
var query = SecItemQuery<GenericPassword>()
5458
query.service = service
5559
if let synchronizable {
@@ -66,7 +70,11 @@ public extension SecItemQuery {
6670
/// - synchronizable: A boolean value indicating whether the item synchronizes through iCloud.
6771
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
6872
/// - Returns: ``SecItemQuery<InternetPassword>``.
69-
static func credential(for user: String, space: WebProtectionSpace, synchronizable: Bool? = nil) -> SecItemQuery<InternetPassword> {
73+
static func credential(
74+
for user: String,
75+
space: WebProtectionSpace,
76+
synchronizable: Bool? = nil
77+
) -> SecItemQuery<InternetPassword> where Value == InternetPassword {
7078
var query = SecItemQuery<InternetPassword>()
7179
query.account = user
7280
query.server = space.host
@@ -89,7 +97,11 @@ public extension SecItemQuery {
8997
/// - synchronizable: A boolean value indicating whether the item synchronizes through iCloud.
9098
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
9199
/// - Returns: ``SecItemQuery<SecKey>``.
92-
static func key(for applicationTag: String? = nil, descriptor: SecKeyDescriptor = .ecPrivateKey, synchronizable: Bool? = nil) -> SecItemQuery<SecKey> {
100+
public static func key(
101+
for applicationTag: String? = nil,
102+
descriptor: SecKeyDescriptor = .ecPrivateKey,
103+
synchronizable: Bool? = nil
104+
) -> SecItemQuery<SecKey> where Value == SecKey {
93105
var query = SecItemQuery<SecKey>()
94106
query.keyClass = descriptor.keyClass
95107
query.keyType = descriptor.keyType
@@ -108,7 +120,10 @@ public extension SecItemQuery {
108120
/// - synchronizable: A boolean value indicating whether the item synchronizes through iCloud.
109121
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
110122
/// - Returns: ``SecItemQuery<SecCertificate>``.
111-
static func certificate(for label: String? = nil, synchronizable: Bool? = nil) -> SecItemQuery<SecCertificate> {
123+
static func certificate(
124+
for label: String? = nil,
125+
synchronizable: Bool? = nil
126+
) -> SecItemQuery<SecCertificate> where Value == SecCertificate {
112127
var query = SecItemQuery<SecCertificate>()
113128
if let label {
114129
query.label = label
@@ -122,7 +137,10 @@ public extension SecItemQuery {
122137
/// - synchronizable: A boolean value indicating whether the item synchronizes through iCloud.
123138
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
124139
/// - Returns: ``SecItemQuery<SecIdentity>``.
125-
static func identity(for label: String? = nil, synchronizable: Bool? = nil) -> SecItemQuery<SecIdentity> {
140+
static func identity(
141+
for label: String? = nil,
142+
synchronizable: Bool? = nil
143+
) -> SecItemQuery<SecIdentity> where Value == SecIdentity {
126144
var query = SecItemQuery<SecIdentity>()
127145
if let label {
128146
query.label = label
@@ -531,19 +549,3 @@ extension SecItemQuery {
531549
set { self[key.rawValue] = newValue }
532550
}
533551
}
534-
535-
// MARK: - Deprecetad
536-
537-
public extension SecItemQuery {
538-
/// A query for a private-key for elliptic curve cryptography (ANSI x9.63).
539-
/// - Note: Suitable for P256, P384, P521 CryptoKit Keys.
540-
/// - Parameters:
541-
/// - applicationTag: An application tag that you can use to identify the key within store.
542-
/// - synchronizable: A boolean value indicating whether the item synchronizes through iCloud.
543-
/// See [Developer Documentation](https://developer.apple.com/documentation/security/ksecattrsynchronizable).
544-
/// - Returns: ``SecItemQuery<SecKey>``.
545-
@available(*, deprecated, renamed: "key(for:synchronizable:)")
546-
static func privateKey(for applicationTag: String? = nil, synchronizable: Bool? = nil) -> SecItemQuery<SecKey> {
547-
return key(for: applicationTag, descriptor: .ecPrivateKey, synchronizable: synchronizable)
548-
}
549-
}

0 commit comments

Comments
 (0)