@onekeyfe/react-native-check-biometric-auth-changed 1.1.20 → 1.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,66 @@
1
1
 
2
2
  import NitroModules
3
3
  import LocalAuthentication
4
+ import Security
4
5
  import ReactNativeNativeLogger
5
6
 
6
7
  class ReactNativeCheckBiometricAuthChanged: HybridReactNativeCheckBiometricAuthChangedSpec {
8
+
9
+ private static let keychainKey = "com.onekey.biometricAuthState"
10
+
11
+ private static func loadDomainState() -> Data? {
12
+ let query: [String: Any] = [
13
+ kSecClass as String: kSecClassGenericPassword,
14
+ kSecAttrAccount as String: keychainKey,
15
+ kSecReturnData as String: true,
16
+ kSecMatchLimit as String: kSecMatchLimitOne
17
+ ]
18
+ var result: AnyObject?
19
+ let status = SecItemCopyMatching(query as CFDictionary, &result)
20
+ if status == errSecSuccess {
21
+ return result as? Data
22
+ }
23
+ return nil
24
+ }
25
+
26
+ private static func saveDomainState(_ data: Data?) {
27
+ guard let data = data else { return }
28
+ let query: [String: Any] = [
29
+ kSecClass as String: kSecClassGenericPassword,
30
+ kSecAttrAccount as String: keychainKey,
31
+ kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
32
+ ]
33
+ let attrs: [String: Any] = [
34
+ kSecValueData as String: data
35
+ ]
36
+ let status = SecItemUpdate(query as CFDictionary, attrs as CFDictionary)
37
+ if status == errSecItemNotFound {
38
+ var addQuery = query
39
+ addQuery[kSecValueData as String] = data
40
+ SecItemAdd(addQuery as CFDictionary, nil)
41
+ }
42
+ }
43
+
7
44
  func checkChanged() throws -> Promise<Bool> {
8
45
  return Promise.async {
9
46
  var changed = false
10
47
  let context = LAContext()
11
48
  _ = context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil)
12
49
  let domainState = context.evaluatedPolicyDomainState
13
- let defaults = UserDefaults.standard
14
- let oldDomainState = defaults.data(forKey: "biometricAuthState")
50
+ let oldDomainState = Self.loadDomainState()
15
51
  if let oldDomainState = oldDomainState {
16
52
  changed = oldDomainState != domainState
17
53
  } else {
18
- OneKeyLog.info("Biometric", "No previous biometric state stored, saving initial state")
54
+ // Migrate from UserDefaults if exists
55
+ let defaults = UserDefaults.standard
56
+ if let legacyState = defaults.data(forKey: "biometricAuthState") {
57
+ changed = legacyState != domainState
58
+ defaults.removeObject(forKey: "biometricAuthState")
59
+ } else {
60
+ OneKeyLog.info("Biometric", "No previous biometric state stored, saving initial state")
61
+ }
19
62
  }
20
- defaults.set(domainState, forKey: "biometricAuthState")
63
+ Self.saveDomainState(domainState)
21
64
  if changed {
22
65
  OneKeyLog.info("Biometric", "Biometric auth change detected")
23
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-check-biometric-auth-changed",
3
- "version": "1.1.20",
3
+ "version": "1.1.21",
4
4
  "description": "react-native-check-biometric-auth-changed",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",