<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Oauth2-Proxy | Tobias Theel | Senior Software Engineer</title><link>https://blog.noobygames.de/tags/oauth2-proxy/</link><description>Remote-first Senior Software Engineer specializing in Go, Cloud-Native architecture, and Game Development. 12+ years of experience in distributed environments.</description><generator>Hugo</generator><language>en</language><copyright>© 2026 Tobias Theel</copyright><lastBuildDate>Sat, 21 Nov 2026 11:30:00 +0100</lastBuildDate><atom:link href="https://blog.noobygames.de/tags/oauth2-proxy/index.xml" rel="self" type="application/rss+xml"/><item><title>gocloak-auth-proxy: a fast, Keycloak-native auth proxy in Go</title><link>https://blog.noobygames.de/blog/gocloak-auth-proxy-keycloak/</link><pubDate>Sat, 21 Nov 2026 11:30:00 +0100</pubDate><dc:creator>Tobias Theel</dc:creator><guid isPermaLink="true">https://blog.noobygames.de/blog/gocloak-auth-proxy-keycloak/</guid><category>Software Engineering</category><category>go</category><category>keycloak</category><category>gocloak</category><category>oidc</category><category>jwt</category><category>traefik</category><category>nginx</category><category>oauth2-proxy</category><category>security</category><category>performance</category><category>open-source</category><description>gocloak-auth-proxy verifies Keycloak JWTs locally for Traefik and nginx forward-auth, as a reverse proxy or with OIDC browser SSO. Benchmarks against oauth2-proxy, gogatekeeper and reference proxies in Rust, Java and Python, the security model and a setup guide.</description><content:encoded><![CDATA[<blockquote>
<p><strong>Series: The gocloak universe.</strong> <a href="/blog/gocloak-mcp-first-mcp-server-homebrew/">Part 1: gocloak-mcp</a> · Part 2: gocloak-auth-proxy (you are here) · <a href="/blog/gocloak-performance-json-v2-http-clients/">Part 3: gocloak performance</a> · <a href="/blog/gocloak-performance-final-benchmarks/">Part 4: final benchmark numbers</a> · <a href="/blog/gocloak-pprof-profiling/">Part 5: pprof</a></p>
</blockquote>
<aside class="tldr" aria-label="Key takeaways">
<p class="tldr-title">TL;DR</p>
<ul>
<li>gocloak-auth-proxy is a Keycloak-only authentication proxy in Go, built on gocloak v15, that runs as forward-auth for Traefik and nginx, as a reverse proxy, or with OIDC browser SSO.</li>
<li>JWT checks in gocloak-auth-proxy run against an in-memory JWKS snapshot, so a valid request never waits for the network.</li>
<li>Only asymmetric algorithms pass (default <code>RS256,ES256</code>): the proxy rejects <code>none</code> and <code>HS*</code> and strips client identity headers in both dash and underscore spellings.</li>
<li>In the project&rsquo;s benchmark, gocloak-auth-proxy v0.2.0 answered 15,002 forward-auth requests per second with a full signature check on every request, 2.5 times as many as oauth2-proxy v7.15.6.</li>
<li>A Rust reference proxy (21,616 requests per second) beats gocloak-auth-proxy without its token cache; with the v0.2.0 cache, gocloak-auth-proxy reaches 25,169.</li>
</ul>

</aside>

<p><strong>Update, 21 November 2026:</strong> gocloak-auth-proxy v0.2.0 is out, with a faster token verifier, a cache for verified tokens and a benchmark suite in the repository. I added its numbers and charts to the benchmark section and updated the install commands; the v0.1.0 numbers stay below for comparison.</p>
<p>In <a href="/blog/gocloak-mcp-first-mcp-server-homebrew/">Part 1</a> I wrote about gocloak v15 and the Keycloak MCP server built on it. At the same time a second project went public: <a href="https://github.com/Nerzal/gocloak-auth-proxy" rel="noopener">gocloak-auth-proxy</a>, an authentication proxy that sits in front of your services and lets only requests with a valid Keycloak token through.</p>
<p>It runs in three modes:</p>
<ul>
<li>Forward-auth: Traefik&rsquo;s <code>forwardAuth</code> or nginx&rsquo;s <code>auth_request</code> ask the proxy on <code>/auth</code> whether a request may pass. The proxy answers 200 with identity headers, or 401/403.</li>
<li>Reverse proxy: the proxy itself forwards verified requests to one upstream.</li>
<li>Browser SSO: OIDC authorization code flow with PKCE, an encrypted session cookie and logout, for web apps that should not deal with OIDC themselves.</li>
</ul>
<p>In the project&rsquo;s own benchmark, v0.2.0 answered 15,002 forward-auth requests per second with a full signature check on every request, 2.5 times as many as oauth2-proxy on the same machine, and 25,169 with its token cache. Below are those numbers with their caveats, my own micro-benchmark run, the security model and a setup for Traefik and nginx.</p>
<p>v0.1.0 was released on 2026-11-21, v0.2.0 the same afternoon. Nobody has audited it yet, and most of the alternatives below have years of production use behind them.</p>
<h2 id="why-another-proxy-for-keycloak">Why another proxy for Keycloak</h2>
<p>Keycloak used to ship its own proxy, Keycloak Gatekeeper, later renamed Louketo. In August 2020 the Keycloak team <a href="https://www.keycloak.org/2020/08/sunsetting-louketo-project.adoc" rel="noopener">announced its end of life</a> and recommended <a href="https://github.com/oauth2-proxy/oauth2-proxy" rel="noopener">oauth2-proxy</a> instead; the last release is 1.0.0 from July 2020 and the <a href="https://github.com/louketo/louketo-proxy" rel="noopener">repository</a> is archived. Since then most Keycloak setups use one of these:</p>
<table>
	<thead>
			<tr>
					<th>Project</th>
					<th>Language</th>
					<th style="text-align: right">Stars</th>
					<th>Latest release</th>
					<th>Keycloak focus</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy" rel="noopener">oauth2-proxy</a></td>
					<td>Go</td>
					<td style="text-align: right">15,392</td>
					<td>v7.15.6 (2026-11-05)</td>
					<td>Generic, with <code>keycloak</code> and <code>keycloak_oidc</code> providers</td>
			</tr>
			<tr>
					<td><a href="https://github.com/pomerium/pomerium" rel="noopener">Pomerium</a></td>
					<td>Go</td>
					<td style="text-align: right">5,113</td>
					<td>v0.34.1 (2026-11-11)</td>
					<td>Generic OIDC, full identity-aware proxy</td>
			</tr>
			<tr>
					<td><a href="https://github.com/ory/oathkeeper" rel="noopener">Ory Oathkeeper</a></td>
					<td>Go</td>
					<td style="text-align: right">3,610</td>
					<td>v26.2.0 (2026-03-20)</td>
					<td>Generic; JWKS URLs configured by hand</td>
			</tr>
			<tr>
					<td><a href="https://github.com/vouch/vouch-proxy" rel="noopener">Vouch Proxy</a></td>
					<td>Go</td>
					<td style="text-align: right">3,290</td>
					<td>v0.50.0</td>
					<td>Generic, nginx <code>auth_request</code></td>
			</tr>
			<tr>
					<td><a href="https://github.com/thomseddon/traefik-forward-auth" rel="noopener">traefik-forward-auth</a></td>
					<td>Go</td>
					<td style="text-align: right">2,396</td>
					<td>v2.3.0 (2024-05-06)</td>
					<td>Generic OIDC</td>
			</tr>
			<tr>
					<td><a href="https://github.com/gogatekeeper/gatekeeper" rel="noopener">gogatekeeper</a></td>
					<td>Go</td>
					<td style="text-align: right">324</td>
					<td>5.1.0 (2026-11-18)</td>
					<td>Yes, the community fork of Keycloak Gatekeeper</td>
			</tr>
			<tr>
					<td><strong>gocloak-auth-proxy</strong></td>
					<td>Go</td>
					<td style="text-align: right">new</td>
					<td>v0.2.0 (2026-11-21)</td>
					<td>Yes, built on gocloak v15</td>
			</tr>
	</tbody>
</table>
<p>(Stars as of 2026-11-21.)</p>
<p>oauth2-proxy is the default answer and a good one. It supports a dozen identity providers, which also means its configuration and code have to cover a dozen identity providers. gogatekeeper is the closest relative: Keycloak-specific, with forward-auth, PKCE, role and group rules, UMA (marked as not production ready in <a href="https://gogatekeeper.github.io/gatekeeper/" rel="noopener">its docs</a>) and OPA. gocloak-auth-proxy is narrower than both: only Keycloak, few dependencies, and a hot path on which a valid request never waits for the network.</p>
<h2 id="how-a-request-is-verified">How a request is verified</h2>
<p>For a request with <code>Authorization: Bearer &lt;JWT&gt;</code> the proxy goes through these steps:</p>
<ol>
<li>Strip every identity header the client sent: <code>X-Auth-Request-*</code>, <code>X-Forwarded-*</code>, <code>Forwarded</code>, <code>X-Real-IP</code>, <code>Authorization</code> after reading it, configured custom headers, and their <code>_</code> variants (more on that below).</li>
<li>Normalize the path and apply the ACL: public paths, optional-auth paths, CORS preflight, route rules. Encoded traversal (<code>..</code>, <code>%2f</code>, <code>%2e</code>, <code>%5c</code>, <code>%23</code>, <code>%00</code>) is rejected.</li>
<li>Parse the JWT header and check the algorithm against the allowlist.</li>
<li>Look up the signing key by <code>kid</code> in an in-memory JWKS snapshot and verify the signature.</li>
<li>Check <code>iss</code>, <code>aud</code>, <code>exp</code>, <code>nbf</code>, <code>typ</code> (only <code>Bearer</code>) and the authorization rules: realm and client roles, groups, scopes, <code>acr</code>, <code>amr</code>, <code>email_verified</code>, <code>azp</code>, Keycloak organization.</li>
<li>Optionally call Keycloak&rsquo;s token introspection or UMA endpoint.</li>
<li>Answer with identity headers (forward-auth) or forward the request upstream (reverse proxy).</li>
</ol>
<p>Steps 1 to 5 need no network call. The JWKS snapshot lives behind a Go <code>atomic.Pointer</code>, so readers never take a lock. When a token arrives with an unknown <code>kid</code>, for example right after a key rotation in Keycloak, the proxy refreshes the key set once, no matter how many requests are waiting for it, with a rate limit and exponential backoff so a flood of forged <code>kid</code> values cannot turn into a flood of requests against Keycloak. A snapshot expires after twice the refresh interval (bounded to one minute through 24 hours). After that <code>/readyz</code> returns 503 and Kubernetes stops sending traffic, instead of the proxy accepting tokens against stale keys forever.</p>
<h3 id="which-tokens-it-refuses">Which tokens it refuses</h3>
<p>The algorithm allowlist defaults to <code>RS256,ES256</code> and accepts only asymmetric algorithms: RS256/384/512, PS256/384/512, ES256/384/512 and EdDSA with Ed25519. <code>none</code> and the HMAC family <code>HS*</code> are rejected at configuration time and at verification time. That closes the classic algorithm confusion attack where a token signed with HS256, using the public RSA key as the HMAC secret, passes a naive verifier. The proxy also refuses tokens with <code>crit</code>, <code>b64</code>, <code>jku</code>, <code>x5u</code> or <code>jwk</code> headers, because those let a token tell the verifier where to fetch its key. <a href="https://www.rfc-editor.org/rfc/rfc8725" rel="noopener">RFC 8725</a>, the JWT best current practices, recommends both.</p>
<p>The relevant standards, if you want to dig deeper: <a href="https://www.rfc-editor.org/rfc/rfc7519" rel="noopener">RFC 7519</a> (JWT), <a href="https://www.rfc-editor.org/rfc/rfc7515" rel="noopener">RFC 7515</a> (JWS), <a href="https://www.rfc-editor.org/rfc/rfc7517" rel="noopener">RFC 7517</a> (JWK), <a href="https://www.rfc-editor.org/rfc/rfc7662" rel="noopener">RFC 7662</a> (token introspection), <a href="https://www.rfc-editor.org/rfc/rfc7636" rel="noopener">RFC 7636</a> (PKCE), <a href="https://openid.net/specs/openid-connect-backchannel-1_0.html" rel="noopener">OpenID Connect Back-Channel Logout</a> and Keycloak&rsquo;s <a href="https://www.keycloak.org/docs/latest/authorization_services/index.html#_service_uma_authorization_process" rel="noopener">UMA 2.0 docs</a>.</p>
<h3 id="header-and-path-confusion">Header and path confusion</h3>
<p>The JWT library takes care of the cryptography. Most auth proxy vulnerabilities come from somewhere else: the proxy and the backend disagree about a header or a path. oauth2-proxy&rsquo;s advisory list from the last 16 months is a good catalog of what goes wrong:</p>
<table>
	<thead>
			<tr>
					<th>Advisory</th>
					<th>Severity</th>
					<th>What happened</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy/security/advisories/GHSA-7rh7-c77v-6434" rel="noopener">CVE-2025-54576</a></td>
					<td>critical</td>
					<td><code>skip_auth_routes</code> matched against the URL including the query string</td>
			</tr>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy/security/advisories/GHSA-vjrc-mh2v-45x6" rel="noopener">CVE-2025-64484</a></td>
					<td>high</td>
					<td>Header smuggling: identity headers spelled with underscores passed through, and WSGI/CGI backends read them as the dash version</td>
			</tr>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy/security/advisories/GHSA-7x63-xv5r-3p2x" rel="noopener">CVE-2026-40575</a></td>
					<td>critical</td>
					<td>Authentication bypass through a spoofed <code>X-Forwarded-Uri</code></td>
			</tr>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy/security/advisories/GHSA-pxq7-h93f-9jrg" rel="noopener">CVE-2026-41059</a></td>
					<td>high</td>
					<td>Fragment confusion in <code>skip_auth_routes</code> and <code>skip_auth_regex</code></td>
			</tr>
			<tr>
					<td><a href="https://github.com/oauth2-proxy/oauth2-proxy/security/advisories/GHSA-5hvv-m4w4-gf6v" rel="noopener">CVE-2026-34457</a></td>
					<td>critical</td>
					<td>A health-check User-Agent match skipped authentication in <code>auth_request</code> mode</td>
			</tr>
	</tbody>
