Labs › security

Build Your Own OAuth 2.0 + OIDC Provider

Build an OAuth 2.0 + OIDC provider from authorization code flow to PKCE to full OIDC discovery — then compare with Spring Authorization Server's production-grade implementation.

go 1.23 advanced ~12h build · ~4h read ~900 LoC (v3)
v0: Authorization Code Flowv1: PKCE + Userinfov2: OIDC Discovery + ID Tokens
☕ Java/Spring integration included co primary

Why this lab exists

OAuth 2.0 is one of the most widely deployed protocols on the internet and one of the most frequently misunderstood. Developers implement clients every day — adding “Login with Google” or generating API tokens — but almost no one has built a provider. The result is a generation of engineers who know how to consume OAuth but cannot reason about what the server is actually enforcing and why.

The confusion shows up in production. Authorization codes replayed because single-use enforcement was skipped. Public clients without PKCE, leaving their flows vulnerable to code interception. OIDC implementations that issue ID tokens but never explain why they exist alongside access tokens. Services that “validate JWTs” by base64-decoding the payload but skipping signature verification.

Building a provider from scratch solves this. When you implement single-use code deletion, you understand why replay attacks work against naive providers. When you implement PKCE yourself — hash the verifier, compare against the stored challenge, reject mismatches — you internalize why PKCE matters for SPAs and mobile apps rather than memorizing a spec paragraph. When you add the OIDC discovery document, the difference between “can app X access resource Y” (OAuth) and “who is user Z” (OIDC) stops being abstract.

This lab follows the same progression that the OAuth 2.0 ecosystem went through historically: authorization code flow first (v0), PKCE to fix public client security (v1), then OIDC to add identity on top of authorization (v2). The Java co-primary implementation uses Spring Authorization Server — the production-grade equivalent — to show how the same concepts map to a hardened real-world library.

What we’re building

An OAuth 2.0 + OIDC provider in Go across three stages, with a Java co-primary implementation using Spring Authorization Server.

We build:

  • Authorization code flow (v0): client registration, redirect URI validation, single-use codes with 10-minute expiry, HS256 JWT access tokens
  • PKCE for public clients (v1): code_challenge stored at /authorize, code_verifier SHA-256 verified at /token, /userinfo endpoint protected by Bearer token
  • OIDC layer (v2): /.well-known/openid-configuration discovery document, /.well-known/jwks.json with RSA public key, RS256 ID token with sub, iss, aud, exp, iat, nonce, email claims
  • HTTP server with simulated login page (cmd/server/main.go): GET /authorize, POST /token, GET /userinfo, GET /.well-known/openid-configuration, GET /.well-known/jwks.json

We explicitly skip (and why):

  • Refresh token rotation: requires persistent storage and token family tracking — see What the Toy Misses
  • Dynamic client registration (RFC 7591): client credential storage and approval workflows significantly increase scope
  • Consent screen: real scope approval UI is important but orthogonal to understanding the protocol mechanics
  • Token revocation (RFC 7009): requires a blocklist store; the single-instance in-memory implementation would create a false sense of security

Mental model

OAuth 2.0 answers one question: can application X access resource Y on behalf of user Z? OIDC answers a different question layered on top: who is user Z? Understanding why these are separate questions is the mental model for this entire lab.

The authorization code flow, step by step

Browser                  Client App               OAuth Provider
   │                         │                         │
   │  1. User clicks "Login" │                         │
   │─────────────────────────▶                         │
   │                         │  2. GET /authorize      │
   │                         │     client_id=X         │
   │                         │     redirect_uri=...    │
   │                         │     scope=openid email  │
   │                         │     state=csrf_token    │
   │                         │─────────────────────────▶
   │                         │                         │
   │  3. Redirect to login   │                         │
   │◀────────────────────────────────────────────────── │
   │                         │                         │
   │  4. User enters creds   │                         │
   │────────────────────────────────────────────────────▶
   │                         │                         │
   │  5. Redirect: ?code=ABC&state=csrf_token           │
   │◀────────────────────────────────────────────────── │
   │                         │                         │
   │  6. Deliver code to     │                         │
   │     client via URL      │                         │
   │─────────────────────────▶                         │
   │                         │  7. POST /token         │
   │                         │     code=ABC            │
   │                         │     client_secret=...   │
   │                         │─────────────────────────▶
   │                         │                         │
   │                         │  8. {access_token, ...} │
   │                         │◀─────────────────────── │
   │                         │                         │
   │                         │  9. GET /api/resource   │
   │                         │  Authorization: Bearer  │
   │                         │─────────────────────────▶ (resource server)

Why the code exists at all: the access token cannot travel through the browser. Browser history, Referer headers, and server logs all capture URL parameters. If the access token appeared in step 5 as a URL parameter (the legacy implicit flow), it would be logged everywhere. The authorization code is a short-lived, single-use intermediary. It can safely travel through the browser because it is useless without the client secret (step 7) — and the client secret is never in the browser.

Why the state parameter matters: step 5 delivers the code via URL redirect to the client app. Without the state check, an attacker can craft a redirect URL with their own authorization code and trick the victim’s browser into delivering it to the client — the client would then exchange the attacker’s code and bind the attacker’s account to the victim’s session. The state parameter is a CSRF token for the OAuth flow itself.

What PKCE adds for public clients

A SPA running in a browser cannot keep a client secret. The secret would be in the JavaScript source — visible to anyone who opens DevTools. Without a client secret, step 7 is unauthenticated: anyone who intercepts the code (via a malicious redirect URI registered with a different app, or browser history theft) can exchange it.

PKCE (RFC 7636) solves this with a cryptographic proof:

Before /authorize:
  Generate random code_verifier  (43-128 chars, high entropy)
  code_challenge = BASE64URL(SHA256(code_verifier))

At /authorize:
  Send code_challenge, code_challenge_method=S256
  Provider stores code_challenge with the authorization code

At /token:
  Send code_verifier
  Provider recomputes BASE64URL(SHA256(code_verifier))
  Compares against stored code_challenge
  Match → legitimate client. Mismatch → reject.

An attacker who intercepts the code at step 5 does not have the code_verifier — it was never transmitted. They cannot compute the verifier from the challenge because SHA-256 is a one-way function. The /token exchange fails with a PKCE mismatch error. Attack blocked.

ID token vs access token

After the OIDC layer is added, /token returns two JWTs:

{
  "access_token": "eyJhbGciOiJIUzI1NiJ9...",  // HS256, for resource servers
  "id_token":     "eyJhbGciOiJSUzI1NiJ9...",  // RS256, for the client app only
  "token_type":   "Bearer",
  "expires_in":   3600
}

The access token proves authorization: “this user authorized scope X.” Resource servers verify it to decide whether to serve a request. They do not care who the user is — only that the scope is correct.

The ID token proves identity: “the authenticated user is alice@example.com with sub=user-1.” The client app uses it to display the user’s name, pre-fill their profile, or look them up in its own database. Resource servers should never receive the ID token — it is for the client app that initiated the login.

Both are JWTs. Access tokens use HS256 (fast, symmetric — only the issuer needs to verify them internally). ID tokens use RS256 (asymmetric — any service can verify them using the public key from /.well-known/jwks.json without sharing a secret).

Prerequisites

  • Go 1.23+
  • Java 17+ and Maven 3.9+ (for the Java co-primary)
  • Familiarity with HTTP request/response — you should be able to read curl commands without explanation
  • JWT structure: if you have not done the JWT Library lab, read RFC 7519 §3 first (five minutes)
  • RSA public-key basics: know that sign uses the private key, verify uses the public key

Stage-by-stage build

Stage 0 Authorization Code Flow ~200 LoC ›
  • Implement Client{id, secret, redirectURIs, public} and AuthorizationCode{code, clientID, userID, scope, redirectURI, expiresAt}
  • Implement Authorize(params) → redirect URL with code and state
  • Implement Exchange(params) → TokenResponse with HS256 access token
  • Enforce single-use: delete code on first Exchange regardless of success or failure
  • Enforce redirect_uri exact match at both /authorize and /token
  • Test: double-use of a code fails; unregistered redirect_uri rejected; wrong client_id rejected

The first thing to get right is the exact-match redirect URI validation. This is not a pedantic spec requirement — it is the primary defense against authorization code injection. If the provider matches by prefix or ignores the redirect_uri at /token, an attacker can register a client with a URI that starts with the same prefix as the legitimate client and redirect authorization codes to themselves.

// hasRedirectURI checks EXACTLY — never by prefix
func (c *Client) hasRedirectURI(redirectURI string) bool {
    for _, u := range c.RedirectURIs {
        if u == redirectURI {
            return true
        }
    }
    return false
}

