go-security is a 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

Verify the dependency graph stays clean:

The 5-line integration

What you get for free:
  • Security headersX-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: no-referrer
  • CSRF protection — session-backed synchronizer token
  • Secure cookiesSecure; HttpOnly; SameSite=Lax
  • Default-deny authorizationNewHandler() errors if your route set has no catch-all

Implementing UserService

The single method LoadUser 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:
Success response:
The session cookie is set automatically. The CSRF cookie (XSRF-TOKEN) is also set on the first GET through the chain.

Logging out

Send a POST with the CSRF token:
The session is invalidated and the cookie cleared.

Running the playground

The repository includes a self-contained playground that demonstrates the full stack including all four OAuth2 providers:
Open http://localhost:8080.

Next steps