Skip to content

Commit

Permalink
fix: ci build (#35)
Browse files Browse the repository at this point in the history
* fix: remove unnecessary source file

* fix: missing plugin references in library import file

* fix: formatting in config definition

* fix: prevent null response in method channel

* fix: downgrade flutter_inappwebview to version 5.8.0

* fix: test imported removed file

* fix: update readme to reflect recent changes to the sdk

* fix: typo in readme
  • Loading branch information
fischermario authored Oct 18, 2023
1 parent 25a02f9 commit 09a3329
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 105 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<String> 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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/casdoor_flutter_sdk.dart
Original file line number Diff line number Diff line change
@@ -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';
14 changes: 0 additions & 14 deletions lib/src/casdoor_flutter_sdk.dart

This file was deleted.

15 changes: 8 additions & 7 deletions lib/src/casdoor_flutter_sdk_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions lib/src/casdoor_flutter_sdk_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class MethodChannelCasdoorFlutterSdk extends CasdoorFlutterSdkPlatform {
@override
Future<bool> clearCache() async {
return await _channel
.invokeMethod('clearCache')
.catchError((err) => throw (Exception(err))) as bool;
.invokeMethod<bool>('clearCache')
.catchError((err) => throw (Exception(err))) ??
false;
}

@override
Future<String> authenticate(CasdoorSdkParams params) async {
return await _channel.invokeMethod('authenticate', <String, dynamic>{
'params': params,
}).catchError((err) => throw (Exception(err))) as String;
return await _channel
.invokeMethod<String>('authenticate', <String, dynamic>{
'params': params,
}).catchError((err) => throw (Exception(err))) ??
'';
}

@override
Expand Down
93 changes: 30 additions & 63 deletions lib/src/casdoor_flutter_sdk_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class InAppAuthBrowser extends InAppBrowser {
}) : super(windowId: windowId, initialUserScripts: initialUserScripts);

Function? onExitCallback;
Future<NavigationActionPolicy> Function(WebUri? url)?
Future<NavigationActionPolicy> Function(Uri? url)?
onShouldOverrideUrlLoadingCallback;

void setOnExitCallback(Function cb) => (onExitCallback = cb);

void setOnShouldOverrideUrlLoadingCallback(
Future<NavigationActionPolicy> Function(WebUri? url) cb) =>
Future<NavigationActionPolicy> Function(Uri? url) cb) =>
onShouldOverrideUrlLoadingCallback = cb;

@override
Expand Down Expand Up @@ -76,16 +76,19 @@ class _FullScreenAuthPageState extends State<FullScreenAuthPage> {
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;
}

Expand Down Expand Up @@ -138,7 +141,7 @@ class _FullScreenAuthPageState extends State<FullScreenAuthPage> {

@override
Widget build(BuildContext context) {
WebView.debugLoggingSettings.enabled = false;
//WebView.debugLoggingSettings.enabled = false;
return (widget.params.isMaterialStyle ?? true)
? materialAuthWidget(context)
: cupertinoAuthWidget(context);
Expand All @@ -150,9 +153,6 @@ class _FullScreenAuthPageState extends State<FullScreenAuthPage> {
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]
Expand All @@ -176,7 +176,6 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform {
),
);

//debugPrint('Result: $result');
if (result is String) {
return result;
}
Expand All @@ -186,6 +185,7 @@ class CasdoorFlutterSdkMobile extends CasdoorFlutterSdkPlatform {

Future<String> _inAppBrowserAuth(CasdoorSdkParams params) async {
final Completer<String> isFinished = Completer<String>();
final InAppAuthBrowser browser = InAppAuthBrowser();

browser.setOnExitCallback(() {
if (!isFinished.isCompleted) {
Expand All @@ -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;
}
Expand All @@ -205,61 +205,31 @@ 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,
),
),
);

return isFinished.future;
}

Future<String> _webAuthSession(CasdoorSdkParams params) async {
if ((session == null) && (await WebAuthenticationSession.isAvailable())) {
bool hasStarted = false;
final Completer<String> isFinished = Completer<String>();

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<String> authenticate(CasdoorSdkParams params) async {
final CasdoorSdkParams newParams =
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -51,7 +52,7 @@ flutter:
linux:
dartPluginClass: CasdoorFlutterSdkDesktop
macos:
dartPluginClass: CasdoorFlutterSdkMobile
dartPluginClass: CasdoorFlutterSdkDesktop
windows:
dartPluginClass: CasdoorFlutterSdkDesktop
web:
Expand Down
4 changes: 1 addition & 3 deletions test/casdoor_flutter_sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
});
}

0 comments on commit 09a3329

Please sign in to comment.