Documentation
¶
Overview ¶
Package auth provides JWT authentication middleware for the REST API
Package auth provides JWT authentication middleware for the REST API
Index ¶
- func AddClaimsToContext(ctx context.Context, claims *Claims) context.Context
- func BasicAuthMiddleware(username, password string) func(http.Handler) http.Handler
- func GenerateSecretKey() ([]byte, error)
- func GetDefaultAdminPassword() (string, error)
- func GetUsernameFromContext(ctx context.Context) string
- func HasRoleInContext(ctx context.Context, role string) bool
- func RequireRole(role string) func(http.Handler) http.Handler
- type Claims
- type Config
- type LoginRequest
- type LoginResponse
- type Middleware
- type User
- type UserManager
- func (um *UserManager) AddUser(username, password, role string) error
- func (um *UserManager) Authenticate(username, password string) (*User, error)
- func (um *UserManager) DeleteUser(username string) error
- func (um *UserManager) ListUsers() []map[string]interface{}
- func (um *UserManager) UserExists(username string) bool
- type UserStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddClaimsToContext ¶
AddClaimsToContext adds JWT claims to the request context
func BasicAuthMiddleware ¶
BasicAuthMiddleware provides basic authentication using Authorization header
func GenerateSecretKey ¶
GenerateSecretKey generates a cryptographically random secret key for JWT signing
func GetDefaultAdminPassword ¶
GetDefaultAdminPassword generates a random password for initial admin setup
func GetUsernameFromContext ¶
GetUsernameFromContext retrieves the username from the request context
func HasRoleInContext ¶
HasRoleInContext checks if the user has a specific role
Types ¶
type Claims ¶
type Claims struct {
Username string `json:"username"`
Roles []string `json:"roles"`
jwt.RegisteredClaims
}
Claims represents JWT claims
func GetClaimsFromContext ¶
GetClaimsFromContext retrieves JWT claims from the request context
type Config ¶
type Config struct {
// SecretKey is the key used to sign JWT tokens
SecretKey []byte
// TokenExpiry is how long tokens are valid
TokenExpiry time.Duration
// AdminUsername is the admin username for login
AdminUsername string
// AdminPassword is the admin password for login
AdminPassword string
}
Config holds JWT authentication configuration
type LoginRequest ¶
LoginRequest represents a login request
type LoginResponse ¶
type LoginResponse struct {
Token string `json:"token"`
ExpiresAt time.Time `json:"expires_at"`
Username string `json:"username"`
Roles []string `json:"roles"`
}
LoginResponse represents a login response
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware provides JWT authentication middleware
func NewMiddleware ¶
func NewMiddleware(config Config) *Middleware
NewMiddleware creates a new authentication middleware
func (*Middleware) Authenticate ¶
func (m *Middleware) Authenticate(username, password string) (*LoginResponse, error)
Authenticate authenticates a user and returns a JWT token
func (*Middleware) GenerateToken ¶
func (m *Middleware) GenerateToken(username string) (string, error)
GenerateToken generates a new JWT token for a user
func (*Middleware) RequireAuth ¶
func (m *Middleware) RequireAuth(next http.Handler) http.Handler
RequireAuth wraps an HTTP handler with JWT authentication
type User ¶
type User struct {
Username string `json:"username"`
Password string `json:"password"` // bcrypt hashed
Roles []string `json:"roles"`
CreatedAt time.Time `json:"created_at"`
LastLogin *time.Time `json:"last_login,omitempty"`
}
User represents a user account
type UserManager ¶
type UserManager struct {
// contains filtered or unexported fields
}
UserManager manages user accounts stored on disk
func NewUserManager ¶
func NewUserManager(dataDir string) (*UserManager, error)
NewUserManager creates a new user manager
func (*UserManager) AddUser ¶
func (um *UserManager) AddUser(username, password, role string) error
AddUser adds a new user
func (*UserManager) Authenticate ¶
func (um *UserManager) Authenticate(username, password string) (*User, error)
Authenticate validates username/password
func (*UserManager) DeleteUser ¶
func (um *UserManager) DeleteUser(username string) error
DeleteUser deletes a user
func (*UserManager) ListUsers ¶
func (um *UserManager) ListUsers() []map[string]interface{}
ListUsers returns all users (without passwords)
func (*UserManager) UserExists ¶
func (um *UserManager) UserExists(username string) bool
UserExists checks if a user exists