guards

package
v0.9.17 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithSessionContext added in v0.9.3

func WithSessionContext(r *http.Request) *http.Request

WithSessionContext returns a new request with a session cache attached to its context. Call this from middleware to enable per-request session caching that is automatically cleaned up when the request completes.

Types

type JWTGuard

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

JWTGuard implements JWT-based authentication for APIs

func NewJWTGuard

func NewJWTGuard(provider auth.UserProvider, config auth.JWTConfig, ctx ...context.Context) *JWTGuard

NewJWTGuard creates a new JWT guard. An optional context.Context can be passed to control the lifecycle of the background cache cleanup goroutine. When the context is cancelled, the cleanup goroutine stops automatically. If no context is provided, call StopCleanup() to stop the goroutine manually.

func (*JWTGuard) Attempt

func (g *JWTGuard) Attempt(w http.ResponseWriter, r *http.Request, credentials map[string]interface{}, remember ...bool) (bool, error)

Attempt validates credentials and generates JWT if valid

func (*JWTGuard) Check

func (g *JWTGuard) Check(r *http.Request) bool

Check if user is authenticated via JWT

func (*JWTGuard) GenerateRefreshToken

func (g *JWTGuard) GenerateRefreshToken(user auth.Authenticatable) (string, error)

GenerateRefreshToken generates a refresh token

func (*JWTGuard) GenerateToken

func (g *JWTGuard) GenerateToken(user auth.Authenticatable, claims ...map[string]interface{}) (string, error)

GenerateToken generates a JWT token for a user

func (*JWTGuard) ID

func (g *JWTGuard) ID(r *http.Request) interface{}

ID returns the authenticated user ID from JWT

func (*JWTGuard) Login

func (g *JWTGuard) Login(w http.ResponseWriter, r *http.Request, user auth.Authenticatable, remember ...bool) error

Login generates a JWT token for the user

func (*JWTGuard) LoginByID

func (g *JWTGuard) LoginByID(w http.ResponseWriter, r *http.Request, id interface{}, remember ...bool) error

LoginByID logs in a user by ID and generates JWT

func (*JWTGuard) Logout

func (g *JWTGuard) Logout(w http.ResponseWriter, r *http.Request) error

Logout revokes the JWT token

func (*JWTGuard) RefreshToken

func (g *JWTGuard) RefreshToken(refreshToken string) (string, error)

RefreshToken refreshes an access token using refresh token

func (*JWTGuard) SetProvider

func (g *JWTGuard) SetProvider(provider auth.UserProvider)

SetProvider sets the user provider

func (*JWTGuard) StopCleanup added in v0.9.3

func (g *JWTGuard) StopCleanup()

StopCleanup stops the background cache cleanup goroutine.

func (*JWTGuard) User

User returns the authenticated user from JWT

func (*JWTGuard) ValidateToken

func (g *JWTGuard) ValidateToken(token string) (*auth.Claims, error)

ValidateToken validates a JWT token

type SessionGuard

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

SessionGuard implements session-based authentication

func NewSessionGuard

func NewSessionGuard(provider auth.UserProvider, config auth.SessionConfig, encryptor ...crypto.Encryptor) (*SessionGuard, error)

NewSessionGuard creates a new session guard. The encryptor parameter is optional — pass nil if crypto is not configured (session guard will still work if a non-cookie store is used later).

func (*SessionGuard) Attempt

func (g *SessionGuard) Attempt(w http.ResponseWriter, r *http.Request, credentials map[string]interface{}, remember ...bool) (bool, error)

Attempt attempts to log in with credentials

func (*SessionGuard) Check

func (g *SessionGuard) Check(r *http.Request) bool

Check if user is authenticated

func (*SessionGuard) ID

func (g *SessionGuard) ID(r *http.Request) interface{}

ID returns the authenticated user ID

func (*SessionGuard) Login

func (g *SessionGuard) Login(w http.ResponseWriter, r *http.Request, user auth.Authenticatable, remember ...bool) error

Login logs in a user

func (*SessionGuard) LoginByID

func (g *SessionGuard) LoginByID(w http.ResponseWriter, r *http.Request, id interface{}, remember ...bool) error

LoginByID logs in a user by ID

func (*SessionGuard) Logout

func (g *SessionGuard) Logout(w http.ResponseWriter, r *http.Request) error

Logout logs out the user

func (*SessionGuard) SetProvider

func (g *SessionGuard) SetProvider(provider auth.UserProvider)

SetProvider sets the user provider

func (*SessionGuard) User

User returns the authenticated user

Jump to

Keyboard shortcuts

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