diff --git a/flutter_web_auth_2/analysis_options.yaml b/flutter_web_auth_2/analysis_options.yaml index 409daf3..024532d 100644 --- a/flutter_web_auth_2/analysis_options.yaml +++ b/flutter_web_auth_2/analysis_options.yaml @@ -42,15 +42,14 @@ linter: # `// ignore_for_file: name_of_lint` syntax on the line or in the file # producing the lint. rules: - # avoid_print: false # Uncomment to disable the `avoid_print` rule - # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule - always_declare_return_types - always_put_control_body_on_new_line - # - always_put_required_named_parameters_first + - always_put_required_named_parameters_first # - always_specify_types - always_use_package_imports - annotate_overrides - # - avoid_annotating_with_dynamic + - annotate_redeclares + - avoid_annotating_with_dynamic - avoid_bool_literals_in_conditional_expressions # - avoid_catches_without_on_clauses - avoid_catching_errors @@ -61,8 +60,9 @@ linter: - avoid_equals_and_hash_code_on_mutable_classes - avoid_escaping_inner_quotes - avoid_field_initializers_in_const_classes + - avoid_final_parameters - avoid_function_literals_in_foreach_calls - # - avoid_implementing_value_types + - avoid_implementing_value_types - avoid_init_to_null - avoid_js_rounded_ints - avoid_multiple_declarations_per_line @@ -91,18 +91,23 @@ linter: - camel_case_extensions - camel_case_types - cancel_subscriptions - # - cascade_invocations + - cascade_invocations - cast_nullable_to_non_nullable - close_sinks - collection_methods_unrelated_type + - combinators_ordering - comment_references + - conditional_uri_does_not_exist - constant_identifier_names - control_flow_in_finally - curly_braces_in_flow_control_structures + - dangling_library_doc_comments - depend_on_referenced_packages - deprecated_consistency - # - diagnostic_describe_all_properties + - deprecated_member_use_from_same_package + - diagnostic_describe_all_properties - directives_ordering + - discarded_futures - do_not_use_environment - empty_catches - empty_constructor_bodies @@ -113,21 +118,29 @@ linter: - flutter_style_todos - hash_and_equals - implementation_imports + - implicit_call_tearoffs + - implicit_reopen + - invalid_case_patterns - join_return_with_assignment - leading_newlines_in_multiline_strings + - library_annotations - library_names - library_prefixes - library_private_types_in_public_api - lines_longer_than_80_chars - literal_only_boolean_expressions + - matching_super_parameters - missing_whitespace_between_adjacent_strings - no_adjacent_strings_in_list - # - no_default_cases + - no_default_cases - no_duplicate_case_values - no_leading_underscores_for_library_prefixes - no_leading_underscores_for_local_identifiers + - no_literal_bool_comparisons - no_logic_in_create_state - no_runtimeType_toString + - no_self_assignments + - no_wildcard_variable_uses - non_constant_identifier_names - noop_primitive_operations - null_check_on_nullable_type_parameter @@ -180,13 +193,15 @@ linter: - prefer_typing_uninitialized_variables - prefer_void_to_null - provide_deprecation_message - # - public_member_api_docs + - public_member_api_docs - recursive_getters - require_trailing_commas + - secure_pubspec_urls - sized_box_for_whitespace + - sized_box_shrink_expand - slash_for_doc_comments - sort_child_properties_last - # - sort_constructors_first + - sort_constructors_first - sort_pub_dependencies - sort_unnamed_constructors_first - test_types_in_equals @@ -194,17 +209,21 @@ linter: - tighten_type_of_initializing_formals - type_annotate_public_apis - type_init_formals + - type_literal_in_constant_pattern - unawaited_futures - unnecessary_await_in_return - unnecessary_brace_in_string_interps + - unnecessary_breaks - unnecessary_const - unnecessary_constructor_name # - unnecessary_final - unnecessary_getters_setters - unnecessary_lambdas - unnecessary_late + - unnecessary_library_directive - unnecessary_new - unnecessary_null_aware_assignments + - unnecessary_null_aware_operator_on_extension_on_nullable - unnecessary_null_checks - unnecessary_null_in_if_null_operators - unnecessary_nullable_for_final_variable_declarations @@ -215,9 +234,14 @@ linter: - unnecessary_string_escapes - unnecessary_string_interpolations - unnecessary_this + - unnecessary_to_list_in_spreads + - unreachable_from_main - unrelated_type_equality_checks - unsafe_html - use_build_context_synchronously + - use_colored_box + - use_decorated_box + - use_enums - use_full_hex_values_for_flutter_colors - use_function_type_syntax_for_parameters - use_if_null_to_convert_nulls_to_bools @@ -229,6 +253,8 @@ linter: - use_rethrow_when_possible - use_setters_to_change_properties - use_string_buffers + - use_string_in_part_of_directives + - use_super_parameters - use_test_throws_matchers - use_to_and_as_if_applicable - valid_regexps diff --git a/flutter_web_auth_2/example/analysis_options.yaml b/flutter_web_auth_2/example/analysis_options.yaml index 409daf3..7261f4f 100644 --- a/flutter_web_auth_2/example/analysis_options.yaml +++ b/flutter_web_auth_2/example/analysis_options.yaml @@ -42,15 +42,14 @@ linter: # `// ignore_for_file: name_of_lint` syntax on the line or in the file # producing the lint. rules: - # avoid_print: false # Uncomment to disable the `avoid_print` rule - # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule - always_declare_return_types - always_put_control_body_on_new_line - # - always_put_required_named_parameters_first + - always_put_required_named_parameters_first # - always_specify_types - always_use_package_imports - annotate_overrides - # - avoid_annotating_with_dynamic + - annotate_redeclares + - avoid_annotating_with_dynamic - avoid_bool_literals_in_conditional_expressions # - avoid_catches_without_on_clauses - avoid_catching_errors @@ -61,8 +60,9 @@ linter: - avoid_equals_and_hash_code_on_mutable_classes - avoid_escaping_inner_quotes - avoid_field_initializers_in_const_classes + - avoid_final_parameters - avoid_function_literals_in_foreach_calls - # - avoid_implementing_value_types + - avoid_implementing_value_types - avoid_init_to_null - avoid_js_rounded_ints - avoid_multiple_declarations_per_line @@ -91,18 +91,23 @@ linter: - camel_case_extensions - camel_case_types - cancel_subscriptions - # - cascade_invocations + - cascade_invocations - cast_nullable_to_non_nullable - close_sinks - collection_methods_unrelated_type + - combinators_ordering - comment_references + - conditional_uri_does_not_exist - constant_identifier_names - control_flow_in_finally - curly_braces_in_flow_control_structures + - dangling_library_doc_comments - depend_on_referenced_packages - deprecated_consistency - # - diagnostic_describe_all_properties + - deprecated_member_use_from_same_package + - diagnostic_describe_all_properties - directives_ordering + - discarded_futures - do_not_use_environment - empty_catches - empty_constructor_bodies @@ -113,21 +118,29 @@ linter: - flutter_style_todos - hash_and_equals - implementation_imports + - implicit_call_tearoffs + - implicit_reopen + - invalid_case_patterns - join_return_with_assignment - leading_newlines_in_multiline_strings + - library_annotations - library_names - library_prefixes - library_private_types_in_public_api - lines_longer_than_80_chars - literal_only_boolean_expressions + - matching_super_parameters - missing_whitespace_between_adjacent_strings - no_adjacent_strings_in_list - # - no_default_cases + - no_default_cases - no_duplicate_case_values - no_leading_underscores_for_library_prefixes - no_leading_underscores_for_local_identifiers + - no_literal_bool_comparisons - no_logic_in_create_state - no_runtimeType_toString + - no_self_assignments + - no_wildcard_variable_uses - non_constant_identifier_names - noop_primitive_operations - null_check_on_nullable_type_parameter @@ -136,7 +149,7 @@ linter: - one_member_abstracts - only_throw_errors - overridden_fields - - package_api_docs + # - package_api_docs - package_names - package_prefixed_library_names - parameter_assignments @@ -183,10 +196,12 @@ linter: # - public_member_api_docs - recursive_getters - require_trailing_commas + - secure_pubspec_urls - sized_box_for_whitespace + - sized_box_shrink_expand - slash_for_doc_comments - sort_child_properties_last - # - sort_constructors_first + - sort_constructors_first - sort_pub_dependencies - sort_unnamed_constructors_first - test_types_in_equals @@ -194,17 +209,21 @@ linter: - tighten_type_of_initializing_formals - type_annotate_public_apis - type_init_formals + - type_literal_in_constant_pattern - unawaited_futures - unnecessary_await_in_return - unnecessary_brace_in_string_interps + - unnecessary_breaks - unnecessary_const - unnecessary_constructor_name # - unnecessary_final - unnecessary_getters_setters - unnecessary_lambdas - unnecessary_late + - unnecessary_library_directive - unnecessary_new - unnecessary_null_aware_assignments + - unnecessary_null_aware_operator_on_extension_on_nullable - unnecessary_null_checks - unnecessary_null_in_if_null_operators - unnecessary_nullable_for_final_variable_declarations @@ -215,9 +234,14 @@ linter: - unnecessary_string_escapes - unnecessary_string_interpolations - unnecessary_this + - unnecessary_to_list_in_spreads + - unreachable_from_main - unrelated_type_equality_checks - unsafe_html - use_build_context_synchronously + - use_colored_box + - use_decorated_box + - use_enums - use_full_hex_values_for_flutter_colors - use_function_type_syntax_for_parameters - use_if_null_to_convert_nulls_to_bools @@ -229,6 +253,8 @@ linter: - use_rethrow_when_possible - use_setters_to_change_properties - use_string_buffers + - use_string_in_part_of_directives + - use_super_parameters - use_test_throws_matchers - use_to_and_as_if_applicable - valid_regexps diff --git a/flutter_web_auth_2/example/lib/main.dart b/flutter_web_auth_2/example/lib/main.dart index 3687dd9..71c0007 100644 --- a/flutter_web_auth_2/example/lib/main.dart +++ b/flutter_web_auth_2/example/lib/main.dart @@ -7,7 +7,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_web_auth_2/flutter_web_auth_2.dart'; -const html = ''' +const _html = ''' @@ -82,10 +82,10 @@ class MyAppState extends State { String _status = ''; @override - void initState() { + Future initState() async { super.initState(); if (!kIsWeb) { - startServer(); + await startServer(); } } @@ -100,7 +100,7 @@ class MyAppState extends State { req.response.headers.add('Content-Type', 'text/html'); req.response.write( - html.replaceFirst( + _html.replaceFirst( 'CALLBACK_URL_HERE', 'foobar://success?code=1337', ), diff --git a/flutter_web_auth_2/lib/flutter_web_auth_2.dart b/flutter_web_auth_2/lib/flutter_web_auth_2.dart index 787e5ba..32604ac 100644 --- a/flutter_web_auth_2/lib/flutter_web_auth_2.dart +++ b/flutter_web_auth_2/lib/flutter_web_auth_2.dart @@ -12,18 +12,19 @@ export 'src/unsupported.dart' if (dart.library.html) 'src/web.dart'; class _OnAppLifecycleResumeObserver extends WidgetsBindingObserver { - final Function onResumed; - _OnAppLifecycleResumeObserver(this.onResumed); + final Future Function() onResumed; + @override - void didChangeAppLifecycleState(AppLifecycleState state) { + Future didChangeAppLifecycleState(AppLifecycleState state) async { if (state == AppLifecycleState.resumed) { - onResumed(); + await onResumed(); } } } +/// Provides all the functions you - as a user - should access class FlutterWebAuth2 { static final RegExp _schemeRegExp = RegExp(r'^[a-z][a-z\d+.-]*$'); @@ -80,7 +81,7 @@ class FlutterWebAuth2 { ); } - /// The plugin has to store the Result callbacks in order to pass + /// The plugin may need to store the resulting callbacks in order to pass /// the result back to the caller of `authenticate`. But if that result never /// comes the callback will dangle around forever. This can be called to /// terminate all `authenticate` calls with an error. diff --git a/flutter_web_auth_2/lib/src/options.dart b/flutter_web_auth_2/lib/src/options.dart index 23b8816..90eef7e 100644 --- a/flutter_web_auth_2/lib/src/options.dart +++ b/flutter_web_auth_2/lib/src/options.dart @@ -44,7 +44,38 @@ const _defaultLandingPage = ''' '''; +/// Provides configuration options for calls to `authenticate`. class FlutterWebAuth2Options { + /// Construct an instance and specify the wanted options. + const FlutterWebAuth2Options({ + bool? preferEphemeral, + this.debugOrigin, + int? intentFlags, + this.windowName, + int? timeout, + String? landingPageHtml, + bool? silentAuth, + bool? useWebview, + }) : preferEphemeral = preferEphemeral ?? false, + intentFlags = intentFlags ?? defaultIntentFlags, + timeout = timeout ?? 5 * 60, + landingPageHtml = landingPageHtml ?? _defaultLandingPage, + silentAuth = silentAuth ?? false, + useWebview = useWebview ?? true; + + /// Construct an instance from JSON format. + FlutterWebAuth2Options.fromJson(Map json) + : this( + preferEphemeral: json['preferEphemeral'], + debugOrigin: json['debugOrigin'], + intentFlags: json['intentFlags'], + windowName: json['windowName'], + timeout: json['timeout'], + landingPageHtml: json['landingPageHtml'], + silentAuth: json['silentAuth'], + useWebview: json['useWebview'], + ); + /// **Only has an effect on iOS and MacOS!** /// If this is `true`, an ephemeral web browser session /// will be used where possible (`prefersEphemeralWebBrowserSession`). @@ -104,34 +135,7 @@ class FlutterWebAuth2Options { /// described in https://github.com/ThexXTURBOXx/flutter_web_auth_2/issues/25 final bool useWebview; - const FlutterWebAuth2Options({ - bool? preferEphemeral, - this.debugOrigin, - int? intentFlags, - this.windowName, - int? timeout, - String? landingPageHtml, - bool? silentAuth, - bool? useWebview, - }) : preferEphemeral = preferEphemeral ?? false, - intentFlags = intentFlags ?? defaultIntentFlags, - timeout = timeout ?? 5 * 60, - landingPageHtml = landingPageHtml ?? _defaultLandingPage, - silentAuth = silentAuth ?? false, - useWebview = useWebview ?? true; - - FlutterWebAuth2Options.fromJson(Map json) - : this( - preferEphemeral: json['preferEphemeral'], - debugOrigin: json['debugOrigin'], - intentFlags: json['intentFlags'], - windowName: json['windowName'], - timeout: json['timeout'], - landingPageHtml: json['landingPageHtml'], - silentAuth: json['silentAuth'], - useWebview: json['useWebview'], - ); - + /// Convert this instance to JSON format. Map toJson() => { 'preferEphemeral': preferEphemeral, 'debugOrigin': debugOrigin, diff --git a/flutter_web_auth_2/lib/src/server.dart b/flutter_web_auth_2/lib/src/server.dart index 632bb87..012c10d 100644 --- a/flutter_web_auth_2/lib/src/server.dart +++ b/flutter_web_auth_2/lib/src/server.dart @@ -13,11 +13,6 @@ class FlutterWebAuth2ServerPlugin extends FlutterWebAuth2Platform { HttpServer? _server; Timer? _authTimeout; - /// Registers the internal server implementation. - static void registerWith() { - FlutterWebAuth2Platform.instance = FlutterWebAuth2ServerPlugin(); - } - @override Future authenticate({ required String url, diff --git a/flutter_web_auth_2/lib/src/web.dart b/flutter_web_auth_2/lib/src/web.dart index a4f23b1..0fc4d78 100644 --- a/flutter_web_auth_2/lib/src/web.dart +++ b/flutter_web_auth_2/lib/src/web.dart @@ -8,7 +8,10 @@ import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platfor import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:url_launcher/url_launcher.dart'; +/// Implements the plugin interface using iframes (for silent authentication) +/// and through an event listener system (for usual authentication). class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { + /// Registers the web implementation. static void registerWith(Registrar registrar) { final channel = MethodChannel( 'flutter_web_auth_2', @@ -16,11 +19,11 @@ class FlutterWebAuth2WebPlugin extends FlutterWebAuth2Platform { registrar, ); final instance = FlutterWebAuth2WebPlugin(); - channel.setMethodCallHandler(instance.handleMethodCall); + channel.setMethodCallHandler(instance._handleMethodCall); FlutterWebAuth2Platform.instance = instance; } - Future handleMethodCall(MethodCall call) async { + Future _handleMethodCall(MethodCall call) async { switch (call.method) { case 'authenticate': final url = call.arguments['url'].toString(); diff --git a/flutter_web_auth_2/lib/src/webview.dart b/flutter_web_auth_2/lib/src/webview.dart index dad99a5..317558c 100644 --- a/flutter_web_auth_2/lib/src/webview.dart +++ b/flutter_web_auth_2/lib/src/webview.dart @@ -8,13 +8,8 @@ import 'package:path_provider/path_provider.dart'; /// Implements the plugin interface using the Webview interface (currently used /// by Windows and Linux). class FlutterWebAuth2WebViewPlugin extends FlutterWebAuth2Platform { - bool authenticated = false; - Webview? webview; - - /// Registers the Webview implementation. - static void registerWith() { - FlutterWebAuth2Platform.instance = FlutterWebAuth2WebViewPlugin(); - } + bool _authenticated = false; + Webview? _webview; @override Future authenticate({ @@ -26,12 +21,12 @@ class FlutterWebAuth2WebViewPlugin extends FlutterWebAuth2Platform { // Microsoft's WebView2 must be installed for this to work throw StateError('Webview is not available'); } - //Reset - authenticated = false; - webview?.close(); + // Reset + _authenticated = false; + _webview?.close(); final c = Completer(); - webview = await WebviewWindow.create( + _webview = await WebviewWindow.create( configuration: CreateConfiguration( windowHeight: 720, windowWidth: 1280, @@ -40,28 +35,28 @@ class FlutterWebAuth2WebViewPlugin extends FlutterWebAuth2Platform { userDataFolderWindows: (await getTemporaryDirectory()).path, ), ); - webview!.addOnUrlRequestCallback((url) { + _webview!.addOnUrlRequestCallback((url) { final uri = Uri.parse(url); if (uri.scheme == callbackUrlScheme) { - authenticated = true; - webview?.close(); + _authenticated = true; + _webview?.close(); /** * Not setting the webview to null will cause a crash if the * application tries to open another webview */ - webview = null; + _webview = null; c.complete(url); } }); unawaited( - webview!.onClose.whenComplete( + _webview!.onClose.whenComplete( () { /** * Not setting the webview to null will cause a crash if the * application tries to open another webview */ - webview = null; - if (!authenticated) { + _webview = null; + if (!_authenticated) { c.completeError( PlatformException(code: 'CANCELED', message: 'User canceled'), ); @@ -69,7 +64,7 @@ class FlutterWebAuth2WebViewPlugin extends FlutterWebAuth2Platform { }, ), ); - webview!.launch(url); + _webview!.launch(url); return c.future; } } diff --git a/flutter_web_auth_2_platform_interface/analysis_options.yaml b/flutter_web_auth_2_platform_interface/analysis_options.yaml index 409daf3..024532d 100644 --- a/flutter_web_auth_2_platform_interface/analysis_options.yaml +++ b/flutter_web_auth_2_platform_interface/analysis_options.yaml @@ -42,15 +42,14 @@ linter: # `// ignore_for_file: name_of_lint` syntax on the line or in the file # producing the lint. rules: - # avoid_print: false # Uncomment to disable the `avoid_print` rule - # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule - always_declare_return_types - always_put_control_body_on_new_line - # - always_put_required_named_parameters_first + - always_put_required_named_parameters_first # - always_specify_types - always_use_package_imports - annotate_overrides - # - avoid_annotating_with_dynamic + - annotate_redeclares + - avoid_annotating_with_dynamic - avoid_bool_literals_in_conditional_expressions # - avoid_catches_without_on_clauses - avoid_catching_errors @@ -61,8 +60,9 @@ linter: - avoid_equals_and_hash_code_on_mutable_classes - avoid_escaping_inner_quotes - avoid_field_initializers_in_const_classes + - avoid_final_parameters - avoid_function_literals_in_foreach_calls - # - avoid_implementing_value_types + - avoid_implementing_value_types - avoid_init_to_null - avoid_js_rounded_ints - avoid_multiple_declarations_per_line @@ -91,18 +91,23 @@ linter: - camel_case_extensions - camel_case_types - cancel_subscriptions - # - cascade_invocations + - cascade_invocations - cast_nullable_to_non_nullable - close_sinks - collection_methods_unrelated_type + - combinators_ordering - comment_references + - conditional_uri_does_not_exist - constant_identifier_names - control_flow_in_finally - curly_braces_in_flow_control_structures + - dangling_library_doc_comments - depend_on_referenced_packages - deprecated_consistency - # - diagnostic_describe_all_properties + - deprecated_member_use_from_same_package + - diagnostic_describe_all_properties - directives_ordering + - discarded_futures - do_not_use_environment - empty_catches - empty_constructor_bodies @@ -113,21 +118,29 @@ linter: - flutter_style_todos - hash_and_equals - implementation_imports + - implicit_call_tearoffs + - implicit_reopen + - invalid_case_patterns - join_return_with_assignment - leading_newlines_in_multiline_strings + - library_annotations - library_names - library_prefixes - library_private_types_in_public_api - lines_longer_than_80_chars - literal_only_boolean_expressions + - matching_super_parameters - missing_whitespace_between_adjacent_strings - no_adjacent_strings_in_list - # - no_default_cases + - no_default_cases - no_duplicate_case_values - no_leading_underscores_for_library_prefixes - no_leading_underscores_for_local_identifiers + - no_literal_bool_comparisons - no_logic_in_create_state - no_runtimeType_toString + - no_self_assignments + - no_wildcard_variable_uses - non_constant_identifier_names - noop_primitive_operations - null_check_on_nullable_type_parameter @@ -180,13 +193,15 @@ linter: - prefer_typing_uninitialized_variables - prefer_void_to_null - provide_deprecation_message - # - public_member_api_docs + - public_member_api_docs - recursive_getters - require_trailing_commas + - secure_pubspec_urls - sized_box_for_whitespace + - sized_box_shrink_expand - slash_for_doc_comments - sort_child_properties_last - # - sort_constructors_first + - sort_constructors_first - sort_pub_dependencies - sort_unnamed_constructors_first - test_types_in_equals @@ -194,17 +209,21 @@ linter: - tighten_type_of_initializing_formals - type_annotate_public_apis - type_init_formals + - type_literal_in_constant_pattern - unawaited_futures - unnecessary_await_in_return - unnecessary_brace_in_string_interps + - unnecessary_breaks - unnecessary_const - unnecessary_constructor_name # - unnecessary_final - unnecessary_getters_setters - unnecessary_lambdas - unnecessary_late + - unnecessary_library_directive - unnecessary_new - unnecessary_null_aware_assignments + - unnecessary_null_aware_operator_on_extension_on_nullable - unnecessary_null_checks - unnecessary_null_in_if_null_operators - unnecessary_nullable_for_final_variable_declarations @@ -215,9 +234,14 @@ linter: - unnecessary_string_escapes - unnecessary_string_interpolations - unnecessary_this + - unnecessary_to_list_in_spreads + - unreachable_from_main - unrelated_type_equality_checks - unsafe_html - use_build_context_synchronously + - use_colored_box + - use_decorated_box + - use_enums - use_full_hex_values_for_flutter_colors - use_function_type_syntax_for_parameters - use_if_null_to_convert_nulls_to_bools @@ -229,6 +253,8 @@ linter: - use_rethrow_when_possible - use_setters_to_change_properties - use_string_buffers + - use_string_in_part_of_directives + - use_super_parameters - use_test_throws_matchers - use_to_and_as_if_applicable - valid_regexps diff --git a/flutter_web_auth_2_platform_interface/lib/flutter_web_auth_2_platform_interface.dart b/flutter_web_auth_2_platform_interface/lib/flutter_web_auth_2_platform_interface.dart index 357a7c7..febfd45 100644 --- a/flutter_web_auth_2_platform_interface/lib/flutter_web_auth_2_platform_interface.dart +++ b/flutter_web_auth_2_platform_interface/lib/flutter_web_auth_2_platform_interface.dart @@ -8,12 +8,14 @@ import 'package:plugin_platform_interface/plugin_platform_interface.dart'; /// changes. Extending this class (using `extends`) ensures that the subclass /// will get the default implementation. abstract class FlutterWebAuth2Platform extends PlatformInterface { + /// Construct a platform instance. FlutterWebAuth2Platform() : super(token: _token); static final Object _token = Object(); static FlutterWebAuth2Platform _instance = FlutterWebAuth2MethodChannel(); + /// Get the currently used platform instance. static FlutterWebAuth2Platform get instance => _instance; /// Platform-specific plugins should set this with their own platform-specific @@ -24,6 +26,19 @@ abstract class FlutterWebAuth2Platform extends PlatformInterface { _instance = instance; } + /// Ask the user to authenticate to the specified web service. + /// + /// The page pointed to by [url] will be loaded and displayed to the user. + /// From the page, the user can authenticate herself and grant access to the + /// app. On completion, the service will send a callback URL with an + /// authentication token, and this URL will be result of the returned + /// [Future]. + /// + /// [callbackUrlScheme] should be a string specifying the scheme of the URL + /// that the page will redirect to upon successful authentication. + /// + /// [options] can be used to specify either both general and + /// platform-specific settings. It should be JSON-formatted. Future authenticate({ required String url, required String callbackUrlScheme, @@ -35,5 +50,9 @@ abstract class FlutterWebAuth2Platform extends PlatformInterface { options: options, ); + /// The plugin may need to store the resulting callbacks in order to pass + /// the result back to the caller of `authenticate`. But if that result never + /// comes the callback will dangle around forever. This can be called to + /// terminate all `authenticate` calls with an error. Future clearAllDanglingCalls() => _instance.clearAllDanglingCalls(); } diff --git a/flutter_web_auth_2_platform_interface/lib/method_channel/method_channel.dart b/flutter_web_auth_2_platform_interface/lib/method_channel/method_channel.dart index c6dd97a..2958af3 100644 --- a/flutter_web_auth_2_platform_interface/lib/method_channel/method_channel.dart +++ b/flutter_web_auth_2_platform_interface/lib/method_channel/method_channel.dart @@ -3,7 +3,7 @@ import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platfor /// Method channel implementation of the [FlutterWebAuth2Platform]. class FlutterWebAuth2MethodChannel extends FlutterWebAuth2Platform { - static const MethodChannel channel = MethodChannel('flutter_web_auth_2'); + static const MethodChannel _channel = MethodChannel('flutter_web_auth_2'); @override Future authenticate({ @@ -11,7 +11,7 @@ class FlutterWebAuth2MethodChannel extends FlutterWebAuth2Platform { required String callbackUrlScheme, required Map options, }) async => - await channel.invokeMethod('authenticate', { + await _channel.invokeMethod('authenticate', { 'url': url, 'callbackUrlScheme': callbackUrlScheme, 'options': options, @@ -20,5 +20,5 @@ class FlutterWebAuth2MethodChannel extends FlutterWebAuth2Platform { @override Future clearAllDanglingCalls() async => - channel.invokeMethod('cleanUpDanglingCalls'); + _channel.invokeMethod('cleanUpDanglingCalls'); }