The single-use enforcement is a delete-on-first-access pattern. The code is removed from the map before any validation occurs. If the client_id doesn’t match or the secret is wrong, the code is already gone. This prevents replay attacks where an attacker observes a failed exchange attempt and then retries with the correct parameters.

// Lock, look up, and delete atomically before any validation
p.mu.Lock()
authCode, ok := p.codes[params.Code]
if ok {
    delete(p.codes, params.Code)
}
p.mu.Unlock()

if !ok {
    return nil, errors.New("oauth: invalid or already-used authorization_code")
}

The HS256 access token uses the same signing approach as the JWT Library lab: base64url(header) + ”.” + base64url(payload) as the signing input, HMAC-SHA256 as the signature. Verification uses hmac.Equal for constant-time comparison — the same reason as in the JWT Library lab. Claims: sub, iss, aud, iat, exp, scope.

The state parameter is echoed back in the redirect URL unchanged. The client app compares the returned state against the value it sent. If they differ, the client must abort — something tampered with the flow. This is the OAuth-specific CSRF defense; it is the client’s responsibility to enforce it, not the provider’s.

Stage 1 PKCE + Userinfo ~380 LoC ›
  • Add CodeChallenge and CodeChallengeMethod fields to AuthorizationCode
  • At /authorize: validate code_challenge_method=S256; store code_challenge with the code
  • At /token: if code has a challenge, require code_verifier; compute BASE64URL(SHA256(code_verifier)); reject mismatch
  • Add Public bool to Client; reject public clients that omit code_challenge at /authorize
  • Implement GET /userinfo: extract Bearer token, verify HMAC-SHA256, return UserinfoResponse{sub, email, name}
  • Test: wrong verifier rejected with PKCE error; public client without PKCE rejected at /authorize

The PKCE verifier check is four lines that prevent a category of attack that the original OAuth 2.0 spec did not anticipate:

if authCode.CodeChallenge != "" {
    if params.CodeVerifier == "" {
        return nil, errors.New("oauth: code_verifier required (PKCE was used)")
    }
    digest := sha256.Sum256([]byte(params.CodeVerifier))
    computed := b64url(digest[:])
    if computed != authCode.CodeChallenge {
        return nil, errors.New("oauth: code_verifier does not match code_challenge")
    }
}

The b64url function uses base64.RawURLEncoding — base64 with URL-safe alphabet (- and _ instead of + and /) and no padding characters. This is the encoding specified by RFC 7636 Appendix A. The difference between RawURLEncoding and StdEncoding is why PKCE implementations sometimes produce verification failures: + vs - and == padding vs no padding.

Why code_challenge_method=plain is rejected: plain PKCE sends the verifier itself as the challenge. An attacker who sees the challenge (it’s in the /authorize URL, which may appear in browser history or Referer headers) already has the verifier. S256 defeats this — SHA-256 is irreversible, so the challenge reveals nothing about the verifier.

The /userinfo endpoint is straightforward: extract the Bearer token from the Authorization header, verify it with VerifyAccessToken (same HMAC check as Exchange), look up the user by the sub claim, return the profile claims. The important design decision is that /userinfo shares the same token verification path as resource servers would. There is no separate “userinfo token” — the same access token serves all purposes.

Public clients (Public: true) must use PKCE at /authorize. This is enforced by checking client.Public before generating the code:

if client.Public {
    if params.CodeChallenge == "" {
        return "", errors.New("oauth: PKCE required for public clients")
    }
    if params.CodeChallengeMethod != "S256" {
        return "", errors.New("oauth: only code_challenge_method=S256 is supported")
    }
}

Public clients omit the client_secret at /token (the field is simply ignored if client.Public is true). PKCE is their authentication mechanism for the token exchange.

Stage 2 OIDC Discovery + ID Tokens ~600 LoC ›
  • Implement DiscoveryDocument() → OIDCDiscovery struct for /.well-known/openid-configuration
  • Implement JWKS() → JWKSet with RSA public key (kty, use, kid, alg, n, e as base64url)
  • Implement issueIDToken(userID, clientID, nonce) → RS256 JWT with sub, iss, aud, exp, iat, nonce, email
  • In Exchange: if 'openid' is in scope, call issueIDToken and include id_token in TokenResponse
  • Test: JWKS kid matches token header kid; nonce echoed in ID token; VerifyIDToken accepts valid, rejects tampered