</table>
<p>All of them are fixed in current releases. I list them because they are exactly the cases gocloak-auth-proxy has tests for: it strips identity headers in both spellings (<code>X-Auth-Request-User</code> and <code>X_Auth_Request_User</code>), never trusts <code>X-Forwarded-*</code> from the client, rejects encoded delimiters and fragments in paths, and has no User-Agent-based exceptions. Tests only cover the cases someone thought of. oauth2-proxy&rsquo;s bugs were found because many people run it and researchers study it. gocloak-auth-proxy has not had that scrutiny yet, and reviews are welcome.</p>
<h2 id="benchmarks">Benchmarks</h2>
<h3 id="head-to-head-v020">Head to head (v0.2.0)</h3>
<p>v0.2.0 brings its benchmark suite into the repository: <code>make bench-compare</code> runs <a href="https://github.com/Nerzal/gocloak-auth-proxy/blob/main/bench/run_compare.sh" rel="noopener"><code>bench/run_compare.sh</code></a>. All proxies ran on the same Linux host (Intel Xeon 6985P-C) with <code>GOMAXPROCS=2</code>, 32 concurrent connections for 5 seconds, a 2048-bit RS256 bearer token, a local mock JWKS and a local mock upstream, with zero errors in every run.</p>
<p>Two token modes matter for reading the numbers. &ldquo;Warm&rdquo; sends the same token on every request. &ldquo;Cold&rdquo; rotates through 2,000 different tokens, and for gocloak-auth-proxy the token cache is switched off (<code>TOKEN_CACHE_SIZE=0</code>), so every request pays for a full signature check.</p>
<p><img src="/img/gocloak/auth-proxy/benchmark-overview.svg" alt="Throughput, latency and memory of gocloak-auth-proxy v0.2.0 against oauth2-proxy and reference proxies in Rust, Java and Python"></p>
<table>
	<thead>
			<tr>
					<th>Proxy</th>
					<th>Tokens</th>
					<th style="text-align: right">Forward-auth RPS</th>
					<th style="text-align: right">p50</th>
					<th style="text-align: right">Reverse proxy RPS</th>
					<th style="text-align: right">p50</th>
					<th style="text-align: right">Memory under load</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>gocloak-auth-proxy v0.2.0</td>
					<td>warm</td>
					<td style="text-align: right">25,169</td>
					<td style="text-align: right">1.08 ms</td>
					<td style="text-align: right">11,248</td>
					<td style="text-align: right">2.51 ms</td>
					<td style="text-align: right">15.3 to 16.1 MB</td>
			</tr>
			<tr>
					<td>gocloak-auth-proxy v0.2.0</td>
					<td>cold</td>
					<td style="text-align: right">15,002</td>
					<td style="text-align: right">1.99 ms</td>
					<td style="text-align: right">7,583</td>
					<td style="text-align: right">3.90 ms</td>
					<td style="text-align: right">15.7 to 16.2 MB</td>
			</tr>
			<tr>
					<td>Rust reference (Axum, <code>jsonwebtoken</code>)</td>
					<td>cold</td>
					<td style="text-align: right">21,616</td>
					<td style="text-align: right">1.32 ms</td>
					<td style="text-align: right">10,629</td>
					<td style="text-align: right">2.86 ms</td>
					<td style="text-align: right">3.8 to 6.3 MB</td>
			</tr>
			<tr>
					<td>oauth2-proxy v7.15.6</td>
					<td>warm</td>
					<td style="text-align: right">6,023</td>
					<td style="text-align: right">4.47 ms</td>
					<td style="text-align: right">2,245</td>
					<td style="text-align: right">13.39 ms</td>
					<td style="text-align: right">26.7 to 27.8 MB</td>
			</tr>
			<tr>
					<td>Java 17 reference (<code>HttpServer</code>)</td>
					<td>cold</td>
					<td style="text-align: right">6,839</td>
					<td style="text-align: right">3.71 ms</td>
					<td style="text-align: right">1,978</td>
					<td style="text-align: right">14.23 ms</td>
					<td style="text-align: right">104.9 to 114.4 MB</td>
			</tr>
			<tr>
					<td>Python 3.11 reference (Uvicorn, PyJWT)</td>
					<td>cold</td>
					<td style="text-align: right">5,811</td>
					<td style="text-align: right">5.14 ms</td>
					<td style="text-align: right">734</td>
					<td style="text-align: right">32.95 ms</td>
					<td style="text-align: right">14.7 MB (master process)</td>
			</tr>
	</tbody>
</table>
<p>The Rust, Java and Python proxies are not products. They are small reference implementations in <a href="https://github.com/Nerzal/gocloak-auth-proxy/tree/main/bench/competitors" rel="noopener"><code>bench/competitors/</code></a>, written for this benchmark to show what each runtime does with the same job. oauth2-proxy is the only established product in the run; gogatekeeper is no longer part of it.</p>
<p>Compared fairly, with a full signature check on every request, gocloak-auth-proxy does 2.5 times the forward-auth throughput of oauth2-proxy and 3.4 times in reverse-proxy mode. With the cache it reaches 4.2 and 5 times. The Rust reference is faster than gocloak-auth-proxy without the cache (21,616 against 15,002 forward-auth requests per second) and needs a quarter of the memory; only with the cache does the Go proxy get ahead. The chart&rsquo;s &ldquo;#1&rdquo; badge counts the warm run. That is a fair description of production, where a client sends the same token until it expires, but it is not the same workload as the cold runs of the reference proxies.</p>
<h3 id="what-v020-changed">What v0.2.0 changed</h3>
<p><img src="/img/gocloak/auth-proxy/pprof-progression.svg" alt="Bytes and allocations per token verification from v0.1.0 to v0.2.0"></p>
<p>Two changes came out of profiling. The JWS verifier now reads the claims with the streaming <code>jsontext</code> package from <code>encoding/json/v2</code> instead of decoding them into maps. For a cold RS256 verification that cuts the bytes from 6,412 to 1,728 (-73 %) and the allocations from 79 to 33 (-58 %); for EdDSA with custom claim headers from 8,540 to 624 bytes (-93 %) and from 104 to 24 allocations (-77 %). The second change is a sharded cache of verified tokens, keyed by the SHA-256 of the token. A hit takes 463 ns and allocates nothing. It still checks <code>exp</code> and <code>nbf</code> on every hit, and whether the signing key is still in the current JWKS snapshot, so a key rotation in Keycloak also invalidates cached tokens. Reading an OIDC session cookie got 40 % faster on top.</p>
<p>The micro-benchmarks from the v0.2.0 README (Go 1.27.4, gocloak v15.1.0, Intel Xeon 6985P-C):</p>
<table>
	<thead>
			<tr>
					<th>Benchmark</th>
					<th style="text-align: right">Time per op</th>
					<th style="text-align: right">Bytes per op</th>
					<th style="text-align: right">Allocs per op</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>BenchmarkPolicy</code></td>
					<td style="text-align: right">128 ns</td>
					<td style="text-align: right">0</td>
					<td style="text-align: right">0</td>
			</tr>
			<tr>
					<td><code>BenchmarkIntrospectorCacheHit</code></td>
					<td style="text-align: right">192 ns</td>
					<td style="text-align: right">0</td>
					<td style="text-align: right">0</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerifyCached</code></td>
					<td style="text-align: right">463 ns</td>
					<td style="text-align: right">0</td>
					<td style="text-align: right">0</td>
			</tr>
			<tr>
					<td><code>BenchmarkForwardAuth</code></td>
					<td style="text-align: right">1.62 µs</td>
					<td style="text-align: right">2,336</td>
					<td style="text-align: right">17</td>
			</tr>
			<tr>
					<td><code>BenchmarkReadSession</code></td>
					<td style="text-align: right">2.76 µs</td>
					<td style="text-align: right">1,328</td>
					<td style="text-align: right">3</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerify/RS256</code></td>
					<td style="text-align: right">30.0 µs</td>
					<td style="text-align: right">1,728</td>
					<td style="text-align: right">33</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerifyEdDSA</code></td>
					<td style="text-align: right">45.5 µs</td>
					<td style="text-align: right">624</td>
					<td style="text-align: right">24</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerify/ES256</code></td>
					<td style="text-align: right">67.7 µs</td>
					<td style="text-align: right">1,536</td>
					<td style="text-align: right">44</td>
			</tr>
	</tbody>
</table>
<p>These come from a different machine than my v0.1.0 run below, so compare the allocation columns, not the times.</p>
<h3 id="the-v010-comparison">The v0.1.0 comparison</h3>
<p>The v0.1.0 comparison came from the project README. All three proxies ran on the same Linux host (Intel Xeon 6985P-C) with <code>GOMAXPROCS=2</code>, against a local mock Keycloak JWKS and a local mock upstream, with a 2048-bit RS256 bearer token on every request, 32 concurrent connections, access logging off and zero errors in every run. The load scripts and the competitor configurations were not in the repository at that point, so this run cannot be reproduced one to one.</p>










<figure class="diagram">
    <div class="diagram-canvas" role="img" aria-label="Forward-auth is the mode Traefik and nginx use. The gap is largest there because the proxy only verifies and answers; in reverse-proxy mode it also forwards the request body upstream.">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 296" width="720" height="296">
