From b72744716db052cb7e7c54d7c8a235c1feea2502 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Thu, 20 Mar 2025 17:33:32 -0500 Subject: [PATCH] prompt password --- web/repertory/lib/helpers.dart | 54 ++++++++++++++++++++-- web/repertory/lib/models/mount_list.dart | 3 +- web/repertory/lib/widgets/ui_settings.dart | 27 ++++++----- 3 files changed, 67 insertions(+), 17 deletions(-) diff --git a/web/repertory/lib/helpers.dart b/web/repertory/lib/helpers.dart index 382a9154..ebf0d8df 100644 --- a/web/repertory/lib/helpers.dart +++ b/web/repertory/lib/helpers.dart @@ -8,6 +8,10 @@ import 'package:sodium_libs/sodium_libs.dart'; typedef Validator = bool Function(String); +class NullPasswordException implements Exception { + String error() => 'password cannot be null'; +} + // ignore: prefer_function_declarations_over_variables final Validator noRestrictedChars = (value) { return [ @@ -226,8 +230,13 @@ bool validateSettings( return failed.isEmpty; } -Map convertAllToString(Map settings) { - final password = 'test'; +Future> convertAllToString( + Map settings, +) async { + final password = await promptPassword(); + if (password == null) { + throw NullPasswordException(); + } settings.forEach((key, value) { if (value is Map) { @@ -260,11 +269,9 @@ String encryptValue(String value, String password) { message: Uint8List.fromList(password.toCharArray()), ); - debugPrint("key: ${base64Encode(keyHash)}"); final crypto = sodium.crypto.aeadXChaCha20Poly1305IETF; final nonce = sodium.secureRandom(crypto.nonceBytes).extractBytes(); - debugPrint("nonce: ${base64Encode(nonce)}"); final data = crypto.encrypt( additionalData: Uint8List.fromList('repertory'.toCharArray()), key: SecureKey.fromList(sodium, keyHash), @@ -274,3 +281,42 @@ String encryptValue(String value, String password) { return base64Encode(Uint8List.fromList([...nonce, ...data])); } + +Future promptPassword() async { + if (constants.navigatorKey.currentContext == null) { + return null; + } + + String updatedValue1 = ''; + return await showDialog( + context: constants.navigatorKey.currentContext!, + builder: (context) { + return AlertDialog( + actions: [ + TextButton( + child: const Text('Cancel'), + onPressed: () => Navigator.of(context).pop(null), + ), + TextButton( + child: const Text('OK'), + onPressed: () { + if (updatedValue1.isEmpty) { + return displayErrorMessage(context, "Password is not valid"); + } + + Navigator.of(context).pop(updatedValue1); + }, + ), + ], + content: TextField( + autofocus: true, + controller: TextEditingController(text: updatedValue1), + obscureText: true, + obscuringCharacter: '*', + onChanged: (value) => updatedValue1 = value, + ), + title: const Text('Enter Authentication Password'), + ); + }, + ); +} diff --git a/web/repertory/lib/models/mount_list.dart b/web/repertory/lib/models/mount_list.dart index a2cca447..b05ca1a7 100644 --- a/web/repertory/lib/models/mount_list.dart +++ b/web/repertory/lib/models/mount_list.dart @@ -94,10 +94,11 @@ class MountList with ChangeNotifier { Map mountConfig, ) async { try { + final map = await convertAllToString(mountConfig); await http.post( Uri.parse( Uri.encodeFull( - '${getBaseUri()}/api/v1/add_mount?name=$name&type=$type&config=${jsonEncode(convertAllToString(mountConfig))}', + '${getBaseUri()}/api/v1/add_mount?name=$name&type=$type&config=${jsonEncode(map)}', ), ), ); diff --git a/web/repertory/lib/widgets/ui_settings.dart b/web/repertory/lib/widgets/ui_settings.dart index 320d495d..be6feb8f 100644 --- a/web/repertory/lib/widgets/ui_settings.dart +++ b/web/repertory/lib/widgets/ui_settings.dart @@ -1,4 +1,4 @@ -import 'dart:convert' show jsonDecode, jsonEncode; +import 'dart:convert' show jsonEncode; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; @@ -100,21 +100,24 @@ class _UISettingsWidgetState extends State { @override void dispose() { - debugPrint('current: ${jsonEncode(widget.settings)}'); - debugPrint('orig: ${jsonEncode(widget.origSettings)}'); if (!DeepCollectionEquality().equals( widget.settings, widget.origSettings, )) { - http - .put( - Uri.parse( - Uri.encodeFull( - '${getBaseUri()}/api/v1/settings?data=${jsonEncode(convertAllToString(widget.settings))}', - ), - ), - ) - .then((_) {}) + convertAllToString(widget.settings) + .then((map) async { + try { + await http.put( + Uri.parse( + Uri.encodeFull( + '${getBaseUri()}/api/v1/settings?data=${jsonEncode(map)}', + ), + ), + ); + } catch (e) { + debugPrint('$e'); + } + }) .catchError((e) { debugPrint('$e'); });