Better to solve multi-factor authenticate

This commit is contained in:
LittleSheep 2024-05-01 11:27:48 +08:00
parent 5d79692766
commit 28c0094837
5 changed files with 38 additions and 19 deletions

View File

@ -26,7 +26,7 @@ class AuthProvider extends ChangeNotifier {
DateTime? lastRefreshedAt;
Future<bool> pickClient() async {
Future<bool> loadClient() async {
if (await storage.containsKey(key: storageKey)) {
try {
final credentials = oauth2.Credentials.fromJson((await storage.read(key: storageKey))!);
@ -43,7 +43,7 @@ class AuthProvider extends ChangeNotifier {
}
Future<oauth2.Client> createClient(BuildContext context, String username, String password) async {
if (await pickClient()) {
if (await loadClient()) {
return client!;
}
@ -92,7 +92,7 @@ class AuthProvider extends ChangeNotifier {
const storage = FlutterSecureStorage();
if (await storage.containsKey(key: storageKey)) {
if (client == null) {
await pickClient();
await loadClient();
}
if (lastRefreshedAt == null || DateTime.now().subtract(const Duration(minutes: 3)).isAfter(lastRefreshedAt!)) {
await refreshToken();

View File

@ -22,7 +22,7 @@ class ChatProvider extends ChangeNotifier {
ChatCallInstance? call;
Future<WebSocketChannel?> connect(AuthProvider auth) async {
if (auth.client == null) await auth.pickClient();
if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null;
await auth.refreshToken();

View File

@ -61,7 +61,7 @@ class NotifyProvider extends ChangeNotifier {
}
Future<WebSocketChannel?> connect(AuthProvider auth) async {
if (auth.client == null) await auth.pickClient();
if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null;
await auth.refreshToken();

View File

@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
import 'package:solian/utils/service_url.dart';
import 'package:solian/widgets/exts.dart';
import 'package:solian/widgets/indent_wrapper.dart';
import 'package:url_launcher/url_launcher_string.dart';
@ -63,6 +64,11 @@ class SignInScreen extends StatelessWidget {
}).catchError((e) {
List<String> messages = e.toString().split('\n');
if (messages.last.contains("risk")) {
final ticketId = RegExp(r"ticketId=(\d+)").firstMatch(messages.last);
if (ticketId == null) {
context
.showErrorDialog("requested to multi-factor authenticate, but the ticket id was not found");
}
showDialog(
context: context,
builder: (context) {
@ -73,7 +79,9 @@ class SignInScreen extends StatelessWidget {
TextButton(
child: Text(AppLocalizations.of(context)!.next),
onPressed: () {
launchUrlString(getRequestUri('passport', '/sign-in').toString());
launchUrlString(
getRequestUri('passport', '/mfa?ticket=${ticketId!.group(1)}').toString(),
);
if (Navigator.canPop(context)) {
Navigator.pop(context);
}

View File

@ -2,17 +2,28 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
extension SolianCommonExtensions on BuildContext {
Future<void> showErrorDialog(dynamic exception) => showDialog<void>(
context: this,
builder: (ctx) => AlertDialog(
title: Text(AppLocalizations.of(this)!.errorHappened),
content: Text(exception.toString()),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: Text(AppLocalizations.of(this)!.confirmOkay),
)
],
),
);
Future<void> showErrorDialog(dynamic exception) {
String formatMessage(dynamic exception) {
final message = exception.toString();
if (message.trim().isEmpty) return '';
return message
.split(' ')
.map((element) => "${element[0].toUpperCase()}${element.substring(1).toLowerCase()}")
.join(" ");
}
return showDialog<void>(
context: this,
builder: (ctx) => AlertDialog(
title: Text(AppLocalizations.of(this)!.errorHappened),
content: Text(formatMessage(exception)),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: Text(AppLocalizations.of(this)!.confirmOkay),
)
],
),
);
}
}