change: Use launch url for mozilla sso

This commit is contained in:
Christian Pauly 2021-02-01 12:12:28 +01:00
parent 4e5e3a80d6
commit a525d4fd45
8 changed files with 77 additions and 72 deletions

View file

@ -30,6 +30,13 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="im.fluffychat" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />

View file

@ -59,6 +59,17 @@
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>im.fluffychat</string>
</array>
</dict>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>

View file

@ -15,6 +15,7 @@ abstract class AppConfig {
static String _privacyUrl = 'https://fluffychat.im/en/privacy.html';
static String get privacyUrl => _privacyUrl;
static const String appId = 'im.fluffychat.FluffyChat';
static const String appOpenUrlScheme = 'im.fluffychat';
static const String sourceCodeUrl = 'https://gitlab.com/famedly/fluffychat';
static const String supportUrl =
'https://gitlab.com/famedly/fluffychat/issues';

View file

@ -26,7 +26,6 @@ import 'package:fluffychat/views/settings_notifications.dart';
import 'package:fluffychat/views/settings_style.dart';
import 'package:fluffychat/views/sign_up.dart';
import 'package:fluffychat/views/sign_up_password.dart';
import 'package:fluffychat/views/sso_web_view.dart';
import 'package:flutter/material.dart';
class FluffyRoutes {
@ -47,8 +46,6 @@ class FluffyRoutes {
return ViewData(mainView: (_) => HomeserverPicker());
case 'login':
return ViewData(mainView: (_) => Login());
case 'sso':
return ViewData(mainView: (_) => SsoWebView());
case 'signup':
if (parts.length == 5 && parts[2] == 'password') {
return ViewData(

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:math';
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
@ -7,9 +8,14 @@ import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/app_config.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flushbar/flushbar_helper.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:url_launcher/url_launcher.dart';
import '../utils/localized_exception_extension.dart';
import 'package:universal_html/prefer_universal/html.dart' as html;
class HomeserverPicker extends StatefulWidget {
@override
@ -21,6 +27,46 @@ class _HomeserverPickerState extends State<HomeserverPicker> {
String _domain = AppConfig.defaultHomeserver;
final TextEditingController _controller =
TextEditingController(text: AppConfig.defaultHomeserver);
StreamSubscription _intentDataStreamSubscription;
void _loginWithToken(String token) {
if (token?.isEmpty ?? true) return;
showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.login(
type: AuthenticationTypes.token,
userIdentifierType: null,
token: token,
initialDeviceDisplayName: Matrix.of(context).clientName,
),
);
}
void _processIncomingSharedText(String text) async {
if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return;
AdaptivePageLayout.of(context).popUntilIsFirst();
final token = Uri.parse(text).queryParameters['loginToken'];
_loginWithToken(token);
}
void _initReceiveSharingContent() {
if (!PlatformInfos.isMobile) return;
_intentDataStreamSubscription =
ReceiveSharingIntent.getTextStream().listen(_processIncomingSharedText);
ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText);
}
@override
void initState() {
super.initState();
_initReceiveSharingContent();
}
@override
void dispose() {
super.dispose();
_intentDataStreamSubscription?.cancel();
}
void _checkHomeserverAction(BuildContext context) async {
try {
@ -40,7 +86,11 @@ class _HomeserverPickerState extends State<HomeserverPicker> {
.pushNamed(AppConfig.enableRegistration ? '/signup' : '/login');
} else if (loginTypes.flows
.any((flow) => flow.type == AuthenticationTypes.sso)) {
await AdaptivePageLayout.of(context).pushNamed('/sso');
final redirectUrl = kIsWeb
? html.window.location.href
: '${Uri.encodeQueryComponent(AppConfig.appOpenUrlScheme.toLowerCase() + '://sso')}';
await launch(
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect?redirectUrl=$redirectUrl');
}
} on String catch (e) {
// ignore: unawaited_futures
@ -62,6 +112,13 @@ class _HomeserverPickerState extends State<HomeserverPicker> {
final padding = EdgeInsets.symmetric(
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 0),
);
if (kIsWeb) {
WidgetsBinding.instance.addPostFrameCallback((_) {
final token =
Uri.parse(html.window.location.href).queryParameters['loginToken'];
_loginWithToken(token);
});
}
return Scaffold(
appBar: AppBar(
title: DefaultAppBarSearchField(

View file

@ -1,60 +0,0 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/matrix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../app_config.dart';
class SsoWebView extends StatefulWidget {
@override
_SsoWebViewState createState() => _SsoWebViewState();
}
class _SsoWebViewState extends State<SsoWebView> {
bool _loading = false;
String _error;
void _login(BuildContext context, String token) async {
setState(() => _loading = true);
try {
await Matrix.of(context).client.login(
type: AuthenticationTypes.token,
userIdentifierType: null,
token: token,
initialDeviceDisplayName: Matrix.of(context).clientName,
);
} catch (e, s) {
Logs().e('Login with token failed', e, s);
setState(() => _error = e.toString());
}
}
@override
Widget build(BuildContext context) {
final url =
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect?redirectUrl=${Uri.encodeQueryComponent(AppConfig.appId.toLowerCase() + '://sso')}';
return Scaffold(
appBar: AppBar(
title: Text(
L10n.of(context).logInTo(Matrix.of(context).client.homeserver?.host ??
L10n.of(context).oopsSomethingWentWrong),
),
),
body: _error != null
? Center(child: Text(_error))
: _loading
? Center(child: CircularProgressIndicator())
: WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
onPageStarted: (url) {
if (url.startsWith(AppConfig.appId.toLowerCase())) {
_login(context,
Uri.parse(url).queryParameters['loginToken']);
}
},
),
);
}
}

View file

@ -1271,13 +1271,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.3"
webview_flutter:
dependency: "direct main"
description:
name: webview_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.7"
win32:
dependency: transitive
description:

View file

@ -35,7 +35,6 @@ dependencies:
path_provider: ^1.6.27
android_path_provider: ^0.1.1
permission_handler: ^5.0.1+1
webview_flutter: ^1.0.7
share: ^0.6.5+4
flutter_secure_storage: ^3.3.5
http: ^0.12.2