feat: Implement matrix native oidc
This commit is contained in:
parent
b1a2b96aa4
commit
c1541bc4bf
6 changed files with 208 additions and 92 deletions
|
|
@ -5,6 +5,7 @@ import 'package:go_router/go_router.dart';
|
|||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
|
||||
import 'package:fluffychat/l10n/l10n.dart';
|
||||
import 'package:fluffychat/pages/sign_in/view_model/flows/oidc_login.dart';
|
||||
import 'package:fluffychat/pages/sign_in/view_model/flows/sso_login.dart';
|
||||
import 'package:fluffychat/pages/sign_in/view_model/model/public_homeserver_data.dart';
|
||||
import 'package:fluffychat/utils/localized_exception_extension.dart';
|
||||
|
|
@ -27,16 +28,33 @@ void connectToHomeserverFlow(
|
|||
}
|
||||
final l10n = L10n.of(context);
|
||||
final client = await Matrix.of(context).getLoginClient();
|
||||
final (_, _, loginFlows, _) = await client.checkHomeserver(homeserver);
|
||||
final (_, _, loginFlows, authMetadata) = await client.checkHomeserver(
|
||||
homeserver,
|
||||
fetchAuthMetadata: true,
|
||||
);
|
||||
|
||||
final regLink = homeserverData.regLink;
|
||||
final supportsSso = loginFlows.any((flow) => flow.type == 'm.login.sso');
|
||||
|
||||
if (!supportsSso) {
|
||||
final regLink = homeserverData.regLink;
|
||||
if ((kIsWeb || PlatformInfos.isLinux) &&
|
||||
(supportsSso || authMetadata != null || (signUp && regLink != null))) {
|
||||
final consent = await showOkCancelAlertDialog(
|
||||
context: context,
|
||||
title: l10n.appWantsToUseForLogin(homeserverInput),
|
||||
message: l10n.appWantsToUseForLoginDescription,
|
||||
okLabel: l10n.continueText,
|
||||
);
|
||||
if (consent != OkCancelResult.ok) return;
|
||||
}
|
||||
|
||||
if (authMetadata != null) {
|
||||
await oidcLoginFlow(client, context, signUp);
|
||||
} else if (supportsSso) {
|
||||
await ssoLoginFlow(client, context, signUp);
|
||||
} else {
|
||||
if (signUp && regLink != null) {
|
||||
await launchUrlString(regLink);
|
||||
}
|
||||
|
||||
final pathSegments = List.of(
|
||||
GoRouter.of(context).routeInformationProvider.value.uri.pathSegments,
|
||||
);
|
||||
|
|
@ -46,18 +64,10 @@ void connectToHomeserverFlow(
|
|||
setState(AsyncSnapshot.withData(ConnectionState.done, true));
|
||||
return;
|
||||
}
|
||||
if (kIsWeb || PlatformInfos.isLinux) {
|
||||
final consent = await showOkCancelAlertDialog(
|
||||
context: context,
|
||||
title: l10n.appWantsToUseForLogin(homeserverInput),
|
||||
message: l10n.appWantsToUseForLoginDescription,
|
||||
okLabel: l10n.continueText,
|
||||
);
|
||||
if (consent != OkCancelResult.ok) return;
|
||||
}
|
||||
await ssoLoginFlow(client, context, signUp);
|
||||
|
||||
setState(AsyncSnapshot.withData(ConnectionState.done, true));
|
||||
if (context.mounted) {
|
||||
setState(AsyncSnapshot.withData(ConnectionState.done, true));
|
||||
}
|
||||
} catch (e, s) {
|
||||
setState(AsyncSnapshot.withError(ConnectionState.done, e, s));
|
||||
if (!context.mounted) return;
|
||||
|
|
|
|||
91
lib/pages/sign_in/view_model/flows/oidc_login.dart
Normal file
91
lib/pages/sign_in/view_model/flows/oidc_login.dart
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/config/setting_keys.dart';
|
||||
import 'package:fluffychat/l10n/l10n.dart';
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
|
||||
|
||||
Future<void> oidcLoginFlow(
|
||||
Client client,
|
||||
BuildContext context,
|
||||
bool signUp,
|
||||
) async {
|
||||
Logs().i('Starting Matrix Native OIDC Flow...');
|
||||
final redirectUrl = kIsWeb
|
||||
? Uri.parse(
|
||||
html.window.location.href,
|
||||
).resolveUri(Uri(pathSegments: ['auth.html']))
|
||||
: (PlatformInfos.isMobile || PlatformInfos.isWeb || PlatformInfos.isMacOS)
|
||||
? Uri.parse('${AppConfig.appOpenUrlScheme.toLowerCase()}:/login')
|
||||
: Uri.parse('http://localhost:3001/login');
|
||||
|
||||
final urlScheme =
|
||||
(PlatformInfos.isMobile || PlatformInfos.isWeb || PlatformInfos.isMacOS)
|
||||
? redirectUrl.scheme
|
||||
: 'http://localhost:3001';
|
||||
|
||||
final clientUri = Uri.parse(AppConfig.website);
|
||||
final supportWebPlatform =
|
||||
kIsWeb &&
|
||||
kReleaseMode &&
|
||||
redirectUrl.scheme == 'https' &&
|
||||
redirectUrl.host.contains(clientUri.host);
|
||||
if (!supportWebPlatform) {
|
||||
Logs().w(
|
||||
'OIDC Application Type web is not supported. Using native now. Please use this instance not in production!',
|
||||
);
|
||||
}
|
||||
|
||||
final oidcClientData = await client.registerOidcClient(
|
||||
redirectUris: [redirectUrl],
|
||||
applicationType: supportWebPlatform
|
||||
? OidcApplicationType.web
|
||||
: OidcApplicationType.native,
|
||||
clientInformation: OidcClientInformation(
|
||||
clientName: AppSettings.applicationName.value,
|
||||
clientUri: clientUri,
|
||||
logoUri: Uri.parse('https://fluffy.chat/assets/favicon.png'),
|
||||
tosUri: null,
|
||||
policyUri: AppConfig.privacyUrl,
|
||||
),
|
||||
);
|
||||
|
||||
final session = await client.initOidcLoginSession(
|
||||
oidcClientData: oidcClientData,
|
||||
redirectUri: redirectUrl,
|
||||
prompt: signUp ? 'create' : null,
|
||||
);
|
||||
|
||||
if (!PlatformInfos.isMobile && !PlatformInfos.isMacOS) {
|
||||
final consent = await showOkCancelAlertDialog(
|
||||
context: context,
|
||||
title: L10n.of(
|
||||
context,
|
||||
).appWantsToUseForLogin(client.homeserver!.toString()),
|
||||
message: L10n.of(context).appWantsToUseForLoginDescription,
|
||||
okLabel: L10n.of(context).continueText,
|
||||
);
|
||||
if (consent != OkCancelResult.ok) return;
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
|
||||
final returnUrlString = await FlutterWebAuth2.authenticate(
|
||||
url: session.authenticationUri.toString(),
|
||||
callbackUrlScheme: urlScheme,
|
||||
options: FlutterWebAuth2Options(useWebview: PlatformInfos.isMobile),
|
||||
);
|
||||
final returnUrl = Uri.parse(returnUrlString);
|
||||
final queryParameters = returnUrl.hasFragment
|
||||
? Uri.parse(returnUrl.fragment).queryParameters
|
||||
: returnUrl.queryParameters;
|
||||
final code = queryParameters['code'] as String;
|
||||
final state = queryParameters['state'] as String;
|
||||
|
||||
await client.oidcLogin(session: session, code: code, state: state);
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ Future<void> ssoLoginFlow(
|
|||
BuildContext context,
|
||||
bool signUp,
|
||||
) async {
|
||||
Logs().i('Starting legacy SSO Flow...');
|
||||
final redirectUrl = kIsWeb
|
||||
? Uri.parse(
|
||||
html.window.location.href,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue