Documentation
¶
Overview ¶
Package security provides HTTP security middleware, authentication, and rate limiting backed by a database.
security.New(
security.WithNonce(),
security.WithHeaders(),
security.WithRateLimit(db, 100, 60),
)
Index ¶
- Variables
- func CheckPassword(hash, password string) bool
- func ClearSessionCookie(w http.ResponseWriter, name string)
- func ClientIP(r *http.Request) string
- func CreateToken(claims map[string]any, secret string, expiry time.Duration) (string, error)
- func HashPassword(password string) (string, error)
- func New(opts ...Option) func(http.Handler) http.Handler
- func NonceFrom(ctx context.Context) string
- func SessionFromRequest(r *http.Request, name string) string
- func SetSessionCookie(w http.ResponseWriter, name, token string, expiry time.Duration)
- func ValidateToken(token, secret string) (map[string]any, error)
- type Option
- type Policy
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidToken = errors.New("invalid token") ErrExpiredToken = errors.New("token expired") )
Functions ¶
func CheckPassword ¶
CheckPassword compares a bcrypt hash with a plaintext password. Returns true if they match.
func ClearSessionCookie ¶
func ClearSessionCookie(w http.ResponseWriter, name string)
ClearSessionCookie removes a session cookie.
func CreateToken ¶
CreateToken creates an HS256 JWT with the given claims, secret, and expiry.
func HashPassword ¶
HashPassword hashes a password with bcrypt.
func New ¶
New creates a security policy middleware from functional options. The returned middleware composes all enabled features into one handler.
func SessionFromRequest ¶
SessionFromRequest extracts a session cookie value from the request. Returns empty string if not found.
func SetSessionCookie ¶
func SetSessionCookie(w http.ResponseWriter, name, token string, expiry time.Duration)
SetSessionCookie sets a secure session cookie with the given token.