Documentation
¶
Overview ¶
Package token provides token-based access control for shield-agent.
Index ¶
- func GenerateToken() (string, error)
- func HashToken(raw string) string
- type Store
- func (s *Store) CountUsage(tokenID string, window time.Duration) (int, error)
- func (s *Store) Create(name, tokenHash string, expiresAt *time.Time, quotaHourly, quotaMonthly int, ...) (string, error)
- func (s *Store) Delete(id string) error
- func (s *Store) GetByHash(hash string) (*Token, error)
- func (s *Store) GetByID(id string) (*Token, error)
- func (s *Store) GetStats(tokenID string, since time.Duration) (*UsageStats, error)
- func (s *Store) List(activeOnly bool) ([]Token, error)
- func (s *Store) RecordUsage(tokenID, method string, success bool, latencyMs float64) error
- func (s *Store) Revoke(id string) error
- type Token
- type UsageRecord
- type UsageStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateToken ¶
GenerateToken creates a random 32-byte hex token string.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store manages tokens in SQLite.
func (*Store) CountUsage ¶
CountUsage returns the number of requests for a token within a time window.
func (*Store) Create ¶
func (s *Store) Create(name, tokenHash string, expiresAt *time.Time, quotaHourly, quotaMonthly int, allowedMethods, ipAllowlist []string) (string, error)
Create stores a new token and returns its ID.
func (*Store) GetStats ¶
GetStats returns aggregated usage statistics for a token within a time range.
func (*Store) RecordUsage ¶
RecordUsage inserts a usage record for a token.
type Token ¶
type Token struct {
ID string `json:"id"`
Name string `json:"name"`
TokenHash string `json:"-"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Active bool `json:"active"`
QuotaHourly int `json:"quota_hourly"`
QuotaMonthly int `json:"quota_monthly"`
AllowedMethods []string `json:"allowed_methods"`
IPAllowlist []string `json:"ip_allowlist"`
}
Token represents an API access token.
func (*Token) IsExpired ¶
IsExpired returns true if the token has an expiration time that has passed.
func (*Token) IsMethodAllowed ¶
IsMethodAllowed returns true if the given method is permitted by this token. An empty allowed_methods list means all methods are permitted.
type UsageRecord ¶
type UsageRecord struct {
TokenID string `json:"token_id"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
Success bool `json:"success"`
LatencyMs float64 `json:"latency_ms"`
}
UsageRecord represents a single token usage entry.
type UsageStats ¶
type UsageStats struct {
TotalRequests int `json:"total_requests"`
SuccessCount int `json:"success_count"`
FailCount int `json:"fail_count"`
AvgLatencyMs float64 `json:"avg_latency_ms"`
HourlyUsage int `json:"hourly_usage"`
MonthlyUsage int `json:"monthly_usage"`
}
UsageStats holds aggregated usage statistics for a token.