Skip to content

Commit

Permalink
fix: check has property namespace for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Jan 22, 2025
1 parent 9423ffe commit 5f53e6d
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 78 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.0.12

* Check biometric changed for iOS

## 1.0.11

* Check has property namespace for Android

## 1.0.10

* Add function isBiometricChanged
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Generate key pair and signing (NIST P-256 EC key pair using ECDSA) using Local A
It is really easy to use! You should ensure that you add the `local_auth_signature` as a dependency in your flutter project.

```yaml
local_auth_signature: "^1.0.8"
local_auth_signature: "^1.0.12"
```
## Usage
Expand All @@ -30,10 +30,26 @@ final _localAuthSignature = LocalAuthSignature.instance;
final _key = 'com.prongbang.signx.key';
```

#### Key Changed
#### Check Fingerprint or Biometric changed

- Android, iOS

```dart
final status = await _localAuthSignature.isBiometricChanged(_key);
```

- iOS

```dart
final status = await _localAuthSignature.keyChanged(_key, publicKey);
await _localAuthSignature.resetBiometricChanged();
```

```dart
try {
final signature = await _localAuthSignature.sign(_key, androidPrompt, iosPrompt);
} on PlatformException catch (e) {
// TODO check from exception code
}
```

#### Create KeyPair
Expand Down
4 changes: 4 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
if (project.android.hasProperty("namespace")) {
namespace 'com.prongbang.local_auth_signature'
}

compileSdk 33

compileOptions {
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
platform :ios, '11.0'
platform :ios, '12.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
12 changes: 6 additions & 6 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ PODS:
- Flutter (1.0.0)
- local_auth_signature (1.0.1):
- Flutter
- SignatureBiometricSwift (~> 1.0.9)
- SignatureBiometricSwift (1.0.9)
- SignatureBiometricSwift (~> 1.0.10)
- SignatureBiometricSwift (1.0.10)

DEPENDENCIES:
- Flutter (from `Flutter`)
Expand All @@ -21,9 +21,9 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
local_auth_signature: 6a1aab72015b8de7f30c7e394e9f110a2e1a7d98
SignatureBiometricSwift: d67ad0a5b8ecf0e88b454bf2bc1a9a6c528bea83
local_auth_signature: 5526cf4d92cc893cefc6368926aefb34f124a07d
SignatureBiometricSwift: e8f35299350fe33b93e7a1342ccb88448c80ff98

PODFILE CHECKSUM: 7368163408c647b7eb699d0d788ba6718e18fb8d
PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048

COCOAPODS: 1.11.3
COCOAPODS: 1.15.2
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class _MyAppState extends State<MyApp> {
final _key = 'com.prongbang.test.key';
final _payload = 'Hello';
String? _publicKey = '';
String? _signature = '';
String? _signature = 'MEUCIBIcOQng5BqDg9tOjayHDMPgiZT48bWJ0AzkSM0WGVcmAiEAkEGVzlZeJuJCz2IUnqJ/c1zzla1TErcnyU1nzkkN/dA=';
String? _verified = '';
String? _status = '';

Expand All @@ -42,6 +42,7 @@ class _MyAppState extends State<MyApp> {
),
IOSPromptInfo(reason: 'Please allow biometric'),
);
await _localAuthSignature.resetBiometricChanged();
setState(() {
_publicKey = publicKey;
});
Expand Down
30 changes: 15 additions & 15 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "2.0.1"
lints:
dependency: transitive
description:
Expand All @@ -113,7 +113,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.9"
version: "1.0.12"
matcher:
dependency: transitive
description:
Expand All @@ -134,10 +134,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.11.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -195,10 +195,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.6.1"
vector_math:
dependency: transitive
description:
Expand All @@ -211,10 +211,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "13.0.0"
sdks:
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
dart: ">=3.2.0-0 <4.0.0"
flutter: ">=2.5.0"
1 change: 1 addition & 0 deletions ios/Classes/SwiftLocalAuthSignatureMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class SwiftLocalAuthSignatureMethod {
static let Sign = "sign"
static let Verify = "verify"
static let isBiometricChanged = "isBiometricChanged"
static let resetBiometricChanged = "resetBiometricChanged"
}
70 changes: 20 additions & 50 deletions ios/Classes/SwiftLocalAuthSignaturePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,35 @@ import LocalAuthentication

public class SwiftLocalAuthSignaturePlugin: NSObject, FlutterPlugin {

private var signatureBiometricManager: SignatureBiometricManager?=nil

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "local_auth_signature", binaryMessenger: registrar.messenger())
let instance = SwiftLocalAuthSignaturePlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}

func isBiometricChanged(name: String) -> Bool {
let context = LAContext()
var error: NSError?
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

// Check if biometric authentication is available
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
// Biometric authentication is not available
return true
if call.method == SwiftLocalAuthSignatureMethod.isBiometricChanged {
let keyConfig = KeyConfig(name: "")
let signatureBiometricManager = LocalSignatureBiometricManager.newInstance(keyConfig: keyConfig)

let isChanged = signatureBiometricManager.biometricsChanged()
if isChanged {
result("changed")
} else {
result("unchanged")
}
return
} else if call.method == SwiftLocalAuthSignatureMethod.resetBiometricChanged {
let keyConfig = KeyConfig(name: "")
let signatureBiometricManager = LocalSignatureBiometricManager.newInstance(keyConfig: keyConfig)

signatureBiometricManager.biometricsPolicyStateReset()

result(true)
return
}

let tag = name.data(using: .utf8)!
let query: [String: Any] = [
kSecClass as String: kSecClassKey,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrApplicationTag as String: tag,
kSecReturnRef as String: true,
kSecUseAuthenticationUI as String: kSecUseAuthenticationUIFail
]

var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)

switch status {
case errSecSuccess:
// Key exists and is accessible without authentication
return false
case errSecItemNotFound:
// Key doesn't exist anymore, biometrics likely changed
return true
case errSecUserCanceled, errSecAuthFailed:
// Key exists but requires authentication (which we've disabled)
// Biometrics are still valid
return false
default:
// Any other error suggests the key is not accessible, biometrics likely changed
print("Unexpected error checking biometric state: \(status)")
return true
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let args = call.arguments as? Dictionary<String, String> else {
result(
FlutterError(
Expand All @@ -77,16 +57,6 @@ public class SwiftLocalAuthSignaturePlugin: NSObject, FlutterPlugin {
}

switch call.method {
case SwiftLocalAuthSignatureMethod.isBiometricChanged:

let isChanged = isBiometricChanged(name: key)
if isChanged {
result("changed")
} else {
result("unchanged")
}

break
case SwiftLocalAuthSignatureMethod.CreateKeyPair:
guard let reason = args[SwiftLocalAuthSignatureArgs.Reason] else {
result(
Expand Down
2 changes: 1 addition & 1 deletion ios/local_auth_signature.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Generate key pair and signing using Local Authentication for Android and iOS.
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.dependency 'SignatureBiometricSwift', '~> 1.0.9'
s.dependency 'SignatureBiometricSwift', '~> 1.0.10'
s.platform = :ios, '11.0'
s.swift_version = ["4.0", "4.1", "4.2", "5.0", "5.1", "5.2", "5.3", "5.4", "5.5", "5.6", "5.7", "5.8", "5.9"]
end
6 changes: 6 additions & 0 deletions lib/local_auth_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ abstract class LocalAuthSignature {
throw UnimplementedError('isBiometricChanged() has not been implemented.');
}

/// Supported for iOS only, do nothing when run on Android and return false always.
Future<bool> resetBiometricChanged() {
throw UnimplementedError(
'resetBiometricChanged() has not been implemented.');
}

Future<String?> createKeyPair(
String key,
AndroidPromptInfo androidPromptInfo,
Expand Down
10 changes: 10 additions & 0 deletions lib/local_auth_signature_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class MethodChannelLocalAuthSignature extends LocalAuthSignature {
@visibleForTesting
final methodChannel = const MethodChannel('local_auth_signature');

@override
Future<bool> resetBiometricChanged() async {
if (Platform.isIOS) {
final result =
await methodChannel.invokeMethod<bool>('resetBiometricChanged', {});
return result ?? false;
}
return false;
}

@override
Future<KeyChangedStatus?> isBiometricChanged(String key) async {
final status = await methodChannel
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: local_auth_signature
description: Generate key pair and signing (NIST P-256 EC key pair using ECDSA) using Local Authentication for Android and iOS.
version: 1.0.10
version: 1.0.12
homepage: https://github.com/prongbang/local_auth_signature

environment:
Expand Down

0 comments on commit 5f53e6d

Please sign in to comment.