web

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Authorization constants
	BearerPrefix = "Bearer"
)

Variables

View Source
var ErrInvalidToken = errors.New("invalid token")

ErrInvalidToken is returned when a token is invalid

Functions

func AuthMigrationMiddleware added in v0.10.1

func AuthMigrationMiddleware(legacy *LegacyAuthProvider, jwt *JWTAuthProvider) func(http.Handler) http.Handler

AuthMigrationMiddleware handles gradual migration from legacy to JWT

func ClearTokenCookie added in v0.10.1

func ClearTokenCookie(w http.ResponseWriter)

ClearTokenCookie removes the JWT token cookie

func ExtractTokenFromRequest added in v0.10.1

func ExtractTokenFromRequest(r *http.Request) string

ExtractTokenFromRequest extracts the JWT token from an HTTP request

func HashPassword added in v0.10.1

func HashPassword(password string) (string, error)

HashPassword generates a bcrypt hash of the password

func MigrateAuthToken added in v0.10.1

func MigrateAuthToken(legacyProvider *LegacyAuthProvider, jwtProvider AuthProvider, legacyToken string) (string, error)

MigrateAuthToken attempts to migrate a legacy token to JWT

func SetTokenCookie added in v0.10.1

func SetTokenCookie(w http.ResponseWriter, token string, expiry time.Duration)

SetTokenCookie sets a JWT token as an HTTP-only cookie

Types

type AuthConfig added in v0.10.1

type AuthConfig struct {
	Enabled      bool   `json:"enabled"`
	Username     string `json:"username"`
	Password     string `json:"password"`     // Deprecated: use PasswordHash instead
	PasswordHash string `json:"passwordHash"` // bcrypt hash of password (preferred)
	SecretKey    string `json:"secretKey"`
	TokenExpiry  int    `json:"tokenExpiry"` // in hours
}

AuthConfig holds authentication configuration

type AuthProvider added in v0.10.1

type AuthProvider interface {
	GenerateToken(username string) (string, error)
	ValidateToken(token string) (interface{}, error)
	Middleware(next http.Handler) http.Handler
}

AuthProvider interface for authentication implementations

func CreateAuthProvider added in v0.10.1

func CreateAuthProvider(config *AuthConfig) (AuthProvider, error)

CreateAuthProvider creates the appropriate auth provider based on configuration

type AuthStatusResponse added in v0.10.1

type AuthStatusResponse struct {
	Authenticated bool      `json:"authenticated"`
	Username      string    `json:"username,omitempty"`
	ExpiresAt     time.Time `json:"expiresAt,omitempty"`
}

AuthStatusResponse represents the auth status response

type Claims added in v0.10.1

type Claims struct {
	Username string `json:"username"`
	jwt.RegisteredClaims
}

Claims represents the JWT claims

type HealthCheck added in v0.10.1

type HealthCheck struct {
	Name        string        `json:"name"`
	Status      HealthStatus  `json:"status"`
	Message     string        `json:"message,omitempty"`
	LastChecked time.Time     `json:"lastChecked"`
	Duration    time.Duration `json:"durationMs"`
}

HealthCheck represents a single health check

type HealthChecker added in v0.10.1

type HealthChecker struct {
	// contains filtered or unexported fields
}

HealthChecker performs health checks

func NewHealthChecker added in v0.10.1

func NewHealthChecker(dockerProvider core.DockerProvider, version string) *HealthChecker

NewHealthChecker creates a new health checker

func (*HealthChecker) GetHealth added in v0.10.1

func (hc *HealthChecker) GetHealth() HealthResponse

GetHealth returns the current health status

func (*HealthChecker) HealthHandler added in v0.10.1

func (hc *HealthChecker) HealthHandler() http.HandlerFunc

HealthHandler returns detailed health information

func (*HealthChecker) LivenessHandler added in v0.10.1

func (hc *HealthChecker) LivenessHandler() http.HandlerFunc

LivenessHandler returns a simple liveness check

