2021-10-26 20:01:53 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2021-10-16 10:33:58 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
2021-10-16 10:33:58 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-10-26 20:01:53 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2021-10-16 10:33:58 +02:00
|
|
|
|
|
|
|
class UiaRequestManager {
|
2021-10-26 20:01:53 +02:00
|
|
|
final MatrixState matrix;
|
2021-10-16 10:33:58 +02:00
|
|
|
final String cachedPassword;
|
|
|
|
|
2021-10-26 20:01:53 +02:00
|
|
|
UiaRequestManager(this.matrix, [this.cachedPassword]);
|
2021-10-16 10:33:58 +02:00
|
|
|
|
|
|
|
Future onUiaRequest(UiaRequest uiaRequest) async {
|
|
|
|
try {
|
|
|
|
if (uiaRequest.state != UiaRequestState.waitForUser ||
|
|
|
|
uiaRequest.nextStages.isEmpty) return;
|
|
|
|
final stage = uiaRequest.nextStages.first;
|
|
|
|
switch (stage) {
|
|
|
|
case AuthenticationTypes.password:
|
|
|
|
final input = cachedPassword ??
|
|
|
|
(await showTextInputDialog(
|
2021-10-26 20:01:53 +02:00
|
|
|
context: matrix.navigatorContext,
|
|
|
|
title: L10n.of(matrix.context).pleaseEnterYourPassword,
|
|
|
|
okLabel: L10n.of(matrix.context).ok,
|
|
|
|
cancelLabel: L10n.of(matrix.context).cancel,
|
2021-10-16 10:33:58 +02:00
|
|
|
textFields: [
|
|
|
|
const DialogTextField(
|
|
|
|
minLines: 1,
|
|
|
|
maxLines: 1,
|
|
|
|
obscureText: true,
|
|
|
|
hintText: '******',
|
|
|
|
)
|
|
|
|
],
|
|
|
|
))
|
|
|
|
?.single;
|
|
|
|
if (input?.isEmpty ?? true) return;
|
|
|
|
return uiaRequest.completeStage(
|
|
|
|
AuthenticationPassword(
|
|
|
|
session: uiaRequest.session,
|
|
|
|
password: input,
|
2021-10-26 20:01:53 +02:00
|
|
|
identifier:
|
|
|
|
AuthenticationUserIdentifier(user: matrix.client.userID),
|
2021-10-16 10:33:58 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
case AuthenticationTypes.emailIdentity:
|
|
|
|
final emailInput = await showTextInputDialog(
|
2021-10-26 20:01:53 +02:00
|
|
|
context: matrix.navigatorContext,
|
|
|
|
message: L10n.of(matrix.context).serverRequiresEmail,
|
|
|
|
okLabel: L10n.of(matrix.context).next,
|
|
|
|
cancelLabel: L10n.of(matrix.context).cancel,
|
2021-10-16 10:33:58 +02:00
|
|
|
textFields: [
|
|
|
|
DialogTextField(
|
2021-10-26 20:01:53 +02:00
|
|
|
hintText: L10n.of(matrix.context).addEmail,
|
2021-10-16 10:33:58 +02:00
|
|
|
keyboardType: TextInputType.emailAddress,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
if (emailInput == null || emailInput.isEmpty) {
|
2021-10-26 20:01:53 +02:00
|
|
|
return uiaRequest
|
|
|
|
.cancel(Exception(L10n.of(matrix.context).serverRequiresEmail));
|
2021-10-16 10:33:58 +02:00
|
|
|
}
|
|
|
|
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
|
2021-10-26 20:01:53 +02:00
|
|
|
final currentThreepidCreds =
|
|
|
|
await matrix.client.requestTokenToRegisterEmail(
|
2021-10-16 10:33:58 +02:00
|
|
|
clientSecret,
|
|
|
|
emailInput.single,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
final auth = AuthenticationThreePidCreds(
|
|
|
|
session: uiaRequest.session,
|
|
|
|
type: AuthenticationTypes.emailIdentity,
|
|
|
|
threepidCreds: [
|
|
|
|
ThreepidCreds(
|
|
|
|
sid: currentThreepidCreds.sid,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
if (OkCancelResult.ok ==
|
|
|
|
await showOkCancelAlertDialog(
|
|
|
|
useRootNavigator: false,
|
2021-10-26 20:01:53 +02:00
|
|
|
context: matrix.navigatorContext,
|
|
|
|
title: L10n.of(matrix.context).weSentYouAnEmail,
|
|
|
|
message: L10n.of(matrix.context).pleaseClickOnLink,
|
|
|
|
okLabel: L10n.of(matrix.context).iHaveClickedOnLink,
|
|
|
|
cancelLabel: L10n.of(matrix.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:
|
|
|
|
await launch(
|
2021-10-26 20:01:53 +02:00
|
|
|
matrix.client.homeserver.toString() +
|
2021-10-16 10:33:58 +02:00
|
|
|
'/_matrix/client/r0/auth/$stage/fallback/web?session=${uiaRequest.session}',
|
|
|
|
);
|
|
|
|
if (OkCancelResult.ok ==
|
|
|
|
await showOkCancelAlertDialog(
|
|
|
|
useRootNavigator: false,
|
2021-10-26 20:01:53 +02:00
|
|
|
message: L10n.of(matrix.context).pleaseFollowInstructionsOnWeb,
|
|
|
|
context: matrix.navigatorContext,
|
|
|
|
okLabel: L10n.of(matrix.context).next,
|
|
|
|
cancelLabel: L10n.of(matrix.context).cancel,
|
2021-10-16 10:33:58 +02:00
|
|
|
)) {
|
|
|
|
return uiaRequest.completeStage(
|
|
|
|
AuthenticationData(session: uiaRequest.session),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return uiaRequest.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|