Documentation
¶
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
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"`
Session SessionConfig `yaml:"session"`
OAuth2 OAuth2Config `yaml:"oauth2"`
Password PasswordConfig `yaml:"password"`
}
Config represents the authentication module configuration
type JWTConfig ¶
type JWTConfig struct {
Secret string `yaml:"secret" required:"true"`
Expiration time.Duration `yaml:"expiration" default:"24h"`
RefreshExpiration time.Duration `yaml:"refresh_expiration" default:"168h"` // 7 days
Issuer string `yaml:"issuer" default:"modular-auth"`
Algorithm string `yaml:"algorithm" default:"HS256"`
}
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
func (*Module) Constructor ¶
func (m *Module) Constructor() modular.ModuleConstructor
Constructor provides dependency injection for the module
func (*Module) Dependencies ¶
Dependencies returns the module dependencies
func (*Module) Init ¶
func (m *Module) Init(app modular.Application) error
Init initializes the authentication module
func (*Module) ProvidesServices ¶
func (m *Module) ProvidesServices() []modular.ServiceProvider
ProvidesServices returns the services provided by this module
func (*Module) RegisterConfig ¶
func (m *Module) RegisterConfig(app modular.Application) error
RegisterConfig registers the module's configuration
func (*Module) RequiresServices ¶
func (m *Module) RequiresServices() []modular.ServiceDependency
RequiresServices returns the services required by this module
type OAuth2Config ¶
type OAuth2Config struct {
Providers map[string]OAuth2Provider `yaml:"providers"`
}
OAuth2Config contains OAuth2/OIDC configuration
type OAuth2Provider ¶
type OAuth2Provider struct {
ClientID string `yaml:"client_id" required:"true"`
ClientSecret string `yaml:"client_secret" required:"true"`
RedirectURL string `yaml:"redirect_url" required:"true"`
Scopes []string `yaml:"scopes"`
AuthURL string `yaml:"auth_url"`
TokenURL string `yaml:"token_url"`
UserInfoURL string `yaml:"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"` // bcrypt, argon2
MinLength int `yaml:"min_length" default:"8"`
RequireUpper bool `yaml:"require_upper" default:"true"`
RequireLower bool `yaml:"require_lower" default:"true"`
RequireDigit bool `yaml:"require_digit" default:"true"`
RequireSpecial bool `yaml:"require_special" default:"false"`
BcryptCost int `yaml:"bcrypt_cost" default:"12"`
}
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"` // memory, redis, database
CookieName string `yaml:"cookie_name" default:"session_id"`
MaxAge time.Duration `yaml:"max_age" default:"24h"`
Secure bool `yaml:"secure" default:"true"`
HTTPOnly bool `yaml:"http_only" default:"true"`
SameSite string `yaml:"same_site" default:"strict"` // strict, lax, none
Domain string `yaml:"domain"`
Path string `yaml:"path" default:"/"`
}
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