2021-05-22 10:25:37 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
import 'package:fluffychat/pages/views/sign_up_view.dart';
|
2021-06-06 17:07:19 +02:00
|
|
|
import 'package:fluffychat/utils/famedlysdk_store.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-05-22 08:53:52 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
2021-04-12 17:31:53 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-05-22 10:25:37 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
import 'package:universal_html/html.dart' as html;
|
2021-04-12 17:31:53 +02:00
|
|
|
|
|
|
|
class SignUp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
SignUpController createState() => SignUpController();
|
|
|
|
}
|
|
|
|
|
|
|
|
class SignUpController extends State<SignUp> {
|
2021-06-06 16:55:31 +02:00
|
|
|
Map<String, dynamic> _rawLoginTypes;
|
|
|
|
bool registrationSupported;
|
2021-06-06 17:07:19 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
List<IdentityProvider> get identityProviders {
|
|
|
|
if (!ssoLoginSupported) return [];
|
|
|
|
final rawProviders = _rawLoginTypes.tryGetList('flows').singleWhere(
|
|
|
|
(flow) =>
|
|
|
|
flow['type'] == AuthenticationTypes.sso)['identity_providers'];
|
|
|
|
return (rawProviders as List)
|
|
|
|
.map((json) => IdentityProvider.fromJson(json))
|
|
|
|
.toList();
|
|
|
|
}
|
2021-05-22 10:25:37 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
bool get passwordLoginSupported =>
|
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.supportedLoginTypes
|
|
|
|
.contains(AuthenticationTypes.password) &&
|
|
|
|
_rawLoginTypes
|
|
|
|
.tryGetList('flows')
|
|
|
|
.any((flow) => flow['type'] == AuthenticationTypes.password);
|
2021-05-22 10:25:37 +02:00
|
|
|
|
|
|
|
bool get ssoLoginSupported =>
|
2021-06-06 16:55:31 +02:00
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.supportedLoginTypes
|
|
|
|
.contains(AuthenticationTypes.sso) &&
|
|
|
|
_rawLoginTypes
|
|
|
|
.tryGetList('flows')
|
|
|
|
.any((flow) => flow['type'] == AuthenticationTypes.sso);
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> getLoginTypes() async {
|
|
|
|
_rawLoginTypes ??= await Matrix.of(context).client.request(
|
|
|
|
RequestType.GET,
|
|
|
|
'/client/r0/login',
|
|
|
|
);
|
|
|
|
if (registrationSupported == null) {
|
|
|
|
try {
|
|
|
|
await Matrix.of(context).client.register();
|
|
|
|
registrationSupported = true;
|
|
|
|
} on MatrixException catch (e) {
|
|
|
|
registrationSupported = e.requireAdditionalAuthentication ?? false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _rawLoginTypes;
|
2021-05-22 10:25:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 17:09:32 +02:00
|
|
|
static const String ssoHomeserverKey = 'sso-homeserver';
|
2021-06-06 17:07:19 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
void ssoLoginAction(String id) {
|
2021-06-06 17:07:19 +02:00
|
|
|
if (kIsWeb) {
|
|
|
|
// We store the homserver in the local storage instead of a redirect
|
|
|
|
// parameter because of possible CSRF attacks.
|
|
|
|
Store().setItem(
|
2021-06-06 17:09:32 +02:00
|
|
|
ssoHomeserverKey, Matrix.of(context).client.homeserver.toString());
|
2021-06-06 17:07:19 +02:00
|
|
|
}
|
2021-05-22 10:25:37 +02:00
|
|
|
final redirectUrl = kIsWeb
|
|
|
|
? html.window.location.href
|
|
|
|
: AppConfig.appOpenUrlScheme.toLowerCase() + '://sso';
|
|
|
|
launch(
|
2021-06-06 16:55:31 +02:00
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect/${Uri.encodeComponent(id)}?redirectUrl=${Uri.encodeQueryComponent(redirectUrl)}');
|
2021-05-22 10:25:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
void signUpAction() => launch(
|
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/static/client/register');
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) => SignUpView(this);
|
|
|
|
}
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
class IdentityProvider {
|
|
|
|
final String id;
|
|
|
|
final String name;
|
|
|
|
final String icon;
|
|
|
|
final String brand;
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
IdentityProvider({this.id, this.name, this.icon, this.brand});
|
2021-04-12 17:31:53 +02:00
|
|
|
|
2021-06-06 16:55:31 +02:00
|
|
|
factory IdentityProvider.fromJson(Map<String, dynamic> json) =>
|
|
|
|
IdentityProvider(
|
|
|
|
id: json['id'],
|
|
|
|
name: json['name'],
|
|
|
|
icon: json['icon'],
|
|
|
|
brand: json['brand'],
|
|
|
|
);
|
2021-04-12 17:31:53 +02:00
|
|
|
}
|