From b08bb91493b7931d1bdc017d069f58e2cba1a457 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sat, 5 Jun 2021 10:48:43 +0200 Subject: [PATCH] Add AddFingerprintCommand --- .../Cli/Commands/FingerprintCommands.cs | 29 +++++++++++++++++-- Akari.Prototype.Server/Program.cs | 1 + 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs b/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs index 8470090..c253d68 100644 --- a/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs +++ b/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs @@ -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; } } diff --git a/Akari.Prototype.Server/Program.cs b/Akari.Prototype.Server/Program.cs index f935346..d9ded07 100644 --- a/Akari.Prototype.Server/Program.cs +++ b/Akari.Prototype.Server/Program.cs @@ -76,6 +76,7 @@ namespace Akari.Prototype.Server services.AddTransient(); services.AddTransient(); + services.AddTransient(); }); public static IHostBuilder CreateWebHostBuilder(string[] args) =>