fluffychat/lib/pages/homeserver_picker/homeserver_picker.dart

222 lines
6.9 KiB
Dart
Raw Normal View History

2021-06-06 17:07:19 +02:00
import 'dart:async';
import 'package:flutter/foundation.dart';
2020-04-12 10:35:45 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2022-02-13 18:29:04 +01:00
import 'package:flutter_web_auth/flutter_web_auth.dart';
2021-06-06 17:07:19 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2022-02-13 18:29:04 +01:00
import 'package:universal_html/html.dart' as html;
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2021-06-06 17:07:19 +02:00
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/setting_keys.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker_view.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/famedlysdk_store.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../../utils/localized_exception_extension.dart';
2020-04-12 10:35:45 +02:00
2021-01-17 15:33:31 +01:00
class HomeserverPicker extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const HomeserverPicker({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-01-17 15:33:31 +01:00
@override
2021-04-09 16:28:26 +02:00
HomeserverPickerController createState() => HomeserverPickerController();
2021-01-17 15:33:31 +01:00
}
2020-04-12 10:35:45 +02:00
2021-04-09 16:28:26 +02:00
class HomeserverPickerController extends State<HomeserverPicker> {
bool isLoading = false;
String domain = AppConfig.defaultHomeserver;
final TextEditingController homeserverController =
2021-01-22 21:39:37 +01:00
TextEditingController(text: AppConfig.defaultHomeserver);
2022-01-29 12:35:03 +01:00
StreamSubscription? _intentDataStreamSubscription;
String? error;
Timer? _coolDown;
2021-06-11 10:08:04 +02:00
void setDomain(String domain) {
this.domain = domain;
_coolDown?.cancel();
if (domain.isNotEmpty) {
2021-11-15 09:48:21 +01:00
_coolDown =
Timer(const Duration(milliseconds: 500), checkHomeserverAction);
2021-06-11 10:08:04 +02:00
}
}
2021-06-06 17:07:19 +02:00
void _loginWithToken(String token) {
2022-01-29 12:35:03 +01:00
if (token.isEmpty) return;
2021-06-06 17:07:19 +02:00
showFutureLoadingDialog(
context: context,
future: () async {
if (Matrix.of(context).getLoginClient().homeserver == null) {
await Matrix.of(context).getLoginClient().checkHomeserver(
2021-06-11 10:08:04 +02:00
await Store()
.getItem(HomeserverPickerController.ssoHomeserverKey),
);
}
await Matrix.of(context).getLoginClient().login(
LoginType.mLoginToken,
token: token,
initialDeviceDisplayName: PlatformInfos.clientName,
);
},
2021-06-06 17:07:19 +02:00
);
}
@override
void initState() {
super.initState();
2021-10-16 09:59:38 +02:00
checkHomeserverAction();
2021-06-06 17:07:19 +02:00
}
@override
void dispose() {
super.dispose();
_intentDataStreamSubscription?.cancel();
}
2021-01-17 15:33:31 +01:00
2022-01-29 12:35:03 +01:00
String? _lastCheckedHomeserver;
2021-11-16 10:23:29 +01:00
2021-04-09 16:28:26 +02:00
/// Starts an analysis of the given homeserver. It uses the current domain and
/// makes sure that it is prefixed with https. Then it searches for the
/// well-known information and forwards to the login page depending on the
2021-06-11 10:08:04 +02:00
/// login type.
2021-10-16 09:59:38 +02:00
Future<void> checkHomeserverAction() async {
2021-06-11 10:08:04 +02:00
_coolDown?.cancel();
2021-11-16 10:23:29 +01:00
if (_lastCheckedHomeserver == domain) return;
2022-01-29 12:35:03 +01:00
if (domain.isEmpty) throw L10n.of(context)!.changeTheHomeserver;
2021-11-16 10:23:29 +01:00
var homeserver = domain;
2021-11-16 10:23:29 +01:00
if (!homeserver.startsWith('https://')) {
homeserver = 'https://$homeserver';
}
2021-01-18 22:59:02 +01:00
2021-11-16 10:23:29 +01:00
setState(() {
error = _rawLoginTypes = registrationSupported = null;
isLoading = true;
});
try {
2022-02-15 12:47:22 +01:00
final summary =
await Matrix.of(context).getLoginClient().checkHomeserver(homeserver);
2022-02-15 12:47:22 +01:00
var jitsi = summary.discoveryInformation?.additionalProperties
2022-01-29 12:35:03 +01:00
.tryGet<Map<String, dynamic>>('im.vector.riot.jitsi')
?.tryGet<String>('preferredDomain');
if (jitsi != null) {
if (!jitsi.endsWith('/')) {
jitsi += '/';
}
Logs().v('Found custom jitsi instance $jitsi');
await Matrix.of(context)
.store
.setItem(SettingKeys.jitsiInstance, jitsi);
AppConfig.jitsiInstance = jitsi;
}
2021-11-15 12:14:00 +01:00
_rawLoginTypes = await Matrix.of(context).getLoginClient().request(
RequestType.GET,
'/client/r0/login',
);
try {
await Matrix.of(context).getLoginClient().register();
registrationSupported = true;
} on MatrixException catch (e) {
2022-01-29 12:35:03 +01:00
registrationSupported = e.requireAdditionalAuthentication;
2021-11-15 12:14:00 +01:00
}
2021-01-17 15:33:31 +01:00
} catch (e) {
2022-01-29 12:35:03 +01:00
setState(() => error = (e).toLocalizedString(context));
2021-01-17 15:33:31 +01:00
} finally {
2021-11-16 10:23:29 +01:00
_lastCheckedHomeserver = domain;
2021-01-17 15:33:31 +01:00
if (mounted) {
2021-04-09 16:28:26 +02:00
setState(() => isLoading = false);
2021-01-17 15:33:31 +01:00
}
2020-04-12 10:35:45 +02:00
}
}
2022-01-29 12:35:03 +01:00
Map<String, dynamic>? _rawLoginTypes;
bool? registrationSupported;
2021-06-11 10:08:04 +02:00
List<IdentityProvider> get identityProviders {
if (!ssoLoginSupported) return [];
2022-01-29 12:35:03 +01:00
final rawProviders = _rawLoginTypes!.tryGetList('flows')!.singleWhere(
2021-06-11 10:08:04 +02:00
(flow) =>
flow['type'] == AuthenticationTypes.sso)['identity_providers'];
final list = (rawProviders as List)
2021-06-11 10:08:04 +02:00
.map((json) => IdentityProvider.fromJson(json))
.toList();
2021-07-15 14:04:29 +02:00
if (PlatformInfos.isCupertinoStyle) {
list.sort((a, b) => a.brand == 'apple' ? -1 : 1);
}
return list;
2021-06-11 10:08:04 +02:00
}
bool get passwordLoginSupported =>
Matrix.of(context)
.client
.supportedLoginTypes
.contains(AuthenticationTypes.password) &&
2022-01-29 12:35:03 +01:00
_rawLoginTypes!
.tryGetList('flows')!
2021-06-11 10:08:04 +02:00
.any((flow) => flow['type'] == AuthenticationTypes.password);
bool get ssoLoginSupported =>
Matrix.of(context)
.client
.supportedLoginTypes
.contains(AuthenticationTypes.sso) &&
2022-01-29 12:35:03 +01:00
_rawLoginTypes!
.tryGetList('flows')!
2021-06-11 10:08:04 +02:00
.any((flow) => flow['type'] == AuthenticationTypes.sso);
static const String ssoHomeserverKey = 'sso-homeserver';
2022-02-13 18:29:04 +01:00
void ssoLoginAction(String id) async {
2021-06-11 10:08:04 +02:00
if (kIsWeb) {
// We store the homserver in the local storage instead of a redirect
// parameter because of possible CSRF attacks.
Store().setItem(ssoHomeserverKey,
Matrix.of(context).getLoginClient().homeserver.toString());
2021-06-11 10:08:04 +02:00
}
final redirectUrl = kIsWeb
2022-02-13 18:29:04 +01:00
? html.window.origin! + '/web/auth.html'
2021-06-18 11:21:00 +02:00
: AppConfig.appOpenUrlScheme.toLowerCase() + '://login';
2021-07-15 14:04:29 +02:00
final url =
'${Matrix.of(context).getLoginClient().homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}';
2022-02-13 18:29:04 +01:00
final urlScheme = Uri.parse(redirectUrl).scheme;
final result = await FlutterWebAuth.authenticate(
url: url, callbackUrlScheme: urlScheme);
final token = Uri.parse(result).queryParameters['loginToken'];
if (token != null) _loginWithToken(token);
2021-06-11 10:08:04 +02:00
}
2021-10-30 14:06:10 +02:00
void signUpAction() => VRouter.of(context).to(
'signup',
queryParameters: {'domain': domain},
);
2021-06-11 10:08:04 +02:00
2020-04-12 10:35:45 +02:00
@override
Widget build(BuildContext context) {
Matrix.of(context).navigatorContext = context;
return HomeserverPickerView(this);
}
2020-04-12 10:35:45 +02:00
}
2021-06-11 10:08:04 +02:00
class IdentityProvider {
2022-01-29 12:35:03 +01:00
final String? id;
final String? name;
final String? icon;
final String? brand;
2021-06-11 10:08:04 +02:00
IdentityProvider({this.id, this.name, this.icon, this.brand});
factory IdentityProvider.fromJson(Map<String, dynamic> json) =>
IdentityProvider(
id: json['id'],
name: json['name'],
icon: json['icon'],
brand: json['brand'],
);
}