Add AddFingerprintCommand

This commit is contained in:
2021-06-05 10:48:43 +02:00
parent f6794a09a3
commit b08bb91493
2 changed files with 28 additions and 2 deletions

View File

@@ -9,6 +9,8 @@ using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Cli.Commands
@@ -18,12 +20,19 @@ namespace Akari.Prototype.Server.Cli.Commands
{
private readonly IFingerprintManager _fingerprintManager;
[Command("fp add", Description = "Add a fingerprint")]
public class AddFingerprint : ICommand
[Command("fp a", Description = "Add a fingerprint")]
public class AddFingerprintCommand : ICommand
{
[CommandParameter(0, Description = "The fingerprint name")]
public string Name { get; init; }
private readonly IFingerprintManager _fingerprintManager;
public AddFingerprintCommand(IFingerprintManager fingerprintManager)
{
_fingerprintManager = fingerprintManager;
}
public ValueTask ExecuteAsync(IConsole console)
{
if (Name.Length > 50)
@@ -31,6 +40,22 @@ namespace Akari.Prototype.Server.Cli.Commands
throw new CommandException("Name can't be more than 50 characters");
}
var data = Encoding.UTF8.GetBytes(Name);
if (data.Length > TcpProviderService.MaxNameLength)
{
throw new CommandException("Name can't be more than 200 bytes (UTF-8)");
}
var hash = Security.NewArgon2idHash(data);
if (!_fingerprintManager.Add(Name, hash))
{
throw new CommandException("A fingerprint with this name is already registered.");
}
console.AsAnsiConsole().Markup($"[green]Successfully added {Name}:\n{hash}[/]");
return default;
}
}

View File

@@ -76,6 +76,7 @@ namespace Akari.Prototype.Server
services.AddTransient<FingerprintCommands>();
services.AddTransient<FingerprintCommands.RemoveFingerprintCommand>();
services.AddTransient<FingerprintCommands.AddFingerprintCommand>();
});
public static IHostBuilder CreateWebHostBuilder(string[] args) =>