api

package
v1.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package api provides Forge-style HTTP handlers for the Keysmith API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	// contains filtered or unexported fields
}

API wires all Forge-style HTTP handlers together for the keysmith system.

func New

func New(eng *keysmith.Engine, router forge.Router) *API

New creates an API from a Keysmith Engine.

func (*API) Handler

func (a *API) Handler() http.Handler

Handler returns the fully assembled http.Handler with all routes.

func (*API) RegisterRoutes

func (a *API) RegisterRoutes(router forge.Router)

RegisterRoutes registers all keysmith API routes into the given Forge router with full OpenAPI metadata.

type AggregationResponse

type AggregationResponse struct {
	KeyID            string    `json:"key_id"`
	TenantID         string    `json:"tenant_id"`
	Period           string    `json:"period"`
	PeriodStart      time.Time `json:"period_start"`
	RequestCount     int64     `json:"request_count"`
	ErrorCount       int64     `json:"error_count"`
	ServerErrorCount int64     `json:"server_error_count"`
	TotalLatency     int64     `json:"total_latency_ms"`
	P50Latency       *int64    `json:"p50_latency_ms,omitempty"`
	P99Latency       *int64    `json:"p99_latency_ms,omitempty"`
}

AggregationResponse is the API representation of aggregated usage.

type AssignScopesRequest

type AssignScopesRequest struct {
	KeyID  string   `path:"keyId" description:"Key ID"`
	Scopes []string `json:"scopes" description:"Scope names to assign"`
}

AssignScopesRequest is the request for assigning scopes to a key.

type CreateKeyRequest

type CreateKeyRequest struct {
	Name        string         `json:"name" description:"Human-readable key name"`
	Description string         `json:"description" description:"Optional description"`
	Prefix      string         `json:"prefix" description:"Key prefix (e.g., sk, pk)"`
	Environment string         `json:"environment" description:"Environment (live, test, staging)"`
	PolicyID    string         `json:"policy_id" description:"Optional policy ID to attach"`
	Scopes      []string       `json:"scopes" description:"Permission scopes to assign"`
	Metadata    map[string]any `json:"metadata" description:"Arbitrary metadata"`
	ExpiresAt   *time.Time     `json:"expires_at" description:"Optional expiration time"`
}

CreateKeyRequest is the request for creating an API key.

type CreatePolicyRequest

type CreatePolicyRequest struct {
	Name            string   `json:"name" description:"Policy name"`
	Description     string   `json:"description" description:"Optional description"`
	RateLimit       int      `json:"rate_limit" description:"Max requests per window"`
	RateLimitWindow string   `json:"rate_limit_window" description:"Window duration (e.g., 1m, 1h)"`
	BurstLimit      int      `json:"burst_limit" description:"Burst allowance"`
	AllowedScopes   []string `json:"allowed_scopes" description:"Scopes this policy grants"`
	AllowedIPs      []string `json:"allowed_ips" description:"IP allowlist (CIDR)"`
	AllowedOrigins  []string `json:"allowed_origins" description:"Origin allowlist"`
	MaxKeyLifetime  string   `json:"max_key_lifetime" description:"Max key lifetime (e.g., 90d)"`
	RotationPeriod  string   `json:"rotation_period" description:"Suggested rotation period (e.g., 30d)"`
	GracePeriod     string   `json:"grace_period" description:"Rotated key grace period (e.g., 24h)"`
	DailyQuota      int64    `json:"daily_quota" description:"Max requests per day (0 = unlimited)"`
	MonthlyQuota    int64    `json:"monthly_quota" description:"Max requests per month (0 = unlimited)"`
}

CreatePolicyRequest is the request for creating a policy.

type CreateScopeRequest

type CreateScopeRequest struct {
	Name        string `json:"name" description:"Scope name (e.g., read:users)"`
	Description string `json:"description" description:"Optional description"`
	Parent      string `json:"parent" description:"Parent scope (e.g., read)"`
}

CreateScopeRequest is the request for creating a scope.

type DeleteKeyRequest

type DeleteKeyRequest struct {
	KeyID string `path:"keyId" description:"Key ID"`
}

DeleteKeyRequest is the request for deleting a key.

type DeletePolicyRequest

type DeletePolicyRequest struct {
	PolicyID string `path:"policyId" description:"Policy ID"`
}

DeletePolicyRequest is the request for deleting a policy.

type DeleteScopeRequest

type DeleteScopeRequest struct {
	ScopeID string `path:"scopeId" description:"Scope ID"`
}

