feat: add scoped tenant api keys
This commit is contained in:
+95
-2
@@ -32,8 +32,6 @@ type Tenant struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
// Tenant description
|
||||
Description string `yaml:"description" json:"description"`
|
||||
// API key for authentication
|
||||
APIKey string `yaml:"api_key" json:"api_key"`
|
||||
// Tenant status (active, inactive)
|
||||
Status string `yaml:"status" json:"status" gorm:"default:'active'"`
|
||||
// Configured retrieval engines
|
||||
@@ -64,6 +62,54 @@ type TenantListResponse struct {
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
// TenantAPIKeyScope is an operation scope for revocable tenant API keys.
|
||||
type TenantAPIKeyScope string
|
||||
|
||||
const (
|
||||
TenantAPIKeyScopeRead TenantAPIKeyScope = "read"
|
||||
TenantAPIKeyScopeWrite TenantAPIKeyScope = "write"
|
||||
TenantAPIKeyScopeAdmin TenantAPIKeyScope = "admin"
|
||||
)
|
||||
|
||||
// TenantAPIKey is the API key metadata returned by list/create APIs.
|
||||
type TenantAPIKey struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
Name string `json:"name"`
|
||||
APIKey string `json:"api_key"`
|
||||
Scopes []TenantAPIKeyScope `json:"scopes"`
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids"`
|
||||
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
||||
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// CreateTenantAPIKeyRequest creates a scoped, revocable tenant API key.
|
||||
type CreateTenantAPIKeyRequest struct {
|
||||
Name string `json:"name"`
|
||||
Scopes []TenantAPIKeyScope `json:"scopes,omitempty"`
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
|
||||
ExpiresAtUnix *int64 `json:"expires_at_unix,omitempty"`
|
||||
}
|
||||
|
||||
// CreatedTenantAPIKey includes the created API key. Token is kept for
|
||||
// backward-compatible clients; APIKey is also returned by list APIs.
|
||||
type CreatedTenantAPIKey struct {
|
||||
TenantAPIKey
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
type tenantAPIKeyListResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data []TenantAPIKey `json:"data"`
|
||||
}
|
||||
|
||||
type tenantAPIKeyCreateResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Data CreatedTenantAPIKey `json:"data"`
|
||||
}
|
||||
|
||||
// CreateTenant creates a new tenant
|
||||
func (c *Client) CreateTenant(ctx context.Context, tenant *Tenant) (*Tenant, error) {
|
||||
resp, err := c.doRequest(ctx, http.MethodPost, "/api/v1/tenants", tenant, nil)
|
||||
@@ -193,6 +239,53 @@ func (c *Client) SearchTenants(ctx context.Context, keyword string, tenantID uin
|
||||
return response.Data.Items, response.Data.Total, nil
|
||||
}
|
||||
|
||||
// ListTenantAPIKeys lists API keys for a tenant.
|
||||
func (c *Client) ListTenantAPIKeys(ctx context.Context, tenantID uint64) ([]TenantAPIKey, error) {
|
||||
path := fmt.Sprintf("/api/v1/tenants/%d/api-keys", tenantID)
|
||||
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response tenantAPIKeyListResponse
|
||||
if err := parseResponse(resp, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return response.Data, nil
|
||||
}
|
||||
|
||||
// CreateTenantAPIKey creates a scoped API key.
|
||||
func (c *Client) CreateTenantAPIKey(
|
||||
ctx context.Context, tenantID uint64, req *CreateTenantAPIKeyRequest,
|
||||
) (*CreatedTenantAPIKey, error) {
|
||||
path := fmt.Sprintf("/api/v1/tenants/%d/api-keys", tenantID)
|
||||
resp, err := c.doRequest(ctx, http.MethodPost, path, req, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response tenantAPIKeyCreateResponse
|
||||
if err := parseResponse(resp, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &response.Data, nil
|
||||
}
|
||||
|
||||
// DeleteTenantAPIKey revokes a tenant API key.
|
||||
func (c *Client) DeleteTenantAPIKey(ctx context.Context, tenantID uint64, keyID uint64) error {
|
||||
path := fmt.Sprintf("/api/v1/tenants/%d/api-keys/%d", tenantID, keyID)
|
||||
resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
return parseResponse(resp, &response)
|
||||
}
|
||||
|
||||
// GetTenantKV retrieves a tenant KV configuration by key
|
||||
func (c *Client) GetTenantKV(ctx context.Context, key string) (json.RawMessage, error) {
|
||||
path := fmt.Sprintf("/api/v1/tenants/kv/%s", key)
|
||||
|
||||
Reference in New Issue
Block a user