The OIDC discovery document is the configuration that makes OAuth providers composable. Instead of hardcoding endpoint URLs, clients fetch /.well-known/openid-configuration at startup and cache it. Spring Security’s OAuth2 client does this automatically when you configure issuer-uri. The document is JSON with required fields defined by OIDC Discovery 1.0 §3:

type OIDCDiscovery struct {
    Issuer                           string   `json:"issuer"`
    AuthorizationEndpoint            string   `json:"authorization_endpoint"`
    TokenEndpoint                    string   `json:"token_endpoint"`
    UserinfoEndpoint                 string   `json:"userinfo_endpoint"`
    JWKsURI                          string   `json:"jwks_uri"`
    ResponseTypesSupported           []string `json:"response_types_supported"`
    IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported"`
    ScopesSupported                  []string `json:"scopes_supported"`
    CodeChallengeMethodsSupported    []string `json:"code_challenge_methods_supported"`
    // ... additional optional fields
}

The JWKS endpoint distributes the RSA public key so any service can verify ID token signatures without a shared secret. The key is represented as a JSON Web Key (RFC 7517) with the modulus n and exponent e as base64url-encoded big-endian bytes:

func (p *Provider) JWKS() *JWKSet {
    pub := &p.rsaKey.PublicKey
    nBytes := pub.N.Bytes()
    eBig := big.NewInt(int64(pub.E))
    eBytes := eBig.Bytes()

    return &JWKSet{Keys: []JWK{{
        Kty: "RSA",
        Use: "sig",
        Kid: p.rsaKeyID,  // matches the "kid" in ID token headers
        Alg: "RS256",
        N:   b64url(nBytes),
        E:   b64url(eBytes), // always "AQAB" for e=65537
    }}}
}

e = "AQAB" decodes to [0x01, 0x00, 0x01] = 65537. This is the RSA public exponent used by virtually every RSA key generated in the last 20 years. It appears in every JWKS endpoint for every OAuth 2.0 provider in the world.

The ID token is a standard RS256 JWT. The critical difference from access tokens: the kid field in the header links the token to the key in the JWKS. When a service receives an ID token and wants to verify it, the lookup is:

  1. Parse the token header, extract kid.
  2. Fetch /.well-known/jwks.json, find the key with matching kid.
  3. Reconstruct the RSA public key from n and e.
  4. Verify the RS256 signature.

The nonce claim is the OIDC replay prevention mechanism for the client app. The client generates a random nonce at /authorize, stores it in session storage, and after receiving the ID token, verifies that the nonce in the token matches what it sent. An attacker who replays a captured ID token cannot satisfy the nonce check.

Run it yourself

Prereqs: Go 1.23+
  1. 1
    Clone and run tests
    git clone https://github.com/pushkarkumar-dev/system-design-labs
    cd system-design-labs/labs/oauth-provider
    go test ./pkg/oauth/ -v
  2. 2
    Start the OAuth provider server
    go run ./cmd/server/
  3. 3
    Fetch the OIDC discovery document
    curl -s http://localhost:9000/.well-known/openid-configuration | jq .
  4. 4
    Fetch the JWKS endpoint
    curl -s http://localhost:9000/.well-known/jwks.json | jq .
  5. 5
    Simulate the authorization code flow (confidential client)
    # Step 1: Set a session cookie (simulating login) and get the code
    curl -s -c /tmp/oauth-cookies.txt -b /tmp/oauth-cookies.txt \
      -X POST http://localhost:9000/login \
      -d 'username=alice&password=password&authorize_query=client_id%3Ddemo-client%26redirect_uri%3Dhttp%3A%2F%2Flocalhost%3A8080%2Fcallback%26scope%3Dopenid%2Bemail%26response_type%3Dcode%26state%3Dabc123' \
      -D - | grep location
  6. 6
    Exchange the code for tokens (replace CODE with value from previous step)
    curl -s -X POST http://localhost:9000/token \
      -d 'grant_type=authorization_code&code=CODE&client_id=demo-client&client_secret=demo-secret&redirect_uri=http://localhost:8080/callback' | jq .
  7. 7
    Call /userinfo with the access token
    curl -s http://localhost:9000/userinfo \
      -H 'Authorization: Bearer ACCESS_TOKEN' | jq .
