refactor(auth): use oauth2.Config + go-oidc Provider for the OIDC flow - #37
Merged
Conversation
Phase B of the auth-code-rationalisation pair. Continues the work of PR #36 (jwt.go -> go-oidc IDTokenVerifier) on the discovery/code-exchange side, replacing 386 lines of hand-rolled HTTP+url.Values plumbing with the canonical golang.org/x/oauth2 and github.com/coreos/go-oidc APIs. What's gone: - OIDCDiscovery struct + discoverOIDC HTTP fetch of the well-known doc. oidc.NewProvider does this and validates the document for us. - discoverOIDCWithRetry's hand-rolled retry. We keep the same retry policy (5 attempts, exponential backoff capped at 4 s) but wrap oidc.NewProvider instead of the raw HTTP call. - Dual-discovery dance for split-horizon issuers (fetch public, fall back to URL substitution). The new code uses oidc.InsecureIssuerURLContext to tell go-oidc which issuer to expect in tokens, and just substitutes the public base on the AuthorizationEndpoint we got from discovery. Single code path. - OIDCTokenResponse struct + exchangeCode's manual POST form-encode. oauth2.Config.Exchange does it, and oauth2.Token.Extra("id_token") pulls the OIDC-specific extra. - Login's hand-built query-string. oauth2.Config.AuthCodeURL(state) returns the full URL. Public surface preserved: - NewOIDCHandler(*config.Config) (*OIDCHandler, error) — same signature, same retry budget at boot, same "fatal if discovery never recovers" semantics (cmd/api/main.go logs Fatal then exits, k8s restarts). - (*OIDCHandler).Login / Callback / Logout fiber handlers — same shape, same returnUrl-via-hash-fragment redirect, same open-redirect guard on the returnUrl. State management unchanged (in-memory map + 5-min cleanup goroutine). Signed-cookie state would survive pod restarts but is a separate behavioural change; out of scope here. Stats: - oidc.go: 386 -> 252 lines (-134, -35%) - Together with PR #36: 655 -> 337 (-318, -48% of pre-refactor auth code) - golang.org/x/oauth2 promoted from indirect to direct (was already in go.sum thanks to other transitive deps).
AYDEV-FR
force-pushed
the
refactor/oidc-oauth2
branch
from
June 4, 2026 19:02
1ac39d6 to
99168b7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase B of the auth-code rationalisation, following PR #36 (
jwt.go→go-oidc.IDTokenVerifier). Replaces 386 lines of hand-rolled HTTP +url.Valuesplumbing with the canonicalgolang.org/x/oauth2andgithub.com/coreos/go-oidcAPIs.What was hand-rolled and is gone
OIDCDiscoverystruct + manual fetch of.well-known/openid-configurationoidc.NewProvider(ctx, issuer)discoverOIDCWithRetry(5-attempt backoff) — wrapping the raw HTTP calloidc.NewProvideroidc.InsecureIssuerURLContextto tell go-oidc which issuer to expect; single code path that just rebasesAuthorizationEndpointonto the public baseOIDCTokenResponsestruct +exchangeCodePOST form-encodeoauth2.Config.Exchange+Token.Extra("id_token")Loginhand-built query stringoauth2.Config.AuthCodeURL(state)Public surface preserved
NewOIDCHandler(*config.Config) (*OIDCHandler, error)— same signature, same retry budget at boot, same fatal-if-never-recovered semantics.(*OIDCHandler).Login / Callback / LogoutFiber handlers — same shape, same hash-fragment redirect with the id_token, same open-redirect guard onreturnUrl.No caller in
cmd/api/main.gochanges.State management
In-memory map + 5-min cleanup goroutine is unchanged. Signed-cookie state would survive pod restarts but is a separate behavioural change — out of scope here.
Stats
oidc.gogolang.org/x/oauth2Build green, existing test suite passes (
internal/auth,internal/handlers).