Skip to content

Commit

Permalink
Config InAppBrowser for External Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozRatej committed Jun 22, 2023
1 parent 747d924 commit 083e707
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
35 changes: 35 additions & 0 deletions lib/components/in_app_browser.dart
Original file line number Diff line number Diff line change
@@ -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<NavigationActionPolicy?>? 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);
}
}
7 changes: 2 additions & 5 deletions lib/models/manifest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) {
Expand Down
54 changes: 32 additions & 22 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,6 +31,7 @@ class WebViewApp extends ConsumerStatefulWidget {

class WebViewAppState extends ConsumerState<WebViewApp> {
late InAppWebViewController webViewController;
late MyInAppBrowser authBrowser;
late Manifest manifest;
late URLRequest _initialRequest;
final _options = InAppWebViewGroupOptions(
Expand All @@ -40,13 +42,29 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
javaScriptEnabled: true,
),
);

PullToRefreshController? _pullToRefreshController;
late PullToRefreshOptions _pullToRefreshOptions;

@override
void initState() {
super.initState();
}

Future<NavigationActionPolicy> 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(
Expand All @@ -73,14 +91,13 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
);
}

Future<NavigationActionPolicy?> _shouldOverrideUrlLoading(InAppWebViewController controller, NavigationAction action) async {
Future<NavigationActionPolicy?> _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
Expand All @@ -92,6 +109,11 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
return NavigationActionPolicy.ALLOW;
}

_concludeAuth(URLRequest request) {
authBrowser.close();
webViewController.loadUrl(urlRequest: request);
}

_onWebViewCreated(InAppWebViewController controller) async {
await controller.addWebMessageListener(
WebMessageListener(
Expand Down Expand Up @@ -145,7 +167,7 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
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<String, String> customHeaders = {};
Expand Down Expand Up @@ -174,7 +196,8 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
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();
}
Expand Down Expand Up @@ -227,17 +250,4 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
),
);
}

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;
}
}

0 comments on commit 083e707

Please sign in to comment.