DeleteScopeRequest is the request for deleting a scope.

type GetKeyRequest

type GetKeyRequest struct {
	KeyID string `path:"keyId" description:"Key ID"`
}

GetKeyRequest is the request for fetching a single key.

type GetKeyUsageAggregateRequest

type GetKeyUsageAggregateRequest struct {
	KeyID  string `path:"keyId" description:"Key ID"`
	Period string `query:"period" description:"Aggregation period (hourly, daily, monthly; default daily)"`
	After  string `query:"after" description:"After timestamp (ISO 8601)"`
	Before string `query:"before" description:"Before timestamp (ISO 8601)"`
}

GetKeyUsageAggregateRequest is the request for aggregated usage.

type GetKeyUsageRequest

type GetKeyUsageRequest struct {
	KeyID  string `path:"keyId" description:"Key ID"`
	After  string `query:"after" description:"After timestamp (ISO 8601)"`
	Before string `query:"before" description:"Before timestamp (ISO 8601)"`
	Limit  int    `query:"limit" description:"Max results (default: 100)"`
	Offset int    `query:"offset" description:"Number of results to skip"`
}

GetKeyUsageRequest is the request for fetching key usage.

type GetPolicyRequest

type GetPolicyRequest struct {
	PolicyID string `path:"policyId" description:"Policy ID"`
}

GetPolicyRequest is the request for fetching a single policy.

type KeyCreateResponse

type KeyCreateResponse struct {
	Key    *KeyResponse `json:"key"`
	RawKey string       `json:"raw_key"`
}

KeyCreateResponse includes the raw key (shown only once at creation).

type KeyResponse

type KeyResponse struct {
	ID          string         `json:"id"`
	TenantID    string         `json:"tenant_id"`
	AppID       string         `json:"app_id"`
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Prefix      string         `json:"prefix"`
	Hint        string         `json:"hint"`
	Environment string         `json:"environment"`
	State       string         `json:"state"`
	PolicyID    string         `json:"policy_id,omitempty"`
	Scopes      []string       `json:"scopes,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
	CreatedBy   string         `json:"created_by,omitempty"`
	ExpiresAt   *time.Time     `json:"expires_at,omitempty"`
	LastUsedAt  *time.Time     `json:"last_used_at,omitempty"`
	RotatedAt   *time.Time     `json:"rotated_at,omitempty"`
	RevokedAt   *time.Time     `json:"revoked_at,omitempty"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
}

KeyResponse is the API representation of a key (raw key is never included).

type ListKeysRequest

type ListKeysRequest struct {
	Environment string `query:"environment" description:"Filter by environment"`
	State       string `query:"state" description:"Filter by state (active, revoked, expired)"`
	PolicyID    string `query:"policy_id" description:"Filter by policy ID"`
	Limit       int    `query:"limit" description:"Max results (default: 50)"`
	Offset      int    `query:"offset" description:"Number of results to skip"`
}

ListKeysRequest is the request for listing keys.

type ListPoliciesRequest

type ListPoliciesRequest struct {
	Limit  int `query:"limit" description:"Max results (default: 50)"`
	Offset int `query:"offset" description:"Number of results to skip"`
}

ListPoliciesRequest is the request for listing policies.

type ListRotationsRequest

type ListRotationsRequest struct {
	KeyID  string `path:"keyId" description:"Key ID"`
	Limit  int    `query:"limit" description:"Max results (default: 50)"`
	Offset int    `query:"offset" description:"Number of results to skip"`
}

ListRotationsRequest is the request for listing rotations.

type ListScopesRequest

type ListScopesRequest struct {
	Parent string `query:"parent" description:"Filter by parent scope"`
	Limit  int    `query:"limit" description:"Max results (default: 50)"`
	Offset int    `query:"offset" description:"Number of results to skip"`
}

ListScopesRequest is the request for listing scopes.

type ListUsageRequest

type ListUsageRequest struct {
	Period string `query:"period" description:"Aggregation period (hourly, daily, monthly; default daily)"`
	After  string `query:"after" description:"After timestamp (ISO 8601)"`
	Before string `query:"before" description:"Before timestamp (ISO 8601)"`
}

ListUsageRequest is the request for listing tenant-wide usage.

type PolicyResponse

