Add QrCode authentication
This commit is contained in:
@ -76,4 +76,5 @@ services:
|
|||||||
- S3_ACCESS_KEY=$MINIO_ROOT_USER
|
- S3_ACCESS_KEY=$MINIO_ROOT_USER
|
||||||
- S3_SECRET_KEY=$MINIO_ROOT_PASSWORD
|
- S3_SECRET_KEY=$MINIO_ROOT_PASSWORD
|
||||||
- REDIS_HOSTNAME=redis
|
- REDIS_HOSTNAME=redis
|
||||||
- REDIS_PASSWORD=${REDIS_PASS:-secretredis}
|
- REDIS_PASSWORD=${REDIS_PASS:-secretredis}
|
||||||
|
- UNSECURE_AUTO_LOGIN_EMAIL=$UNSECURE_AUTO_LOGIN_EMAIL
|
||||||
|
3
moneymgr_mobile/devtools_options.yaml
Normal file
3
moneymgr_mobile/devtools_options.yaml
Normal 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:
|
@ -58,7 +58,7 @@ enum AuthState {
|
|||||||
unknown(redirectPath: homePage, allowedPaths: [homePage]),
|
unknown(redirectPath: homePage, allowedPaths: [homePage]),
|
||||||
unauthenticated(
|
unauthenticated(
|
||||||
redirectPath: authPage,
|
redirectPath: authPage,
|
||||||
allowedPaths: [authPage, manualAuthPage, settingsPage],
|
allowedPaths: [authPage, qrAuthPath, manualAuthPage, settingsPage],
|
||||||
),
|
),
|
||||||
authenticated(
|
authenticated(
|
||||||
redirectPath: homePage,
|
redirectPath: homePage,
|
||||||
|
@ -35,12 +35,14 @@ class BaseAuthPage extends StatelessWidget {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: SingleChildScrollView(
|
body: SingleChildScrollView(
|
||||||
child: SeparatedColumn(
|
child: IntrinsicHeight(
|
||||||
padding: EdgeInsets.all(context.gutter),
|
child: SeparatedColumn(
|
||||||
separatorBuilder: () => const Gutter(),
|
padding: EdgeInsets.all(context.gutter),
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
separatorBuilder: () => const Gutter(),
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: children,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: children,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -24,7 +24,7 @@ class LoginScreen extends HookConsumerWidget {
|
|||||||
label: "Enter manually authentication information",
|
label: "Enter manually authentication information",
|
||||||
),
|
),
|
||||||
_LoginChoice(
|
_LoginChoice(
|
||||||
route: manualAuthPage,
|
route: qrAuthPath,
|
||||||
icon: Icons.qr_code_2,
|
icon: Icons.qr_code_2,
|
||||||
label: "Scan authentication Qr Code",
|
label: "Scan authentication Qr Code",
|
||||||
),
|
),
|
||||||
|
80
moneymgr_mobile/lib/routes/login/qr_auth_screen.dart
Normal file
80
moneymgr_mobile/lib/routes/login/qr_auth_screen.dart
Normal 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),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|||||||
import 'package:moneymgr_mobile/providers/auth_state.dart';
|
import 'package:moneymgr_mobile/providers/auth_state.dart';
|
||||||
import 'package:moneymgr_mobile/routes/login/login_screen.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/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/routes/settings/settings_screen.dart';
|
||||||
import 'package:moneymgr_mobile/services/router/routes_list.dart';
|
import 'package:moneymgr_mobile/services/router/routes_list.dart';
|
||||||
import 'package:moneymgr_mobile/widgets/scaffold_with_navigation.dart';
|
import 'package:moneymgr_mobile/widgets/scaffold_with_navigation.dart';
|
||||||
@ -61,6 +62,7 @@ GoRouter router(Ref ref) {
|
|||||||
routes: [
|
routes: [
|
||||||
GoRoute(path: homePage, builder: (_, __) => const Scaffold()),
|
GoRoute(path: homePage, builder: (_, __) => const Scaffold()),
|
||||||
GoRoute(path: authPage, builder: (_, __) => const LoginScreen()),
|
GoRoute(path: authPage, builder: (_, __) => const LoginScreen()),
|
||||||
|
GoRoute(path: qrAuthPath, builder: (_, __) => const QrAuthScreen()),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: manualAuthPage,
|
path: manualAuthPage,
|
||||||
builder: (_, __) => const ManualAuthScreen(),
|
builder: (_, __) => const ManualAuthScreen(),
|
||||||
|
@ -4,6 +4,9 @@ const homePage = "/";
|
|||||||
/// Authentication path
|
/// Authentication path
|
||||||
const authPage = "/login";
|
const authPage = "/login";
|
||||||
|
|
||||||
|
/// Qr Code authentication
|
||||||
|
const qrAuthPath = "/login/qr";
|
||||||
|
|
||||||
/// Manual authentication
|
/// Manual authentication
|
||||||
const manualAuthPage = "/login/manual";
|
const manualAuthPage = "/login/manual";
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@ class SecureStorage {
|
|||||||
if (tokenStr != null) {
|
if (tokenStr != null) {
|
||||||
return ApiToken.fromJson(jsonDecode(tokenStr));
|
return ApiToken.fromJson(jsonDecode(tokenStr));
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set auth token
|
/// Set auth token
|
||||||
|
@ -15,18 +15,21 @@ extension BuildContextX on BuildContext {
|
|||||||
|
|
||||||
/// Shows a floating snack bar with text as its content.
|
/// Shows a floating snack bar with text as its content.
|
||||||
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showTextSnackBar(
|
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> showTextSnackBar(
|
||||||
String text,
|
String text, {
|
||||||
) =>
|
Duration? duration,
|
||||||
ScaffoldMessenger.of(this).showSnackBar(SnackBar(
|
}) => ScaffoldMessenger.of(this).showSnackBar(
|
||||||
behavior: SnackBarBehavior.floating,
|
SnackBar(
|
||||||
content: Text(text),
|
behavior: SnackBarBehavior.floating,
|
||||||
));
|
content: Text(text),
|
||||||
|
duration: duration ?? Duration(milliseconds: 4000),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
void showAppLicensePage() => showLicensePage(
|
void showAppLicensePage() => showLicensePage(
|
||||||
context: this,
|
context: this,
|
||||||
useRootNavigator: true,
|
useRootNavigator: true,
|
||||||
applicationName: 'MoneyMgr',
|
applicationName: 'MoneyMgr',
|
||||||
applicationLegalese: '(c) Pierre HUBERT 2025 - ${DateTime.now().year}'
|
applicationLegalese: '(c) Pierre HUBERT 2025 - ${DateTime.now().year}',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,11 +6,13 @@ import FlutterMacOS
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
import flutter_secure_storage_macos
|
import flutter_secure_storage_macos
|
||||||
|
import mobile_scanner
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
import shared_preferences_foundation
|
import shared_preferences_foundation
|
||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
|
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
|
||||||
|
MobileScannerPlugin.register(with: registry.registrar(forPlugin: "MobileScannerPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
}
|
}
|
||||||
|
@ -632,6 +632,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
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:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -1071,4 +1079,4 @@ packages:
|
|||||||
version: "3.1.3"
|
version: "3.1.3"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=3.8.1 <4.0.0"
|
dart: ">=3.8.1 <4.0.0"
|
||||||
flutter: ">=3.27.0"
|
flutter: ">=3.29.0"
|
||||||
|
@ -73,6 +73,9 @@ dependencies:
|
|||||||
# API requests
|
# API requests
|
||||||
dio: ^5.8.0+1
|
dio: ^5.8.0+1
|
||||||
|
|
||||||
|
# Qr Code library
|
||||||
|
mobile_scanner: ^7.0.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Reference in New Issue
Block a user