Files
duplicati/Duplicati/CommandLine/ServerUtil/Commands/ChangePassword.cs
T
Kenneth Skovhede 8250a5befa Refactored SecretProviderHelper to replace in Uri instances instead of raw string.
This fixes some issues with URL encoding/decoding.
Due to the legacy class `Duplicati.Library.Utility.Uri` there are two replacements done.
2024-11-04 15:57:56 +01:00

37 lines
1.3 KiB
C#

using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using Duplicati.Library.Main;
namespace Duplicati.CommandLine.ServerUtil.Commands;
public static class ChangePassword
{
public static Command Create() =>
new Command("change-password", "Changes the server password")
{
new Argument<string>("new-password", "The new password to use") {
Arity = ArgumentArity.ZeroOrOne
},
}
.WithHandler(CommandHandler.Create<Settings, string>(async (settings, newPassword) =>
{
// Ask for previous password first, if needed
var connection = await settings.GetConnection();
if (string.IsNullOrWhiteSpace(newPassword))
newPassword = HelperMethods.ReadPasswordFromConsole("Please provide the new password: ");
if (string.IsNullOrWhiteSpace(newPassword))
throw new UserReportedException("No password provided");
if (settings.SecretProvider != null)
{
var opts = new Dictionary<string, string?>() { { "password", newPassword } };
await settings.ReplaceSecrets(opts).ConfigureAwait(false);
newPassword = opts["password"]!;
}
await connection.ChangePassword(newPassword);
}));
}