Expected output
--- go test ./pkg/oauth/ -v ---
=== RUN   TestAuthorizationCodeIssuedAndConsumedOnce
  oauth_test.go:101: second Exchange correctly rejected: oauth: invalid or already-used authorization_code
--- PASS: TestAuthorizationCodeIssuedAndConsumedOnce (0.12s)
=== RUN   TestExpiredCodeRejected
--- PASS: TestExpiredCodeRejected (0.12s)
=== RUN   TestPKCEVerifierMismatchRejected
  oauth_test.go:191: wrong verifier correctly rejected: oauth: code_verifier does not match code_challenge (PKCE verification failed)
--- PASS: TestPKCEVerifierMismatchRejected (0.12s)
=== RUN   TestPKCEHappyPath
  oauth_test.go:234: PKCE flow succeeded: access_token length=175
--- PASS: TestPKCEHappyPath (0.13s)
=== RUN   TestUserinfoRequiresValidBearer
  oauth_test.go:285: Userinfo response: sub=user-1 email=alice@example.com name=Alice
--- PASS: TestUserinfoRequiresValidBearer (0.12s)
=== RUN   TestJWKSReturnsCorrectRSAPublicKey
  oauth_test.go:362: ID token verified successfully: sub=user-1 iss=http://localhost:9000
--- PASS: TestJWKSReturnsCorrectRSAPublicKey (0.25s)
=== RUN   TestOIDCDiscoveryDocument
--- PASS: TestOIDCDiscoveryDocument (0.12s)
=== RUN   TestWrongRedirectURIRejected
--- PASS: TestWrongRedirectURIRejected (0.12s)
=== RUN   TestPublicClientRequiresPKCE
--- PASS: TestPublicClientRequiresPKCE (0.12s)
PASS

--- GET /.well-known/jwks.json ---
{
"keys": [{
  "kty": "RSA", "use": "sig", "kid": "key-a1b2c3d4",
  "alg": "RS256", "n": "0vx7agoebGcQ...", "e": "AQAB"
}]
}

--- POST /token response ---
{
"access_token": "eyJhbGciOiJIUzI1NiJ9...",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "openid email",
"id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleS1hMWIyYzNkNCJ9..."
}

--- GET /userinfo ---
{ "sub": "user-1", "email": "alice@example.com", "name": "Alice" }

Benchmarks

Benchmark results on M2 MacBook Pro, 16GB RAM
Metric Result Notes
/authorize redirect latency 0.5 ms Client lookup + 32-byte crypto/rand + URL construction. rand is the bottleneck.
/token (HS256 access token, PKCE) 2 ms Code lookup + SHA-256 PKCE verify + HS256 JWT sign + JSON marshal.
/token (RS256 ID token, openid scope) 3.5 ms All of HS256 + RSA-2048 modular exponentiation (~2ms). ID token is the expensive part.
/userinfo (valid Bearer token) 0.8 ms HMAC-SHA256 verify + map lookup. No I/O, pure CPU.
/.well-known/jwks.json 0.1 ms Pre-computed static JSON from memory. Cache-Control: max-age=86400 in production.
Authorization code reuse rate 0 % (always rejected) Codes are deleted on first use regardless of success or failure.

At 1,000 logins per second, HS256 token issuance consumes roughly 2 CPU-seconds per wall-clock second — still comfortable on a single core. The RS256 ID token path at 1,000 logins/sec would consume 3.5 CPU-seconds, approaching single-core saturation. Production OAuth providers horizontally scale the token endpoint and pre-generate RSA key material at startup to avoid key generation latency on hot paths.

The 0.1ms for JWKS is important: it should be cached for 24 hours (Cache-Control: max-age=86400) in production. Clients fetching it on every token verification would add unnecessary load for a document that changes only during key rotation.

Stress tests

The test suite covers the critical security invariants. For stress testing and security verification:

# Run all tests with race detector
cd labs/oauth-provider
go test ./pkg/oauth/ -race -v

# Concurrent token issuance — 100 goroutines racing on the codes map
go test ./pkg/oauth/ -run TestConcurrentExchange -race

# Verify code replay attack is blocked at load
# The codes map is protected by sync.Mutex — test confirms no double-exchange
go test ./pkg/oauth/ -run TestAuthorizationCodeIssuedAndConsumedOnce -count=100

For end-to-end load testing against the running server:

# Start the server
go run ./cmd/server/ &

