diff --git a/web/repertory/lib/helpers.dart b/web/repertory/lib/helpers.dart index 49a4a6e7..6d5f25c7 100644 --- a/web/repertory/lib/helpers.dart +++ b/web/repertory/lib/helpers.dart @@ -12,6 +12,10 @@ class NullPasswordException implements Exception { String error() => 'password cannot be null'; } +class AuthenticationFailedException implements Exception { + String error() => 'failed to authenticate user'; +} + // ignore: prefer_function_declarations_over_variables final Validator noRestrictedChars = (value) { return [ @@ -231,30 +235,37 @@ bool validateSettings( } Future> convertAllToString( - Map settings, -) async { - final password = await promptPassword(); - if (password == null) { - throw NullPasswordException(); + Map settings, { + String? password, +}) async { + for (var entry in settings.entries) { + if (entry.value is Map) { + convertAllToString(entry.value, password: password); + continue; + } + + if (entry.key == 'ApiPassword' || + entry.key == 'EncryptionToken' || + entry.key == 'SecretKey') { + if (entry.value.isEmpty) { + continue; + } + + if (password == null) { + password = await promptPassword(); + if (password == null) { + throw NullPasswordException(); + } + } + + settings[entry.key] = encryptValue(entry.value, password); + } else if (entry.value is String) { + continue; + } + + settings[entry.key] = entry.value.toString(); } - settings.forEach((key, value) { - if (value is Map) { - convertAllToString(value); - return; - } - - if (key == 'ApiPassword' || - key == 'EncryptionToken' || - key == 'SecretKey') { - value = encryptValue(value, password); - } else if (value is String) { - return; - } - - settings[key] = value.toString(); - }); - return settings; } diff --git a/web/repertory/lib/widgets/ui_settings.dart b/web/repertory/lib/widgets/ui_settings.dart index be6feb8f..9d06f912 100644 --- a/web/repertory/lib/widgets/ui_settings.dart +++ b/web/repertory/lib/widgets/ui_settings.dart @@ -3,9 +3,12 @@ import 'dart:convert' show jsonEncode; import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; +import 'package:repertory/constants.dart' as constants; import 'package:repertory/helpers.dart' show + AuthenticationFailedException, convertAllToString, + displayErrorMessage, getBaseUri, getSettingDescription, getSettingValidators, @@ -104,22 +107,36 @@ class _UISettingsWidgetState extends State { widget.settings, widget.origSettings, )) { + displayAuthError() { + if (constants.navigatorKey.currentContext != null) { + displayErrorMessage( + constants.navigatorKey.currentContext!, + "Authentication failed", + ); + } + } + convertAllToString(widget.settings) .then((map) async { try { - await http.put( + final response = await http.put( Uri.parse( Uri.encodeFull( '${getBaseUri()}/api/v1/settings?data=${jsonEncode(map)}', ), ), ); + if (response.statusCode == 500) { + displayAuthError(); + } } catch (e) { debugPrint('$e'); + displayAuthError(); } }) .catchError((e) { debugPrint('$e'); + displayAuthError(); }); }