Files
tatuchat/lib/utils/sign_in_flows/sso_login.dart
Welton Moura 0037b0b9cf
Some checks failed
Main Deploy Workflow / deploy_web (push) Has been cancelled
Main Deploy Workflow / deploy_playstore_internal (push) Has been cancelled
Close stale issues and PRs / stale (push) Has been cancelled
Initial commit
2026-09-11 15:59:56 -03:00

75 lines
2.3 KiB
Dart

// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
//
// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:tatchat/l10n/l10n.dart';
import 'package:tatchat/utils/platform_infos.dart';
import 'package:tatchat/utils/sign_in_flows/calc_redirect_url.dart';
import 'package:tatchat/widgets/adaptive_dialogs/show_modal_action_popup.dart';
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
import 'package:material_ui/material_ui.dart';
import 'package:matrix/matrix.dart';
Future<void> ssoLoginFlow(
Client client,
BuildContext context,
bool signUp,
List<LoginFlow> loginFlows,
) async {
final (redirectUrl, urlScheme) = calcRedirectUrl(withAuthHtmlPath: true);
Logs().i('Starting legacy SSO Flow with redirect URL', redirectUrl);
final ssoProviders =
(loginFlows
.firstWhere((flow) => flow.type == 'm.login.sso')
.additionalProperties['identity_providers']
as List?)
?.map(
(json) => (
name: json['name'] as String,
id: json['id'] as String,
brand: json['brand'] as String?,
icon: json['icon'] as String?,
),
)
.toList();
final provider = ssoProviders == null
? null
: await showModalActionPopup(
context: context,
title: L10n.of(context).logInTo(client.homeserver!.host),
actions: ssoProviders
.map(
(provider) =>
AdaptiveModalAction(label: provider.name, value: provider),
)
.toList(),
);
final url = client.homeserver!.replace(
path:
'/_matrix/client/v3/login/sso/redirect${provider == null ? '' : '/${provider.id}'}',
queryParameters: {
'redirectUrl': redirectUrl.toString(),
'action': signUp ? 'register' : 'login',
},
);
final result = await FlutterWebAuth2.authenticate(
url: url.toString(),
callbackUrlScheme: urlScheme,
options: FlutterWebAuth2Options(useWebview: PlatformInfos.isMobile),
);
final token = Uri.parse(result).queryParameters['loginToken'];
if (token?.isEmpty ?? false) return;
await client.login(
LoginType.mLoginToken,
token: token,
initialDeviceDisplayName: PlatformInfos.appDisplayName,
);
}