# Load test /token with hey (install: go install github.com/rakyll/hey@latest)
# Pre-issue 1000 codes, then exchange them concurrently
hey -n 1000 -c 50 -m POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code&code=PREGEN_CODE&client_id=demo-client&client_secret=demo-secret&redirect_uri=http://localhost:8080/callback" \
  http://localhost:9000/token

Two specific failure modes worth testing manually:

Code replay attack: issue a code, exchange it successfully, then immediately replay the same code. The second attempt must return invalid or already-used authorization_code. This is enforced by the delete-before-validate pattern in Exchange() — the code is gone from the map before any other check runs.

PKCE verifier brute force: submit the correct code with a wrong code_verifier. The provider computes SHA256(wrong_verifier) and compares against the stored challenge. The comparison is string equality, not constant-time, so there is no timing leak to exploit. Even if there were, SHA-256 output is 32 bytes — an attacker cannot narrow down the verifier from timing information on a 256-bit value.

What the toy misses

What the toy misses Honest gaps vs. production
  • Refresh token rotation Refresh tokens must be rotated on each use (RFC 6819 §4.3.6). A stolen refresh token used before the legitimate client causes a family of tokens to be revoked. Tracking token families requires persistent storage with atomic compare-and-swap semantics — not achievable in our in-memory map.
  • Token introspection (RFC 7662) Resource servers need to validate opaque tokens without parsing JWTs. The introspection endpoint requires secure server-to-server authentication (typically a client credential grant) and a token store lookup on every API request.
  • Dynamic client registration (RFC 7591) Production providers let clients register programmatically — essential for developer portals and automated infrastructure. Requires client credential storage, redirect URI validation against a safelist, and scope approval workflows that a human reviews.
  • Consent screen and scope approval Real OAuth flows show users exactly what permissions they are granting: 'This app wants to read your email and contacts.' Storing consent records (user X approved scopes A,B for client Y on date Z) and re-using approvals across sessions requires a persistent consent store and a UI that does not exist in this lab.
  • Clock skew tolerance in JWT validation The exp check (now greater than exp → reject) is naive. Distributed systems introduce clock skew between issuers and verifiers. Real implementations allow 30-60 seconds of leeway and must handle the nbf (not-before) claim correctly. Our verifier will reject tokens from a server whose clock is 30 seconds ahead of the verifier's clock.
  • Token revocation (RFC 7009) Logout requires server-side token invalidation. For stateless JWTs this means a blocklist (Redis SET with TTL equal to the token's remaining lifetime). Coordinating revocation across multiple provider instances requires a shared store or an event stream — neither of which exists in our single-process in-memory implementation.

Wiring it into a Java/Spring service

☕

Wiring it into a Java/Spring service

co primary spring-authorization-server
Maven dependencies
org.springframework.security:spring-security-oauth2-authorization-server:1.3.1
org.springframework.boot:spring-boot-starter-security:3.3.0
com.nimbusds:nimbus-jose-jwt:9.40

Go reference — what we built

The Go implementation is a from-scratch OAuth 2.0 + OIDC provider in ~900 lines. No external dependencies beyond the standard library. Every operation is explicit: single-use code enforcement with a delete-before-validate pattern, PKCE SHA-256 verify, HS256 and RS256 JWT construction, JWKS endpoint with RSA public key serialization, and OIDC discovery document generation.

The key design decisions: redirect URIs are exact-match only. Codes are deleted before validation. Public clients require PKCE. The HMAC secret and RSA key are set at construction time and never change. The provider is a single struct with a mutex — simple to reason about, not production-scale.

Java co-primary — Spring Authorization Server

Spring Authorization Server (spring-authorization-server) is the Spring ecosystem’s production-grade OAuth 2.0 + OIDC provider. It is the successor to the deprecated Spring Security OAuth2 project and was built from the ground up for Spring Boot 3.x and Jakarta EE. Every concept from our Go implementation has a direct counterpart.

Registered clients: RegisteredClientRepository

Our Go Client struct becomes Spring’s RegisteredClient. The repository holds all registered clients — equivalent to our Provider.clients map, but typically backed by a database:

@Bean
public RegisteredClientRepository registeredClientRepository() {
    // Confidential client — equivalent to Go Client{Public: false}
    RegisteredClient confidentialClient = RegisteredClient
        .withId(UUID.randomUUID().toString())
        .clientId("demo-client")
        .clientSecret("{noop}demo-secret")          // {noop} = plaintext for demo
        .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
        .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
        .redirectUri("http://localhost:8080/callback")
        .scope(OidcScopes.OPENID)
        .scope(OidcScopes.EMAIL)
        .clientSettings(ClientSettings.builder()
            .requireProofKey(false)    // PKCE optional for confidential clients
            .requireAuthorizationConsent(true)
            .build())
        .build();

    // Public client (SPA) — equivalent to Go Client{Public: true}
    RegisteredClient publicClient = RegisteredClient
        .withId(UUID.randomUUID().toString())
        .clientId("demo-spa")
        .clientAuthenticationMethod(ClientAuthenticationMethod.NONE) // no secret
        .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
        .redirectUri("http://localhost:3000/callback")
        .scope(OidcScopes.OPENID)
        .clientSettings(ClientSettings.builder()
            .requireProofKey(true)     // PKCE required — same as Go Public:true
            .build())
        .build();

    return new InMemoryRegisteredClientRepository(confidentialClient, publicClient);
}

requireProofKey(true) is the exact equivalent of our if client.Public { if params.CodeChallenge == "" { return error } } check. Spring enforces it in PkceTokenGrantAuthenticationProvider — the same three-step logic: require challenge at authorize, store challenge with code, verify verifier at token exchange.

Security configuration: applyDefaultSecurity

The most important Spring Authorization Server method is OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(). It wires all the OAuth 2.0 and OIDC endpoints in a single call:

@Configuration
@EnableWebSecurity
public class AuthorizationServerConfig {

    @Bean
    @Order(1)
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
            throws Exception {
        // applyDefaultSecurity registers:
        //   GET  /authorize                        — authorization endpoint
        //   POST /token                            — token endpoint
        //   GET  /userinfo                         — UserInfo endpoint (OIDC)
        //   GET  /.well-known/openid-configuration — OIDC discovery
        //   GET  /.well-known/jwks.json            — JWKS endpoint
        //   POST /oauth2/revoke                    — token revocation (RFC 7009)
        //   POST /oauth2/introspect                — token introspection (RFC 7662)
        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);

        http
            .getConfigurer(OidcConfigurer.class)
            .enable();  // enable OIDC UserInfo and discovery endpoints

        http
            .exceptionHandling(e -> e
                .defaultAuthenticationEntryPointFor(
                    new LoginUrlAuthenticationEntryPoint("/login"),
                    new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
                )
            )
            .oauth2ResourceServer(r -> r.jwt(Customizer.withDefaults()));

        return http.build();
    }

    @Bean
    @Order(2)
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
            throws Exception {
        http
            .authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
            .formLogin(Customizer.withDefaults()); // the login page our Go demo has manually
        return http.build();
    }
}

