feat(admincli): add generate key command

This commit is contained in:
Stevan Freeborn
2026-02-06 06:53:20 -06:00
parent 9b4f0c6b9e
commit b2086d8185
4 changed files with 21 additions and 3 deletions
+10 -2
View File
@@ -3,7 +3,9 @@ namespace FiscalOS.AdminCLI;
internal sealed class App( internal sealed class App(
IAnsiConsole console, IAnsiConsole console,
IPasswordHasher passwordHasher, IPasswordHasher passwordHasher,
IServiceScopeFactory serviceScopeFactory IServiceScopeFactory serviceScopeFactory,
IEncryptor encryptor,
IKeyRing keyRing
) : IHostedService ) : IHostedService
{ {
private readonly IAnsiConsole _console = console; private readonly IAnsiConsole _console = console;
@@ -32,13 +34,19 @@ internal sealed class App(
); );
var hashedPassword = passwordHasher.Hash(password); var hashedPassword = passwordHasher.Hash(password);
var user = User.From(username, hashedPassword); var userEncryptionKey = await encryptor.GenerateEncryptedKeyAsync(cancellationToken);
var user = User.From(username, hashedPassword, userEncryptionKey);
await appDbContext.Users.AddAsync(user, cancellationToken); await appDbContext.Users.AddAsync(user, cancellationToken);
await appDbContext.SaveChangesAsync(cancellationToken); await appDbContext.SaveChangesAsync(cancellationToken);
_console.MarkupLine($"User [green]{username}[/] created successfully."); _console.MarkupLine($"User [green]{username}[/] created successfully.");
break; break;
case Commands.GenerateKey:
var key = encryptor.GenerateKey();
var keyRingEntry = await keyRing.SaveKeyAsync(key);
_console.MarkupLine($"Key generated with id [green]{keyRingEntry.KeyId}[/] generated successfully.");
break;
case Commands.Exit: case Commands.Exit:
default: default:
break; break;
+6 -1
View File
@@ -3,7 +3,12 @@ namespace FiscalOS.AdminCLI;
internal static class Commands internal static class Commands
{ {
public const string CreateUser = "Create User"; public const string CreateUser = "Create User";
public const string GenerateKey = "Generate Key";
public const string Exit = "Exit"; public const string Exit = "Exit";
public static readonly string[] All = [CreateUser, Exit]; public static readonly string[] All = [
CreateUser,
GenerateKey,
Exit
];
} }
+1
View File
@@ -1,6 +1,7 @@
global using FiscalOS.AdminCLI; global using FiscalOS.AdminCLI;
global using FiscalOS.Core.Authentication; global using FiscalOS.Core.Authentication;
global using FiscalOS.Core.Identity; global using FiscalOS.Core.Identity;
global using FiscalOS.Core.Security;
global using FiscalOS.Infra.Data; global using FiscalOS.Infra.Data;
global using FiscalOS.Infra.DependencyInjection; global using FiscalOS.Infra.DependencyInjection;
@@ -1,5 +1,9 @@
{ {
"AppDbContextOptions": { "AppDbContextOptions": {
"DatabaseFilePath": "DatabaseFilePath" "DatabaseFilePath": "DatabaseFilePath"
},
"FileKeyRingOptions": {
"KeysDirectoryPath": "KeysDirectoryPath",
"PrimaryKeyId": "PrimaryKeyId"
} }
} }