From 8027229a7fbd56123758d60b788f123d833dbf95 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Mon, 7 Jun 2021 15:42:59 +0200 Subject: [PATCH] Add MasterCommands --- .../Cli/Commands/MasterCommands.cs | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Akari.Prototype.Server/Cli/Commands/MasterCommands.cs diff --git a/Akari.Prototype.Server/Cli/Commands/MasterCommands.cs b/Akari.Prototype.Server/Cli/Commands/MasterCommands.cs new file mode 100644 index 0000000..6c02d1a --- /dev/null +++ b/Akari.Prototype.Server/Cli/Commands/MasterCommands.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Akari.Prototype.Server.Services; +using Akari.Prototype.Server.Utils; +using Akari.Prototype.Server.Utils.Extensions; +using CliFx; +using CliFx.Attributes; +using CliFx.Exceptions; +using CliFx.Infrastructure; +using Spectre.Console; + +namespace Akari.Prototype.Server.Cli.Commands +{ + [Command("m", Description = "Manage master password")] + public class MasterCommands : ICommand + { + [Command("m c", Description = "Create master password")] + public class CreateMasterPasswordCommand : ICommand + { + private readonly IMasterKeyService _masterKeyService; + + public CreateMasterPasswordCommand(IMasterKeyService masterKeyService) + { + _masterKeyService = masterKeyService; + } + + public ValueTask ExecuteAsync(IConsole console) + { + var ansiConsole = console.AsAnsiConsole(); + + if (_masterKeyService.IsPasswordSet) + { + throw new CommandException("Can't create master password if it's already set"); + } + + ansiConsole.WriteLine(); + + string password = ansiConsole.Prompt( + new TextPrompt("Enter [yellow]master password[/]") + .PromptStyle("red") + .Secret() + ); + + string passwordConfirm = ansiConsole.Prompt( + new TextPrompt("Confirm [yellow]master password[/]") + .PromptStyle("red") + .Secret() + ); + + ansiConsole.WriteLine(); + + if (password != passwordConfirm) + { + throw new CommandException("Password doesn't match"); + } + + if (!_masterKeyService.CreatePassword(password)) + { + throw new CommandException("Created master password but can't login"); + } + + ansiConsole.Markup("[green]Successfully created [yellow]master password[/][/]"); + + return default; + } + } + + [Command("m l", Description = "Login and check master password")] + public class LoginMasterPasswordCommand : ICommand + { + private readonly IMasterKeyService _masterKeyService; + + public LoginMasterPasswordCommand(IMasterKeyService masterKeyService) + { + _masterKeyService = masterKeyService; + } + + public ValueTask ExecuteAsync(IConsole console) + { + var ansiConsole = console.AsAnsiConsole(); + + if (!_masterKeyService.IsPasswordSet) + { + throw new CommandException("Can't login if no master password has been set"); + } + + ansiConsole.WriteLine(); + + if (!CliUtils.Login(_masterKeyService, ansiConsole)) + { + ansiConsole.Markup("[red]Invalid [yellow]master password[/][/]"); + } + else + { + ansiConsole.Markup("[green]Successfully logged in[/]"); + } + + return default; + } + } + + private readonly IMasterKeyService _masterKeyService; + + public MasterCommands(IMasterKeyService masterKeyService) + { + _masterKeyService = masterKeyService; + } + + public ValueTask ExecuteAsync(IConsole console) + { + var ansiConsole = console.AsAnsiConsole(); + + if (!_masterKeyService.IsPasswordSet) + { + ansiConsole.Markup("[yellow]Master password[/] is not set"); + } + else + { + ansiConsole.Markup("[yellow]Master password[/] is set"); + } + + return default; + } + } +}