<text x="20" y="28" class="d-t d-bold" font-size="15">Requests per second at 32 concurrent connections (higher is better)</text>
<text x="20" y="48" class="d-m" font-size="11.5">Every request carries a 2048-bit RS256 JWT that the proxy verifies before it answers.</text>
<rect x="20" y="62" width="12" height="12" rx="3" fill="#4285f4"/>
<text x="38" y="72" class="d-t" font-size="11.5">gocloak-auth-proxy v0.1.0</text>
<rect x="222.0" y="62" width="12" height="12" rx="3" fill="#d97706"/>
<text x="240.0" y="72" class="d-t" font-size="11.5">gogatekeeper v5.1.0</text>
<rect x="385.6" y="62" width="12" height="12" rx="3" fill="#a142f4"/>
<text x="403.6" y="72" class="d-t" font-size="11.5">oauth2-proxy v7.15.6</text>
<line x1="190.0" y1="84" x2="190.0" y2="244" class="d-divider"/>
<text x="190.0" y="260" class="d-m" font-size="11" text-anchor="middle">0</text>
<line x1="317.8" y1="84" x2="317.8" y2="244" class="d-divider"/>
<text x="317.8" y="260" class="d-m" font-size="11" text-anchor="middle">5k</text>
<line x1="445.6" y1="84" x2="445.6" y2="244" class="d-divider"/>
<text x="445.6" y="260" class="d-m" font-size="11" text-anchor="middle">10k</text>
<line x1="573.3" y1="84" x2="573.3" y2="244" class="d-divider"/>
<text x="573.3" y="260" class="d-m" font-size="11" text-anchor="middle">15k</text>
<text x="180" y="127.0" class="d-t" font-size="12" text-anchor="end">Forward-auth</text>
<path d="M190 92 H590.4 Q594.4 92 594.4 96 V106 Q594.4 110 590.4 110 H190 Z" fill="#4285f4"/>
<text x="600.4" y="105" class="d-t" font-size="11">15,825 RPS <tspan class="d-m">p50 1.80 ms</tspan></text>
<path d="M190 114 H334.2 Q338.2 114 338.2 118 V128 Q338.2 132 334.2 132 H190 Z" fill="#d97706"/>
<text x="344.2" y="127" class="d-t" font-size="11">5,801 RPS <tspan class="d-m">p50 5.04 ms</tspan></text>
<path d="M190 136 H355.3 Q359.3 136 359.3 140 V150 Q359.3 154 355.3 154 H190 Z" fill="#a142f4"/>
<text x="365.3" y="149" class="d-t" font-size="11">6,625 RPS <tspan class="d-m">p50 4.09 ms</tspan></text>
<text x="180" y="211.0" class="d-t" font-size="12" text-anchor="end">Reverse proxy</text>
<path d="M190 176 H409.3 Q413.3 176 413.3 180 V190 Q413.3 194 409.3 194 H190 Z" fill="#4285f4"/>
<text x="419.3" y="189" class="d-t" font-size="11">8,737 RPS <tspan class="d-m">p50 3.36 ms</tspan></text>
<path d="M190 198 H307.5 Q311.5 198 311.5 202 V212 Q311.5 216 307.5 216 H190 Z" fill="#d97706"/>
<text x="317.5" y="211" class="d-t" font-size="11">4,754 RPS <tspan class="d-m">p50 6.22 ms</tspan></text>
<path d="M190 220 H258.9 Q262.9 220 262.9 224 V234 Q262.9 238 258.9 238 H190 Z" fill="#a142f4"/>
<text x="268.9" y="233" class="d-t" font-size="11">2,851 RPS <tspan class="d-m">p50 10.55 ms</tspan></text>
<line x1="190" y1="84" x2="190" y2="244" class="d-arrow"/>
<text x="20" y="284" class="d-m" font-size="10.5">Source: gocloak-auth-proxy README. Same host, Xeon 6985P-C, GOMAXPROCS=2, RS256 JWT, 0 errors.</text>
</svg>

    </div>
    <figcaption class="diagram-caption">Forward-auth is the mode Traefik and nginx use. The gap is largest there because the proxy only verifies and answers; in reverse-proxy mode it also forwards the request body upstream.</figcaption>
</figure>

<table>
	<thead>
			<tr>
					<th>Proxy</th>
					<th style="text-align: right">Forward-auth RPS</th>
					<th style="text-align: right">p50</th>
					<th style="text-align: right">Reverse proxy RPS</th>
					<th style="text-align: right">p50</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>gocloak-auth-proxy v0.1.0</td>
					<td style="text-align: right">15,825</td>
					<td style="text-align: right">1.80 ms</td>
					<td style="text-align: right">8,737</td>
					<td style="text-align: right">3.36 ms</td>
			</tr>
			<tr>
					<td>gogatekeeper v5.1.0</td>
					<td style="text-align: right">5,801</td>
					<td style="text-align: right">5.04 ms</td>
					<td style="text-align: right">4,754</td>
					<td style="text-align: right">6.22 ms</td>
			</tr>
			<tr>
					<td>oauth2-proxy v7.15.6</td>
					<td style="text-align: right">6,625</td>
					<td style="text-align: right">4.09 ms</td>
					<td style="text-align: right">2,851</td>
					<td style="text-align: right">10.55 ms</td>
			</tr>
	</tbody>
</table>










<figure class="diagram">
    <div class="diagram-canvas" role="img" aria-label="Binary size and idle memory from the README. Dependencies are the require entries in each project&#39;s go.mod at the release tag, direct and indirect.">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 240" width="720" height="240">
<text x="20" y="28" class="d-t d-bold" font-size="15">Footprint: smaller is better in every panel</text>
<text x="20" y="48" class="d-m" font-size="11.5">Each panel has its own scale. Dependencies = require entries (direct + indirect) in go.mod at each release tag, counted 2026-11-21.</text>
<rect x="20" y="62" width="12" height="12" rx="3" fill="#4285f4"/>
<text x="38" y="72" class="d-t" font-size="11.5">gocloak-auth-proxy v0.1.0</text>
<rect x="222.0" y="62" width="12" height="12" rx="3" fill="#d97706"/>
<text x="240.0" y="72" class="d-t" font-size="11.5">gogatekeeper v5.1.0</text>
<rect x="385.6" y="62" width="12" height="12" rx="3" fill="#a142f4"/>
<text x="403.6" y="72" class="d-t" font-size="11.5">oauth2-proxy v7.15.6</text>
<text x="20" y="104" class="d-t d-bold" font-size="12.5">Binary size</text>
<path d="M20 116 H45.9 Q49.9 116 49.9 120 V130 Q49.9 134 45.9 134 H20 Z" fill="#4285f4"/>
<text x="55.9" y="129" class="d-t" font-size="11">8.6 MB</text>
<path d="M20 142 H151.6 Q155.6 142 155.6 146 V156 Q155.6 160 151.6 160 H20 Z" fill="#d97706"/>
<text x="161.6" y="155" class="d-t" font-size="11">39 MB</text>
<path d="M20 168 H162.0 Q166.0 168 166.0 172 V182 Q166.0 186 162.0 186 H20 Z" fill="#a142f4"/>
<text x="172.0" y="181" class="d-t" font-size="11">42 MB</text>
<line x1="20" y1="112" x2="20" y2="194" class="d-arrow"/>
<text x="256" y="104" class="d-t d-bold" font-size="12.5">Idle memory (RSS)</text>
<path d="M256 116 H298.8 Q302.8 116 302.8 120 V130 Q302.8 134 298.8 134 H256 Z" fill="#4285f4"/>
<text x="308.8" y="129" class="d-t" font-size="11">9.1 MB</text>
<path d="M256 142 H398.0 Q402.0 142 402.0 146 V156 Q402.0 160 398.0 160 H256 Z" fill="#d97706"/>
<text x="408.0" y="155" class="d-t" font-size="11">28.4 MB</text>
<path d="M256 168 H359.4 Q363.4 168 363.4 172 V182 Q363.4 186 359.4 186 H256 Z" fill="#a142f4"/>
<text x="369.4" y="181" class="d-t" font-size="11">20.9 MB</text>
<line x1="256" y1="112" x2="256" y2="194" class="d-arrow"/>
<text x="492" y="104" class="d-t d-bold" font-size="12.5">go.mod dependencies</text>
<path d="M492 116 H504.8 Q508.8 116 508.8 120 V130 Q508.8 134 504.8 134 H492 Z" fill="#4285f4"/>
<text x="514.8" y="129" class="d-t" font-size="11">12 </text>
<path d="M492 142 H634.0 Q638.0 142 638.0 146 V156 Q638.0 160 634.0 160 H492 Z" fill="#d97706"/>
<text x="644.0" y="155" class="d-t" font-size="11">104 </text>
<path d="M492 168 H590.5 Q594.5 168 594.5 172 V182 Q594.5 186 590.5 186 H492 Z" fill="#a142f4"/>
<text x="600.5" y="181" class="d-t" font-size="11">73 </text>
<line x1="492" y1="112" x2="492" y2="194" class="d-arrow"/>
<text x="20" y="228" class="d-m" font-size="10.5">Size and memory: gocloak-auth-proxy README (Xeon 6985P-C). Dependencies: go.mod require entries at each release tag.</text>
</svg>

    </div>
    <figcaption class="diagram-caption">Binary size and idle memory from the README. Dependencies are the require entries in each project&rsquo;s go.mod at the release tag, direct and indirect.</figcaption>
</figure>

<p>At the v0.1.0 tag, gocloak-auth-proxy&rsquo;s go.mod has 12 require entries (2 direct, 10 indirect; the README still says 8). gogatekeeper 5.1.0 has 104 and oauth2-proxy v7.15.6 has 73. Fewer dependencies does not make code correct, but every module is code you have to trust and update.</p>
<p>In forward-auth mode a valid request costs one signature verification and a few map lookups, as the micro-benchmarks below show. A benchmark against mock services measures the proxy alone; in production the upstream will usually dominate.</p>
<h3 id="micro-benchmarks-on-my-machine">Micro-benchmarks on my machine</h3>
<p>I ran <code>make bench</code> on v0.1.0 on my machine (AMD Ryzen AI 9 HX 370, Windows 11, Go 1.26.2):</p>
<table>
	<thead>
			<tr>
					<th>Benchmark</th>
					<th style="text-align: right">Time per op</th>
					<th style="text-align: right">Bytes per op</th>
					<th style="text-align: right">Allocs per op</th>
					<th>What it covers</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>BenchmarkIntrospectorCacheHit</code></td>
					<td style="text-align: right">55 ns</td>
					<td style="text-align: right">0</td>
					<td style="text-align: right">0</td>
					<td>Introspection cache hit, SHA-256 keyed</td>
			</tr>
			<tr>
					<td><code>BenchmarkPolicy</code></td>
					<td style="text-align: right">90 ns</td>
					<td style="text-align: right">0</td>
					<td style="text-align: right">0</td>
					<td>Path normalization, ACL, route role check</td>
			</tr>
			<tr>
					<td><code>BenchmarkForwardAuth</code></td>
					<td style="text-align: right">1.75 µs</td>
					<td style="text-align: right">2,416</td>
					<td style="text-align: right">25</td>
					<td>Header scrubbing and identity headers (mock verifier)</td>
			</tr>
			<tr>
					<td><code>BenchmarkReadSession</code></td>
					<td style="text-align: right">7.3 µs</td>
					<td style="text-align: right">3,024</td>
					<td style="text-align: right">10</td>
					<td>AES-256-GCM session cookie decryption and decode</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerify/RS256</code></td>
					<td style="text-align: right">38.3 µs</td>
					<td style="text-align: right">7,704</td>
					<td style="text-align: right">113</td>
					<td>Full RS256 verification with claims and roles</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerify/ES256</code></td>
					<td style="text-align: right">51.4 µs</td>
					<td style="text-align: right">7,352</td>
					<td style="text-align: right">125</td>
					<td>Same with ES256 (P-256)</td>
			</tr>
			<tr>
					<td><code>BenchmarkVerifyEdDSA</code></td>
					<td style="text-align: right">85.0 µs</td>
					<td style="text-align: right">10,576</td>
					<td style="text-align: right">176</td>
					<td>Ed25519 plus custom claim headers</td>
			</tr>
	</tbody>
</table>
<p>The v0.1.0 README&rsquo;s numbers came from Go 1.27 on Linux and show fewer allocations (79 for RS256, for example), so the toolchain matters here; compare runs only on the same setup. The picture is the same on both: signature verification is about 95 percent of the CPU time of a request, and everything around it is small change. RS256 is the fastest algorithm to verify, which is convenient because it is Keycloak&rsquo;s default.</p>
<h2 id="setting-it-up">Setting it up</h2>
<h3 id="1-a-client-in-keycloak">1. A client in Keycloak</h3>
<p>For forward-auth and reverse-proxy mode the proxy only needs the issuer URL and an audience; it does not need a client secret. Keycloak puts <code>account</code> into <code>aud</code> by default, so add an <em>Audience</em> mapper to a client scope of the clients that call your API, with your API&rsquo;s name (for example <code>my-api</code>) as the included audience.</p>
<p>For browser SSO, create a confidential client with the standard flow enabled and <code>https://app.example.com/oauth2/callback</code> as the redirect URI.</p>
<h3 id="2-run-it">2. Run it</h3>
<p>There are static binaries for Linux and macOS (amd64 and arm64) with checksums and GitHub build provenance, and a multi-arch distroless image on GHCR. On Windows use the container or <code>go install</code>.</p>
<div class="os-tabs">
<div class="os-tab" data-label="Linux / macOS">
<p class="os-tab-label">Linux / macOS</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">docker run --rm -p 8080:8080 <span class="se">\
</span></span></span><span class="line"><span class="cl">  -e <span class="nv">ISSUER</span><span class="o">=</span>https://keycloak.example.com/realms/myrealm <span class="se">\
</span></span></span><span class="line"><span class="cl">  -e <span class="nv">AUDIENCE</span><span class="o">=</span>my-api <span class="se">\
</span></span></span><span class="line"><span class="cl">  -e <span class="nv">REQUIRED_REALM_ROLES</span><span class="o">=</span>reader <span class="se">\
</span></span></span><span class="line"><span class="cl">  ghcr.io/nerzal/gocloak-auth-proxy:v0.2.0
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># or the binary, with provenance check</span>
</span></span><span class="line"><span class="cl"><span class="nv">VERSION</span><span class="o">=</span>0.2.0<span class="p">;</span> <span class="nv">OS</span><span class="o">=</span>linux<span class="p">;</span> <span class="nv">ARCH</span><span class="o">=</span>amd64
</span></span><span class="line"><span class="cl">curl -fsSLO <span class="s2">&#34;https://github.com/Nerzal/gocloak-auth-proxy/releases/download/v</span><span class="si">${</span><span class="nv">VERSION</span><span class="si">}</span><span class="s2">/gocloak-auth-proxy_</span><span class="si">${</span><span class="nv">VERSION</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">OS</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">ARCH</span><span class="si">}</span><span class="s2">.tar.gz&#34;</span>
</span></span><span class="line"><span class="cl">gh attestation verify <span class="s2">&#34;gocloak-auth-proxy_</span><span class="si">${</span><span class="nv">VERSION</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">OS</span><span class="si">}</span><span class="s2">_</span><span class="si">${</span><span class="nv">ARCH</span><span class="si">}</span><span class="s2">.tar.gz&#34;</span> -R Nerzal/gocloak-auth-proxy
</span></span></code></pre></div>
</div>

<div class="os-tab" data-label="Windows (PowerShell)">
<p class="os-tab-label">Windows (PowerShell)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="n">docker</span> <span class="n">run</span> <span class="p">-</span><span class="n">-rm</span> <span class="n">-p</span> <span class="mf">8080</span><span class="err">:</span><span class="mf">8080</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">-e</span> <span class="n">ISSUER</span><span class="p">=</span><span class="n">https</span><span class="err">:</span><span class="p">//</span><span class="n">keycloak</span><span class="p">.</span><span class="py">example</span><span class="p">.</span><span class="n">com</span><span class="p">/</span><span class="n">realms</span><span class="p">/</span><span class="n">myrealm</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">-e</span> <span class="n">AUDIENCE</span><span class="p">=</span><span class="nb">my-api</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">-e</span> <span class="n">REQUIRED_REALM_ROLES</span><span class="p">=</span><span class="n">reader</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">ghcr</span><span class="p">.</span><span class="n">io</span><span class="p">/</span><span class="n">nerzal</span><span class="p">/</span><span class="nb">gocloak-auth</span><span class="n">-proxy:v0</span><span class="p">.</span><span class="py">2</span><span class="p">.</span><span class="py">0</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c"># or build it with Go 1.27+</span>
</span></span><span class="line"><span class="cl"><span class="n">go</span> <span class="n">install</span> <span class="n">github</span><span class="p">.</span><span class="n">com</span><span class="p">/</span><span class="n">Nerzal</span><span class="p">/</span><span class="nb">gocloak-auth</span><span class="n">-proxy</span><span class="p">/</span><span class="n">cmd</span><span class="p">/</span><span class="nb">gocloak-auth</span><span class="n">-proxy</span><span class="nv">@latest</span>
</span></span></code></pre></div>
</div>

</div>

<p>Without <code>UPSTREAM_URL</code> the proxy runs in forward-auth mode on <code>/auth</code>. <code>/healthz</code> and <code>/readyz</code> are public, and Prometheus metrics go to a separate port if you set <code>METRICS_ADDR</code>. Docker is at <a href="https://docs.docker.com/get-started/get-docker/" rel="noopener">docs.docker.com</a>, the GitHub CLI for <code>gh attestation</code> at <a href="https://cli.github.com/" rel="noopener">cli.github.com</a>.</p>
<p>Test it with a token from Keycloak:</p>
<div class="os-tabs">
<div class="os-tab" data-label="Linux / macOS">
<p class="os-tab-label">Linux / macOS</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">TOKEN</span><span class="o">=</span><span class="k">$(</span>curl -s https://keycloak.example.com/realms/myrealm/protocol/openid-connect/token <span class="se">\
</span></span></span><span class="line"><span class="cl">  -d <span class="nv">grant_type</span><span class="o">=</span>client_credentials -d <span class="nv">client_id</span><span class="o">=</span>my-client -d <span class="nv">client_secret</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$CLIENT_SECRET</span><span class="s2">&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">  <span class="p">|</span> jq -r .access_token<span class="k">)</span>
</span></span><span class="line"><span class="cl">curl -i -H <span class="s2">&#34;Authorization: Bearer </span><span class="nv">$TOKEN</span><span class="s2">&#34;</span> http://localhost:8080/auth
</span></span></code></pre></div>
</div>

