Fix AddFingerprintCommand

This commit is contained in:
2021-06-05 11:34:59 +02:00
parent 933d123d44
commit 2af811873b

View File

@@ -41,21 +41,28 @@ 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)
if (Encoding.UTF8.GetBytes(Name).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))
if (_fingerprintManager.Contains(Name))
{
throw new CommandException("A fingerprint with this name is already registered.");
}
console.AsAnsiConsole().Markup($"[green]Successfully added {Name}:\n{hash}[/]");
var token = new byte[TcpProviderService.MaxTokenLength];
RandomNumberGenerator.Fill(token);
var hash = Security.NewArgon2idHash(token);
_fingerprintManager.Add(Name, hash);
console.AsAnsiConsole().MarkupLine($"[green]Successfully added {Name}\n[/]");
console.AsAnsiConsole().MarkupLine($"[yellow]Token: {Convert.ToBase64String(token)}[/]");
console.AsAnsiConsole().MarkupLine($"[grey]Hash: {hash}\n[/]");
console.AsAnsiConsole().MarkupLine($"[silver]Note that you won't be able to retrieve the [yellow]token[/] later, it's [red]never stored[/][/]");
return default;
}