Documentation
¶
Overview ¶
Package auth provides session-based authentication middleware for the Rex router. It uses secure cookie sessions to maintain authentication state and supports storing custom user state in the session. It also provide JWT and BasicAuth middleware. View the README for more information.
Index ¶
- Variables
- func BasicAuth(username, password string, realm ...string) rex.Middleware
- func CreateJWTToken(secret string, payload any, exp time.Duration) (string, error)
- func DefaultErrorHandler(c *rex.Context) error
- func JWT(secret string, skipFunc func(c *rex.Context) bool) rex.Middleware
- func JWTAuthSkipped(r *http.Request) bool
- func JwtClaims(req *http.Request) (jwt.MapClaims, error)
- func VerifyJWToken(secret, tokenString string) (jwt.MapClaims, error)
- type CookieAuth
- type CookieConfig
- type CtxKey
Constants ¶
This section is empty.
Variables ¶
var ErrNotInitialized = errors.New("auth: cookie auth is not initialized")
ErrNotInitialized is returned when a CookieAuth instance is nil or missing its store.
Functions ¶
func BasicAuth ¶
func BasicAuth(username, password string, realm ...string) rex.Middleware
BasicAuth returns middleware that protects routes with HTTP Basic authentication. If the credentials are invalid, it responds with status 401. The default realm is "Restricted".
func CreateJWTToken ¶
CreateJWTToken creates a JWT token with the given payload and expiry duration. The token is signed with the secret key using HMAC SHA-256.
func DefaultErrorHandler ¶ added in v1.9.0
DefaultErrorHandler returns HTTP 401 for unauthenticated requests.
func JWT ¶
JWT creates a JWT middleware with the given secret and options. If skipFunc returns true, authentication is skipped.
func JWTAuthSkipped ¶ added in v1.0.3
JWTAuthSkipped reports whether JWT authentication was skipped for the request.
func JwtClaims ¶ added in v1.0.5
JwtClaims returns the JWT claims stored on the request context. It should be called after JWT verification has completed.
func VerifyJWToken ¶
VerifyJWToken verifies the given JWT token with the secret key. Returns the claims if the token is valid, otherwise an error. The token is verified using the HMAC256 algorithm. The default claims are stored in the "payload" key and the expiry time in the "exp" key.
Types ¶
type CookieAuth ¶ added in v1.9.0
type CookieAuth struct {
// contains filtered or unexported fields
}
CookieAuth encapsulates session cookie authentication state and behavior.
func NewCookieAuth ¶ added in v1.9.0
func NewCookieAuth(sessionName string, keyPairs [][]byte, userType any, config CookieConfig) (*CookieAuth, error)
NewCookieAuth creates a cookie authentication instance with its own store and session name.
func (*CookieAuth) Clear ¶ added in v1.9.0
func (a *CookieAuth) Clear(c *rex.Context)
Clear deletes authentication state for this instance.
func (*CookieAuth) Middleware ¶ added in v1.9.0
func (a *CookieAuth) Middleware() rex.Middleware
Middleware returns the cookie authentication middleware for this instance.
func (*CookieAuth) SetState ¶ added in v1.9.0
func (a *CookieAuth) SetState(c *rex.Context, state any) error
SetState stores authentication state for this instance.
type CookieConfig ¶
type CookieConfig struct {
// Cookie options.
// Default: HttpOnly=true, SameSite=Strict(always), MaxAge=24hrs, Domain=/,secure=false
Options *sessions.Options
// Skip authentication for certain requests
SkipAuth func(c *rex.Context) bool
// Called when authentication fails
ErrorHandler func(c *rex.Context) error
}
CookieConfig defines the behavior of the cookie authentication middleware.