type PolicyResponse struct {
	ID              string         `json:"id"`
	TenantID        string         `json:"tenant_id"`
	AppID           string         `json:"app_id"`
	Name            string         `json:"name"`
	Description     string         `json:"description,omitempty"`
	RateLimit       int            `json:"rate_limit"`
	RateLimitWindow string         `json:"rate_limit_window"`
	BurstLimit      int            `json:"burst_limit"`
	AllowedScopes   []string       `json:"allowed_scopes,omitempty"`
	AllowedIPs      []string       `json:"allowed_ips,omitempty"`
	AllowedOrigins  []string       `json:"allowed_origins,omitempty"`
	AllowedMethods  []string       `json:"allowed_methods,omitempty"`
	AllowedPaths    []string       `json:"allowed_paths,omitempty"`
	MaxKeyLifetime  string         `json:"max_key_lifetime,omitempty"`
	RotationPeriod  string         `json:"rotation_period,omitempty"`
	GracePeriod     string         `json:"grace_period"`
	DailyQuota      int64          `json:"daily_quota"`
	MonthlyQuota    int64          `json:"monthly_quota"`
	Metadata        map[string]any `json:"metadata,omitempty"`
	CreatedAt       time.Time      `json:"created_at"`
	UpdatedAt       time.Time      `json:"updated_at"`
}

PolicyResponse is the API representation of a policy.

type ReactivateKeyRequest

type ReactivateKeyRequest struct {
	KeyID string `path:"keyId" description:"Key ID"`
}

ReactivateKeyRequest is the request for reactivating a key.

type RemoveScopesRequest

type RemoveScopesRequest struct {
	KeyID  string   `path:"keyId" description:"Key ID"`
	Scopes []string `json:"scopes" description:"Scope names to remove"`
}

RemoveScopesRequest is the request for removing scopes from a key.

type RevokeKeyRequest

type RevokeKeyRequest struct {
	KeyID  string `path:"keyId" description:"Key ID to revoke"`
	Reason string `json:"reason" description:"Revocation reason"`
}

RevokeKeyRequest is the request for revoking a key.

type RotateKeyRequest

type RotateKeyRequest struct {
	KeyID  string `path:"keyId" description:"Key ID to rotate"`
	Reason string `json:"reason" description:"Rotation reason (manual, compromise, policy)"`
}

RotateKeyRequest is the request for rotating a key.

type RotationResponse

type RotationResponse struct {
	ID        string    `json:"id"`
	KeyID     string    `json:"key_id"`
	TenantID  string    `json:"tenant_id"`
	Reason    string    `json:"reason"`
	GraceTTL  string    `json:"grace_ttl"`
	GraceEnds time.Time `json:"grace_ends"`
	RotatedBy string    `json:"rotated_by,omitempty"`
	CreatedAt time.Time `json:"created_at"`
}

RotationResponse is the API representation of a rotation record.

type ScopeResponse

type ScopeResponse struct {
	ID          string         `json:"id"`
	TenantID    string         `json:"tenant_id"`
	AppID       string         `json:"app_id"`
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	Parent      string         `json:"parent,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
	CreatedAt   time.Time      `json:"created_at"`
}

ScopeResponse is the API representation of a scope.

type SuspendKeyRequest

type SuspendKeyRequest struct {
	KeyID string `path:"keyId" description:"Key ID"`
}

SuspendKeyRequest is the request for suspending a key.

type UpdatePolicyRequest

type UpdatePolicyRequest struct {
	PolicyID string `path:"policyId" description:"Policy ID"`
	CreatePolicyRequest
}

UpdatePolicyRequest is the request for updating a policy.

type UsageResponse

type UsageResponse struct {
	ID         string         `json:"id"`
	KeyID      string         `json:"key_id"`
	TenantID   string         `json:"tenant_id"`
	Endpoint   string         `json:"endpoint"`
	Method     string         `json:"method"`
	StatusCode int            `json:"status_code"`
	IPAddress  string         `json:"ip_address,omitempty"`
	UserAgent  string         `json:"user_agent,omitempty"`
	LatencyMs  int64          `json:"latency_ms"`
	Metadata   map[string]any `json:"metadata,omitempty"`
	CreatedAt  time.Time      `json:"created_at"`
}

UsageResponse is the API representation of a usage record.

type ValidateKeyRequest

type ValidateKeyRequest struct {
	RawKey string `json:"raw_key" description:"The raw API key to validate"`
}

ValidateKeyRequest is the request for validating a raw key.

type ValidationResponse

type ValidationResponse struct {
	Valid  bool         `json:"valid"`
	Key    *KeyResponse `json:"key,omitempty"`
	Scopes []string     `json:"scopes,omitempty"`
}

ValidationResponse is the API representation of a key validation result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL