From 0413983f056dbfb4af490668ed4d9e8251dbd651 Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Wed, 10 Jan 2024 23:17:25 +0100 Subject: [PATCH 1/9] Add windows support for custom schemes --- flutter_web_auth_2/example/lib/main.dart | 23 +++++++---- flutter_web_auth_2/example/pubspec.yaml | 3 +- .../lib/flutter_web_auth_2.dart | 1 + flutter_web_auth_2/lib/src/windows.dart | 40 +++++++++++++++++++ flutter_web_auth_2/pubspec.yaml | 5 ++- 5 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 flutter_web_auth_2/lib/src/windows.dart diff --git a/flutter_web_auth_2/example/lib/main.dart b/flutter_web_auth_2/example/lib/main.dart index 2c3e6b6..305dc1a 100644 --- a/flutter_web_auth_2/example/lib/main.dart +++ b/flutter_web_auth_2/example/lib/main.dart @@ -1,12 +1,14 @@ import 'dart:async'; import 'dart:io' show HttpServer, Platform; +import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:flutter/foundation.dart'; 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 = + ''' @@ -63,7 +65,12 @@ const html = ''' '''; -void main() => runApp(const MyApp()); +void main(List args) { + if (runWebViewTitleBarWidget(args)) { + return; + } + runApp(const MyApp()); +} class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @@ -95,7 +102,7 @@ class MyAppState extends State { // Windows needs some callback URL on localhost req.response.write( - (Platform.isWindows || Platform.isLinux) + (Platform.isLinux) ? html.replaceFirst( 'CALLBACK_URL_HERE', 'http://localhost:43824/success?code=1337', @@ -110,7 +117,7 @@ class MyAppState extends State { }); } - Future authenticate() async { + Future authenticate(BuildContext context) async { setState(() { _status = ''; }); @@ -122,9 +129,7 @@ class MyAppState extends State { // Windows needs some callback URL on localhost final callbackUrlScheme = - !kIsWeb && (Platform.isWindows || Platform.isLinux) - ? 'http://localhost:43824' - : 'foobar'; + !kIsWeb && (Platform.isLinux) ? 'http://localhost:43824' : 'foobar'; try { final result = await FlutterWebAuth2.authenticate( @@ -157,7 +162,9 @@ class MyAppState extends State { Text('Status: $_status\n'), const SizedBox(height: 80), ElevatedButton( - onPressed: authenticate, + onPressed: () async { + await authenticate(context); + }, child: const Text('Authenticate'), ), ], diff --git a/flutter_web_auth_2/example/pubspec.yaml b/flutter_web_auth_2/example/pubspec.yaml index 283ecb6..f0b2263 100644 --- a/flutter_web_auth_2/example/pubspec.yaml +++ b/flutter_web_auth_2/example/pubspec.yaml @@ -10,7 +10,8 @@ dependencies: cupertino_icons: ^1.0.3 flutter: sdk: flutter - flutter_web_auth_2: ^3.0.0 + flutter_web_auth_2: + path: ../../flutter_web_auth_2 dev_dependencies: flutter_lints: ^3.0.0 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 1867d7d..c9084b9 100644 --- a/flutter_web_auth_2/lib/flutter_web_auth_2.dart +++ b/flutter_web_auth_2/lib/flutter_web_auth_2.dart @@ -10,6 +10,7 @@ export 'src/options.dart'; export 'src/unsupported.dart' if (dart.library.io) 'src/server.dart' if (dart.library.html) 'src/web.dart'; +export 'src/windows.dart'; class _OnAppLifecycleResumeObserver extends WidgetsBindingObserver { final Function onResumed; diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart new file mode 100644 index 0000000..d0e1ee9 --- /dev/null +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -0,0 +1,40 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:desktop_webview_window/desktop_webview_window.dart'; +import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; + +class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { + static void registerWith() { + FlutterWebAuth2Platform.instance = FlutterWebAuth2WindowsPlugin(); + } + + @override + Future authenticate({ + required String url, + required String callbackUrlScheme, + required Map options, + }) async { + if (!await WebviewWindow.isWebviewAvailable()) { + throw StateError('Webview is not available'); + } + final c = Completer(); + final webview = await WebviewWindow.create( + configuration: CreateConfiguration( + windowHeight: 720, + windowWidth: 1280, + title: 'easyroam authentication', + titleBarTopPadding: Platform.isMacOS ? 20 : 0, + ), + ); + webview.addOnUrlRequestCallback((url) { + final uri = Uri.parse(url); + if (uri.scheme == callbackUrlScheme) { + c.complete(url); + webview.close(); + } + }); + webview.launch(url); + return c.future; + } +} diff --git a/flutter_web_auth_2/pubspec.yaml b/flutter_web_auth_2/pubspec.yaml index 1e0bd44..6a7f094 100644 --- a/flutter_web_auth_2/pubspec.yaml +++ b/flutter_web_auth_2/pubspec.yaml @@ -24,6 +24,7 @@ environment: flutter: ">=3.0.0" dependencies: + desktop_webview_window: ^0.2.3 flutter: sdk: flutter flutter_web_auth_2_platform_interface: ^3.0.0 @@ -54,5 +55,5 @@ flutter: pluginClass: FlutterWebAuth2WebPlugin fileName: src/web.dart windows: - dartPluginClass: FlutterWebAuth2ServerPlugin - fileName: src/server.dart + dartPluginClass: FlutterWebAuth2WindowsPlugin + fileName: src/windows.dart From c74f56e2db01c5f88f61642e3430b718bce645b0 Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Wed, 10 Jan 2024 23:45:11 +0100 Subject: [PATCH 2/9] Small changes --- flutter_web_auth_2/example/pubspec.yaml | 1 + flutter_web_auth_2/lib/src/windows.dart | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/flutter_web_auth_2/example/pubspec.yaml b/flutter_web_auth_2/example/pubspec.yaml index f0b2263..a90b9b3 100644 --- a/flutter_web_auth_2/example/pubspec.yaml +++ b/flutter_web_auth_2/example/pubspec.yaml @@ -8,6 +8,7 @@ environment: dependencies: cupertino_icons: ^1.0.3 + desktop_webview_window: ^0.2.3 flutter: sdk: flutter flutter_web_auth_2: diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index d0e1ee9..6a1f486 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -5,6 +5,9 @@ import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { + bool authenticated = false; + Webview? webview; + static void registerWith() { FlutterWebAuth2Platform.instance = FlutterWebAuth2WindowsPlugin(); } @@ -16,25 +19,32 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { required Map options, }) async { if (!await WebviewWindow.isWebviewAvailable()) { + //Microsofts WebView2 must be installed for this to work throw StateError('Webview is not available'); } + //Reset + authenticated = false; + webview?.close(); + final c = Completer(); - final webview = await WebviewWindow.create( + + webview = await WebviewWindow.create( configuration: CreateConfiguration( windowHeight: 720, windowWidth: 1280, - title: 'easyroam authentication', + title: 'Authenticate', titleBarTopPadding: Platform.isMacOS ? 20 : 0, ), ); - webview.addOnUrlRequestCallback((url) { + webview!.addOnUrlRequestCallback((url) { final uri = Uri.parse(url); if (uri.scheme == callbackUrlScheme) { + authenticated = true; c.complete(url); - webview.close(); + webview?.close(); } }); - webview.launch(url); + webview!.launch(url); return c.future; } } From c44ccce9f6fe832a1b02fbccaa6b472968d8421f Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Thu, 11 Jan 2024 00:16:18 +0100 Subject: [PATCH 3/9] add error on cancel --- flutter_web_auth_2/lib/src/windows.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index 6a1f486..951947b 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:desktop_webview_window/desktop_webview_window.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { @@ -44,6 +45,14 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { webview?.close(); } }); + unawaited(webview!.onClose.whenComplete(() => { + if (!authenticated) + { + c.completeError( + PlatformException(code: 'CANCELED', message: 'User canceled'), + ) + }, + })); webview!.launch(url); return c.future; } From c2347d3bc92996c0760f81e7bd6d7a4e6079aaab Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Thu, 11 Jan 2024 00:58:02 +0100 Subject: [PATCH 4/9] Add path_provider dependency to use the tempData folder for the webview --- flutter_web_auth_2/lib/src/windows.dart | 7 ++++++- flutter_web_auth_2/pubspec.yaml | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index 951947b..ddc38e9 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -2,8 +2,10 @@ import 'dart:async'; import 'dart:io'; import 'package:desktop_webview_window/desktop_webview_window.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter/services.dart'; import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; +import 'package:path_provider/path_provider.dart'; class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { bool authenticated = false; @@ -28,13 +30,16 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { webview?.close(); final c = Completer(); - + debugPrint( + '''Launching webview with url: $url, callbackUrlScheme: $callbackUrlScheme, tmpDir: ${(await getTemporaryDirectory()).path}''', + ); webview = await WebviewWindow.create( configuration: CreateConfiguration( windowHeight: 720, windowWidth: 1280, title: 'Authenticate', titleBarTopPadding: Platform.isMacOS ? 20 : 0, + userDataFolderWindows: (await getTemporaryDirectory()).path, ), ); webview!.addOnUrlRequestCallback((url) { diff --git a/flutter_web_auth_2/pubspec.yaml b/flutter_web_auth_2/pubspec.yaml index 6a7f094..658423e 100644 --- a/flutter_web_auth_2/pubspec.yaml +++ b/flutter_web_auth_2/pubspec.yaml @@ -30,6 +30,7 @@ dependencies: flutter_web_auth_2_platform_interface: ^3.0.0 flutter_web_plugins: sdk: flutter + path_provider: ^2.1.2 url_launcher: ^6.1.6 window_to_front: ^0.0.3 From c82d8433751c9db40efc8b5d178518207ba2c7b0 Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Thu, 11 Jan 2024 01:23:31 +0100 Subject: [PATCH 5/9] fix ci? --- flutter_web_auth_2/example/lib/main.dart | 3 +-- flutter_web_auth_2/lib/src/windows.dart | 10 +++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/flutter_web_auth_2/example/lib/main.dart b/flutter_web_auth_2/example/lib/main.dart index 305dc1a..60990ae 100644 --- a/flutter_web_auth_2/example/lib/main.dart +++ b/flutter_web_auth_2/example/lib/main.dart @@ -7,8 +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 = ''' diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index ddc38e9..b12cac4 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -50,14 +50,18 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { webview?.close(); } }); - unawaited(webview!.onClose.whenComplete(() => { + unawaited( + webview!.onClose.whenComplete( + () => { if (!authenticated) { c.completeError( PlatformException(code: 'CANCELED', message: 'User canceled'), - ) + ), }, - })); + }, + ), + ); webview!.launch(url); return c.future; } From c1666e2b30a6b8b11a354412b30c813418430755 Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Thu, 11 Jan 2024 01:30:31 +0100 Subject: [PATCH 6/9] Remove BuildContext from authenticate --- flutter_web_auth_2/example/lib/main.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flutter_web_auth_2/example/lib/main.dart b/flutter_web_auth_2/example/lib/main.dart index 60990ae..0423cc1 100644 --- a/flutter_web_auth_2/example/lib/main.dart +++ b/flutter_web_auth_2/example/lib/main.dart @@ -116,7 +116,7 @@ class MyAppState extends State { }); } - Future authenticate(BuildContext context) async { + Future authenticate() async { setState(() { _status = ''; }); @@ -162,7 +162,7 @@ class MyAppState extends State { const SizedBox(height: 80), ElevatedButton( onPressed: () async { - await authenticate(context); + await authenticate(); }, child: const Text('Authenticate'), ), From 96e804f7216da7fe6bf4325e950f088880ea48b9 Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Sat, 13 Jan 2024 15:26:56 +0100 Subject: [PATCH 7/9] Remove dead code --- flutter_web_auth_2/lib/src/windows.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index b12cac4..89ed29b 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:flutter/cupertino.dart'; @@ -38,7 +37,7 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { windowHeight: 720, windowWidth: 1280, title: 'Authenticate', - titleBarTopPadding: Platform.isMacOS ? 20 : 0, + titleBarTopPadding: 0, userDataFolderWindows: (await getTemporaryDirectory()).path, ), ); From efe8282e2f432a12524ce1544eaf29b6ab097d1d Mon Sep 17 00:00:00 2001 From: Mino5531 Date: Sat, 13 Jan 2024 17:07:41 +0100 Subject: [PATCH 8/9] Revert changes that are not required when using melos --- flutter_web_auth_2/example/pubspec.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flutter_web_auth_2/example/pubspec.yaml b/flutter_web_auth_2/example/pubspec.yaml index a90b9b3..2587630 100644 --- a/flutter_web_auth_2/example/pubspec.yaml +++ b/flutter_web_auth_2/example/pubspec.yaml @@ -11,8 +11,7 @@ dependencies: desktop_webview_window: ^0.2.3 flutter: sdk: flutter - flutter_web_auth_2: - path: ../../flutter_web_auth_2 + flutter_web_auth_2: ^3.0.0 dev_dependencies: flutter_lints: ^3.0.0 From d5c2de044615cfb6b9d5bedab3cdf10cdc48664b Mon Sep 17 00:00:00 2001 From: Long Yang Paffrath Date: Mon, 15 Jan 2024 22:39:07 +0100 Subject: [PATCH 9/9] Fix crash/error when opening another webview after closing one * Fix crash/error when trying to open another window after closing a webview during the same application run --- flutter_web_auth_2/lib/src/windows.dart | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart index 89ed29b..0030ad8 100644 --- a/flutter_web_auth_2/lib/src/windows.dart +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -45,19 +45,28 @@ class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { final uri = Uri.parse(url); if (uri.scheme == callbackUrlScheme) { authenticated = true; - c.complete(url); webview?.close(); + /** + * Not setting the webview to null will cause a crash if the + * application tries to open another webview + */ + webview = null; + c.complete(url); } }); unawaited( webview!.onClose.whenComplete( - () => { - if (!authenticated) - { - c.completeError( - PlatformException(code: 'CANCELED', message: 'User canceled'), - ), - }, + () { + /** + * Not setting the webview to null will cause a crash if the + * application tries to open another webview + */ + webview = null; + if (!authenticated) { + c.completeError( + PlatformException(code: 'CANCELED', message: 'User canceled'), + ); + } }, ), );