Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[local_auth] Implemented plugin to support local auth in macOS #5766

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions packages/local_auth/local_auth_darwin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
## 1.2.0

* Renames the package previously published as [`local_auth_ios`](https://pub.dev/packages/local_auth_ios)
* Adds support for macOS
2 changes: 1 addition & 1 deletion packages/local_auth/local_auth_darwin/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# local_auth_darwin

The iOS implementation of [`local_auth`][1].
The iOS and macOS implementation of [`local_auth`][1].

## Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if TARGET_OS_OSX
#import <FlutterMacOS/FlutterMacOS.h>
#else
#import <Flutter/Flutter.h>
#endif

#import "messages.g.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ - (void)showAlertWithMessage:(NSString *)message
dismissButtonTitle:(NSString *)dismissButtonTitle
openSettingsButtonTitle:(NSString *)openSettingsButtonTitle
completion:(FLAAuthCompletion)completion {
#if TARGET_OS_OSX
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@""];
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this line actually do anything? What's the default value?

[alert setInformativeText:message];

if (openSettingsButtonTitle != nil) {
[alert addButtonWithTitle:openSettingsButtonTitle];
[alert setShowsHelp:YES];
[alert setHelpAnchor:@"OpenSettings"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Where did this magic string come from; is there a default system help anchor with this name? This needs a code comment explaining it.

}
[alert addButtonWithTitle:dismissButtonTitle];

NSModalResponse response = [alert runModal];
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use beginSheetModalForWindow:completionHandler: with the window obtained from the registrar, and handleSucceeded: should be called from the completion.

You'll nee do adjust the plugin's init methods to take the registrar from registerWithRegistrar: and keep a reference to it.


if (response == NSAlertFirstButtonReturn) {
NSURL *url = [NSURL URLWithString:@"x-apple.systempreferences:"];
[[NSWorkspace sharedWorkspace] openURL:url];
[self handleSucceeded:NO withCompletion:completion];
} else {
[self handleSucceeded:NO withCompletion:completion];
}
#else
UIAlertController *alert =
[UIAlertController alertControllerWithTitle:@""
message:message
Expand Down Expand Up @@ -185,6 +207,7 @@ - (void)showAlertWithMessage:(NSString *)message
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alert
animated:YES
completion:nil];
#endif
}

- (void)handleAuthReplyWithSuccess:(BOOL)success
Expand Down Expand Up @@ -260,7 +283,12 @@ - (void)handleError:(NSError *)authError

#pragma mark - AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
#if TARGET_OS_OSX
- (void)applicationDidBecomeActive:(NSNotification *)notification
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does macOS need this flow at all?

#else
- (void)applicationDidBecomeActive:(UIApplication *)application
#endif
{
if (self.lastCallState != nil) {
[self authenticateWithOptions:_lastCallState.options
strings:_lastCallState.strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#if TARGET_OS_OSX
#import <FlutterMacOS/FlutterMacOS.h>
#else
#import <Flutter/Flutter.h>
#endif

#import <LocalAuthentication/LocalAuthentication.h>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef NS_ENUM(NSUInteger, FLAAuthResult) {
- (instancetype)initWithValue:(FLAAuthResult)value;
@end

/// Pigeon equivalent of the subset of BiometricType used by iOS.
/// Pigeon equivalent of the subset of BiometricType used by iOS and macOS.
typedef NS_ENUM(NSUInteger, FLAAuthBiometric) {
FLAAuthBiometricFace = 0,
FLAAuthBiometricFingerprint = 1,
Expand All @@ -50,9 +50,10 @@ typedef NS_ENUM(NSUInteger, FLAAuthBiometric) {
@class FLAAuthResultDetails;
@class FLAAuthBiometricWrapper;

/// Pigeon version of IOSAuthMessages, plus the authorization reason.
/// Pigeon version of IOSAuthMessages and MacOSAuthMessages, plus the
/// authorization reason.
///
/// See auth_messages_ios.dart for details.
/// See auth_messages_ios.dart and auth_messages_macos.dart for details.
@interface FLAAuthStrings : NSObject
/// `init` unavailable to enforce nonnull fields, see the `make` class method.
- (instancetype)init NS_UNAVAILABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ - (instancetype)initWithValue:(FLAAuthResult)value {
}
@end

/// Pigeon equivalent of the subset of BiometricType used by iOS.
/// Pigeon equivalent of the subset of BiometricType used by iOS and macOS.
@implementation FLAAuthBiometricBox
- (instancetype)initWithValue:(FLAAuthBiometric)value {
self = [super init];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ Downloaded by pub (not CocoaPods).
s.documentation_url = 'https://pub.dev/packages/local_auth_darwin'
s.source_files = 'Classes/**/*'
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.platform = :ios, '12.0'
s.ios.dependency 'Flutter'
s.osx.dependency 'FlutterMacOS'
s.ios.deployment_target = '12.0'
s.osx.deployment_target = '10.15'
Copy link
Contributor

Choose a reason for hiding this comment

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

Why 10.15 rather than 10.14?

s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
s.resource_bundles = {'local_auth_darwin_privacy' => ['Resources/PrivacyInfo.xcprivacy']}
end

6 changes: 3 additions & 3 deletions packages/local_auth/local_auth_darwin/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ flutter_ios_podfile_setup
target 'Runner' do
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
inherit! :search_paths

pod 'OCMock','3.5'
end
pod 'OCMock','3.5'
end
end

post_install do |installer|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSFaceIDUsageDescription</key>
<string>App needs to authenticate using faces.</string>
<key>NSFaceIDUsageDescription</key>
<string>App needs to authenticate using faces.</string>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
Expand Down
10 changes: 8 additions & 2 deletions packages/local_auth/local_auth_darwin/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ class _MyAppState extends State<MyApp> {
});
authenticated = await LocalAuthPlatform.instance.authenticate(
localizedReason: 'Let OS determine authentication method',
authMessages: <AuthMessages>[const IOSAuthMessages()],
authMessages: <AuthMessages>[
const IOSAuthMessages(),
const MacOSAuthMessages(),
],
options: const AuthenticationOptions(
stickyAuth: true,
),
Expand Down Expand Up @@ -118,7 +121,10 @@ class _MyAppState extends State<MyApp> {
authenticated = await LocalAuthPlatform.instance.authenticate(
localizedReason:
'Scan your fingerprint (or face or whatever) to authenticate',
authMessages: <AuthMessages>[const IOSAuthMessages()],
authMessages: <AuthMessages>[
const IOSAuthMessages(),
const MacOSAuthMessages(),
],
options: const AuthenticationOptions(
stickyAuth: true,
biometricOnly: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Flutter-related
**/Flutter/ephemeral/
**/Pods/

# Xcode-related
**/dgph
**/xcuserdata/
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "ephemeral/Flutter-Generated.xcconfig"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "ephemeral/Flutter-Generated.xcconfig"
46 changes: 46 additions & 0 deletions packages/local_auth/local_auth_darwin/example/macos/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
platform :osx, '10.15'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}

def flutter_root
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__)
unless File.exist?(generated_xcode_build_settings_path)
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first"
end

File.foreach(generated_xcode_build_settings_path) do |line|
matches = line.match(/FLUTTER_ROOT\=(.*)/)
return matches[1].strip if matches
end
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\""
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_macos_podfile_setup

target 'Runner' do
use_frameworks!
use_modular_headers!

flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__))

target 'RunnerTests' do
inherit! :search_paths

pod 'OCMock','3.5'
end
end

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_macos_build_settings(target)
end
end
Loading