Documentation
¶
Index ¶
- Variables
- func NewAPIKeyModule(logger logging.Logger, deps *core.Dependencies) core.Module
- func SetupAPIKeyRoutes(r chi.Router, handler *APIKeyHandler, ...)
- type APIKeyDTO
- type APIKeyHandler
- func (h *APIKeyHandler) CreateAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) DeleteAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) GetAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) GetAPIKeyStats(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) ListAPIKeys(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) RevokeAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) RotateAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) UpdateAPIKey(w http.ResponseWriter, r *http.Request)
- func (h *APIKeyHandler) ValidateAPIKey(w http.ResponseWriter, r *http.Request)
- type APIKeyModule
- func (m *APIKeyModule) IncrementUsage(ctx context.Context, keyID uuid.UUID, ipAddress string)
- func (m *APIKeyModule) Name() string
- func (m *APIKeyModule) RegisterPrivateRoutes(r chi.Router)
- func (m *APIKeyModule) RegisterPublicRoutes(r chi.Router)
- func (m *APIKeyModule) ValidateAPIKey(ctx context.Context, apiKey string) (*ValidatedAPIKey, error)
- type APIKeyService
- func (s *APIKeyService) Create(ctx context.Context, userID uuid.UUID, name string, roleCodes []string, ...) (*CreateAPIKeyResponse, error)
- func (s *APIKeyService) Delete(ctx context.Context, id uuid.UUID) error
- func (s *APIKeyService) Get(ctx context.Context, id uuid.UUID) (*APIKeyDTO, error)
- func (s *APIKeyService) GetStats(ctx context.Context, id uuid.UUID) (*APIKeyStatsDTO, error)
- func (s *APIKeyService) IncrementUsage(ctx context.Context, id uuid.UUID, ip string) error
- func (s *APIKeyService) List(ctx context.Context, userID uuid.UUID, page, pageSize int) ([]*APIKeyDTO, int, error)
- func (s *APIKeyService) Revoke(ctx context.Context, id uuid.UUID) error
- func (s *APIKeyService) Rotate(ctx context.Context, id uuid.UUID) (*CreateAPIKeyResponse, error)
- func (s *APIKeyService) Update(ctx context.Context, id uuid.UUID, name string, description string, ...) (*APIKeyDTO, error)
- func (s *APIKeyService) Validate(ctx context.Context, key string) (*ValidatedAPIKey, error)
- type APIKeyStatsDTO
- type CreateAPIKeyRequest
- type CreateAPIKeyResponse
- type UpdateAPIKeyRequest
- type ValidateAPIKeyRequest
- type ValidateAPIKeyResponse
- type ValidatedAPIKey
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAPIKeyNotFound indicates the API key was not found ErrAPIKeyNotFound = errors.New("api key not found") // ErrAPIKeyInactive indicates the API key is not active ErrAPIKeyInactive = errors.New("api key is inactive") // ErrAPIKeyExpired indicates the API key has expired ErrAPIKeyExpired = errors.New("api key has expired") // ErrInvalidAPIKey indicates the API key is invalid ErrInvalidAPIKey = errors.New("invalid api key") )
Functions ¶
func NewAPIKeyModule ¶
func SetupAPIKeyRoutes ¶
func SetupAPIKeyRoutes(r chi.Router, handler *APIKeyHandler, jwtMiddleware func(http.Handler) http.Handler)
SetupAPIKeyRoutes registers all API key routes
Types ¶
type APIKeyDTO ¶
type APIKeyDTO struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Key string `json:"key"` // Only the prefix, e.g., "pk_live_****"
Permissions []string `json:"permissions,omitempty"`
DataFilters map[string]any `json:"dataFilters,omitempty"`
RateLimit int `json:"rateLimit"`
DailyLimit int `json:"dailyLimit"`
UsageCount int64 `json:"usageCount"`
LastUsedAt *time.Time `json:"lastUsedAt,omitempty"`
LastUsedIP string `json:"lastUsedIp,omitempty"`
ExpiresAt *time.Time `json:"expiresAt,omitempty"`
ProjectID *uuid.UUID `json:"projectId,omitempty"`
IsActive bool `json:"isActive"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
APIKeyDTO represents an API key response (never includes raw key)
type APIKeyHandler ¶
type APIKeyHandler struct {
// contains filtered or unexported fields
}
APIKeyHandler handles API key related HTTP requests
func NewAPIKeyHandler ¶
func NewAPIKeyHandler(apiKeyService *APIKeyService, logger logging.Logger) *APIKeyHandler
NewAPIKeyHandler creates a new API key handler
func (*APIKeyHandler) CreateAPIKey ¶
func (h *APIKeyHandler) CreateAPIKey(w http.ResponseWriter, r *http.Request)
CreateAPIKey handles POST /api-keys @Summary Create a new API key @Tags API Keys @Accept json @Produce json @Param request body CreateAPIKeyRequest true "API key creation data" @Success 201 {object} CreateAPIKeyResponse @Router /api-keys [post]
func (*APIKeyHandler) DeleteAPIKey ¶
func (h *APIKeyHandler) DeleteAPIKey(w http.ResponseWriter, r *http.Request)
DeleteAPIKey handles DELETE /api-keys/{id} @Summary Delete an API key (soft delete) @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Success 200 {object} map[string]string @Router /api-keys/{id} [delete]
func (*APIKeyHandler) GetAPIKey ¶
func (h *APIKeyHandler) GetAPIKey(w http.ResponseWriter, r *http.Request)
GetAPIKey handles GET /api-keys/{id} @Summary Get a specific API key by ID @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Success 200 {object} APIKeyDTO @Router /api-keys/{id} [get]
func (*APIKeyHandler) GetAPIKeyStats ¶
func (h *APIKeyHandler) GetAPIKeyStats(w http.ResponseWriter, r *http.Request)
GetAPIKeyStats handles GET /api-keys/{id}/stats @Summary Get usage statistics for an API key @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Success 200 {object} APIKeyStatsDTO @Router /api-keys/{id}/stats [get]
func (*APIKeyHandler) ListAPIKeys ¶
func (h *APIKeyHandler) ListAPIKeys(w http.ResponseWriter, r *http.Request)
ListAPIKeys handles GET /api-keys @Summary List all API keys for authenticated user @Tags API Keys @Accept json @Produce json @Param page query int false "Page number" default(1) @Param pageSize query int false "Page size" default(20) @Success 200 {array} APIKeyDTO @Router /api-keys [get]
func (*APIKeyHandler) RevokeAPIKey ¶
func (h *APIKeyHandler) RevokeAPIKey(w http.ResponseWriter, r *http.Request)
RevokeAPIKey handles POST /api-keys/{id}/revoke @Summary Revoke (deactivate) an API key @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Success 200 {object} map[string]string @Router /api-keys/{id}/revoke [post]
func (*APIKeyHandler) RotateAPIKey ¶
func (h *APIKeyHandler) RotateAPIKey(w http.ResponseWriter, r *http.Request)
RotateAPIKey handles POST /api-keys/{id}/rotate @Summary Rotate (regenerate) an API key @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Success 200 {object} CreateAPIKeyResponse @Router /api-keys/{id}/rotate [post]
func (*APIKeyHandler) UpdateAPIKey ¶
func (h *APIKeyHandler) UpdateAPIKey(w http.ResponseWriter, r *http.Request)
UpdateAPIKey handles PUT /api-keys/{id} @Summary Update an API key @Tags API Keys @Accept json @Produce json @Param id path string true "API Key ID" @Param request body UpdateAPIKeyRequest true "API key update data" @Success 200 {object} APIKeyDTO @Router /api-keys/{id} [put]
func (*APIKeyHandler) ValidateAPIKey ¶
func (h *APIKeyHandler) ValidateAPIKey(w http.ResponseWriter, r *http.Request)
ValidateAPIKey handles POST /api-keys/validate @Summary Validate an API key @Tags API Keys @Accept json @Produce json @Param request body ValidateAPIKeyRequest true "API key to validate" @Success 200 {object} ValidateAPIKeyResponse @Router /api-keys/validate [post]
type APIKeyModule ¶
type APIKeyModule struct {
// contains filtered or unexported fields
}
func (*APIKeyModule) IncrementUsage ¶
IncrementUsage increments the usage counter for an API key
func (*APIKeyModule) Name ¶
func (m *APIKeyModule) Name() string
func (*APIKeyModule) RegisterPrivateRoutes ¶
func (m *APIKeyModule) RegisterPrivateRoutes(r chi.Router)
RegisterPrivateRoutes registers protected API key endpoints
func (*APIKeyModule) RegisterPublicRoutes ¶
func (m *APIKeyModule) RegisterPublicRoutes(r chi.Router)
RegisterPublicRoutes - apikey module has no public routes
func (*APIKeyModule) ValidateAPIKey ¶
func (m *APIKeyModule) ValidateAPIKey(ctx context.Context, apiKey string) (*ValidatedAPIKey, error)
ValidateAPIKey validates an API key and returns validation info
type APIKeyService ¶
type APIKeyService struct {
// contains filtered or unexported fields
}
APIKeyService handles API key business logic
func NewAPIKeyService ¶
func NewAPIKeyService(client *ent.Client, logger logging.Logger) *APIKeyService
NewAPIKeyService creates a new API key service
func (*APIKeyService) Create ¶
func (s *APIKeyService) Create( ctx context.Context, userID uuid.UUID, name string, roleCodes []string, description string, expiresAt *time.Time, rateLimit int, dailyLimit int, environment string, projectID *uuid.UUID, ) (*CreateAPIKeyResponse, error)
Create creates a new API key
func (*APIKeyService) GetStats ¶
func (s *APIKeyService) GetStats(ctx context.Context, id uuid.UUID) (*APIKeyStatsDTO, error)
GetStats retrieves usage statistics for an API key
func (*APIKeyService) IncrementUsage ¶
IncrementUsage tracks API key usage
func (*APIKeyService) List ¶
func (s *APIKeyService) List(ctx context.Context, userID uuid.UUID, page, pageSize int) ([]*APIKeyDTO, int, error)
List returns all API keys for a user (paginated)
func (*APIKeyService) Rotate ¶
func (s *APIKeyService) Rotate(ctx context.Context, id uuid.UUID) (*CreateAPIKeyResponse, error)
Rotate regenerates the API key secret
func (*APIKeyService) Update ¶
func (s *APIKeyService) Update( ctx context.Context, id uuid.UUID, name string, description string, rateLimit int, dailyLimit int, ) (*APIKeyDTO, error)
Update updates API key properties
func (*APIKeyService) Validate ¶
func (s *APIKeyService) Validate(ctx context.Context, key string) (*ValidatedAPIKey, error)
Validate validates an API key and returns its info
type APIKeyStatsDTO ¶
type APIKeyStatsDTO struct {
UsageCount int64 `json:"usageCount"`
LastUsedAt *time.Time `json:"lastUsedAt,omitempty"`
LastUsedIP string `json:"lastUsedIp,omitempty"`
IsActive bool `json:"isActive"`
ExpiresAt *time.Time `json:"expiresAt,omitempty"`
}
APIKeyStatsDTO represents usage statistics
type CreateAPIKeyRequest ¶
type CreateAPIKeyRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExpiresAt *time.Time `json:"expiresAt,omitempty"` // nil means no expiration
RateLimit int `json:"rateLimit,omitempty"`
DailyLimit int `json:"dailyLimit,omitempty"`
Environment string `json:"environment,omitempty"`
ProjectID string `json:"projectId,omitempty"`
}
CreateAPIKeyRequest represents the request to create an API key
type CreateAPIKeyResponse ¶
type CreateAPIKeyResponse struct {
APIKey *APIKeyDTO `json:"apiKey"`
SecretKey string `json:"secretKey"` // Raw key, shown only once
}
CreateAPIKeyResponse includes the raw key (only returned once)
type UpdateAPIKeyRequest ¶
type UpdateAPIKeyRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
RateLimit int `json:"rateLimit,omitempty"`
DailyLimit int `json:"dailyLimit,omitempty"`
}
UpdateAPIKeyRequest represents the request to update an API key
type ValidateAPIKeyRequest ¶
type ValidateAPIKeyRequest struct {
Key string `json:"key"`
}
ValidateAPIKeyRequest represents the request to validate an API key
type ValidateAPIKeyResponse ¶
type ValidateAPIKeyResponse struct {
Valid bool `json:"valid"`
KeyID uuid.UUID `json:"keyId,omitempty"`
Name string `json:"name,omitempty"`
Permissions []string `json:"permissions,omitempty"`
DataFilters map[string]any `json:"dataFilters,omitempty"`
RateLimit int `json:"rateLimit,omitempty"`
DailyLimit int `json:"dailyLimit,omitempty"`
OwnerID uuid.UUID `json:"ownerId,omitempty"`
RoleID *uuid.UUID `json:"roleId,omitempty"`
ProjectID *uuid.UUID `json:"projectId,omitempty"`
}
ValidateAPIKeyResponse represents the validation response
type ValidatedAPIKey ¶
type ValidatedAPIKey struct {
ID uuid.UUID
Name string
Permissions []string
DataFilters map[string]any
RateLimit int
DailyLimit int
UsageCount int64
OwnerID uuid.UUID
RoleID *uuid.UUID
Roles []string
TenantID string
ProjectID *uuid.UUID
IsSuperAdmin bool
}
ValidatedAPIKey represents a validated API key with loaded relationships