Documentation
¶
Overview ¶
Package auth provides authentication and authorization functionality for modular applications. This module supports JWT tokens, session management, and OAuth2 flows.
The auth module provides:
- User authentication with configurable stores
- JWT token generation and validation
- Session management with configurable backends
- OAuth2 integration support
- Password hashing and validation
Usage:
app.RegisterModule(auth.NewModule())
The module registers an "auth" service that implements the AuthService interface, providing methods for user login, token validation, and session management.
Configuration:
The module requires an "auth" configuration section with JWT secrets, session settings, and OAuth2 configuration.
Index ¶
- Constants
- Variables
- func NewModule() modular.Module
- type AuthContext
- type AuthService
- type Claims
- type Config
- type JWTConfig
- type MemorySessionStore
- func (s *MemorySessionStore) Cleanup(ctx context.Context) error
- func (s *MemorySessionStore) Delete(ctx context.Context, sessionID string) error
- func (s *MemorySessionStore) Get(ctx context.Context, sessionID string) (*Session, error)
- func (s *MemorySessionStore) Store(ctx context.Context, session *Session) error
- type MemoryUserStore
- func (s *MemoryUserStore) CreateUser(ctx context.Context, user *User) error
- func (s *MemoryUserStore) DeleteUser(ctx context.Context, userID string) error
- func (s *MemoryUserStore) GetUser(ctx context.Context, userID string) (*User, error)
- func (s *MemoryUserStore) GetUserByEmail(ctx context.Context, email string) (*User, error)
- func (s *MemoryUserStore) UpdateUser(ctx context.Context, user *User) error
- type Middleware
- type Module
- func (m *Module) Constructor() modular.ModuleConstructor
- func (m *Module) Dependencies() []string
- func (m *Module) Init(app modular.Application) error
- func (m *Module) Name() string
- func (m *Module) ProvidesServices() []modular.ServiceProvider
- func (m *Module) RegisterConfig(app modular.Application) error
- func (m *Module) RequiresServices() []modular.ServiceDependency
- func (m *Module) Start(ctx context.Context) error
- func (m *Module) Stop(ctx context.Context) error
- type OAuth2Config
- type OAuth2Provider
- type OAuth2Result
- type PasswordConfig
- type Service
- func (s *Service) CreateSession(userID string, metadata map[string]interface{}) (*Session, error)
- func (s *Service) DeleteSession(sessionID string) error
- func (s *Service) ExchangeOAuth2Code(provider, code, state string) (*OAuth2Result, error)
- func (s *Service) GenerateToken(userID string, customClaims map[string]interface{}) (*TokenPair, error)
- func (s *Service) GetOAuth2AuthURL(provider, state string) (string, error)
- func (s *Service) GetSession(sessionID string) (*Session, error)
- func (s *Service) HashPassword(password string) (string, error)
- func (s *Service) RefreshSession(sessionID string) (*Session, error)
- func (s *Service) RefreshToken(refreshTokenString string) (*TokenPair, error)
- func (s *Service) ValidatePasswordStrength(password string) error
- func (s *Service) ValidateToken(tokenString string) (*Claims, error)
- func (s *Service) VerifyPassword(hashedPassword, password string) error
- type Session
- type SessionConfig
- type SessionStore
- type TokenPair
- type User
- type UserStore
Constants ¶
const ( // ServiceName is the name used to register the auth service. // Other modules can reference this service by this name for dependency injection. ServiceName = "auth" )
Variables ¶
var ( ErrInvalidConfig = errors.New("invalid auth configuration") ErrInvalidCredentials = errors.New("invalid credentials") ErrTokenExpired = errors.New("token has expired") ErrTokenInvalid = errors.New("token is invalid") ErrTokenMalformed = errors.New("token is malformed") ErrUserNotFound = errors.New("user not found") ErrUserAlreadyExists = errors.New("user already exists") ErrPasswordTooWeak = errors.New("password does not meet requirements") ErrSessionNotFound = errors.New("session not found") ErrSessionExpired = errors.New("session has expired") ErrOAuth2Failed = errors.New("oauth2 authentication failed") ErrProviderNotFound = errors.New("oauth2 provider not found") )
Auth module specific errors
Functions ¶
Types ¶
type AuthContext ¶
type AuthContext struct { User *User `json:"user"` Session *Session `json:"session"` Claims *Claims `json:"claims"` Permissions []string `json:"permissions"` Roles []string `json:"roles"` }
AuthContext represents authentication context in HTTP requests
type AuthService ¶
type AuthService interface { // JWT operations GenerateToken(userID string, claims map[string]interface{}) (*TokenPair, error) ValidateToken(token string) (*Claims, error) RefreshToken(refreshToken string) (*TokenPair, error) // Password operations HashPassword(password string) (string, error) VerifyPassword(hashedPassword, password string) error ValidatePasswordStrength(password string) error // Session operations CreateSession(userID string, metadata map[string]interface{}) (*Session, error) GetSession(sessionID string) (*Session, error) DeleteSession(sessionID string) error RefreshSession(sessionID string) (*Session, error) // OAuth2 operations GetOAuth2AuthURL(provider, state string) (string, error) ExchangeOAuth2Code(provider, code, state string) (*OAuth2Result, error) }
AuthService defines the main authentication service interface
type Claims ¶
type Claims struct { UserID string `json:"user_id"` Email string `json:"email"` Roles []string `json:"roles"` Permissions []string `json:"permissions"` IssuedAt time.Time `json:"iat"` ExpiresAt time.Time `json:"exp"` Issuer string `json:"iss"` Subject string `json:"sub"` Custom map[string]interface{} `json:"custom,omitempty"` }
Claims represents JWT token claims
type Config ¶
type Config struct { JWT JWTConfig `yaml:"jwt" env:"JWT"` Session SessionConfig `yaml:"session" env:"SESSION"` OAuth2 OAuth2Config `yaml:"oauth2" env:"OAUTH2"` Password PasswordConfig `yaml:"password" env:"PASSWORD"` }
Config represents the authentication module configuration
type JWTConfig ¶
type JWTConfig struct { Secret string `yaml:"secret" required:"true" env:"SECRET"` Expiration time.Duration `yaml:"expiration" default:"24h" env:"EXPIRATION"` RefreshExpiration time.Duration `yaml:"refresh_expiration" default:"168h" env:"REFRESH_EXPIRATION"` // 7 days Issuer string `yaml:"issuer" default:"modular-auth" env:"ISSUER"` Algorithm string `yaml:"algorithm" default:"HS256" env:"ALGORITHM"` }
JWTConfig contains JWT-related configuration
type MemorySessionStore ¶
type MemorySessionStore struct {
// contains filtered or unexported fields
}
MemorySessionStore implements SessionStore interface using in-memory storage
func NewMemorySessionStore ¶
func NewMemorySessionStore() *MemorySessionStore
NewMemorySessionStore creates a new in-memory session store
func (*MemorySessionStore) Cleanup ¶
func (s *MemorySessionStore) Cleanup(ctx context.Context) error
Cleanup removes expired sessions
func (*MemorySessionStore) Delete ¶
func (s *MemorySessionStore) Delete(ctx context.Context, sessionID string) error
Delete removes a session
type MemoryUserStore ¶
type MemoryUserStore struct {
// contains filtered or unexported fields
}
MemoryUserStore implements UserStore interface using in-memory storage
func NewMemoryUserStore ¶
func NewMemoryUserStore() *MemoryUserStore
NewMemoryUserStore creates a new in-memory user store
func (*MemoryUserStore) CreateUser ¶
func (s *MemoryUserStore) CreateUser(ctx context.Context, user *User) error
CreateUser creates a new user
func (*MemoryUserStore) DeleteUser ¶
func (s *MemoryUserStore) DeleteUser(ctx context.Context, userID string) error
DeleteUser deletes a user
func (*MemoryUserStore) GetUserByEmail ¶
GetUserByEmail retrieves a user by email
func (*MemoryUserStore) UpdateUser ¶
func (s *MemoryUserStore) UpdateUser(ctx context.Context, user *User) error
UpdateUser updates an existing user
type Middleware ¶
type Middleware interface { RequireAuth(next http.Handler) http.Handler OptionalAuth(next http.Handler) http.Handler RequireRole(role string) func(http.Handler) http.Handler RequirePermission(permission string) func(http.Handler) http.Handler }
Middleware defines authentication middleware interface
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module implements the modular.Module interface for authentication. It provides comprehensive authentication and authorization functionality including JWT tokens, sessions, and OAuth2 support.
The module is designed to work with pluggable stores for users and sessions, defaulting to in-memory implementations if external stores are not provided.
func (*Module) Constructor ¶
func (m *Module) Constructor() modular.ModuleConstructor
Constructor provides dependency injection for the module. This method creates the authentication service with injected dependencies, using fallback implementations for optional services that aren't provided.
The constructor pattern allows the module to be reconstructed with proper dependency injection after all required services have been resolved.
Dependencies resolved:
- user_store: External user storage (falls back to memory store)
- session_store: External session storage (falls back to memory store)
func (*Module) Dependencies ¶
Dependencies returns the module dependencies. The auth module has no required module dependencies, making it suitable for use as a foundation module that other modules can depend on.
func (*Module) Init ¶
func (m *Module) Init(app modular.Application) error
Init initializes the authentication module. This method validates the configuration and prepares the module for use. The actual service creation happens in the Constructor method to support dependency injection of user and session stores.
func (*Module) Name ¶
Name returns the module name. This name is used for dependency resolution and service registration.
func (*Module) ProvidesServices ¶
func (m *Module) ProvidesServices() []modular.ServiceProvider
ProvidesServices returns the services provided by this module. The auth module provides an authentication service that implements the AuthService interface, offering methods for user login, token validation, and session management.
func (*Module) RegisterConfig ¶
func (m *Module) RegisterConfig(app modular.Application) error
RegisterConfig registers the module's configuration requirements. This method sets up the configuration structure for the auth module, allowing the application to load authentication-related settings.
The auth module expects configuration for:
- JWT secret keys and token expiration
- Session configuration (timeouts, secure flags)
- OAuth2 provider settings
- Password policy settings
func (*Module) RequiresServices ¶
func (m *Module) RequiresServices() []modular.ServiceDependency
RequiresServices returns the services required by this module. The auth module can optionally use external user and session stores. If these services are not provided, the module will fall back to in-memory implementations suitable for development and testing.
Optional services:
- user_store: Implementation of UserStore interface for persistent user data
- session_store: Implementation of SessionStore interface for session persistence
type OAuth2Config ¶
type OAuth2Config struct {
Providers map[string]OAuth2Provider `yaml:"providers" env:"PROVIDERS"`
}
OAuth2Config contains OAuth2/OIDC configuration
type OAuth2Provider ¶
type OAuth2Provider struct { ClientID string `yaml:"client_id" required:"true" env:"CLIENT_ID"` ClientSecret string `yaml:"client_secret" required:"true" env:"CLIENT_SECRET"` RedirectURL string `yaml:"redirect_url" required:"true" env:"REDIRECT_URL"` Scopes []string `yaml:"scopes" env:"SCOPES"` AuthURL string `yaml:"auth_url" env:"AUTH_URL"` TokenURL string `yaml:"token_url" env:"TOKEN_URL"` UserInfoURL string `yaml:"user_info_url" env:"USER_INFO_URL"` }
OAuth2Provider represents an OAuth2 provider configuration
type OAuth2Result ¶
type OAuth2Result struct { Provider string `json:"provider"` UserInfo map[string]interface{} `json:"user_info"` AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` ExpiresAt time.Time `json:"expires_at"` }
OAuth2Result represents the result of OAuth2 authentication
type PasswordConfig ¶
type PasswordConfig struct { Algorithm string `yaml:"algorithm" default:"bcrypt" env:"ALGORITHM"` // bcrypt, argon2 MinLength int `yaml:"min_length" default:"8" env:"MIN_LENGTH"` RequireUpper bool `yaml:"require_upper" default:"true" env:"REQUIRE_UPPER"` RequireLower bool `yaml:"require_lower" default:"true" env:"REQUIRE_LOWER"` RequireDigit bool `yaml:"require_digit" default:"true" env:"REQUIRE_DIGIT"` RequireSpecial bool `yaml:"require_special" default:"false" env:"REQUIRE_SPECIAL"` BcryptCost int `yaml:"bcrypt_cost" default:"12" env:"BCRYPT_COST"` }
PasswordConfig contains password-related configuration
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements the AuthService interface
func NewService ¶
func NewService(config *Config, userStore UserStore, sessionStore SessionStore) *Service
NewService creates a new authentication service
func (*Service) CreateSession ¶
CreateSession creates a new user session
func (*Service) DeleteSession ¶
DeleteSession removes a session
func (*Service) ExchangeOAuth2Code ¶
func (s *Service) ExchangeOAuth2Code(provider, code, state string) (*OAuth2Result, error)
ExchangeOAuth2Code exchanges an OAuth2 authorization code for user info
func (*Service) GenerateToken ¶
func (s *Service) GenerateToken(userID string, customClaims map[string]interface{}) (*TokenPair, error)
GenerateToken creates a new JWT token pair
func (*Service) GetOAuth2AuthURL ¶
GetOAuth2AuthURL returns the OAuth2 authorization URL for a provider
func (*Service) GetSession ¶
GetSession retrieves a session by ID
func (*Service) HashPassword ¶
HashPassword hashes a password using bcrypt
func (*Service) RefreshSession ¶
RefreshSession extends a session's expiration time
func (*Service) RefreshToken ¶
RefreshToken creates a new token pair using a refresh token
func (*Service) ValidatePasswordStrength ¶
ValidatePasswordStrength validates password against configured requirements
func (*Service) ValidateToken ¶
ValidateToken validates a JWT token and returns the claims
func (*Service) VerifyPassword ¶
VerifyPassword verifies a password against its hash
type Session ¶
type Session struct { ID string `json:"id"` UserID string `json:"user_id"` CreatedAt time.Time `json:"created_at"` ExpiresAt time.Time `json:"expires_at"` IPAddress string `json:"ip_address"` UserAgent string `json:"user_agent"` Active bool `json:"active"` Metadata map[string]interface{} `json:"metadata,omitempty"` }
Session represents a user session
type SessionConfig ¶
type SessionConfig struct { Store string `yaml:"store" default:"memory" env:"STORE"` // memory, redis, database CookieName string `yaml:"cookie_name" default:"session_id" env:"COOKIE_NAME"` MaxAge time.Duration `yaml:"max_age" default:"24h" env:"MAX_AGE"` Secure bool `yaml:"secure" default:"true" env:"SECURE"` HTTPOnly bool `yaml:"http_only" default:"true" env:"HTTP_ONLY"` SameSite string `yaml:"same_site" default:"strict" env:"SAME_SITE"` // strict, lax, none Domain string `yaml:"domain" env:"DOMAIN"` Path string `yaml:"path" default:"/" env:"PATH"` }
SessionConfig contains session-related configuration
type SessionStore ¶
type SessionStore interface { Store(ctx context.Context, session *Session) error Get(ctx context.Context, sessionID string) (*Session, error) Delete(ctx context.Context, sessionID string) error Cleanup(ctx context.Context) error // Remove expired sessions }
SessionStore defines the interface for session storage operations
type TokenPair ¶
type TokenPair struct { AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` TokenType string `json:"token_type"` ExpiresIn int64 `json:"expires_in"` ExpiresAt time.Time `json:"expires_at"` }
TokenPair represents an access token and refresh token pair
type User ¶
type User struct { ID string `json:"id"` Email string `json:"email"` PasswordHash string `json:"-"` // Never serialize password hash Roles []string `json:"roles"` Permissions []string `json:"permissions"` Active bool `json:"active"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` LastLoginAt *time.Time `json:"last_login_at,omitempty"` Metadata map[string]interface{} `json:"metadata,omitempty"` }
User represents a user in the authentication system
type UserStore ¶
type UserStore interface { GetUser(ctx context.Context, userID string) (*User, error) GetUserByEmail(ctx context.Context, email string) (*User, error) CreateUser(ctx context.Context, user *User) error UpdateUser(ctx context.Context, user *User) error DeleteUser(ctx context.Context, userID string) error }
UserStore defines the interface for user storage operations