securityhttp is the top-level assembly package. It composes every security
middleware into a single http.Handler through functional options that enforce
fail-loud validation at assembly time.
Import
Options and Assembly
All configuration goes through functional options passed toNew() or NewHandler().
NewHandler assembles the chain, wraps your http.Handler with the full security
chain, and returns an http.Handler ready for http.ListenAndServe.
Option Functions
WithSessionStore
WithLogin.
s— anysession.Storeimplementation (memory, or a distributed store)users— reloaded on every request (SessionAuthentication), so a role change or a disabled/locked account takes effect immediately, never a stale cached principalcookie— an optionalsession.CookieConfigthat overrides the secure default (pass at most one)
session.DefaultCookieConfig()): SESSION=...; Path=/; Secure; HttpOnly; SameSite=Lax.
WithLogin
path (empty string uses "/login"),
authenticated by m — an auth.Manager (the interface *authn.Manager
satisfies). Requires WithSessionStore to be set; New/NewHandler return an
error otherwise. On success it writes the session cookie via
web/auth.LoginHandler.
auth.LoginConfig
(in web/auth) does have UsernameField / PasswordField / OnSuccess
fields, but NewHandler only populates Manager, Store, and Cookie; to
customize those, call auth.LoginHandler directly instead of WithLogin.
WithLoginThrottle
lockout’s job). Opt-in.
WithCSRF
CSRFDisabled
WithLogin requires AllowLoginWithoutCSRF.
AllowLoginWithoutCSRF
New() or NewHandler() error for WithLogin + CSRFDisabled. Use only when CSRF
is enforced at the edge (gateway, CDN) and you’ve audited it.
WithCORS
New() or NewHandler() errors if credentials are enabled with a wildcard
origin.
WithRateLimit
ratelimit.Middleware). cfg.Limiter
is required.
WithAuthorize
authz route set from authz.Routes().
New() or NewHandler() surfaces the default-deny completeness error if the set
has no AnyRequest() and no authz.AllowUnmatched(). The two-value form allows
passing authz.Routes(...) directly — Go accepts a two-value function call as
the argument list to a function taking two parameters, so no intermediate
if err != nil check is needed:
AllowUnmatched is an authz.RoutesOption passed into authz.Routes(...)
as an argument:
WithHeaders
WithObserve
observe.EventAccessDenied, a 429 increments
observe.EventRateLimited, and so on. Only the fixed, low-cardinality Event
enum is ever recorded — never a username, IP, or token.
WithAnonymous
ROLE_ANONYMOUS).
Not (yet) on the Options
Logout and remember-me are real go-security features, but the options
don’t wire them in yet — mount them yourself against the handler returned by
NewHandler():
- Logout:
web/auth.LogoutHandler(web/auth.LogoutConfig{...}) - Remember-me:
web/rememberme.NewTokenBased(web/rememberme.TokenBasedConfig{...})
New vs NewHandler
Middleware ordering
The chain enforces a security-correct fixed order.SessionAuthentication runs
before CSRF so the synchronizer token can be located on the session, and
Login is mounted inside the chain (via pathMux) so CSRF still applies to
the login POST:
Login is mounted inside your handler (at the path you specified), so CSRF
protection applies to the login POST.
Validation at Assembly Time
New() / NewHandler() return an error for:
Always-on defaults
Every chain assembled withNew() or NewHandler() includes, at zero configuration cost:
