Skip to content

refactor(auth): use oauth2.Config + go-oidc Provider for the OIDC flow - #37

Merged
AYDEV-FR merged 1 commit into
mainfrom
refactor/oidc-oauth2
Jun 4, 2026
Merged

refactor(auth): use oauth2.Config + go-oidc Provider for the OIDC flow#37
AYDEV-FR merged 1 commit into
mainfrom
refactor/oidc-oauth2

Conversation

@AYDEV-FR

@AYDEV-FR AYDEV-FR commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Phase B of the auth-code rationalisation, following PR #36 (jwt.gogo-oidc.IDTokenVerifier). Replaces 386 lines of hand-rolled HTTP + url.Values plumbing with the canonical golang.org/x/oauth2 and github.com/coreos/go-oidc APIs.

What was hand-rolled and is gone

Hand-rolled Replaced by
OIDCDiscovery struct + manual fetch of .well-known/openid-configuration oidc.NewProvider(ctx, issuer)
discoverOIDCWithRetry (5-attempt backoff) — wrapping the raw HTTP call Same retry shape, wrapping oidc.NewProvider
Dual-discovery dance for split-horizon issuers (fetch public, fall back to URL substitution) oidc.InsecureIssuerURLContext to tell go-oidc which issuer to expect; single code path that just rebases AuthorizationEndpoint onto the public base
OIDCTokenResponse struct + exchangeCode POST form-encode oauth2.Config.Exchange + Token.Extra("id_token")
Login hand-built query string oauth2.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 / Logout Fiber handlers — same shape, same hash-fragment redirect with the id_token, same open-redirect guard on returnUrl.

No caller in cmd/api/main.go changes.

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

Before After Δ
oidc.go 386 252 -134 (-35%)
Net diff +133 / -267
Combined with PR #36 655 337 -318 (-48% of all auth code)
golang.org/x/oauth2 indirect direct (already in go.sum via transitive deps)

Build green, existing test suite passes (internal/auth, internal/handlers).

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
AYDEV-FR force-pushed the refactor/oidc-oauth2 branch from 1ac39d6 to 99168b7 Compare June 4, 2026 19:02
@AYDEV-FR
AYDEV-FR merged commit 11f5a11 into main Jun 4, 2026
6 of 7 checks passed
@AYDEV-FR
AYDEV-FR deleted the refactor/oidc-oauth2 branch June 4, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant