Browse Source

Use authentication context whenever available

To access keychain items with the same authentication context
Jesse Armand 8 years ago
parent
commit
9a6b044472
1 changed files with 23 additions and 0 deletions
  1. 23 0
      Lib/KeychainAccess/Keychain.swift

+ 23 - 0
Lib/KeychainAccess/Keychain.swift

@@ -389,6 +389,12 @@ public final class Keychain {
         return options.authenticationPrompt
     }
 
+    @available(iOS 9.0, OSX 10.11, *)
+    @available(watchOS, unavailable)
+    public var authenticationContext: Any? {
+        return options.authenticationContext
+    }
+
     fileprivate let options: Options
 
     // MARK:
@@ -489,6 +495,14 @@ public final class Keychain {
         return Keychain(options)
     }
 
+    @available(iOS 9.0, OSX 10.11, *)
+    @available(watchOS, unavailable)
+    public func authenticationContext(_ authenticationContext: Any) -> Keychain {
+        var options = self.options
+        options.authenticationContext = authenticationContext
+        return Keychain(options)
+    }
+
     // MARK:
 
     public func get(_ key: String) throws -> String? {
@@ -1050,6 +1064,7 @@ struct Options {
     var comment: String?
 
     var authenticationPrompt: String?
+    var authenticationContext: Any?
 
     var attributes = [String: Any]()
 }
@@ -1186,6 +1201,14 @@ extension Options {
             }
         }
 
+        #if os(iOS) || os(macOS)
+        if #available(iOS 9.0, OSX 10.11, *) {
+            if authenticationContext != nil {
+                query[UseAuthenticationContext] = authenticationContext
+            }
+        }
+        #endif
+
         return query
     }