Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[local_auth] Fix callback thread handling #3778

Merged
Merged
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
4 changes: 4 additions & 0 deletions packages/local_auth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3

* Fix crashes due to threading issues in iOS implementation.

## 1.1.2

* Update Jetpack dependencies to latest stable versions.
Expand Down
6 changes: 6 additions & 0 deletions packages/local_auth/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ flutter_ios_podfile_setup

target 'Runner' do
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))

target 'XCTests' do
inherit! :search_paths

pod 'OCMock', '3.5'
end
end

post_install do |installer|
Expand Down
194 changes: 167 additions & 27 deletions packages/local_auth/example/ios/Runner.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3398D2CC26163948005A052F"
BuildableName = "XCTests.xctest"
BlueprintName = "XCTests"
ReferencedContainer = "container:Runner.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
22 changes: 22 additions & 0 deletions packages/local_auth/example/ios/XCTests/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
112 changes: 64 additions & 48 deletions packages/local_auth/ios/Classes/FLTLocalAuthPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
#import "FLTLocalAuthPlugin.h"

@interface FLTLocalAuthPlugin ()
@property(copy, nullable) NSDictionary<NSString *, NSNumber *> *lastCallArgs;
@property(nullable) FlutterResult lastResult;
@property(nonatomic, copy, nullable) NSDictionary<NSString *, NSNumber *> *lastCallArgs;
@property(nonatomic, nullable) FlutterResult lastResult;
// For unit tests to inject dummy LAContext instances that will be used when a new context would
// normally be created. Each call to createAuthContext will remove the current first element from
// the array.
- (void)setAuthContextOverrides:(NSArray<LAContext *> *)authContexts;
@end

@implementation FLTLocalAuthPlugin
@implementation FLTLocalAuthPlugin {
NSMutableArray<LAContext *> *_authContextOverrides;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FlutterMethodChannel *channel =
Expand Down Expand Up @@ -40,6 +46,19 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result

#pragma mark Private Methods

- (void)setAuthContextOverrides:(NSArray<LAContext *> *)authContexts {
_authContextOverrides = [authContexts mutableCopy];
}

- (LAContext *)createAuthContext {
if ([_authContextOverrides count] > 0) {
LAContext *context = [_authContextOverrides firstObject];
[_authContextOverrides removeObjectAtIndex:0];
return context;
}
return [[LAContext alloc] init];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each self.authContext is going to call the constructor (when no _authContextOverride is provided)

If we always wants a new instance (which i think that's the case), we should rename it do something doesn't sound too much like a getter. Maybe: - (LAContext *)getNewAuthContext`?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to createAuthContext since create is a standard idiom. Also future-proofed the override by making it better fit the fact that this is really, as you point out, a factory rather than a getter; it now uses an array of overrides, so a test could run a flow with multiple API calls.

}

- (void)alertMessage:(NSString *)message
firstButton:(NSString *)firstButton
flutterResult:(FlutterResult)result
Expand Down Expand Up @@ -75,7 +94,7 @@ - (void)alertMessage:(NSString *)message
}

- (void)getAvailableBiometrics:(FlutterResult)result {
LAContext *context = [[LAContext alloc] init];
LAContext *context = self.createAuthContext;
NSError *authError = nil;
NSMutableArray<NSString *> *biometrics = [[NSMutableArray<NSString *> alloc] init];
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
Expand All @@ -96,9 +115,10 @@ - (void)getAvailableBiometrics:(FlutterResult)result {
}
result(biometrics);
}

- (void)authenticateWithBiometrics:(NSDictionary *)arguments
withFlutterResult:(FlutterResult)result {
LAContext *context = [[LAContext alloc] init];
LAContext *context = self.createAuthContext;
NSError *authError = nil;
self.lastCallArgs = nil;
self.lastResult = nil;
Expand All @@ -109,35 +129,20 @@ - (void)authenticateWithBiometrics:(NSDictionary *)arguments
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:arguments[@"localizedReason"]
reply:^(BOOL success, NSError *error) {
if (success) {
result(@YES);
} else {
switch (error.code) {
case LAErrorPasscodeNotSet:
case LAErrorTouchIDNotAvailable:
case LAErrorTouchIDNotEnrolled:
case LAErrorTouchIDLockout:
[self handleErrors:error
flutterArguments:arguments
withFlutterResult:result];
return;
case LAErrorSystemCancel:
if ([arguments[@"stickyAuth"] boolValue]) {
self.lastCallArgs = arguments;
self.lastResult = result;
return;
}
}
result(@NO);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self handleAuthReplyWithSuccess:success
error:error
flutterArguments:arguments
flutterResult:result];
});
}];
} else {
[self handleErrors:authError flutterArguments:arguments withFlutterResult:result];
}
}

- (void)authenticate:(NSDictionary *)arguments withFlutterResult:(FlutterResult)result {
LAContext *context = [[LAContext alloc] init];
LAContext *context = self.createAuthContext;
NSError *authError = nil;
_lastCallArgs = nil;
_lastResult = nil;
Expand All @@ -148,27 +153,12 @@ - (void)authenticate:(NSDictionary *)arguments withFlutterResult:(FlutterResult)
[context evaluatePolicy:kLAPolicyDeviceOwnerAuthentication
localizedReason:arguments[@"localizedReason"]
reply:^(BOOL success, NSError *error) {
if (success) {
result(@YES);
} else {
switch (error.code) {
case LAErrorPasscodeNotSet:
case LAErrorTouchIDNotAvailable:
case LAErrorTouchIDNotEnrolled:
case LAErrorTouchIDLockout:
[self handleErrors:error
flutterArguments:arguments
withFlutterResult:result];
return;
case LAErrorSystemCancel:
if ([arguments[@"stickyAuth"] boolValue]) {
self->_lastCallArgs = arguments;
self->_lastResult = result;
return;
}
}
result(@NO);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self handleAuthReplyWithSuccess:success
error:error
flutterArguments:arguments
flutterResult:result];
});
}];
} else {
[self handleErrors:authError flutterArguments:arguments withFlutterResult:result];
Expand All @@ -178,6 +168,32 @@ - (void)authenticate:(NSDictionary *)arguments withFlutterResult:(FlutterResult)
}
}

- (void)handleAuthReplyWithSuccess:(BOOL)success
error:(NSError *)error
flutterArguments:(NSDictionary *)arguments
flutterResult:(FlutterResult)result {
NSAssert([NSThread isMainThread], @"Response handling must be done on the main thread.");
if (success) {
result(@YES);
} else {
switch (error.code) {
case LAErrorPasscodeNotSet:
case LAErrorTouchIDNotAvailable:
case LAErrorTouchIDNotEnrolled:
case LAErrorTouchIDLockout:
[self handleErrors:error flutterArguments:arguments withFlutterResult:result];
return;
case LAErrorSystemCancel:
if ([arguments[@"stickyAuth"] boolValue]) {
self->_lastCallArgs = arguments;
self->_lastResult = result;
return;
}
}
result(@NO);
}
}

- (void)handleErrors:(NSError *)authError
flutterArguments:(NSDictionary *)arguments
withFlutterResult:(FlutterResult)result {
Expand Down
Loading