auth

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package auth provides JWT authentication middleware for the REST API

Package auth provides JWT authentication middleware for the REST API

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddClaimsToContext

func AddClaimsToContext(ctx context.Context, claims *Claims) context.Context

AddClaimsToContext adds JWT claims to the request context

func BasicAuthMiddleware

func BasicAuthMiddleware(username, password string) func(http.Handler) http.Handler

BasicAuthMiddleware provides basic authentication using Authorization header

func GenerateSecretKey

func GenerateSecretKey() ([]byte, error)

GenerateSecretKey generates a cryptographically random secret key for JWT signing

func GetDefaultAdminPassword

func GetDefaultAdminPassword() (string, error)

GetDefaultAdminPassword generates a random password for initial admin setup

func GetUsernameFromContext

func GetUsernameFromContext(ctx context.Context) string

GetUsernameFromContext retrieves the username from the request context

func HasRoleInContext

func HasRoleInContext(ctx context.Context, role string) bool

HasRoleInContext checks if the user has a specific role

func RequireRole

func RequireRole(role string) func(http.Handler) http.Handler

RequireRole is an HTTP middleware that requires 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

func GetClaimsFromContext(ctx context.Context) *Claims

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

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

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

type UserStore

type UserStore struct {
	Users    []*User `json:"users"`
	FilePath string  `json:"-"`
}

UserStore is the on-disk storage for users

Jump to

Keyboard shortcuts

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