From 747d92454edaef6a7dd8533c18c7846f7f122c18 Mon Sep 17 00:00:00 2001 From: primozratej Date: Sun, 11 Jun 2023 18:15:04 +0400 Subject: [PATCH 1/3] Fix external Auth --- lib/pages/web_view.dart | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/pages/web_view.dart b/lib/pages/web_view.dart index 57bf574..f1af618 100644 --- a/lib/pages/web_view.dart +++ b/lib/pages/web_view.dart @@ -75,8 +75,11 @@ class WebViewAppState extends ConsumerState { Future _shouldOverrideUrlLoading(InAppWebViewController controller, NavigationAction action) async { // 1st check if url is not def. app url and open it in a browser or inApp. + final url = action.request.url!.origin; - if (!url.startsWith(manifest.baseUrl)) { + + HumHub instance = await ref.read(humHubProvider).getInstance(); + if (!url.startsWith(manifest.baseUrl) && instance.isHideOpener && whitelistRedirects(url)) { launchUrl(action.request.url!, mode: LaunchMode.externalApplication); return NavigationActionPolicy.CANCEL; } @@ -224,4 +227,17 @@ class WebViewAppState extends ConsumerState { ), ); } + + bool whitelistRedirects(String url) { + for (var element in [ + "https://github.com/login/oauth/authorize", + "https://login.live.com/oauth20_authorize", + "https://www.facebook.com/dialog/oauth", + "https://discord.com/api/oauth2/authorize", + "https://www.linkedin.com/oauth/v2/authorization" + ]) { + if(url.contains(element)) return true; + } + return false; + } } From 083e707e945cc18465f0c6afac23cb2eff563647 Mon Sep 17 00:00:00 2001 From: primozratej Date: Thu, 22 Jun 2023 11:20:13 +0400 Subject: [PATCH 2/3] Config InAppBrowser for External Auth --- lib/components/in_app_browser.dart | 35 +++++++++++++++++++ lib/models/manifest.dart | 7 ++-- lib/pages/web_view.dart | 54 ++++++++++++++++++------------ 3 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 lib/components/in_app_browser.dart diff --git a/lib/components/in_app_browser.dart b/lib/components/in_app_browser.dart new file mode 100644 index 0000000..93ee339 --- /dev/null +++ b/lib/components/in_app_browser.dart @@ -0,0 +1,35 @@ +import 'dart:async'; +import 'dart:developer'; + +import 'package:flutter/material.dart'; +import 'package:flutter_inappwebview/flutter_inappwebview.dart'; +import 'package:humhub/models/manifest.dart'; + +class MyInAppBrowser extends InAppBrowser { + final Manifest manifest; + final InAppBrowserClassOptions options = InAppBrowserClassOptions( + crossPlatform: InAppBrowserOptions(hideUrlBar: false, toolbarTopBackgroundColor: Colors.grey), + inAppWebViewGroupOptions: InAppWebViewGroupOptions( + crossPlatform: InAppWebViewOptions(javaScriptEnabled: true, useShouldOverrideUrlLoading: true), + ), + ); + + final Function concludeAuth; + + MyInAppBrowser({required this.manifest, required this.concludeAuth}); + + @override + Future? shouldOverrideUrlLoading(NavigationAction navigationAction) async { + log("Browser closed!"); + + if (navigationAction.request.url!.origin.startsWith(manifest.baseUrl)) { + concludeAuth(navigationAction.request); + return NavigationActionPolicy.CANCEL; + } + return NavigationActionPolicy.ALLOW; + } + + launchUrl(URLRequest urlRequest) { + openUrlRequest(urlRequest: urlRequest, options: options); + } +} diff --git a/lib/models/manifest.dart b/lib/models/manifest.dart index 6e9c5d1..9c00d4f 100644 --- a/lib/models/manifest.dart +++ b/lib/models/manifest.dart @@ -12,11 +12,8 @@ class Manifest { this.backgroundColor, this.themeColor); String get baseUrl { - int index = startUrl.indexOf("humhub.com"); - if (index != -1) { - return startUrl.substring(0, index + "humhub.com".length); - } - throw Exception("Can't define base url"); + Uri url = Uri.parse(startUrl); + return url.origin; } factory Manifest.fromJson(Map json) { diff --git a/lib/pages/web_view.dart b/lib/pages/web_view.dart index f1af618..ab3b3d0 100644 --- a/lib/pages/web_view.dart +++ b/lib/pages/web_view.dart @@ -18,6 +18,7 @@ import 'package:permission_handler/permission_handler.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:humhub/util/router.dart' as m; +import '../components/in_app_browser.dart'; import '../models/hum_hub.dart'; class WebViewApp extends ConsumerStatefulWidget { @@ -30,6 +31,7 @@ class WebViewApp extends ConsumerStatefulWidget { class WebViewAppState extends ConsumerState { late InAppWebViewController webViewController; + late MyInAppBrowser authBrowser; late Manifest manifest; late URLRequest _initialRequest; final _options = InAppWebViewGroupOptions( @@ -40,13 +42,29 @@ class WebViewAppState extends ConsumerState { javaScriptEnabled: true, ), ); + PullToRefreshController? _pullToRefreshController; late PullToRefreshOptions _pullToRefreshOptions; + @override + void initState() { + super.initState(); + } + + Future shouldOverride(NavigationAction navigationAction) async { + return NavigationActionPolicy.ALLOW; + } + @override Widget build(BuildContext context) { - _initialRequest = getInitRequest(context); + _initialRequest = _initRequest; _pullToRefreshController = initPullToRefreshController; + authBrowser = MyInAppBrowser( + manifest: manifest, + concludeAuth: (URLRequest request) { + _concludeAuth(request); + }, + ); return WillPopScope( onWillPop: () => webViewController.exitApp(context, ref), child: Scaffold( @@ -73,14 +91,13 @@ class WebViewAppState extends ConsumerState { ); } - Future _shouldOverrideUrlLoading(InAppWebViewController controller, NavigationAction action) async { + Future _shouldOverrideUrlLoading( + InAppWebViewController controller, NavigationAction action) async { // 1st check if url is not def. app url and open it in a browser or inApp. - final url = action.request.url!.origin; - - HumHub instance = await ref.read(humHubProvider).getInstance(); - if (!url.startsWith(manifest.baseUrl) && instance.isHideOpener && whitelistRedirects(url)) { - launchUrl(action.request.url!, mode: LaunchMode.externalApplication); + if (!url.startsWith(manifest.baseUrl)) { + authBrowser.launchUrl(action.request); + /*launchUrl(action.request.url!, mode: LaunchMode.inAppWebView);*/ return NavigationActionPolicy.CANCEL; } // 2nd Append customHeader if url is in app redirect and CANCEL the requests without custom headers @@ -92,6 +109,11 @@ class WebViewAppState extends ConsumerState { return NavigationActionPolicy.ALLOW; } + _concludeAuth(URLRequest request) { + authBrowser.close(); + webViewController.loadUrl(urlRequest: request); + } + _onWebViewCreated(InAppWebViewController controller) async { await controller.addWebMessageListener( WebMessageListener( @@ -145,7 +167,7 @@ class WebViewAppState extends ConsumerState { return request; } - URLRequest getInitRequest(BuildContext context) { + URLRequest get _initRequest { //Append random hash to customHeaders in this state the header should always exist. bool isHideDialog = ref.read(humHubProvider).isHideDialog; Map customHeaders = {}; @@ -174,7 +196,8 @@ class WebViewAppState extends ConsumerState { if (url!.path.contains('/user/auth/login')) { webViewController.evaluateJavascript(source: "document.querySelector('#login-rememberme').checked=true"); webViewController.evaluateJavascript( - source: "document.querySelector('#account-login-form > div.form-group.field-login-rememberme').style.display='none';"); + source: + "document.querySelector('#account-login-form > div.form-group.field-login-rememberme').style.display='none';"); } _pullToRefreshController?.endRefreshing(); } @@ -227,17 +250,4 @@ class WebViewAppState extends ConsumerState { ), ); } - - bool whitelistRedirects(String url) { - for (var element in [ - "https://github.com/login/oauth/authorize", - "https://login.live.com/oauth20_authorize", - "https://www.facebook.com/dialog/oauth", - "https://discord.com/api/oauth2/authorize", - "https://www.linkedin.com/oauth/v2/authorization" - ]) { - if(url.contains(element)) return true; - } - return false; - } } From 71c70b1ee334dd1188170ac970a40fcefefd6a0b Mon Sep 17 00:00:00 2001 From: primozratej Date: Thu, 22 Jun 2023 11:57:03 +0400 Subject: [PATCH 3/3] Set toolbarTopBackgroundColor to manifest theme color --- lib/components/in_app_browser.dart | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/components/in_app_browser.dart b/lib/components/in_app_browser.dart index 93ee339..bb9c437 100644 --- a/lib/components/in_app_browser.dart +++ b/lib/components/in_app_browser.dart @@ -1,22 +1,22 @@ import 'dart:async'; import 'dart:developer'; - -import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:humhub/models/manifest.dart'; +import 'package:humhub/util/extensions.dart'; class MyInAppBrowser extends InAppBrowser { final Manifest manifest; - final InAppBrowserClassOptions options = InAppBrowserClassOptions( - crossPlatform: InAppBrowserOptions(hideUrlBar: false, toolbarTopBackgroundColor: Colors.grey), - inAppWebViewGroupOptions: InAppWebViewGroupOptions( - crossPlatform: InAppWebViewOptions(javaScriptEnabled: true, useShouldOverrideUrlLoading: true), - ), - ); - + late InAppBrowserClassOptions options; final Function concludeAuth; - MyInAppBrowser({required this.manifest, required this.concludeAuth}); + MyInAppBrowser({required this.manifest, required this.concludeAuth}) { + options = InAppBrowserClassOptions( + crossPlatform: InAppBrowserOptions(hideUrlBar: false, toolbarTopBackgroundColor: HexColor(manifest.themeColor)), + inAppWebViewGroupOptions: InAppWebViewGroupOptions( + crossPlatform: InAppWebViewOptions(javaScriptEnabled: true, useShouldOverrideUrlLoading: true), + ), + ); + } @override Future? shouldOverrideUrlLoading(NavigationAction navigationAction) async {