Documentation
¶
Overview ¶
contract.go: Wires the apikey plugin into the forge-dashboard contract surface via plugin.ContractContributor.
Package apikey provides an API key authentication plugin for AuthSome.
The apikey plugin enables machine-to-machine authentication. API keys are created per-user, hashed with SHA-256, and identified by a visible prefix during lookup. The plugin registers HTTP routes for key lifecycle management and an authentication strategy that checks the Authorization or X-API-Key headers.
Index ¶
- Variables
- type Config
- type CreateKeyRequest
- type CreateKeyResponse
- type KeyListItem
- type ListKeysRequest
- type ListKeysResponse
- type Plugin
- func (p *Plugin) DeclareSettings(m *settings.Manager) error
- func (p *Plugin) Name() string
- func (p *Plugin) OnInit(_ context.Context, engine plugin.Engine) error
- func (p *Plugin) RegisterContract(d *dispatcher.Dispatcher, reg contract.Registry, wreg contract.WardenRegistry, ...) error
- func (p *Plugin) RegisterRoutes(router forge.Router) error
- func (p *Plugin) SetStore(s apikey.Store)
- func (p *Plugin) Strategy() strategy.Strategy
- func (p *Plugin) StrategyPriority() int
- type PrincipalAuthGate
- type PrincipalResolver
- type RevokeKeyRequest
- type UserResolver
Constants ¶
This section is empty.
Variables ¶
var ( // SettingMaxKeysPerUser controls the maximum number of active API keys per user. SettingMaxKeysPerUser = settings.Define("apikey.max_keys_per_user", 0, settings.WithDisplayName("Max Keys Per User"), settings.WithDescription("Maximum number of active API keys per user (0 = unlimited)"), settings.WithCategory("API Keys"), settings.WithScopes(settings.ScopeGlobal, settings.ScopeApp), settings.WithEnforceable(), settings.WithInputType(formconfig.FieldNumber), settings.WithUIValidation(formconfig.Validation{Min: intPtr(0), Max: intPtr(100)}), settings.WithHelpText("Set to 0 for unlimited keys. Default: 0"), settings.WithOrder(10), ) // SettingDefaultExpirySeconds controls the default TTL for newly created keys. SettingDefaultExpirySeconds = settings.Define("apikey.default_expiry_seconds", 0, settings.WithDisplayName("Default Key Expiry (seconds)"), settings.WithDescription("Default TTL for newly created API keys in seconds (0 = no expiry)"), settings.WithCategory("API Keys"), settings.WithScopes(settings.ScopeGlobal, settings.ScopeApp), settings.WithInputType(formconfig.FieldNumber), settings.WithUIValidation(formconfig.Validation{Min: intPtr(0)}), settings.WithHelpText("Set to 0 for keys that never expire. Default: 0"), settings.WithOrder(20), ) )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// PathPrefix is the HTTP path prefix for API key routes.
// Defaults to "/v1/keys".
PathPrefix string
// MaxKeysPerUser limits the number of active API keys per user.
// 0 means unlimited.
MaxKeysPerUser int
// DefaultExpiry sets the default TTL for newly created keys.
// Zero means keys never expire.
DefaultExpiry time.Duration
}
Config configures the API key plugin.
type CreateKeyRequest ¶ added in v1.9.0
type CreateKeyRequest struct {
AppID string `json:"app_id"`
// UserID names the key's owner. Optional — defaults to the authenticated
// caller. Naming another user requires the manage/apikey permission.
UserID string `json:"user_id,omitempty"`
Name string `json:"name"`
Scopes []string `json:"scopes,omitempty"`
}
CreateKeyRequest is the request body for creating an API key.
type CreateKeyResponse ¶ added in v1.9.0
type CreateKeyResponse struct {
ID string `json:"id"`
Key string `json:"key"`
KeyPrefix string `json:"key_prefix"`
PublicKey string `json:"public_key"`
PublicKeyPrefix string `json:"public_key_prefix"`
Name string `json:"name"`
Scopes []string `json:"scopes,omitempty"`
ExpiresAt *string `json:"expires_at,omitempty"`
CreatedAt string `json:"created_at"`
}
CreateKeyResponse is returned when a key is created.
type KeyListItem ¶ added in v1.9.0
type KeyListItem struct {
ID string `json:"id"`
Name string `json:"name"`
KeyPrefix string `json:"key_prefix"`
PublicKeyPrefix string `json:"public_key_prefix,omitempty"`
Scopes []string `json:"scopes,omitempty"`
ExpiresAt *string `json:"expires_at,omitempty"`
LastUsedAt *string `json:"last_used_at,omitempty"`
Revoked bool `json:"revoked"`
CreatedAt string `json:"created_at"`
}
KeyListItem represents an API key in list responses (no raw key).
type ListKeysRequest ¶ added in v1.9.0
type ListKeysRequest struct {
AppID string `query:"app_id"`
UserID string `query:"user_id,omitempty"`
}
ListKeysRequest contains query parameters for listing API keys.
type ListKeysResponse ¶ added in v1.9.0
type ListKeysResponse struct {
Keys []KeyListItem `json:"keys"`
Total int `json:"total"`
}
ListKeysResponse is the response for listing API keys.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin is the API key authentication plugin.
func New ¶ added in v1.9.0
New creates a new API key plugin with the given configuration. The API key store is resolved automatically from the engine during OnInit.
func (*Plugin) DeclareSettings ¶ added in v1.9.0
DeclareSettings implements plugin.SettingsProvider.
func (*Plugin) RegisterContract ¶ added in v1.9.0
func (p *Plugin) RegisterContract( d *dispatcher.Dispatcher, reg contract.Registry, wreg contract.WardenRegistry, engine plugin.Engine, ) error
func (*Plugin) RegisterRoutes ¶
RegisterRoutes registers API key management routes on a forge.Router.
func (*Plugin) SetStore ¶ added in v1.9.0
SetStore allows direct API key store injection for testing.
func (*Plugin) StrategyPriority ¶ added in v1.9.0
StrategyPriority returns the evaluation priority for the API key strategy. API key auth is evaluated after session-based auth (priority 100).
type PrincipalAuthGate ¶ added in v1.9.0
type PrincipalAuthGate interface {
Authorize(ctx context.Context, a *principal.AuthAttempt) error
Observe(ctx context.Context, a *principal.AuthAttempt, s *session.Session)
}
PrincipalAuthGate scores a machine caller and may deny it. The apikey plugin holds this rather than the engine so it does not import authsome.
Authorize runs the BeforePrincipalAuth chain; a returned error must abort the request. Observe runs after a session has been minted and never blocks authentication: errors from it are logged, not propagated.
type PrincipalResolver ¶ added in v1.9.0
PrincipalResolver resolves any caller, human or otherwise, by ref. Taken as a function rather than the whole engine so the strategy keeps its narrow dependency surface, matching UserResolver above.
type RevokeKeyRequest ¶ added in v1.9.0
type RevokeKeyRequest struct {
KeyID string `path:"keyId"`
}
RevokeKeyRequest contains the path parameter for revoking an API key.