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 to New() 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

Enables session-backed authentication. Required before WithLogin.
  • s — any session.Store implementation (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 principal
  • cookie — an optional session.CookieConfig that overrides the secure default (pass at most one)
Default cookie (session.DefaultCookieConfig()): SESSION=...; Path=/; Secure; HttpOnly; SameSite=Lax.

WithLogin

Mounts a JSON login endpoint at 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.
There is no options parameter — request/response field names, redirects, and failure status are not configurable through the option. 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

Fronts the login endpoint with a per-(username+IP) throttle, so credential stuffing is slowed without locking the account (that’s lockout’s job). Opt-in.

WithCSRF

Overrides the default session-backed synchronizer with a custom repository. The double-submit repository is useful for stateless APIs:

CSRFDisabled

Disables CSRF entirely. Only for bearer-token APIs where session cookies are not used. Combining with WithLogin requires AllowLoginWithoutCSRF.

AllowLoginWithoutCSRF

Suppresses the New() or NewHandler() error for WithLogin + CSRFDisabled. Use only when CSRF is enforced at the edge (gateway, CDN) and you’ve audited it.

WithCORS

Enables CORS. New() or NewHandler() errors if credentials are enabled with a wildcard origin.

WithRateLimit

Adds the global per-IP rate-limit leg (ratelimit.Middleware). cfg.Limiter is required.

WithAuthorize

Takes a compiled 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

Extends the default security headers. HSTS example:

WithObserve

Wires metrics and structured-log hooks through the chain — a denied authorization increments 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

Overrides the authorities granted to the anonymous token injected for unauthenticated callers (default: 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

Both fail on misconfiguration rather than returning a half-wired handler.

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 with New() or NewHandler() includes, at zero configuration cost: