Finish authentication flow

This commit is contained in:
Christian Pauly 2020-01-14 15:53:35 +01:00
parent b6ff2483f0
commit 4d027ff7f9
6 changed files with 86 additions and 17 deletions

View file

@ -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: <Widget>[
IconButton(
icon: Icon(Icons.check),
onPressed: () {
Navigator.of(context).pop();
onAuthDone();
},
)
],
),
body: WebView(
initialUrl: url,
javascriptMode: JavascriptMode.unrestricted,
),
);
}
}

View file

@ -7,8 +7,6 @@ import 'package:flutter/material.dart';
import 'chat_list.dart'; import 'chat_list.dart';
const String defaultHomeserver = "https://matrix.org";
class Login extends StatefulWidget { class Login extends StatefulWidget {
@override @override
_LoginState createState() => _LoginState(); _LoginState createState() => _LoginState();
@ -18,7 +16,7 @@ class _LoginState extends State<Login> {
final TextEditingController usernameController = TextEditingController(); final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController(); final TextEditingController passwordController = TextEditingController();
final TextEditingController serverController = final TextEditingController serverController =
TextEditingController(text: "matrix.org"); TextEditingController(text: "matrix-client.matrix.org");
String usernameError; String usernameError;
String passwordError; String passwordError;
String serverError; String serverError;
@ -98,7 +96,7 @@ class _LoginState extends State<Login> {
controller: serverController, controller: serverController,
decoration: InputDecoration( decoration: InputDecoration(
icon: Icon(Icons.domain), icon: Icon(Icons.domain),
hintText: "matrix.org", hintText: "matrix-client.matrix.org",
errorText: serverError, errorText: serverError,
errorMaxLines: 1, errorMaxLines: 1,
prefixText: "https://", prefixText: "https://",

View file

@ -18,7 +18,7 @@ class SignUp extends StatefulWidget {
class _SignUpState extends State<SignUp> { class _SignUpState extends State<SignUp> {
final TextEditingController usernameController = TextEditingController(); final TextEditingController usernameController = TextEditingController();
final TextEditingController serverController = final TextEditingController serverController =
TextEditingController(text: "matrix.org"); TextEditingController(text: "matrix-client.matrix.org");
String usernameError; String usernameError;
String serverError; String serverError;
bool loading = false; bool loading = false;
@ -97,7 +97,7 @@ class _SignUpState extends State<SignUp> {
controller: serverController, controller: serverController,
decoration: InputDecoration( decoration: InputDecoration(
icon: Icon(Icons.domain), icon: Icon(Icons.domain),
hintText: "matrix.org", hintText: "matrix-client.matrix.org",
errorText: serverError, errorText: serverError,
errorMaxLines: 1, errorMaxLines: 1,
prefixText: "https://", prefixText: "https://",

View file

@ -4,6 +4,7 @@ import 'dart:math';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/app_route.dart'; import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/auth_web_view.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:toast/toast.dart'; import 'package:toast/toast.dart';
@ -23,6 +24,7 @@ class _SignUpPasswordState extends State<SignUpPassword> {
String passwordError; String passwordError;
bool loading = false; bool loading = false;
bool showPassword = true; bool showPassword = true;
int currentStage = 0;
void _signUpAction(BuildContext context, {Map<String, dynamic> auth}) async { void _signUpAction(BuildContext context, {Map<String, dynamic> auth}) async {
MatrixState matrix = Matrix.of(context); MatrixState matrix = Matrix.of(context);
@ -38,8 +40,14 @@ class _SignUpPasswordState extends State<SignUpPassword> {
try { try {
print("[Sign Up] Create account..."); print("[Sign Up] Create account...");
setState(() => loading = true);
Future<LoginState> waitForLogin = Future<LoginState> waitForLogin =
matrix.client.onLoginStateChanged.stream.first; matrix.client.onLoginStateChanged.stream.first;
if (auth == null) {
currentStage = 0;
} else {
currentStage++;
}
await matrix.client.register( await matrix.client.register(
username: widget.username, username: widget.username,
password: passwordController.text, password: passwordController.text,
@ -51,17 +59,28 @@ class _SignUpPasswordState extends State<SignUpPassword> {
if (exception.requireAdditionalAuthentication) { if (exception.requireAdditionalAuthentication) {
print(exception.raw); print(exception.raw);
if (exception.authenticationFlows.indexWhere((a) => final List<String> stages = exception.authenticationFlows
a.stages.length == 1 && a.stages.first == "m.login.dummy") != .firstWhere((a) => !a.stages.contains("m.login.email.identity"))
-1) { .stages;
if (stages[currentStage] == "m.login.dummy") {
_signUpAction(context, auth: { _signUpAction(context, auth: {
"type": "m.login.dummy", "type": stages[currentStage],
"session": exception.session, "session": exception.session,
}); });
} else { } else {
setState(() => passwordError = await Navigator.of(context).push(
"The server requires unsupported authentication flows"); AppRoute.defaultRoute(
setState(() => loading = false); context,
AuthWebView(
stages[currentStage],
exception.session,
() => _signUpAction(context, auth: {
"session": exception.session,
}),
),
),
);
return; return;
} }
} else { } else {
@ -92,9 +111,10 @@ class _SignUpPasswordState extends State<SignUpPassword> {
} catch (exception) { } catch (exception) {
Toast.show("Could not set profile picture", context, duration: 5); Toast.show("Could not set profile picture", context, duration: 5);
} }
if (matrix.client.isLogged()) {
await Navigator.of(context).pushAndRemoveUntil( await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, ChatListView()), (r) => false); AppRoute.defaultRoute(context, ChatListView()), (r) => false);
}
setState(() => loading = false); setState(() => loading = false);
} }

View file

@ -380,6 +380,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" 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: xml:
dependency: transitive dependency: transitive
description: description:
@ -396,4 +403,4 @@ packages:
version: "2.2.0" version: "2.2.0"
sdks: sdks:
dart: ">=2.6.0 <3.0.0" dart: ">=2.6.0 <3.0.0"
flutter: ">=1.12.8 <2.0.0" flutter: ">=1.12.13+hotfix.5 <2.0.0"

View file

@ -43,6 +43,7 @@ dependencies:
flutter_local_notifications: ^0.9.1+2 flutter_local_notifications: ^0.9.1+2
link_text: ^0.1.1 link_text: ^0.1.1
path_provider: ^1.5.1 path_provider: ^1.5.1
webview_flutter: ^0.3.19+4
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: