[WIP] feat: scoped api keys

This commit is contained in:
Iris System 2025-08-17 02:47:01 -07:00
parent e7ee593a85
commit 06cb160f95
45 changed files with 1264 additions and 154 deletions

View file

@ -33,6 +33,11 @@ public partial class CommandTree
public static Command ConfigProxySwitch = new Command("config proxyswitch", "config proxyswitch [new|add|off]", "Switching behavior when proxy tags are used");
public static Command ConfigNameFormat = new Command("config nameformat", "config nameformat [format]", "Changes your system's username formatting");
public static Command ConfigServerNameFormat = new Command("config servernameformat", "config servernameformat [format]", "Changes your system's username formatting in the current server");
public static Command ApiKeyCreate = new Command("system apikey new", "system apikey new <name> <type>", "Create a new API key");
public static Command ApiKeyList = new Command("system apikey list", "system apikey list", "Show current API keys");
public static Command ApiKeyRename = new Command("system apikey <key> rename", "system apikey <key> rename <name>", "Rename an existing API key");
public static Command ApiKeyDelete = new Command("system apikey <key> delete", "system apikey <key> delete", "Delete an existing API key");
public static Command ApiKeyDeleteAll = new Command("system apikey deleteall", "system apikey deleteall", "Delete all existing API keys");
public static Command AutoproxySet = new Command("autoproxy", "autoproxy [off|front|latch|member]", "Sets your system's autoproxy mode for the current server");
public static Command AutoproxyOff = new Command("autoproxy off", "autoproxy off", "Disables autoproxying for your system in the current server");
public static Command AutoproxyFront = new Command("autoproxy front", "autoproxy front", "Sets your system's autoproxy in this server to proxy the first member currently registered as front");

View file

@ -218,6 +218,8 @@ public partial class CommandTree
// todo: these aren't deprecated but also shouldn't be here
else if (ctx.Match("webhook", "hook"))
await ctx.Execute<Api>(null, m => m.SystemWebhook(ctx));
else if (ctx.Match("apikey", "apikeys", "apitoken", "apitokens"))
await HandleSystemApiKeyCommand(ctx);
else if (ctx.Match("proxy"))
await ctx.Execute<SystemEdit>(SystemProxy, m => m.SystemProxy(ctx));
@ -322,6 +324,42 @@ public partial class CommandTree
await ctx.CheckSystem(target).Execute<Random>(MemberRandom, m => m.Member(ctx, target));
}
private async Task HandleSystemApiKeyCommand(Context ctx)
{
ctx.CheckSystem();
if (ctx.Match("new", "n", "add", "create", "register"))
await ctx.Execute<Api>(ApiKeyCreate, c => c.ApiKeyCreate(ctx));
else if (ctx.Match("list", "ls", "l"))
await ctx.Execute<Api>(ApiKeyList, c => c.ApiKeyList(ctx));
else if (ctx.Match("deleteall", "removeall", "destroyall", "eraseall", "revokeall", "yeetall"))
await ctx.Execute<Api>(ApiKeyDeleteAll, c => c.ApiKeyDeleteAll(ctx));
else if (!ctx.HasNext())
await PrintCommandExpectedError(ctx, ApiKeyCreate, ApiKeyList, ApiKeyRename, ApiKeyDelete, ApiKeyDeleteAll);
else
{
PKApiKey? key = null!;
var input = ctx.PeekArgument();
if (Guid.TryParse(input, out var keyId))
key = await ctx.Repository.GetApiKey(keyId);
else if (await ctx.Repository.GetApiKeyByName(ctx.System.Id, input) is PKApiKey keyByName)
key = keyByName;
if (key == null || key.System != ctx.System.Id)
{
await ctx.Reply($"{Emojis.Error} API key with name \"{ctx.PopArgument()}\" not found.");
return;
}
ctx.PopArgument();
if (ctx.Match("rename", "name", "changename", "setname", "rn"))
await ctx.Execute<Api>(ApiKeyRename, c => c.ApiKeyRename(ctx, key));
else if (ctx.Match("delete", "remove", "destroy", "erase", "yeet"))
await ctx.Execute<Api>(ApiKeyDelete, c => c.ApiKeyDelete(ctx, key));
else
await PrintCommandNotFoundError(ctx, ApiKeyRename, ApiKeyDelete);
}
}
private async Task HandleMemberCommand(Context ctx)
{
if (ctx.Match("new", "n", "add", "create", "register"))