From 0fee7abc7c80a51f2bb17c472b2f0e31f0645f6a Mon Sep 17 00:00:00 2001 From: LordMathis Date: Sat, 6 Dec 2025 22:20:05 +0100 Subject: [PATCH] Simplify create key request format --- pkg/server/handlers_auth.go | 29 ++++++++++++----------------- webui/src/types/apiKey.ts | 12 ++++-------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/server/handlers_auth.go b/pkg/server/handlers_auth.go index 7b77906..e2a4bf5 100644 --- a/pkg/server/handlers_auth.go +++ b/pkg/server/handlers_auth.go @@ -11,17 +11,12 @@ import ( "github.com/go-chi/chi/v5" ) -// InstancePermission defines the permissions for an API key on a specific instance. -type InstancePermission struct { - InstanceID int `json:"instance_id"` -} - // CreateKeyRequest represents the request body for creating a new API key. type CreateKeyRequest struct { - Name string - PermissionMode auth.PermissionMode - ExpiresAt *int64 - InstancePermissions []InstancePermission + Name string `json:"name"` + PermissionMode auth.PermissionMode `json:"permission_mode"` + ExpiresAt *int64 `json:"expires_at,omitempty"` + InstanceIDs []int `json:"instance_ids,omitempty"` } // CreateKeyResponse represents the response returned when creating a new API key. @@ -87,8 +82,8 @@ func (h *Handler) CreateKey() http.HandlerFunc { writeError(w, http.StatusBadRequest, "invalid_permission_mode", "Permission mode must be 'allow_all' or 'per_instance'") return } - if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstancePermissions) == 0 { - writeError(w, http.StatusBadRequest, "missing_permissions", "Instance permissions required when permission mode is 'per_instance'") + if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstanceIDs) == 0 { + writeError(w, http.StatusBadRequest, "missing_permissions", "Instance IDs required when permission mode is 'per_instance'") return } if req.ExpiresAt != nil && *req.ExpiresAt <= time.Now().Unix() { @@ -108,9 +103,9 @@ func (h *Handler) CreateKey() http.HandlerFunc { instanceIDMap[inst.ID] = true } - for _, perm := range req.InstancePermissions { - if !instanceIDMap[perm.InstanceID] { - writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", perm.InstanceID)) + for _, instanceID := range req.InstanceIDs { + if !instanceIDMap[instanceID] { + writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", instanceID)) return } } @@ -142,12 +137,12 @@ func (h *Handler) CreateKey() http.HandlerFunc { UpdatedAt: now, } - // Convert InstancePermissions to KeyPermissions + // Convert InstanceIDs to KeyPermissions var keyPermissions []auth.KeyPermission - for _, perm := range req.InstancePermissions { + for _, instanceID := range req.InstanceIDs { keyPermissions = append(keyPermissions, auth.KeyPermission{ KeyID: 0, // Will be set by database after key creation - InstanceID: perm.InstanceID, + InstanceID: instanceID, }) } diff --git a/webui/src/types/apiKey.ts b/webui/src/types/apiKey.ts index 3dd68c0..0889c9f 100644 --- a/webui/src/types/apiKey.ts +++ b/webui/src/types/apiKey.ts @@ -15,14 +15,10 @@ export interface ApiKey { } export interface CreateKeyRequest { - Name: string - PermissionMode: PermissionMode - ExpiresAt?: number - InstancePermissions: InstancePermission[] -} - -export interface InstancePermission { - InstanceID: number + name: string + permission_mode: PermissionMode + expires_at?: number + instance_ids: number[] } export interface CreateKeyResponse extends ApiKey {