Documentation
¶
Index ¶
- Constants
- func CreateClaimsFromUser(user *database.User) token.Claims
- func GetUserFromContext(ctx context.Context) *database.User
- func IsAdmin(ctx context.Context) bool
- func IsAuthenticated(ctx context.Context) bool
- func JWTMiddleware(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
- func RequireAdmin(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
- func RequireAuth(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
- type Config
- type Service
- func (s *Service) AuthService() *auth.Service
- func (s *Service) AuthenticateUser(username, password string) (*database.User, error)
- func (s *Service) CreateOrUpdateUser(claims token.Claims) (*database.User, error)
- func (s *Service) GetUserFromToken(tokenStr string) (*database.User, error)
- func (s *Service) HashPassword(password string) (string, error)
- func (s *Service) IsUserAdmin(userID string) (bool, error)
- func (s *Service) RegisterUser(username, email, password string) (*database.User, error)
- func (s *Service) SetupProviders(config *Config) error
- func (s *Service) TokenService() *token.Service
Constants ¶
const UserContextKey contextKey = "user"
Variables ¶
This section is empty.
Functions ¶
func CreateClaimsFromUser ¶
CreateClaimsFromUser creates JWT claims from a database user
func GetUserFromContext ¶
GetUserFromContext extracts user from request context
func IsAuthenticated ¶
IsAuthenticated checks if the request has a valid authenticated user
func JWTMiddleware ¶
func JWTMiddleware(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
JWTMiddleware provides JWT authentication middleware
func RequireAdmin ¶
func RequireAdmin(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
RequireAdmin middleware requires admin privileges for protected routes
func RequireAuth ¶
func RequireAuth(tokenService *token.Service, userRepo *database.UserRepository) func(http.Handler) http.Handler
RequireAuth middleware requires authentication for protected routes
Types ¶
type Config ¶
type Config struct {
// JWT configuration
JWTSecret string // JWT signing secret
TokenDuration time.Duration // JWT token duration
CookieDomain string // Cookie domain
CookieSecure bool // Secure cookie flag
CookieSameSite http.SameSite // SameSite cookie attribute
// Direct authentication
DirectAuthEnabled bool // Enable direct username/password authentication
DirectAuthSalt string // Salt for direct authentication
// Application settings
Issuer string // JWT issuer
Audience string // JWT audience
}
Config represents authentication service configuration
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns default authentication configuration
func LoadConfigFromEnv ¶
func LoadConfigFromEnv() *Config
LoadConfigFromEnv loads configuration from environment variables
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles authentication operations using go-pkgz/auth
func NewService ¶
func NewService(config *Config, userRepo *database.UserRepository) (*Service, error)
NewService creates a new authentication service
func (*Service) AuthService ¶
AuthService returns the underlying auth service
func (*Service) AuthenticateUser ¶
AuthenticateUser verifies username/password and returns user
func (*Service) CreateOrUpdateUser ¶
CreateOrUpdateUser creates or updates a user based on token claims
func (*Service) GetUserFromToken ¶
GetUserFromToken extracts user information from JWT token
func (*Service) HashPassword ¶
HashPassword hashes a password using bcrypt
func (*Service) IsUserAdmin ¶
IsUserAdmin checks if a user has admin privileges
func (*Service) RegisterUser ¶
RegisterUser creates a new user with username and password
func (*Service) SetupProviders ¶
SetupProviders configures authentication providers
func (*Service) TokenService ¶
TokenService returns the token service for JWT operations