From 3658bdcba0e8944539abd0f27369398a4a8e0435 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Mon, 7 Jun 2021 18:38:23 +0200 Subject: [PATCH] Add AddFingerprintToApplicationCommand --- .../Cli/Commands/ApplicationCommands.cs | 73 ++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/Akari.Prototype.Server/Cli/Commands/ApplicationCommands.cs b/Akari.Prototype.Server/Cli/Commands/ApplicationCommands.cs index 8c3f152..d31cda0 100644 --- a/Akari.Prototype.Server/Cli/Commands/ApplicationCommands.cs +++ b/Akari.Prototype.Server/Cli/Commands/ApplicationCommands.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; +using Akari.Prototype.Server.Options; using Akari.Prototype.Server.Services; using Akari.Prototype.Server.Utils; using Akari.Prototype.Server.Utils.Extensions; @@ -9,6 +11,10 @@ using CliFx; using CliFx.Attributes; using CliFx.Exceptions; using CliFx.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Spectre.Console; namespace Akari.Prototype.Server.Cli.Commands @@ -81,12 +87,77 @@ namespace Akari.Prototype.Server.Cli.Commands throw new CommandException($"The application {Name} doesn't exist"); } - console.AsAnsiConsole().Markup($"[green]Successfully deleted {Name}[/]"); + console.AsAnsiConsole().MarkupLine($"[green]Successfully deleted {Name}[/]"); return default; } } + [Command("app f", Description = "Add a fingerprint to and application")] + public class AddFingerprintToApplicationCommand : ICommand + { + [CommandParameter(0, Description = "Application to add fingerprint to")] + public string Application { get; init; } + + [CommandParameter(1, Description = "Fingerprint to add")] + public string Fingerprint { get; init; } + + private readonly IServiceProvider _serviceProvider; + private readonly IApplicationsManager _applications; + private readonly IMasterKeyService _masterKeyService; + + public AddFingerprintToApplicationCommand(IServiceProvider serviceProvider, IApplicationsManager applications, IMasterKeyService masterKeyService) + { + _serviceProvider = serviceProvider; + _applications = applications; + _masterKeyService = masterKeyService; + } + + public async ValueTask ExecuteAsync(IConsole console) + { + var ansiConsole = console.AsAnsiConsole(); + + if (!_applications.Contains(Application)) + { + throw new CommandException("No application exist with this name"); + } + + if (_applications.IsFingerprintRegistered(Application, Fingerprint)) + { + throw new CommandException($"'{Fingerprint}' is already registered for '{Application}'"); + } + + if (!CliUtils.Login(_masterKeyService, ansiConsole)) + { + throw new CommandException("Can't proceed without master service login"); + } + + using var scope = _serviceProvider.CreateScope(); + + var services = scope.ServiceProvider; + + using var provider = new TcpProviderService(services.GetRequiredService>(), + services.GetRequiredService>(), + services.GetRequiredService(), + services.GetRequiredService()); + + var source = new CancellationTokenSource(); + + await provider.StartAsync(source.Token); + + ansiConsole.WriteLine("Press a key when the fingerprint has been auth"); + + console.Input.Read(); + + if (!_applications.AddFingerprint(Application, Fingerprint)) + { + throw new CommandException($"Fingerprint '{Fingerprint}' has not been auth"); + } + + ansiConsole.WriteLine($"[green]Successfully added '{Fingerprint}' to '{Application}'[/]"); + } + } + private readonly IApplicationsManager _applications; public ApplicationCommands(IApplicationsManager applications)