<div class="os-tab" data-label="Windows (PowerShell)">
<p class="os-tab-label">Windows (PowerShell)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nv">$token</span> <span class="p">=</span> <span class="p">(</span><span class="nb">Invoke-RestMethod</span> <span class="n">-Method</span> <span class="n">Post</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">-Uri</span> <span class="n">https</span><span class="err">:</span><span class="p">//</span><span class="n">keycloak</span><span class="p">.</span><span class="py">example</span><span class="p">.</span><span class="n">com</span><span class="p">/</span><span class="n">realms</span><span class="p">/</span><span class="n">myrealm</span><span class="p">/</span><span class="n">protocol</span><span class="p">/</span><span class="nb">openid-connect</span><span class="p">/</span><span class="n">token</span> <span class="p">`</span>
</span></span><span class="line"><span class="cl">  <span class="n">-Body</span> <span class="vm">@</span><span class="p">{</span> <span class="n">grant_type</span> <span class="p">=</span> <span class="s2">&#34;client_credentials&#34;</span><span class="p">;</span> <span class="n">client_id</span> <span class="p">=</span> <span class="s2">&#34;my-client&#34;</span><span class="p">;</span> <span class="n">client_secret</span> <span class="p">=</span> <span class="nv">$env:CLIENT_SECRET</span> <span class="p">}).</span><span class="py">access_token</span>
</span></span><span class="line"><span class="cl"><span class="nb">Invoke-WebRequest</span> <span class="n">-Uri</span> <span class="n">http</span><span class="err">:</span><span class="p">//</span><span class="n">localhost</span><span class="err">:</span><span class="mf">8080</span><span class="p">/</span><span class="n">auth</span> <span class="n">-Headers</span> <span class="vm">@</span><span class="p">{</span> <span class="n">Authorization</span> <span class="p">=</span> <span class="s2">&#34;Bearer </span><span class="nv">$token</span><span class="s2">&#34;</span> <span class="p">}</span> <span class="p">|</span>
</span></span><span class="line"><span class="cl">  <span class="nb">Select-Object</span> <span class="n">StatusCode</span><span class="p">,</span> <span class="n">Headers</span>
</span></span></code></pre></div>
</div>

</div>

<p>A 200 with <code>X-Auth-Request-User</code>, <code>-Email</code>, <code>-Groups</code> and <code>-Roles</code> headers means it works. A 401 means the token was rejected; the JSON log on stderr says why, without logging the token.</p>
<h3 id="3-traefik">3. Traefik</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">http</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">middlewares</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">keycloak-auth</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">forwardAuth</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">address</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;http://auth-proxy:8080/auth&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">trustForwardHeader</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">authResponseHeaders</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="s2">&#34;X-Auth-Request-User&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="s2">&#34;X-Auth-Request-Email&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="s2">&#34;X-Auth-Request-Groups&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="s2">&#34;X-Auth-Request-Roles&#34;</span><span class="w">
</span></span></span></code></pre></div><p><code>trustForwardHeader: false</code> matters. With <code>true</code>, Traefik passes the client&rsquo;s own <code>X-Forwarded-*</code> headers to the auth service. The proxy strips them anyway, but there is no reason to send them.</p>
<h3 id="4-nginx">4. nginx</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-nginx" data-lang="nginx"><span class="line"><span class="cl"><span class="k">location</span> <span class="p">=</span> <span class="s">/_auth</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kn">internal</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_pass</span> <span class="s">http://auth_proxy/auth</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_pass_request_body</span> <span class="no">off</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">Content-Length</span> <span class="s">&#34;&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">Authorization</span> <span class="nv">$http_authorization</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">location</span> <span class="s">/</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kn">auth_request</span> <span class="s">/_auth</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">auth_request_set</span> <span class="nv">$auth_user</span> <span class="nv">$upstream_http_x_auth_request_user</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">auth_request_set</span> <span class="nv">$auth_roles</span> <span class="nv">$upstream_http_x_auth_request_roles</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1"># do not forward client headers, including spoofed identity headers
</span></span></span><span class="line"><span class="cl">    <span class="kn">proxy_pass_request_headers</span> <span class="no">off</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">Content-Type</span> <span class="nv">$http_content_type</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">Accept</span> <span class="nv">$http_accept</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">X-Auth-Request-User</span> <span class="nv">$auth_user</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">X-Auth-Request-Roles</span> <span class="nv">$auth_roles</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_set_header</span> <span class="s">Authorization</span> <span class="s">&#34;&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kn">proxy_pass</span> <span class="s">http://application</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p><code>proxy_pass_request_headers off</code> looks drastic, but it is the only reliable way to make sure the backend never sees an <code>X_Auth_Request_User</code> the client made up. Add back the headers your app needs explicitly.</p>
<h3 id="5-route-rules-step-up-and-sso">5. Route rules, step-up and SSO</h3>
<p>In reverse-proxy mode, <code>ROUTE_RULES</code> sets requirements per method and path. This requires the <code>admin</code> realm role, authentication level <code>aal2</code> and an MFA login for every <code>POST</code> under <code>/admin</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">ROUTE_RULES</span><span class="o">=</span><span class="s1">&#39;POST:/admin/*=realm:admin,acr:aal2,amr:mfa&#39;</span>
</span></span></code></pre></div><p>For browser SSO set <code>OIDC_ENABLED=true</code>, <code>CLIENT_ID</code>, <code>CLIENT_SECRET</code>, <code>OIDC_REDIRECT_URL</code> and a cookie secret of at least 32 characters:</p>
<div class="os-tabs">
<div class="os-tab" data-label="Linux / macOS">
<p class="os-tab-label">Linux / macOS</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">OIDC_COOKIE_SECRET</span><span class="o">=</span><span class="s2">&#34;</span><span class="k">$(</span>openssl rand -base64 32<span class="k">)</span><span class="s2">&#34;</span>
</span></span></code></pre></div>
</div>

<div class="os-tab" data-label="Windows (PowerShell)">
<p class="os-tab-label">Windows (PowerShell)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nv">$bytes</span> <span class="p">=</span> <span class="p">[</span><span class="no">byte[]</span><span class="p">]::</span><span class="n">new</span><span class="p">(</span><span class="mf">32</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">[</span><span class="no">System.Security.Cryptography.RandomNumberGenerator</span><span class="p">]::</span><span class="n">Fill</span><span class="p">(</span><span class="nv">$bytes</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nv">$env:OIDC_COOKIE_SECRET</span> <span class="p">=</span> <span class="p">[</span><span class="no">Convert</span><span class="p">]::</span><span class="n">ToBase64String</span><span class="p">(</span><span class="nv">$bytes</span><span class="p">)</span>
</span></span></code></pre></div>
</div>

</div>

<p>The session lives in AES-256-GCM encrypted cookies, split into chunks because Keycloak tokens with many roles easily exceed the 4 KB cookie limit. The default cookie name has the <code>__Host-</code> prefix, which browsers only accept with <code>Secure</code>, without <code>Domain</code> and with <code>Path=/</code>. Refresh tokens are rotated once even when several tabs ask at the same moment, <code>/oauth2/logout</code> is CSRF protected, and Keycloak can end sessions through back-channel logout. The proxy strips its own cookies before forwarding, so the upstream never sees the session.</p>
<p>If you run Keycloak behind your own CA or with mutual TLS, <code>KEYCLOAK_CA_FILE</code> and <code>KEYCLOAK_CLIENT_CERT_FILE</code>/<code>KEYCLOAK_CLIENT_KEY_FILE</code> cover that. In my <span class="link-upcoming">Neuroclip post about Kubernetes behind Keycloak</span> I describe the same pattern from the application side: the app knows nothing about OIDC, and the proxy in front handles it.</p>
<h2 id="which-one-should-you-use">Which one should you use?</h2>
<ul>
<li>Several identity providers, or GitHub and Google login next to Keycloak: oauth2-proxy.</li>
<li>Identity-aware access for a whole company, with device posture and a policy language: Pomerium.</li>
<li>Keycloak only, and you want OPA policies, Redis-backed sessions and a project with years of history: gogatekeeper.</li>
<li>Keycloak only, forward-auth in Traefik or nginx with the lowest latency and a small dependency tree, and you are fine with v0.2.0: gocloak-auth-proxy.</li>
</ul>
<p>Reports from real traffic, and especially about where it breaks, are welcome as issues or private security reports in the <a href="https://github.com/Nerzal/gocloak-auth-proxy" rel="noopener">repository</a>.</p>
]]></content:encoded></item></channel></rss>