From 4d027ff7f906a31d1ffe48c65dbdf1bbaa13d7ed Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 14 Jan 2020 15:53:35 +0100 Subject: [PATCH] Finish authentication flow --- lib/views/auth_web_view.dart | 43 +++++++++++++++++++++++++++++++++ lib/views/login.dart | 6 ++--- lib/views/sign_up.dart | 4 +-- lib/views/sign_up_password.dart | 40 ++++++++++++++++++++++-------- pubspec.lock | 9 ++++++- pubspec.yaml | 1 + 6 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 lib/views/auth_web_view.dart diff --git a/lib/views/auth_web_view.dart b/lib/views/auth_web_view.dart new file mode 100644 index 0000000..db69385 --- /dev/null +++ b/lib/views/auth_web_view.dart @@ -0,0 +1,43 @@ +import 'package:fluffychat/components/matrix.dart'; +import 'package:flutter/material.dart'; +import 'package:webview_flutter/webview_flutter.dart'; + +class AuthWebView extends StatelessWidget { + final String authType; + final String session; + final Function onAuthDone; + + const AuthWebView(this.authType, this.session, this.onAuthDone); + + @override + Widget build(BuildContext context) { + final String url = Matrix.of(context).client.homeserver + + "/_matrix/client/r0/auth/$authType/fallback/web?session=$session"; + print(url); + return Scaffold( + appBar: AppBar( + title: Text("Authentication"), + leading: IconButton( + icon: Icon(Icons.arrow_back), + onPressed: () { + Navigator.of(context).pop(); + Navigator.of(context).pop(); + }, + ), + actions: [ + IconButton( + icon: Icon(Icons.check), + onPressed: () { + Navigator.of(context).pop(); + onAuthDone(); + }, + ) + ], + ), + body: WebView( + initialUrl: url, + javascriptMode: JavascriptMode.unrestricted, + ), + ); + } +} diff --git a/lib/views/login.dart b/lib/views/login.dart index 98250e9..9a3cea5 100644 --- a/lib/views/login.dart +++ b/lib/views/login.dart @@ -7,8 +7,6 @@ import 'package:flutter/material.dart'; import 'chat_list.dart'; -const String defaultHomeserver = "https://matrix.org"; - class Login extends StatefulWidget { @override _LoginState createState() => _LoginState(); @@ -18,7 +16,7 @@ class _LoginState extends State { final TextEditingController usernameController = TextEditingController(); final TextEditingController passwordController = TextEditingController(); final TextEditingController serverController = - TextEditingController(text: "matrix.org"); + TextEditingController(text: "matrix-client.matrix.org"); String usernameError; String passwordError; String serverError; @@ -98,7 +96,7 @@ class _LoginState extends State { controller: serverController, decoration: InputDecoration( icon: Icon(Icons.domain), - hintText: "matrix.org", + hintText: "matrix-client.matrix.org", errorText: serverError, errorMaxLines: 1, prefixText: "https://", diff --git a/lib/views/sign_up.dart b/lib/views/sign_up.dart index 66da5c9..b69a4e7 100644 --- a/lib/views/sign_up.dart +++ b/lib/views/sign_up.dart @@ -18,7 +18,7 @@ class SignUp extends StatefulWidget { class _SignUpState extends State { final TextEditingController usernameController = TextEditingController(); final TextEditingController serverController = - TextEditingController(text: "matrix.org"); + TextEditingController(text: "matrix-client.matrix.org"); String usernameError; String serverError; bool loading = false; @@ -97,7 +97,7 @@ class _SignUpState extends State { controller: serverController, decoration: InputDecoration( icon: Icon(Icons.domain), - hintText: "matrix.org", + hintText: "matrix-client.matrix.org", errorText: serverError, errorMaxLines: 1, prefixText: "https://", diff --git a/lib/views/sign_up_password.dart b/lib/views/sign_up_password.dart index fa764da..34395a2 100644 --- a/lib/views/sign_up_password.dart +++ b/lib/views/sign_up_password.dart @@ -4,6 +4,7 @@ import 'dart:math'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/utils/app_route.dart'; +import 'package:fluffychat/views/auth_web_view.dart'; import 'package:flutter/material.dart'; import 'package:toast/toast.dart'; @@ -23,6 +24,7 @@ class _SignUpPasswordState extends State { String passwordError; bool loading = false; bool showPassword = true; + int currentStage = 0; void _signUpAction(BuildContext context, {Map auth}) async { MatrixState matrix = Matrix.of(context); @@ -38,8 +40,14 @@ class _SignUpPasswordState extends State { try { print("[Sign Up] Create account..."); + setState(() => loading = true); Future waitForLogin = matrix.client.onLoginStateChanged.stream.first; + if (auth == null) { + currentStage = 0; + } else { + currentStage++; + } await matrix.client.register( username: widget.username, password: passwordController.text, @@ -51,17 +59,28 @@ class _SignUpPasswordState extends State { if (exception.requireAdditionalAuthentication) { print(exception.raw); - if (exception.authenticationFlows.indexWhere((a) => - a.stages.length == 1 && a.stages.first == "m.login.dummy") != - -1) { + final List stages = exception.authenticationFlows + .firstWhere((a) => !a.stages.contains("m.login.email.identity")) + .stages; + + if (stages[currentStage] == "m.login.dummy") { _signUpAction(context, auth: { - "type": "m.login.dummy", + "type": stages[currentStage], "session": exception.session, }); } else { - setState(() => passwordError = - "The server requires unsupported authentication flows"); - setState(() => loading = false); + await Navigator.of(context).push( + AppRoute.defaultRoute( + context, + AuthWebView( + stages[currentStage], + exception.session, + () => _signUpAction(context, auth: { + "session": exception.session, + }), + ), + ), + ); return; } } else { @@ -92,9 +111,10 @@ class _SignUpPasswordState extends State { } catch (exception) { Toast.show("Could not set profile picture", context, duration: 5); } - await Navigator.of(context).pushAndRemoveUntil( - AppRoute.defaultRoute(context, ChatListView()), (r) => false); - + if (matrix.client.isLogged()) { + await Navigator.of(context).pushAndRemoveUntil( + AppRoute.defaultRoute(context, ChatListView()), (r) => false); + } setState(() => loading = false); } diff --git a/pubspec.lock b/pubspec.lock index 3c50442..dd1ea0a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -380,6 +380,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.8" + webview_flutter: + dependency: "direct main" + description: + name: webview_flutter + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.19+4" xml: dependency: transitive description: @@ -396,4 +403,4 @@ packages: version: "2.2.0" sdks: dart: ">=2.6.0 <3.0.0" - flutter: ">=1.12.8 <2.0.0" + flutter: ">=1.12.13+hotfix.5 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 8cfbecd..af1c204 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,7 @@ dependencies: flutter_local_notifications: ^0.9.1+2 link_text: ^0.1.1 path_provider: ^1.5.1 + webview_flutter: ^0.3.19+4 dev_dependencies: flutter_test: