diff --git a/README.md b/README.md index 18f592e..5f3c017 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Initialization requires 6 parameters, which are all str type: Set the callbackuri parameter by judging different platforms ``` - final platform = await CasdoorFlutterSdkPlatform.instance.getPlatformVersion() ?? ""; + final platform = await CasdoorFlutterSdkPlatform.getPlatformVersion(); String callbackUri; if (platform == "web") { callbackUri = "${_config.redirectUri}.html"; @@ -109,21 +109,25 @@ Notes for different platforms: ## Android and iOS -Please check the [documentation](https://inappwebview.dev/docs/intro) of the InAppWebView package for more details. +Please check the [documentation](https://inappwebview.dev/docs/intro) of the InAppWebView package for more details about setting up the project. -## Android +## Linux and macOS -Increase the SDK version in `android/app/build.gradle` to 34: +Add the package `desktop_webview_window: ^0.2.3` inside *dependencies* to your *pubspec.yaml* file. + +Modify your *main* function to look like the following: ``` -... -android { - compileSdkVersion 34 -... +void main(List args) async { + WidgetsFlutterBinding.ensureInitialized(); + if (runWebViewTitleBarWidget(args)) { + return; + } + // your code goes here ... + runApp(const MyApp()); +} ``` -## Windows and Linux - Please check the [documentation](https://pub.dev/packages/desktop_webview_window) of the desktop_webview_window package for more details. ## Web @@ -216,6 +220,20 @@ isTokenExpired() isNonce() ``` +# Caveats + +## Windows + +There is a known bug in the desktop_webview_window package that causes random crashes of the browser window (see [issue](https://github.com/MixinNetwork/flutter-plugins/issues/283)). + +## Linux (Ubuntu) + +Do not install Flutter or Visual Studio Code using Snap as this will prevent the code from building or running successfully. You need to install the Flutter and Visual Studio Code packages manually. + +## macOS + +There are instances where JavaScript is not working inside WKWebView. Please report any bugs that may occur. + # Example See at: https://github.com/casdoor/casdoor-flutter-example diff --git a/lib/casdoor_flutter_sdk.dart b/lib/casdoor_flutter_sdk.dart index d6ad608..aec3bcf 100644 --- a/lib/casdoor_flutter_sdk.dart +++ b/lib/casdoor_flutter_sdk.dart @@ -1,4 +1,6 @@ export 'src/casdoor.dart'; export 'src/casdoor_flutter_sdk_config.dart'; +export 'src/casdoor_flutter_sdk_desktop.dart'; export 'src/casdoor_flutter_sdk_exceptions.dart'; +export 'src/casdoor_flutter_sdk_mobile.dart'; export 'src/casdoor_flutter_sdk_platform_interface.dart'; diff --git a/lib/src/casdoor_flutter_sdk.dart b/lib/src/casdoor_flutter_sdk.dart deleted file mode 100644 index b5b7d35..0000000 --- a/lib/src/casdoor_flutter_sdk.dart +++ /dev/null @@ -1,14 +0,0 @@ -// You have generated a new plugin project without specifying the `--platforms` -// flag. A plugin project with no platform support was generated. To add a -// platform, run `flutter create -t plugin --platforms .` under the -// same directory. You can also find a detailed instruction on how to add -// platforms in the `pubspec.yaml` at -// https://flutter.dev/docs/development/packages-and-plugins/developing-packages#plugin-platforms. - -import 'casdoor_flutter_sdk_platform_interface.dart'; - -class CasdoorFlutterSdk { - Future getPlatformVersion() { - return CasdoorFlutterSdkPlatform().getPlatformVersion(); - } -} diff --git a/lib/src/casdoor_flutter_sdk_config.dart b/lib/src/casdoor_flutter_sdk_config.dart index 342a404..3c89ea7 100644 --- a/lib/src/casdoor_flutter_sdk_config.dart +++ b/lib/src/casdoor_flutter_sdk_config.dart @@ -22,13 +22,14 @@ class AuthConfig { final String callbackUrlScheme; final String appName; - AuthConfig( - {required this.clientId, - required this.serverUrl, - required this.organizationName, - required this.appName, - this.redirectUri = "casdoor://callback", - this.callbackUrlScheme = "casdoor"}); + AuthConfig({ + required this.clientId, + required this.serverUrl, + required this.organizationName, + required this.appName, + this.redirectUri = 'casdoor://callback', + this.callbackUrlScheme = 'casdoor', + }); } class CasdoorSdkParams { diff --git a/lib/src/casdoor_flutter_sdk_method_channel.dart b/lib/src/casdoor_flutter_sdk_method_channel.dart index 02df5de..ee39552 100644 --- a/lib/src/casdoor_flutter_sdk_method_channel.dart +++ b/lib/src/casdoor_flutter_sdk_method_channel.dart @@ -24,15 +24,18 @@ class MethodChannelCasdoorFlutterSdk extends CasdoorFlutterSdkPlatform { @override Future clearCache() async { return await _channel - .invokeMethod('clearCache') - .catchError((err) => throw (Exception(err))) as bool; + .invokeMethod('clearCache') + .catchError((err) => throw (Exception(err))) ?? + false; } @override Future authenticate(CasdoorSdkParams params) async { - return await _channel.invokeMethod('authenticate', { - 'params': params, - }).catchError((err) => throw (Exception(err))) as String; + return await _channel + .invokeMethod('authenticate', { + 'params': params, + }).catchError((err) => throw (Exception(err))) ?? + ''; } @override diff --git a/lib/src/casdoor_flutter_sdk_mobile.dart b/lib/src/casdoor_flutter_sdk_mobile.dart index 855a2fd..b92f3a4 100644 --- a/lib/src/casdoor_flutter_sdk_mobile.dart +++ b/lib/src/casdoor_flutter_sdk_mobile.dart @@ -28,13 +28,13 @@ class InAppAuthBrowser extends InAppBrowser { }) : super(windowId: windowId, initialUserScripts: initialUserScripts); Function? onExitCallback; - Future Function(WebUri? url)? + Future Function(Uri? url)? onShouldOverrideUrlLoadingCallback; void setOnExitCallback(Function cb) => (onExitCallback = cb); void setOnShouldOverrideUrlLoadingCallback( - Future Function(WebUri? url) cb) => + Future Function(Uri? url) cb) => onShouldOverrideUrlLoadingCallback = cb; @override @@ -76,16 +76,19 @@ class _FullScreenAuthPageState extends State { return Stack( children: [ InAppWebView( - initialUrlRequest: URLRequest(url: WebUri(widget.params.url)), - initialSettings: InAppWebViewSettings( - clearCache: widget.params.clearCache, - clearSessionCache: widget.params.clearCache, + initialUrlRequest: URLRequest(url: Uri.parse(widget.params.url)), + initialOptions: InAppWebViewGroupOptions( + crossPlatform: InAppWebViewOptions( + clearCache: widget.params.clearCache!, + useShouldOverrideUrlLoading: true, + useOnLoadResource: true, + ), ), shouldOverrideUrlLoading: (controller, navigationAction) async { final uri = navigationAction.request.url!; if (uri.scheme == widget.params.callbackUrlScheme) { - Navigator.pop(ctx, uri.rawValue); + Navigator.pop(ctx, uri.toString()); return NavigationActionPolicy.CANCEL; } @@ -138,7 +141,7 @@ class _FullScreenAuthPageState extends State { @override Widget build(BuildContext context) { - WebView.debugLoggingSettings.enabled = false; + //WebView.debugLoggingSettings.enabled = false; return (widget.params.isMaterialStyle ?? true) ? materialAuthWidget(context) : cupertinoAuthWidget(context); @@ -150,9 +153,6 @@ class _FullScreenAuthPageState extends State { class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { CasdoorFlutterSdkMobile() : super.create(); - // FixMe: session to find out active browser - WebAuthenticationSession? session; - final InAppAuthBrowser browser = InAppAuthBrowser(); bool willClearCache = false; /// Registers this class as the default instance of [PathProviderPlatform] @@ -176,7 +176,6 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { ), ); - //debugPrint('Result: $result'); if (result is String) { return result; } @@ -186,6 +185,7 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { Future _inAppBrowserAuth(CasdoorSdkParams params) async { final Completer isFinished = Completer(); + final InAppAuthBrowser browser = InAppAuthBrowser(); browser.setOnExitCallback(() { if (!isFinished.isCompleted) { @@ -196,7 +196,7 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { browser.setOnShouldOverrideUrlLoadingCallback((returnUrl) async { if (returnUrl != null) { if (returnUrl.scheme == params.callbackUrlScheme) { - isFinished.complete(returnUrl.rawValue); + isFinished.complete(returnUrl.toString()); browser.close(); return NavigationActionPolicy.CANCEL; } @@ -205,18 +205,24 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { }); await browser.openUrlRequest( - urlRequest: URLRequest(url: WebUri(params.url)), - settings: InAppBrowserClassSettings( - browserSettings: InAppBrowserSettings( + urlRequest: URLRequest(url: Uri.parse(params.url)), + options: InAppBrowserClassOptions( + inAppWebViewGroupOptions: InAppWebViewGroupOptions( + crossPlatform: InAppWebViewOptions( + clearCache: params.clearCache!, + useOnLoadResource: true, + useShouldOverrideUrlLoading: true, + ), + ), + crossPlatform: InAppBrowserOptions( hideUrlBar: true, - hideDefaultMenuItems: true, + toolbarTopBackgroundColor: Colors.grey.shade300, + ), + android: AndroidInAppBrowserOptions( toolbarTopFixedTitle: 'Login', ), - webViewSettings: InAppWebViewSettings( - useShouldOverrideUrlLoading: true, - useOnLoadResource: true, - clearCache: params.clearCache, - clearSessionCache: params.clearCache, + ios: IOSInAppBrowserOptions( + hideToolbarBottom: true, ), ), ); @@ -224,42 +230,6 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { return isFinished.future; } - Future _webAuthSession(CasdoorSdkParams params) async { - if ((session == null) && (await WebAuthenticationSession.isAvailable())) { - bool hasStarted = false; - final Completer isFinished = Completer(); - - session = await WebAuthenticationSession.create( - url: WebUri(params.url), - callbackURLScheme: params.callbackUrlScheme, - initialSettings: WebAuthenticationSessionSettings( - prefersEphemeralWebBrowserSession: params.preferEphemeral, - ), - onComplete: (returnUrl, error) async { - if (returnUrl != null) { - isFinished.complete(returnUrl.rawValue); - } - await session?.dispose(); - session = null; - if (!isFinished.isCompleted) { - isFinished.completeError(CasdoorAuthCancelledException); - } - }, - ); - - if (await session?.canStart() ?? false) { - hasStarted = await session?.start() ?? false; - } - if (!hasStarted) { - throw CasdoorMobileWebAuthSessionFailedException; - } - - return isFinished.future; - } else { - throw CasdoorMobileWebAuthSessionNotAvailableException; - } - } - @override Future authenticate(CasdoorSdkParams params) async { final CasdoorSdkParams newParams = @@ -273,12 +243,9 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform { .contains(defaultTargetPlatform)) && (params.showFullscreen == true)) { return _fullScreenAuth(newParams); - } else if ((defaultTargetPlatform == TargetPlatform.android) && - (params.showFullscreen != true)) { - return _inAppBrowserAuth(newParams); - } else { - return _webAuthSession(newParams); } + + return _inAppBrowserAuth(newParams); } @override diff --git a/pubspec.yaml b/pubspec.yaml index 62c6e92..d97d736 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,8 +17,8 @@ dependencies: path: ^1.8.2 path_provider: ^2.1.1 desktop_webview_window: ^0.2.3 - flutter_inappwebview: ^6.0.0-beta.25 - http: ^0.13.4 + flutter_inappwebview: ^5.8.0 + http: '>=0.13.4 <1.0.0' crypto: ^3.0.2 jwt_decoder: ^2.0.1 @@ -43,6 +43,7 @@ flutter: # All these are used by the tooling to maintain consistency when # adding or updating assets for this project. plugin: + implements: casdoor_flutter_sdk platforms: android: dartPluginClass: CasdoorFlutterSdkMobile @@ -51,7 +52,7 @@ flutter: linux: dartPluginClass: CasdoorFlutterSdkDesktop macos: - dartPluginClass: CasdoorFlutterSdkMobile + dartPluginClass: CasdoorFlutterSdkDesktop windows: dartPluginClass: CasdoorFlutterSdkDesktop web: diff --git a/test/casdoor_flutter_sdk_test.dart b/test/casdoor_flutter_sdk_test.dart index efeec90..551501d 100644 --- a/test/casdoor_flutter_sdk_test.dart +++ b/test/casdoor_flutter_sdk_test.dart @@ -13,7 +13,6 @@ // limitations under the License. import 'package:casdoor_flutter_sdk/casdoor_flutter_sdk.dart'; -import 'package:casdoor_flutter_sdk/src/casdoor_flutter_sdk.dart'; import 'package:casdoor_flutter_sdk/src/casdoor_flutter_sdk_method_channel.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; @@ -45,11 +44,10 @@ void main() { }); test('getPlatformVersion', () async { - final CasdoorFlutterSdk casdoorFlutterSdkPlugin = CasdoorFlutterSdk(); final MockCasdoorFlutterSdkPlatform fakePlatform = MockCasdoorFlutterSdkPlatform(); CasdoorFlutterSdkPlatform.instance = fakePlatform; - expect(await casdoorFlutterSdkPlugin.getPlatformVersion(), '42'); + expect(await CasdoorFlutterSdkPlatform().getPlatformVersion(), '42'); }); }