Browse Source

Merge pull request #345 from kishikawakatsumi/jessearmand-authentication-context

Specify `authenticationContext` type
Kishikawa Katsumi 8 years ago
parent
commit
f5e80943b8
1 changed files with 28 additions and 0 deletions
  1. 28 0
      Lib/KeychainAccess/Keychain.swift

+ 28 - 0
Lib/KeychainAccess/Keychain.swift

@@ -25,6 +25,9 @@
 
 import Foundation
 import Security
+#if os(iOS) || os(OSX)
+import LocalAuthentication
+#endif
 
 public let KeychainAccessErrorDomain = "com.kishikawakatsumi.KeychainAccess.error"
 
@@ -389,6 +392,13 @@ public final class Keychain {
         return options.authenticationPrompt
     }
 
+    #if os(iOS) || os(OSX)
+    @available(iOS 9.0, OSX 10.11, *)
+    public var authenticationContext: LAContext? {
+        return options.authenticationContext as? LAContext
+    }
+    #endif
+
     fileprivate let options: Options
 
     // MARK:
@@ -489,6 +499,15 @@ public final class Keychain {
         return Keychain(options)
     }
 
+    #if os(iOS) || os(OSX)
+    @available(iOS 9.0, OSX 10.11, *)
+    public func authenticationContext(_ authenticationContext: LAContext) -> Keychain {
+        var options = self.options
+        options.authenticationContext = authenticationContext
+        return Keychain(options)
+    }
+    #endif
+
     // MARK:
 
     public func get(_ key: String) throws -> String? {
@@ -1050,6 +1069,7 @@ struct Options {
     var comment: String?
 
     var authenticationPrompt: String?
+    var authenticationContext: AnyObject?
 
     var attributes = [String: Any]()
 }
@@ -1186,6 +1206,14 @@ extension Options {
             }
         }
 
+        #if !os(watchOS)
+        if #available(iOS 9.0, OSX 10.11, *) {
+            if authenticationContext != nil {
+                query[UseAuthenticationContext] = authenticationContext
+            }
+        }
+        #endif
+
         return query
     }