func (*HealthChecker) ReadinessHandler added in v0.10.1

func (hc *HealthChecker) ReadinessHandler() http.HandlerFunc

ReadinessHandler returns readiness status

type HealthResponse added in v0.10.1

type HealthResponse struct {
	Status    HealthStatus           `json:"status"`
	Timestamp time.Time              `json:"timestamp"`
	Uptime    float64                `json:"uptimeSeconds"`
	Version   string                 `json:"version"`
	Checks    map[string]HealthCheck `json:"checks"`
	System    SystemInfo             `json:"system"`
}

HealthResponse represents the health check response

type HealthStatus added in v0.10.1

type HealthStatus string

HealthStatus represents the overall health status

const (
	HealthStatusHealthy   HealthStatus = "healthy"
	HealthStatusDegraded  HealthStatus = "degraded"
	HealthStatusUnhealthy HealthStatus = "unhealthy"
)

type JWTAuthProvider added in v0.10.1

type JWTAuthProvider struct {
	// contains filtered or unexported fields
}

JWTAuthProvider wraps the new JWTManager for compatibility

func NewJWTAuthProvider added in v0.10.1

func NewJWTAuthProvider(secretKey string, expiryHours int) (*JWTAuthProvider, error)

NewJWTAuthProvider creates a JWT auth provider

func (*JWTAuthProvider) GenerateToken added in v0.10.1

func (j *JWTAuthProvider) GenerateToken(username string) (string, error)

func (*JWTAuthProvider) Middleware added in v0.10.1

func (j *JWTAuthProvider) Middleware(next http.Handler) http.Handler

func (*JWTAuthProvider) ValidateToken added in v0.10.1

func (j *JWTAuthProvider) ValidateToken(token string) (interface{}, error)

type JWTAuthStatus added in v0.10.1

type JWTAuthStatus struct {
	// contains filtered or unexported fields
}

JWTAuthStatus handles authentication status checks

func NewJWTAuthStatus added in v0.10.1

func NewJWTAuthStatus(jm *JWTManager) *JWTAuthStatus

NewJWTAuthStatus creates a new auth status handler

func (*JWTAuthStatus) ServeHTTP added in v0.10.1

func (h *JWTAuthStatus) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the auth status request

type JWTLoginHandler added in v0.10.1

type JWTLoginHandler struct {
	// contains filtered or unexported fields
}

JWTLoginHandler handles login requests with JWT

func NewJWTLoginHandler added in v0.10.1

func NewJWTLoginHandler(config *AuthConfig, jm *JWTManager) *JWTLoginHandler

NewJWTLoginHandler creates a new JWT login handler

func (*JWTLoginHandler) ServeHTTP added in v0.10.1

