Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeychainAttribute: Return correct names of keychain attributes #238

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions GTMAppAuth/Sources/KeychainStore/KeychainAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class KeychainAttribute: NSObject {
/// Indicates whether to treat macOS keychain items like iOS keychain items.
///
/// This attribute will set `kSecUseDataProtectionKeychain` as true in the Keychain query.
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
case useDataProtectionKeychain
/// The `String` name for the access group to use in the Keychain query.
case accessGroup(String)
Expand All @@ -32,9 +33,13 @@ public final class KeychainAttribute: NSObject {
public var keyName: String {
switch self {
case .useDataProtectionKeychain:
return "kSecUseDataProtectionKeychain"
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) {
return kSecUseDataProtectionKeychain as String
}

fatalError(".useDataProtectionKeychain is not available")
case .accessGroup:
return "kSecKeychainAccessGroup"
return kSecAttrAccessGroup as String
}
}
}
Expand All @@ -53,7 +58,7 @@ public final class KeychainAttribute: NSObject {
/// Creates an instance of `KeychainAttribute` whose attribute is set to
/// `.useDataProtectionKeychain`.
/// - Returns: An instance of `KeychainAttribute`.
@available(macOS 10.15, *)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@objc public static let useDataProtectionKeychain = KeychainAttribute(
attribute: .useDataProtectionKeychain
)
Expand Down
9 changes: 0 additions & 9 deletions GTMAppAuth/Sources/KeychainStore/KeychainHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public protocol KeychainHelper {
final class KeychainWrapper: KeychainHelper {
let accountName = "OAuth"
let keychainAttributes: Set<KeychainAttribute>
@available(macOS 10.15, *)
private var isMaxMacOSVersionGreaterThanTenOneFive: Bool {
let tenOneFive = OperatingSystemVersion(majorVersion: 10, minorVersion: 15, patchVersion: 0)
return ProcessInfo().isOperatingSystemAtLeast(tenOneFive)
}

init(keychainAttributes: Set<KeychainAttribute> = []) {
self.keychainAttributes = keychainAttributes
Expand All @@ -54,11 +49,7 @@ final class KeychainWrapper: KeychainHelper {
keychainAttributes.forEach { configuration in
switch configuration.attribute {
case .useDataProtectionKeychain:
#if os(macOS) && isMaxMacOSVersionGreaterThanTenOneFive
if #available(macOS 10.15, *) {
query[configuration.attribute.keyName] = kCFBooleanTrue
}
#endif
case .accessGroup(let name):
query[configuration.attribute.keyName] = name
}
Expand Down
21 changes: 21 additions & 0 deletions GTMAppAuth/Tests/Unit/KeychainStore/KeychainStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: useDataProtectionAttributeSet,
accountName: fakeWithDataProtection.accountName,
Expand Down Expand Up @@ -93,6 +95,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithAccessGroup.accountName,
Expand Down Expand Up @@ -130,6 +134,9 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)
XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithDataProtectionAndAccessGroup.accountName,
Expand Down Expand Up @@ -172,6 +179,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: useDataProtectionAttributeSet,
accountName: fakeWithDataProtection.accountName,
Expand Down Expand Up @@ -200,6 +209,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithAccessGroup.accountName,
Expand Down Expand Up @@ -238,6 +249,9 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)
XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithDataProtectionAndAccessGroup.accountName,
Expand Down Expand Up @@ -280,6 +294,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: useDataProtectionAttributeSet,
accountName: fakeWithDataProtection.accountName,
Expand Down Expand Up @@ -308,6 +324,8 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithAccessGroup.accountName,
Expand Down Expand Up @@ -346,6 +364,9 @@ class KeychainStoreTests: XCTestCase {
return
}

XCTAssertEqual(testQuery[kSecUseDataProtectionKeychain as String], kCFBooleanTrue)
XCTAssertEqual(testQuery[kSecAttrAccessGroup as String], expectedGroupName)

let comparisonQuery = comparisonKeychainQuery(
withAttributes: accessGroupAttributeSet,
accountName: fakeWithDataProtectionAndAccessGroup.accountName,
Expand Down