Simplify create key request format

This commit is contained in:
2025-12-06 22:20:05 +01:00
parent 02193bd309
commit 0fee7abc7c
2 changed files with 16 additions and 25 deletions

View File

@@ -11,17 +11,12 @@ import (
"github.com/go-chi/chi/v5" "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. // CreateKeyRequest represents the request body for creating a new API key.
type CreateKeyRequest struct { type CreateKeyRequest struct {
Name string Name string `json:"name"`
PermissionMode auth.PermissionMode PermissionMode auth.PermissionMode `json:"permission_mode"`
ExpiresAt *int64 ExpiresAt *int64 `json:"expires_at,omitempty"`
InstancePermissions []InstancePermission InstanceIDs []int `json:"instance_ids,omitempty"`
} }
// CreateKeyResponse represents the response returned when creating a new API key. // 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'") writeError(w, http.StatusBadRequest, "invalid_permission_mode", "Permission mode must be 'allow_all' or 'per_instance'")
return return
} }
if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstancePermissions) == 0 { if req.PermissionMode == auth.PermissionModePerInstance && len(req.InstanceIDs) == 0 {
writeError(w, http.StatusBadRequest, "missing_permissions", "Instance permissions required when permission mode is 'per_instance'") writeError(w, http.StatusBadRequest, "missing_permissions", "Instance IDs required when permission mode is 'per_instance'")
return return
} }
if req.ExpiresAt != nil && *req.ExpiresAt <= time.Now().Unix() { if req.ExpiresAt != nil && *req.ExpiresAt <= time.Now().Unix() {
@@ -108,9 +103,9 @@ func (h *Handler) CreateKey() http.HandlerFunc {
instanceIDMap[inst.ID] = true instanceIDMap[inst.ID] = true
} }
for _, perm := range req.InstancePermissions { for _, instanceID := range req.InstanceIDs {
if !instanceIDMap[perm.InstanceID] { if !instanceIDMap[instanceID] {
writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", perm.InstanceID)) writeError(w, http.StatusBadRequest, "invalid_instance_id", fmt.Sprintf("Instance ID %d does not exist", instanceID))
return return
} }
} }
@@ -142,12 +137,12 @@ func (h *Handler) CreateKey() http.HandlerFunc {
UpdatedAt: now, UpdatedAt: now,
} }
// Convert InstancePermissions to KeyPermissions // Convert InstanceIDs to KeyPermissions
var keyPermissions []auth.KeyPermission var keyPermissions []auth.KeyPermission
for _, perm := range req.InstancePermissions { for _, instanceID := range req.InstanceIDs {
keyPermissions = append(keyPermissions, auth.KeyPermission{ keyPermissions = append(keyPermissions, auth.KeyPermission{
KeyID: 0, // Will be set by database after key creation KeyID: 0, // Will be set by database after key creation
InstanceID: perm.InstanceID, InstanceID: instanceID,
}) })
} }

View File

@@ -15,14 +15,10 @@ export interface ApiKey {
} }
export interface CreateKeyRequest { export interface CreateKeyRequest {
Name: string name: string
PermissionMode: PermissionMode permission_mode: PermissionMode
ExpiresAt?: number expires_at?: number
InstancePermissions: InstancePermission[] instance_ids: number[]
}
export interface InstancePermission {
InstanceID: number
} }
export interface CreateKeyResponse extends ApiKey { export interface CreateKeyResponse extends ApiKey {