auth

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("invalid or expired password reset token")

Functions

func GuestMiddleware

func GuestMiddleware(guard Guard, redirectTo string) func(http.Handler) http.Handler

GuestMiddleware allows only unauthenticated users (useful for login/register pages).

func Middleware

func Middleware(guard Guard) func(http.Handler) http.Handler

Middleware returns an HTTP middleware that ensures the user is authenticated using the provided Guard.

func RequireVerified

func RequireVerified(guard Guard, redirectTo string) func(http.Handler) http.Handler

RequireVerified is a middleware that ensures the authenticated user has verified their email.

func SessionFixationMiddleware

func SessionFixationMiddleware(guard Guard) func(http.Handler) http.Handler

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.

func User

func User(r *http.Request) any

User retrieves the authenticated user from the HTTP request context. It returns nil if no user is authenticated.

func UserID

func UserID(r *http.Request) string

UserID retrieves the ID of the authenticated user from the HTTP request context. It assumes the user implements the Authenticatable interface.

Types

type Authenticatable

type Authenticatable interface {
	GetAuthIdentifier() string
	GetAuthPassword() string
}

Authenticatable is an interface that users must implement to be logged in via SessionGuard.

type ContextKey

type ContextKey string
const UserContextKey ContextKey = "user"

type GenericUser

type GenericUser struct {
	ID       int
	Name     string
	Email    string
	Password string
}

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

func NewLockout(maxAttempts, lockoutMinutes int) *Lockout

NewLockout creates a new lockout manager.

func (*Lockout) IsLocked

func (l *Lockout) IsLocked(key string) bool

Key can be email, IP, or user ID.

func (*Lockout) RecordFailure

func (l *Lockout) RecordFailure(key string)

func (*Lockout) Reset

func (l *Lockout) Reset(key string)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager resolves the guards configured for the application.

func NewManager

func NewManager() *Manager

NewManager creates a new Auth Manager.

func (*Manager) AddGuard

func (m *Manager) AddGuard(name string, guard Guard)

AddGuard registers a new guard.

func (*Manager) Guard

func (m *Manager) Guard(name string) Guard

Guard retrieves a guard instance by name.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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