Documentation
¶
Index ¶
- Variables
- func GuestMiddleware(guard Guard, redirectTo string) func(http.Handler) http.Handler
- func Middleware(guard Guard) func(http.Handler) http.Handler
- func RequireVerified(guard Guard, redirectTo string) func(http.Handler) http.Handler
- func SessionFixationMiddleware(guard Guard) func(http.Handler) http.Handler
- func User(r *http.Request) any
- func UserID(r *http.Request) string
- type Authenticatable
- type ContextKey
- type GenericUser
- type Guard
- type Lockout
- type Manager
- type ORMUserProvider
- func (p *ORMUserProvider) RetrieveByCredentials(credentials map[string]any) any
- func (p *ORMUserProvider) RetrieveByID(identifier string) any
- func (p *ORMUserProvider) RetrieveByToken(identifier string, token string) any
- func (p *ORMUserProvider) UpdateRememberToken(user any, token string)
- func (p *ORMUserProvider) ValidateCredentials(user any, credentials map[string]any) bool
- type SessionGuard
- func (g *SessionGuard) Attempt(credentials map[string]any, remember ...bool) bool
- func (g *SessionGuard) Check() bool
- func (g *SessionGuard) Guest() bool
- func (g *SessionGuard) ID() string
- func (g *SessionGuard) Login(user any, remember ...bool)
- func (g *SessionGuard) LoginByID(userID string, remember ...bool)
- func (g *SessionGuard) Logout()
- func (g *SessionGuard) RegenerateSession()
- func (g *SessionGuard) SetUser(user any)
- func (g *SessionGuard) User() any
- type UserProvider
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidToken = errors.New("invalid or expired password reset token")
Functions ¶
func GuestMiddleware ¶
GuestMiddleware allows only unauthenticated users (useful for login/register pages).
func Middleware ¶
Middleware returns an HTTP middleware that ensures the user is authenticated using the provided Guard.
func RequireVerified ¶
RequireVerified is a middleware that ensures the authenticated user has verified their email.
func SessionFixationMiddleware ¶
SessionFixationMiddleware regenerates the session ID on authentication state change. Use this middleware to prevent session fixation attacks. It should be applied after the Auth middleware.
Types ¶
type Authenticatable ¶
Authenticatable is an interface that users must implement to be logged in via SessionGuard.
type GenericUser ¶
GenericUser is a basic Authenticatable implementation for ORMUserProvider.
func (*GenericUser) GetAuthIdentifier ¶
func (u *GenericUser) GetAuthIdentifier() string
func (*GenericUser) GetAuthPassword ¶
func (u *GenericUser) GetAuthPassword() string
type Guard ¶
type Guard interface {
// Check determines if the current user is authenticated.
Check() bool
// Guest determines if the current user is a guest.
Guest() bool
// User returns the currently authenticated user.
User() any
// ID returns the ID of the currently authenticated user.
ID() string
// Attempt tries to authenticate a user using the given credentials.
Attempt(credentials map[string]any, remember ...bool) bool
// Login logs a user into the application.
Login(user any, remember ...bool)
// Logout logs the user out of the application.
Logout()
}
Guard defines the interface for authenticating users per request.
type Lockout ¶
type Lockout struct {
// contains filtered or unexported fields
}
Lockout tracks failed login attempts and locks users out.
func NewLockout ¶
NewLockout creates a new lockout manager.
func (*Lockout) RecordFailure ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager resolves the guards configured for the application.
type ORMUserProvider ¶
type ORMUserProvider struct {
// contains filtered or unexported fields
}
ORMUserProvider is a UserProvider implementation that retrieves users from the ORM.
func NewORMUserProvider ¶
func NewORMUserProvider(db *orm.DB, model Authenticatable) *ORMUserProvider
NewORMUserProvider creates a new ORM-backed user provider.
func (*ORMUserProvider) RetrieveByCredentials ¶
func (p *ORMUserProvider) RetrieveByCredentials(credentials map[string]any) any
func (*ORMUserProvider) RetrieveByID ¶
func (p *ORMUserProvider) RetrieveByID(identifier string) any
func (*ORMUserProvider) RetrieveByToken ¶
func (p *ORMUserProvider) RetrieveByToken(identifier string, token string) any
func (*ORMUserProvider) UpdateRememberToken ¶
func (p *ORMUserProvider) UpdateRememberToken(user any, token string)
func (*ORMUserProvider) ValidateCredentials ¶
func (p *ORMUserProvider) ValidateCredentials(user any, credentials map[string]any) bool
type SessionGuard ¶
type SessionGuard struct {
// contains filtered or unexported fields
}
SessionGuard implements the Guard interface using standard HTTP sessions.
func NewSessionGuard ¶
func NewSessionGuard(name string, provider UserProvider, session *session.Manager) *SessionGuard
NewSessionGuard creates a new session guard instance.
func (*SessionGuard) Attempt ¶
func (g *SessionGuard) Attempt(credentials map[string]any, remember ...bool) bool
func (*SessionGuard) Check ¶
func (g *SessionGuard) Check() bool
func (*SessionGuard) Guest ¶
func (g *SessionGuard) Guest() bool
func (*SessionGuard) ID ¶
func (g *SessionGuard) ID() string
func (*SessionGuard) Login ¶
func (g *SessionGuard) Login(user any, remember ...bool)
func (*SessionGuard) LoginByID ¶
func (g *SessionGuard) LoginByID(userID string, remember ...bool)
LoginByID logs in a user by their ID string directly.
func (*SessionGuard) Logout ¶
func (g *SessionGuard) Logout()
func (*SessionGuard) RegenerateSession ¶
func (g *SessionGuard) RegenerateSession()
RegenerateSession regenerates the session ID (for security after login or privilege changes).
func (*SessionGuard) SetUser ¶
func (g *SessionGuard) SetUser(user any)
SetUser sets the authenticated user directly (useful for testing or manual auth).
func (*SessionGuard) User ¶
func (g *SessionGuard) User() any
type UserProvider ¶
type UserProvider interface {
// RetrieveByID retrieves a user by their unique identifier.
RetrieveByID(identifier string) any
// RetrieveByToken retrieves a user by their unique identifier and "remember me" token.
RetrieveByToken(identifier string, token string) any
// UpdateRememberToken updates the "remember me" token for the given user.
UpdateRememberToken(user any, token string)
// RetrieveByCredentials retrieves a user by the given credentials.
RetrieveByCredentials(credentials map[string]any) any
// ValidateCredentials validates a user against the given credentials.
ValidateCredentials(user any, credentials map[string]any) bool
}
UserProvider abstracts how users are retrieved from the storage mechanism.