The difference from our Go implementation: Spring handles PKCE enforcement, single-use code deletion, redirect URI validation, and state parameter handling automatically. Our Go code made all of these explicit — Spring encapsulates them behind applyDefaultSecurity.

PKCE in Spring: PkceTokenGrantAuthenticationProvider

Spring’s PKCE implementation lives in PkceTokenGrantAuthenticationProvider. When you call requireProofKey(true) on a client, this provider is activated automatically. Its logic mirrors our Go implementation exactly:

// Spring's internal PkceTokenGrantAuthenticationProvider (simplified for clarity)
// This runs at POST /token when the client has requireProofKey=true

private void validateCodeVerifier(
        OAuth2Authorization authorization,
        OAuth2AuthorizationCodeAuthenticationToken authenticationToken) {

    // Retrieve the stored code_challenge from the authorization
    String codeChallenge = authorization.getAttribute(
        PkceParameterNames.CODE_CHALLENGE);
    String codeChallengeMethod = authorization.getAttribute(
        PkceParameterNames.CODE_CHALLENGE_METHOD);

    // Get the code_verifier from the /token request
    String codeVerifier = authenticationToken.getAdditionalParameters()
        .get(PkceParameterNames.CODE_VERIFIER).toString();

    // Compute BASE64URL(SHA256(code_verifier)) — same as our Go code
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] digest = md.digest(codeVerifier.getBytes(StandardCharsets.US_ASCII));
    String computed = Base64.getUrlEncoder().withoutPadding()
        .encodeToString(digest);

    // Compare against the stored challenge
    if (!computed.equals(codeChallenge)) {
        throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_GRANT);
    }
}

This is functionally identical to our Go implementation:

