Authenticator & OIDC
go-bananas has exactly one authentication seam — the Authenticator interface —
and no dependency on any identity provider. You implement it; the framework gates
routes with it.
type Authenticator interface {
// Authenticate returns the principal, or (nil, nil) for an anonymous request.
// A non-nil error results in a 500.
Authenticate(r *http.Request) (principal any, err error)
}
Gate routes with RequireAuthenticated:
me := r.NewRoute().Subrouter()
me.Use(middleware.RequireAuthenticated(myAuth, h))
me.HandleFunc("/me", profile).Methods(http.MethodGet)
On success the principal is stored on the context; retrieve it anywhere with
webctx.PrincipalFromContext(ctx). An anonymous request gets a 401; an error
gets a 500.
A session-backed authenticator
The simplest implementation reads a value an earlier login flow stored on the session:
type SessionAuthenticator struct{}
func (SessionAuthenticator) Authenticate(r *http.Request) (any, error) {
s := webctx.SessionFromContext(r.Context())
if s == nil {
return nil, nil
}
u, _ := s.Values["user"].(*User)
return u, nil // nil when anonymous
}
Wiring OIDC
OIDC is a wiring exercise on top of the framework, not a framework dependency. The plumbing it needs — sessions, CSRF, secure headers, idle expiry — already ships here; the OIDC client itself lives in your application (or, as in the example, a separate module). A typical flow:
GET /login— generatestateandnonce, store them on the session, and redirect to the provider’s authorization endpoint (oauth2.Config.AuthCodeURL).GET /auth/callback— readstate,nonce(and any PKCE verifier) into locals and delete them from the session first, before writing anything. Then comparestatein constant time (subtle.ConstantTimeCompare), handle an OAutherrorparameter without calling the token endpoint, exchange the code, verify the ID token andnonce, and extract claims.- Reset the session at login — assign a fresh
session.Valuesmap before storing your user. This defends against session fixation, rotates the CSRF token, and restarts theCheckSessionMaxAgeclock. Authenticatereads that user back, as above.POST /logout— log out with a POST form that carries{{ .csrfField }}, routed with.Methods(http.MethodPost)behindHandleCSRF. AGET /logoutlets any site sign your users out.
Things to get right:
- Session writes after the first byte are lost.
RequireSessionsaves the session when the response starts, so deleting flow state in adefer(which runs after the response is written) never reaches the cookie, and a failed callback could be retried with the same state. - Only trust verified emails. If you authorize by email address, require the
email_verifiedclaim to be the JSON booleantrue. Better still, key grants on the subject (sub).
The runnable
examples/ssr-oidc
application implements this end to end with
coreos/go-oidc and
golang.org/x/oauth2 — and keeps those
dependencies out of the core module by being a separate module.