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';
const String defaultHomeserver = "https://matrix.org";
class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
@ -18,7 +16,7 @@ class _LoginState extends State<Login> {
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<Login> {
controller: serverController,
decoration: InputDecoration(
icon: Icon(Icons.domain),
hintText: "matrix.org",
hintText: "matrix-client.matrix.org",
errorText: serverError,
errorMaxLines: 1,
prefixText: "https://",

View File

@ -18,7 +18,7 @@ class SignUp extends StatefulWidget {
class _SignUpState extends State<SignUp> {
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<SignUp> {
controller: serverController,
decoration: InputDecoration(
icon: Icon(Icons.domain),
hintText: "matrix.org",
hintText: "matrix-client.matrix.org",
errorText: serverError,
errorMaxLines: 1,
prefixText: "https://",

View File

@ -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<SignUpPassword> {
String passwordError;
bool loading = false;
bool showPassword = true;
int currentStage = 0;
void _signUpAction(BuildContext context, {Map<String, dynamic> auth}) async {
MatrixState matrix = Matrix.of(context);
@ -38,8 +40,14 @@ class _SignUpPasswordState extends State<SignUpPassword> {
try {
print("[Sign Up] Create account...");
setState(() => loading = true);
Future<LoginState> 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<SignUpPassword> {
if (exception.requireAdditionalAuthentication) {
print(exception.raw);
if (exception.authenticationFlows.indexWhere((a) =>
a.stages.length == 1 && a.stages.first == "m.login.dummy") !=
-1) {
final List<String> 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<SignUpPassword> {
} 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);
}

View File

@ -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"

View File

@ -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: