auth

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package auth provides authentication and RBAC for ChronosOS.

Index

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

func JWTMiddleware(cfg JWTConfig) func(http.Handler) http.Handler

JWTMiddleware returns HTTP middleware that validates JWT bearer tokens.

func RequireRole added in v0.3.0

func RequireRole(svc *Service, required Role) func(http.Handler) http.Handler

RequireRole returns middleware that checks the authenticated user has at least the specified role. Must be used after JWT or API key middleware.

func WithUser added in v0.3.0

func WithUser(ctx context.Context, claims *UserClaims) context.Context

WithUser adds UserClaims to the context.

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

type APIKeyEntry struct {
	Scope  string
	UserID string
}

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 Role

type Role string

Role defines an RBAC role.

const (
	RoleAdmin  Role = "admin"
	RoleUser   Role = "user"
	RoleViewer Role = "viewer"
)

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.

Jump to

Keyboard shortcuts

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