func (h *JWTLoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the login request

type JWTLogoutHandler added in v0.10.1

type JWTLogoutHandler struct {
	// contains filtered or unexported fields
}

JWTLogoutHandler handles logout requests

func NewJWTLogoutHandler added in v0.10.1

func NewJWTLogoutHandler(jm *JWTManager) *JWTLogoutHandler

NewJWTLogoutHandler creates a new JWT logout handler

func (*JWTLogoutHandler) ServeHTTP added in v0.10.1

func (h *JWTLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the logout request

type JWTManager added in v0.10.1

type JWTManager struct {
	// contains filtered or unexported fields
}

JWTManager handles JWT-based authentication

func NewJWTManager added in v0.10.1

func NewJWTManager(secretKey string, expiryHours int) (*JWTManager, error)

NewJWTManager creates a new JWT manager

func (*JWTManager) GenerateToken added in v0.10.1

func (jm *JWTManager) GenerateToken(username string) (string, error)

GenerateToken creates a new JWT token

func (*JWTManager) Middleware added in v0.10.1

func (jm *JWTManager) Middleware(next http.Handler) http.Handler

Middleware creates an HTTP middleware for JWT authentication

func (*JWTManager) RefreshToken added in v0.10.1

func (jm *JWTManager) RefreshToken(tokenString string) (string, error)

RefreshToken generates a new token with extended expiry

func (*JWTManager) ValidateToken added in v0.10.1

func (jm *JWTManager) ValidateToken(tokenString string) (*Claims, error)

ValidateToken validates a JWT token and returns the claims

type JWTRefreshHandler added in v0.10.1

type JWTRefreshHandler struct {
	// contains filtered or unexported fields
}

JWTRefreshHandler handles token refresh requests

func NewJWTRefreshHandler added in v0.10.1

func NewJWTRefreshHandler(jm *JWTManager) *JWTRefreshHandler

NewJWTRefreshHandler creates a new JWT refresh handler

func (*JWTRefreshHandler) ServeHTTP added in v0.10.1

func (h *JWTRefreshHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles the token refresh request

type LegacyAuthProvider added in v0.10.1

type LegacyAuthProvider struct {
	// contains filtered or unexported fields
}

LegacyAuthProvider wraps the old TokenManager for compatibility

func NewLegacyAuthProvider added in v0.10.1

func NewLegacyAuthProvider(secretKey string, expiryHours int) *LegacyAuthProvider

NewLegacyAuthProvider creates a legacy auth provider

func (*LegacyAuthProvider) GenerateToken added in v0.10.1

func (l *LegacyAuthProvider) GenerateToken(username string) (string, error)

func (*LegacyAuthProvider) Middleware added in v0.10.1

func (l *LegacyAuthProvider) Middleware(next http.Handler) http.Handler

func (*LegacyAuthProvider) ValidateToken added in v0.10.1

func (l *LegacyAuthProvider) ValidateToken(token string) (interface{}, error)

type LoginHandler added in v0.10.1

type LoginHandler struct {
	// contains filtered or unexported fields
}

LoginHandler handles authentication requests

func NewLoginHandler added in v0.10.1

func NewLoginHandler(config *AuthConfig, tm *TokenManager) *LoginHandler

func (*LoginHandler) ServeHTTP added in v0.10.1

func (h *LoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type LoginRequest added in v0.10.1

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

LoginRequest represents the login request body

type LoginResponse added in v0.10.1

type LoginResponse struct {
	Token     string    `json:"token"`
	ExpiresAt time.Time `json:"expiresAt"`
	Username  string    `json:"username"`
}

LoginResponse represents the login response

type LogoutHandler added in v0.10.1

type LogoutHandler struct {
	// contains filtered or unexported fields
}

LogoutHandler handles logout requests

func NewLogoutHandler added in v0.10.1

func NewLogoutHandler(tm *TokenManager) *LogoutHandler

func (*LogoutHandler) ServeHTTP added in v0.10.1

func (h *LogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type RateLimiter added in v0.10.1

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter manages login attempt rate limiting

func NewRateLimiter added in v0.10.1

func NewRateLimiter(ratePerMinute, burst int) *RateLimiter

func (*RateLimiter) Allow added in v0.10.1

func (rl *RateLimiter) Allow(key string) bool

func (*RateLimiter) CleanupOldLimiters added in v0.10.1

func (rl *RateLimiter) CleanupOldLimiters()

CleanupOldLimiters removes limiters that haven't been used recently

func (*RateLimiter) GetLimiter added in v0.10.1

func (rl *RateLimiter) GetLimiter(key string) *rate.Limiter

type RefreshResponse added in v0.10.1

type RefreshResponse struct {
	Token     string    `json:"token"`
	ExpiresAt time.Time `json:"expiresAt"`
}

RefreshResponse represents the refresh token response

type SecureAuthConfig added in v0.10.1

type SecureAuthConfig struct {
	Enabled      bool   `json:"enabled"`
	Username     string `json:"username"`
	PasswordHash string `json:"passwordHash"` // bcrypt hash of password
	SecretKey    string `json:"secretKey"`
	TokenExpiry  int    `json:"tokenExpiry"` // in hours
	MaxAttempts  int    `json:"maxAttempts"` // max login attempts per minute
}

SecureAuthConfig holds secure authentication configuration

type SecureLoginHandler added in v0.10.1

type SecureLoginHandler struct {
	// contains filtered or unexported fields
}

SecureLoginHandler handles authentication with security best practices

func NewSecureLoginHandler added in v0.10.1

func NewSecureLoginHandler(config *SecureAuthConfig, tm *SecureTokenManager, rl *RateLimiter) *SecureLoginHandler

func (*SecureLoginHandler) ServeHTTP added in v0.10.1

func (h *SecureLoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type SecureTokenManager added in v0.10.1

type SecureTokenManager struct {
	// contains filtered or unexported fields
}

SecureTokenManager handles token management with enhanced security

func NewSecureTokenManager added in v0.10.1

func NewSecureTokenManager(secretKey string, expiryHours int) (*SecureTokenManager, error)

func (*SecureTokenManager) GenerateCSRFToken added in v0.10.1

func (tm *SecureTokenManager) GenerateCSRFToken() (string, error)

GenerateCSRFToken creates a new CSRF token

func (*SecureTokenManager) GenerateToken added in v0.10.1

func (tm *SecureTokenManager) GenerateToken(username string) (string, error)

GenerateToken creates a new authentication token

func (*SecureTokenManager) RevokeToken added in v0.10.1

func (tm *SecureTokenManager) RevokeToken(token string)

RevokeToken invalidates a token

func (*SecureTokenManager) ValidateCSRFToken added in v0.10.1

func (tm *SecureTokenManager) ValidateCSRFToken(token string) bool

ValidateCSRFToken checks if a CSRF token is valid

func (*SecureTokenManager) ValidateToken added in v0.10.1

func (tm *SecureTokenManager) ValidateToken(token string) (*TokenData, bool)

ValidateToken checks if a token is valid

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string, s *core.Scheduler, cfg interface{}, provider core.DockerProvider) *Server

func (*Server) GetHTTPServer added in v0.10.1

func (s *Server) GetHTTPServer() *http.Server

GetHTTPServer returns the underlying http.Server for graceful shutdown support

func (*Server) HTTPServer added in v0.9.0

func (s *Server) HTTPServer() *http.Server

HTTPServer returns the underlying http.Server used by the web interface. It is exposed for tests and may change if the Server struct evolves.

func (*Server) RegisterHealthEndpoints added in v0.10.1

func (s *Server) RegisterHealthEndpoints(hc *HealthChecker)

RegisterHealthEndpoints registers health check endpoints on the server

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

func (*Server) Start

func (s *Server) Start() error

type SystemInfo added in v0.10.1

type SystemInfo struct {
	GoVersion    string `json:"goVersion"`
	NumGoroutine int    `json:"goroutines"`
	NumCPU       int    `json:"cpus"`
	MemoryAlloc  uint64 `json:"memoryAllocBytes"`
	MemoryTotal  uint64 `json:"memoryTotalBytes"`
	GCRuns       uint32 `json:"gcRuns"`
}

SystemInfo contains system-level information

type TokenData added in v0.10.1

type TokenData struct {
	Username  string    `json:"username"`
	ExpiresAt time.Time `json:"expiresAt"`
}

type TokenManager added in v0.10.1

type TokenManager struct {
	// contains filtered or unexported fields
}

Simple JWT implementation (for demonstration - in production use a proper JWT library)

func NewTokenManager added in v0.10.1

func NewTokenManager(secretKey string, expiryHours int) *TokenManager

func (*TokenManager) GenerateToken added in v0.10.1

func (tm *TokenManager) GenerateToken(username string) (string, error)

GenerateToken creates a new authentication token

func (*TokenManager) RevokeToken added in v0.10.1

func (tm *TokenManager) RevokeToken(token string)

RevokeToken invalidates a token

func (*TokenManager) ValidateToken added in v0.10.1

func (tm *TokenManager) ValidateToken(token string) (*TokenData, bool)

ValidateToken checks if a token is valid

Jump to

Keyboard shortcuts

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