Documentation
¶
Overview ¶
Package auth provides authentication and RBAC for ChronosOS.
Index ¶
- func APIKeyMiddleware(cfg APIKeyConfig) func(http.Handler) http.Handler
- func CreateTestToken(claims UserClaims, secret string) string
- func JWTMiddleware(cfg JWTConfig) func(http.Handler) http.Handler
- func RequireRole(svc *Service, required Role) func(http.Handler) http.Handler
- func WithUser(ctx context.Context, claims *UserClaims) context.Context
- type APIKeyConfig
- type APIKeyEntry
- type JWTConfig
- type Role
- type Service
- type UserClaims
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyMiddleware ¶ added in v0.3.0
func APIKeyMiddleware(cfg APIKeyConfig) func(http.Handler) http.Handler
APIKeyMiddleware returns HTTP middleware that validates API keys from the configured header (default: X-Api-Key).
func CreateTestToken ¶ added in v0.3.0
func CreateTestToken(claims UserClaims, secret string) string
CreateTestToken creates a simple HMAC-SHA256 signed JWT for testing.
func JWTMiddleware ¶ added in v0.3.0
JWTMiddleware returns HTTP middleware that validates JWT bearer tokens.
func RequireRole ¶ added in v0.3.0
RequireRole returns middleware that checks the authenticated user has at least the specified role. Must be used after JWT or API key middleware.
Types ¶
type APIKeyConfig ¶ added in v0.3.0
type APIKeyConfig struct {
HeaderName string
Keys map[string]APIKeyEntry
SkipPaths []string
}
APIKeyConfig holds configuration for API key authentication.
type APIKeyEntry ¶ added in v0.3.0
APIKeyEntry represents a configured API key with associated metadata.
type JWTConfig ¶ added in v0.3.0
type JWTConfig struct {
Secret string
Issuer string
SkipPaths []string
AllowExpiredAt time.Duration
}
JWTConfig holds configuration for JWT authentication.
type Service ¶
type Service struct{}
Service manages authentication and authorization.
func NewService ¶
func NewService() *Service
func (*Service) CheckPermission ¶
func (s *Service) CheckPermission(claims *UserClaims, required Role) bool
CheckPermission verifies a user has the required role.
type UserClaims ¶ added in v0.3.0
type UserClaims struct {
UserID string `json:"user_id"`
Roles []string `json:"roles"`
Exp int64 `json:"exp"`
}
UserClaims represents the decoded user information from a JWT.
func UserFromContext ¶ added in v0.3.0
func UserFromContext(ctx context.Context) (*UserClaims, bool)
UserFromContext extracts UserClaims from the request context.