2021-10-30 14:06:10 +02:00
|
|
|
import 'dart:async';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2021-10-26 20:08:17 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2021-10-16 10:33:58 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
2021-10-26 20:08:17 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-10-16 10:33:58 +02:00
|
|
|
|
2021-10-26 20:08:17 +02:00
|
|
|
extension UiaRequestManager on MatrixState {
|
|
|
|
Future uiaRequestHandler(UiaRequest uiaRequest) async {
|
2021-10-16 10:33:58 +02:00
|
|
|
try {
|
|
|
|
if (uiaRequest.state != UiaRequestState.waitForUser ||
|
2021-10-30 14:06:10 +02:00
|
|
|
uiaRequest.nextStages.isEmpty) {
|
|
|
|
Logs().d('Uia Request Stage: ${uiaRequest.state}');
|
|
|
|
return;
|
|
|
|
}
|
2021-10-16 10:33:58 +02:00
|
|
|
final stage = uiaRequest.nextStages.first;
|
2021-10-30 14:06:10 +02:00
|
|
|
Logs().d('Uia Request Stage: $stage');
|
2021-10-16 10:33:58 +02:00
|
|
|
switch (stage) {
|
|
|
|
case AuthenticationTypes.password:
|
|
|
|
final input = cachedPassword ??
|
|
|
|
(await showTextInputDialog(
|
2021-10-26 20:08:17 +02:00
|
|
|
context: navigatorContext,
|
2021-12-03 17:29:32 +01:00
|
|
|
title: L10n.of(context)!.pleaseEnterYourPassword,
|
|
|
|
okLabel: L10n.of(context)!.ok,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
2021-10-16 10:33:58 +02:00
|
|
|
textFields: [
|
|
|
|
const DialogTextField(
|
|
|
|
minLines: 1,
|
|
|
|
maxLines: 1,
|
|
|
|
obscureText: true,
|
|
|
|
hintText: '******',
|
|
|
|
)
|
|
|
|
],
|
|
|
|
))
|
|
|
|
?.single;
|
2021-12-03 17:29:32 +01:00
|
|
|
if (input == null || input.isEmpty) {
|
2021-10-30 14:06:10 +02:00
|
|
|
return uiaRequest.cancel();
|
|
|
|
}
|
2021-10-16 10:33:58 +02:00
|
|
|
return uiaRequest.completeStage(
|
|
|
|
AuthenticationPassword(
|
|
|
|
session: uiaRequest.session,
|
|
|
|
password: input,
|
2021-12-03 17:29:32 +01:00
|
|
|
identifier: AuthenticationUserIdentifier(user: client.userID!),
|
2021-10-16 10:33:58 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
case AuthenticationTypes.emailIdentity:
|
2022-01-29 12:35:03 +01:00
|
|
|
final currentThreepidCreds = this.currentThreepidCreds;
|
|
|
|
if (currentThreepidCreds == null) {
|
2021-10-30 14:06:10 +02:00
|
|
|
return uiaRequest.cancel(
|
2021-12-03 17:29:32 +01:00
|
|
|
UiaException(L10n.of(widget.context)!.serverRequiresEmail),
|
2021-10-30 14:06:10 +02:00
|
|
|
);
|
2021-10-16 10:33:58 +02:00
|
|
|
}
|
|
|
|
final auth = AuthenticationThreePidCreds(
|
|
|
|
session: uiaRequest.session,
|
|
|
|
type: AuthenticationTypes.emailIdentity,
|
2021-11-04 16:09:12 +01:00
|
|
|
threepidCreds: ThreepidCreds(
|
|
|
|
sid: currentThreepidCreds.sid,
|
|
|
|
clientSecret: currentClientSecret,
|
|
|
|
),
|
2021-10-16 10:33:58 +02:00
|
|
|
);
|
|
|
|
if (OkCancelResult.ok ==
|
|
|
|
await showOkCancelAlertDialog(
|
|
|
|
useRootNavigator: false,
|
2021-10-26 20:08:17 +02:00
|
|
|
context: navigatorContext,
|
2021-12-03 17:29:32 +01:00
|
|
|
title: L10n.of(context)!.weSentYouAnEmail,
|
|
|
|
message: L10n.of(context)!.pleaseClickOnLink,
|
|
|
|
okLabel: L10n.of(context)!.iHaveClickedOnLink,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
2021-10-16 10:33:58 +02:00
|
|
|
)) {
|
|
|
|
return uiaRequest.completeStage(auth);
|
|
|
|
}
|
|
|
|
return uiaRequest.cancel();
|
|
|
|
case AuthenticationTypes.dummy:
|
|
|
|
return uiaRequest.completeStage(
|
|
|
|
AuthenticationData(
|
|
|
|
type: AuthenticationTypes.dummy,
|
|
|
|
session: uiaRequest.session,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
default:
|
2021-10-30 14:06:10 +02:00
|
|
|
final url = Uri.parse(client.homeserver.toString() +
|
|
|
|
'/_matrix/client/r0/auth/$stage/fallback/web?session=${uiaRequest.session}');
|
2022-02-14 15:59:10 +01:00
|
|
|
launch(
|
|
|
|
url.toString(),
|
|
|
|
forceSafariVC: true,
|
2022-02-17 20:47:26 +01:00
|
|
|
forceWebView: false,
|
2022-02-14 15:59:10 +01:00
|
|
|
);
|
|
|
|
if (OkCancelResult.ok ==
|
|
|
|
await showOkCancelAlertDialog(
|
|
|
|
useRootNavigator: false,
|
|
|
|
message: L10n.of(context)!.pleaseFollowInstructionsOnWeb,
|
|
|
|
context: navigatorContext,
|
|
|
|
okLabel: L10n.of(context)!.next,
|
|
|
|
cancelLabel: L10n.of(context)!.cancel,
|
|
|
|
)) {
|
|
|
|
return uiaRequest.completeStage(
|
|
|
|
AuthenticationData(session: uiaRequest.session),
|
2021-10-16 10:33:58 +02:00
|
|
|
);
|
|
|
|
} else {
|
2022-02-14 15:59:10 +01:00
|
|
|
return uiaRequest.cancel();
|
2021-10-16 10:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().e('Error while background UIA', e, s);
|
2021-10-26 15:39:54 +02:00
|
|
|
return uiaRequest.cancel(e is Exception ? e : Exception(e));
|
2021-10-16 10:33:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-30 14:06:10 +02:00
|
|
|
|
|
|
|
class UiaException implements Exception {
|
|
|
|
final String reason;
|
|
|
|
|
|
|
|
UiaException(this.reason);
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => reason;
|
|
|
|
}
|