net/http-native security framework for Go covering
session management, authentication, authorization, OAuth2/JWT, and CSRF. It
assembles into an http.Handler via a fluent builder and requires only
golang.org/x/crypto beyond the standard library.
Installation
The 5-line integration
- Security headers —
X-Content-Type-Options: nosniff,X-Frame-Options: DENY,Referrer-Policy: no-referrer - CSRF protection — session-backed synchronizer token
- Secure cookies —
Secure; HttpOnly; SameSite=Lax - Default-deny authorization —
NewHandler()errors if your route set has no catch-all
Implementing UserService
The single methodLoadUser loads a user by username. Implement it over your
existing user table:
SessionAuthentication reloads the user on every request, so role changes
and account status take effect immediately without requiring a re-login.
Logging in
The login endpoint (POST /login) expects JSON:
XSRF-TOKEN) is also
set on the first GET through the chain.
Logging out
Send a POST with the CSRF token:Running the playground
The repository includes a self-contained playground that demonstrates the full stack including all four OAuth2 providers:Next steps
- Core Concepts — Authentication, Authority, the security context
- securityhttp — full API reference
- authn — Providers — add OAuth2, JWT, API key, or MFA