digest := sha256.Sum256([]byte(params.CodeVerifier))
computed := b64url(digest[:])
if computed != authCode.CodeChallenge {
    return nil, errors.New("oauth: code_verifier does not match code_challenge")
}

The algorithm is the same standard (RFC 7636). The implementation is the same computation. Spring just calls it from a provider that is registered into the security filter chain, where our Go code calls it inline in Exchange().

RSA key configuration and JWKS

Spring Authorization Server generates and manages the RSA key pair for ID token signing through JWKSource. The JWKS endpoint is served automatically once the key source is configured:

@Bean
public JWKSource jwkSource() {
    // Generate RSA key pair — equivalent to our rsa.GenerateKey(rand.Reader, 2048)
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(2048);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    // Build the JWK with kid — equivalent to our Provider.rsaKeyID
    RSAKey rsaKey = new RSAKey.Builder(publicKey)
        .privateKey(privateKey)
        .keyID(UUID.randomUUID().toString())  // the "kid" field in JWKS and token headers
        .build();

    JWKSet jwkSet = new JWKSet(rsaKey);
    // ImmutableJWKSet serves the public key at /.well-known/jwks.json
    // The private key never leaves the JWKSource
    return new ImmutableJWKSet<>(jwkSet);
}

@Bean
public JwtDecoder jwtDecoder(JWKSource jwkSource) {
    return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}

The /.well-known/jwks.json response matches our Go JWKS output format exactly — same RFC 7517 structure with kty, use, kid, alg, n, e fields. Any client that can verify our Go provider’s ID tokens can verify Spring’s, because the JWK format is a standard.

nimbus-jose-jwt for resource server verification

On the resource server side (a separate microservice that receives access tokens), Spring uses nimbus-jose-jwt to verify tokens against the JWKS endpoint. This is the same library as the JWT Library lab — the resource server configuration is identical whether the issuer is our Go provider or Spring Authorization Server:

// Resource server configuration — works against any OAuth 2.0 / OIDC provider
@Bean
public SecurityFilterChain resourceServerFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
        .oauth2ResourceServer(oauth2 -> oauth2
            .jwt(jwt -> jwt
                // Point to our Go provider's discovery document
                // Spring fetches /.well-known/openid-configuration at startup,
                // discovers the jwks_uri, and caches the public key
                .jwkSetUri("http://localhost:9000/.well-known/jwks.json")
            )
        );
    return http.build();
}

Or with auto-discovery from the issuer URL:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:9000

Spring fetches http://localhost:9000/.well-known/openid-configuration, parses the jwks_uri field, fetches the JWKS, and uses the public key to verify all incoming Bearer tokens. This works against our Go provider without any modification — the discovery document and JWKS format are standards.

The shared lesson

Every concept in our Go provider has a direct counterpart in Spring Authorization Server:

Go ProviderSpring Authorization Server
Client{Public: true}ClientSettings.requireProofKey(true)
delete(p.codes, code) before validateOAuth2AuthorizationService.save() with consumed flag
client.hasRedirectURI() exact matchOAuth2AuthorizationCodeRequestAuthenticationValidator
issueIDToken() RS256 JWTOidcIdTokenCustomizer via JWKSource
JWKS() endpointNimbusJwkSetEndpointFilter
DiscoveryDocument()OidcProviderConfigurationEndpointFilter

Spring Authorization Server is not magic. It is the same protocol, the same RFCs, implemented with production-grade error handling, persistent storage adapters, and Spring’s dependency injection model. Building the Go version first means reading the Spring source code becomes straightforward: you know what each class is doing because you implemented the same logic yourself.

What surprised me

Where to go deeper

  • Previous in security: JWT Library — the HS256 and RS256 JWT construction used here is explained in detail, including the algorithm confusion attack that makes algorithm pinning essential
  • Previous in security: Password Hashing — the login step in any OAuth provider needs to hash and verify passwords; understanding Argon2id and peppering applies directly
  • Next in security: mTLS From Scratch — mutual TLS is an alternative authentication method for confidential clients at the /token endpoint (client_auth_method=tls_client_auth), replacing client secrets entirely
  • System design questions this unlocks: How does Google handle 1 billion OAuth token verifications per day without every resource server calling the authorization server? (Answer: stateless JWT verification with JWKS caching.) Why does a mobile app need PKCE but a server-side web app does not? How would you implement token revocation across a fleet of 1,000 stateless API servers without a shared database?