Add QrCode authentication

This commit is contained in:
2025-07-07 19:44:17 +02:00
parent 28d47917cf
commit 0a87ac572b
13 changed files with 125 additions and 17 deletions

View File

@ -77,3 +77,4 @@ services:
- S3_SECRET_KEY=$MINIO_ROOT_PASSWORD
- REDIS_HOSTNAME=redis
- REDIS_PASSWORD=${REDIS_PASS:-secretredis}
- UNSECURE_AUTO_LOGIN_EMAIL=$UNSECURE_AUTO_LOGIN_EMAIL

View File

@ -0,0 +1,3 @@
description: This file stores settings for Dart & Flutter DevTools.
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
extensions:

View File

@ -58,7 +58,7 @@ enum AuthState {
unknown(redirectPath: homePage, allowedPaths: [homePage]),
unauthenticated(
redirectPath: authPage,
allowedPaths: [authPage, manualAuthPage, settingsPage],
allowedPaths: [authPage, qrAuthPath, manualAuthPage, settingsPage],
),
authenticated(
redirectPath: homePage,

View File

@ -35,12 +35,14 @@ class BaseAuthPage extends StatelessWidget {
],
),
body: SingleChildScrollView(
child: SeparatedColumn(
padding: EdgeInsets.all(context.gutter),
separatorBuilder: () => const Gutter(),
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: children,
child: IntrinsicHeight(
child: SeparatedColumn(
padding: EdgeInsets.all(context.gutter),
separatorBuilder: () => const Gutter(),
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: children,
),
),
),
);

View File

@ -24,7 +24,7 @@ class LoginScreen extends HookConsumerWidget {
label: "Enter manually authentication information",
),
_LoginChoice(
route: manualAuthPage,
route: qrAuthPath,
icon: Icons.qr_code_2,
label: "Scan authentication Qr Code",
),

View File

@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:logging/logging.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:moneymgr_mobile/providers/auth_state.dart';
import 'package:moneymgr_mobile/services/api/api_token.dart';
import 'package:moneymgr_mobile/services/router/routes_list.dart';
import 'package:moneymgr_mobile/utils/extensions.dart';
class QrAuthScreen extends HookConsumerWidget {
const QrAuthScreen({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final loading = useState(false);
handleCapture(BarcodeCapture barcodes) async {
if (loading.value) return;
if (barcodes.barcodes.length != 1) return;
final b = barcodes.barcodes[0];
if (b.format != BarcodeFormat.qrCode) {
context.showTextSnackBar(
"Only QrCode are supported!",
duration: Duration(seconds: 1),
);
return;
}
final value = b.rawValue ?? "";
Logger.root.finest("Decoded QrCode: $value");
if (!value.startsWith("moneymgr://")) {
context.showTextSnackBar(
"Not a MoneyMgr Qr Code!",
duration: Duration(seconds: 1),
);
return;
}
// Decode token
final uri = Uri.parse(
value.replaceFirst("moneymgr://", "http://test.com/?"),
);
final token = ApiToken(
apiUrl: uri.queryParameters["api"] ?? "",
tokenId: int.tryParse(uri.queryParameters["id"] ?? "") ?? 0,
tokenValue: uri.queryParameters["secret"] ?? "",
);
// Attempt to authenticate using token
try {
loading.value = true;
await ref.read(currentAuthStateProvider.notifier).setAuthToken(token);
if (context.mounted) {
if (context.canPop()) context.pop();
context.replace(profilePage);
}
} catch (e, s) {
Logger.root.severe("Failed to authenticate user! $e $s");
if (context.mounted) {
context.showTextSnackBar("Failed to authenticate user! $e");
}
} finally {
loading.value = false;
}
}
return Scaffold(
appBar: AppBar(title: Text('QR Authentication')),
body: MobileScanner(onDetect: handleCapture),
);
}
}

View File

@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:moneymgr_mobile/providers/auth_state.dart';
import 'package:moneymgr_mobile/routes/login/login_screen.dart';
import 'package:moneymgr_mobile/routes/login/manual_auth_screen.dart';
import 'package:moneymgr_mobile/routes/login/qr_auth_screen.dart';
import 'package:moneymgr_mobile/routes/settings/settings_screen.dart';
import 'package:moneymgr_mobile/services/router/routes_list.dart';
import 'package:moneymgr_mobile/widgets/scaffold_with_navigation.dart';
@ -61,6 +62,7 @@ GoRouter router(Ref ref) {
routes: [
GoRoute(path: homePage, builder: (_, __) => const Scaffold()),
GoRoute(path: authPage, builder: (_, __) => const LoginScreen()),
GoRoute(path: qrAuthPath, builder: (_, __) => const QrAuthScreen()),
GoRoute(
path: manualAuthPage,
builder: (_, __) => const ManualAuthScreen(),

View File

@ -4,6 +4,9 @@ const homePage = "/";
/// Authentication path
const authPage = "/login";
/// Qr Code authentication
const qrAuthPath = "/login/qr";
/// Manual authentication
const manualAuthPage = "/login/manual";

View File

@ -51,6 +51,7 @@ class SecureStorage {
if (tokenStr != null) {
return ApiToken.fromJson(jsonDecode(tokenStr));
}
return null;
}
/// Set auth token

View File

@ -15,18 +15,21 @@ extension BuildContextX on BuildContext {
/// Shows a floating snack bar with text as its content.
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showTextSnackBar(
String text,
) =>
ScaffoldMessenger.of(this).showSnackBar(SnackBar(
behavior: SnackBarBehavior.floating,
content: Text(text),
));
String text, {
Duration? duration,
}) => ScaffoldMessenger.of(this).showSnackBar(
SnackBar(
behavior: SnackBarBehavior.floating,
content: Text(text),
duration: duration ?? Duration(milliseconds: 4000),
),
);
void showAppLicensePage() => showLicensePage(
context: this,
useRootNavigator: true,
applicationName: 'MoneyMgr',
applicationLegalese: '(c) Pierre HUBERT 2025 - ${DateTime.now().year}'
applicationLegalese: '(c) Pierre HUBERT 2025 - ${DateTime.now().year}',
);
}

View File

@ -6,11 +6,13 @@ import FlutterMacOS
import Foundation
import flutter_secure_storage_macos
import mobile_scanner
import path_provider_foundation
import shared_preferences_foundation
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
}

View File

@ -632,6 +632,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.0"
mobile_scanner:
dependency: "direct main"
description:
name: mobile_scanner
sha256: "54005bdea7052d792d35b4fef0f84ec5ddc3a844b250ecd48dc192fb9b4ebc95"
url: "https://pub.dev"
source: hosted
version: "7.0.1"
package_config:
dependency: transitive
description:
@ -1071,4 +1079,4 @@ packages:
version: "3.1.3"
sdks:
dart: ">=3.8.1 <4.0.0"
flutter: ">=3.27.0"
flutter: ">=3.29.0"

View File

@ -73,6 +73,9 @@ dependencies:
# API requests
dio: ^5.8.0+1
# Qr Code library
mobile_scanner: ^7.0.1
dev_dependencies:
flutter_test:
sdk: flutter