<?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/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>de</language><copyright>© 2026 Tobias Theel</copyright><lastBuildDate>Sat, 21 Nov 2026 11:30:00 +0100</lastBuildDate><atom:link href="https://blog.noobygames.de/de/tags/oauth2-proxy/index.xml" rel="self" type="application/rss+xml"/><item><title>gocloak-auth-proxy: ein schneller, Keycloak-nativer Auth-Proxy in Go</title><link>https://blog.noobygames.de/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/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 prüft Keycloak-JWTs lokal für Forward-Auth in Traefik und nginx, als Reverse-Proxy oder mit OIDC-Browser-SSO. Benchmarks gegen oauth2-proxy, gogatekeeper und Referenz-Proxys in Rust, Java und Python, das Sicherheitsmodell und eine Anleitung zur Einrichtung.</description><content:encoded><![CDATA[<blockquote>
<p><strong>Serie: Das gocloak-Universum.</strong> <a href="/de/blog/gocloak-mcp-first-mcp-server-homebrew/">Teil 1: gocloak-mcp</a> · Teil 2: gocloak-auth-proxy (du bist hier) · <a href="/de/blog/gocloak-performance-json-v2-http-clients/">Teil 3: gocloak-Performance</a> · <a href="/de/blog/gocloak-performance-final-benchmarks/">Teil 4: die finalen Benchmark-Zahlen</a> · <a href="/de/blog/gocloak-pprof-profiling/">Teil 5: pprof</a></p>
</blockquote>
<aside class="tldr" aria-label="Das Wichtigste in Kürze">
<p class="tldr-title">TL;DR</p>
<ul>
<li>gocloak-auth-proxy ist ein Authentifizierungs-Proxy nur für Keycloak, in Go auf Basis von gocloak v15, und läuft als Forward-Auth für Traefik und nginx, als Reverse-Proxy oder mit OIDC-Browser-SSO.</li>
<li>Die JWT-Prüfung in gocloak-auth-proxy läuft gegen einen JWKS-Snapshot im Speicher, deshalb wartet ein gültiger Request nie aufs Netzwerk.</li>
<li>Durch kommen nur asymmetrische Algorithmen (Standard <code>RS256,ES256</code>): Der Proxy lehnt <code>none</code> und <code>HS*</code> ab und entfernt Identity-Header vom Client in beiden Schreibweisen, mit Bindestrich und mit Unterstrich.</li>
<li>Im projekteigenen Benchmark beantwortete gocloak-auth-proxy v0.2.0 mit vollständiger Signaturprüfung bei jedem Request 15.002 Forward-Auth-Requests pro Sekunde, 2,5-mal so viele wie oauth2-proxy v7.15.6.</li>
<li>Eine Rust-Referenz (21.616 Requests pro Sekunde) ist schneller als gocloak-auth-proxy ohne Token-Cache; mit dem Cache aus v0.2.0 kommt gocloak-auth-proxy auf 25.169.</li>
</ul>

</aside>

<p><strong>Update, 21. November 2026:</strong> gocloak-auth-proxy v0.2.0 ist erschienen, mit schnellerem Token-Verifier, einem Cache für geprüfte Tokens und einer Benchmark-Suite im Repository. Ich habe seine Zahlen und Charts in den Benchmark-Teil aufgenommen und die Installationsbefehle aktualisiert; die Zahlen von v0.1.0 bleiben zum Vergleich stehen.</p>
<p>In <a href="/de/blog/gocloak-mcp-first-mcp-server-homebrew/">Teil 1</a> habe ich über gocloak v15 und den darauf aufbauenden Keycloak MCP-Server geschrieben. Zur selben Zeit ging ein zweites Projekt online: <a href="https://github.com/Nerzal/gocloak-auth-proxy" rel="noopener">gocloak-auth-proxy</a>, ein Authentifizierungs-Proxy, der vor deinen Services sitzt und nur Requests mit gültigem Keycloak-Token durchlässt.</p>
<p>Er läuft in drei Modi:</p>
<ul>
<li>Forward-Auth: Traefiks <code>forwardAuth</code> oder nginx&rsquo; <code>auth_request</code> fragen den Proxy unter <code>/auth</code>, ob ein Request durchdarf. Der Proxy antwortet mit 200 und Identity-Headern oder mit 401/403.</li>
<li>Reverse-Proxy: Der Proxy leitet geprüfte Requests selbst an einen Upstream weiter.</li>
<li>Browser-SSO: OIDC Authorization Code Flow mit PKCE, verschlüsseltem Session-Cookie und Logout, für Web-Apps, die sich nicht selbst um OIDC kümmern sollen.</li>
</ul>
<p>Im projekteigenen Benchmark beantwortete v0.2.0 mit vollständiger Signaturprüfung bei jedem Request 15.002 Forward-Auth-Requests pro Sekunde, 2,5-mal so viele wie oauth2-proxy auf derselben Maschine, und mit Token-Cache 25.169. Unten findest du diese Zahlen samt Einschränkungen, meinen eigenen Micro-Benchmark-Lauf, das Sicherheitsmodell und ein Setup für Traefik und nginx.</p>
<p>v0.1.0 ist am 2026-11-21 erschienen, v0.2.0 am Nachmittag desselben Tages. Niemand hat den Code bisher auditiert, und die meisten Alternativen weiter unten haben Jahre im Produktionseinsatz hinter sich.</p>
<h2 id="warum-noch-ein-proxy-für-keycloak">Warum noch ein Proxy für Keycloak</h2>
<p>Keycloak hatte früher einen eigenen Proxy, Keycloak Gatekeeper, später in Louketo umbenannt. Im August 2020 <a href="https://www.keycloak.org/2020/08/sunsetting-louketo-project.adoc" rel="noopener">kündigte das Keycloak-Team das Ende an</a> und empfahl stattdessen <a href="https://github.com/oauth2-proxy/oauth2-proxy" rel="noopener">oauth2-proxy</a>. Das letzte Release ist 1.0.0 vom Juli 2020, das <a href="https://github.com/louketo/louketo-proxy" rel="noopener">Repository</a> ist archiviert. Seitdem setzen die meisten Keycloak-Setups auf eines dieser Projekte:</p>
<table>
	<thead>
			<tr>
					<th>Projekt</th>
					<th>Sprache</th>
					<th style="text-align: right">Stars</th>
					<th>Letztes Release</th>
					<th>Keycloak-Fokus</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>Generisch, mit den Providern <code>keycloak</code> und <code>keycloak_oidc</code></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>Generisches OIDC, vollständiger 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>Generisch; JWKS-URLs werden von Hand konfiguriert</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>Generisch, 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>Generisches 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>Ja, der Community-Fork von Keycloak Gatekeeper</td>
			</tr>
			<tr>
					<td><strong>gocloak-auth-proxy</strong></td>
					<td>Go</td>
					<td style="text-align: right">neu</td>
					<td>v0.2.0 (2026-11-21)</td>
					<td>Ja, gebaut auf gocloak v15</td>
			</tr>
	</tbody>
</table>
<p>(Stars mit Stand 2026-11-21.)</p>
<p>oauth2-proxy ist die Standardantwort, und zwar eine gute. Er unterstützt ein Dutzend Identity Provider, was eben auch heißt, dass Konfiguration und Code ein Dutzend Identity Provider abdecken müssen. gogatekeeper ist der nächste Verwandte: Keycloak-spezifisch, mit Forward-Auth, PKCE, Rollen- und Gruppenregeln, UMA (laut <a href="https://gogatekeeper.github.io/gatekeeper/" rel="noopener">Doku</a> nicht produktionsreif) und OPA. gocloak-auth-proxy ist schmaler als beide: nur Keycloak, wenige Dependencies und ein Hot Path, auf dem ein gültiger Request nie aufs Netzwerk wartet.</p>
<h2 id="wie-ein-request-geprüft-wird">Wie ein Request geprüft wird</h2>
<p>Bei einem Request mit <code>Authorization: Bearer &lt;JWT&gt;</code> durchläuft der Proxy diese Schritte:</p>
<ol>
<li>Alle Identity-Header entfernen, die der Client mitgeschickt hat: <code>X-Auth-Request-*</code>, <code>X-Forwarded-*</code>, <code>Forwarded</code>, <code>X-Real-IP</code>, <code>Authorization</code> nach dem Auslesen, konfigurierte eigene Header und jeweils ihre Varianten mit <code>_</code> (mehr dazu weiter unten).</li>
<li>Den Pfad normalisieren und die ACL anwenden: öffentliche Pfade, Pfade mit optionaler Authentifizierung, CORS-Preflight, Route-Regeln. Kodierte Traversal-Sequenzen (<code>..</code>, <code>%2f</code>, <code>%2e</code>, <code>%5c</code>, <code>%23</code>, <code>%00</code>) werden abgelehnt.</li>
<li>Den JWT-Header parsen und den Algorithmus gegen die Allowlist prüfen.</li>
<li>Den Signaturschlüssel per <code>kid</code> in einem JWKS-Snapshot im Speicher nachschlagen und die Signatur prüfen.</li>
<li><code>iss</code>, <code>aud</code>, <code>exp</code>, <code>nbf</code>, <code>typ</code> (nur <code>Bearer</code>) und die Autorisierungsregeln prüfen: Realm- und Client-Rollen, Gruppen, Scopes, <code>acr</code>, <code>amr</code>, <code>email_verified</code>, <code>azp</code>, Keycloak-Organisation.</li>
<li>Optional den Token-Introspection- oder UMA-Endpoint von Keycloak aufrufen.</li>
<li>Mit Identity-Headern antworten (Forward-Auth) oder den Request an den Upstream weiterleiten (Reverse-Proxy).</li>
</ol>
<p>Die Schritte 1 bis 5 brauchen keinen Netzwerkaufruf. Der JWKS-Snapshot liegt hinter einem Go-<code>atomic.Pointer</code>, Leser nehmen also nie einen Lock. Kommt ein Token mit unbekannter <code>kid</code> an, zum Beispiel direkt nach einer Schlüsselrotation in Keycloak, lädt der Proxy das Key Set genau einmal neu, egal wie viele Requests darauf warten. Dazu gibt es ein Rate Limit und exponentielles Backoff, damit eine Flut gefälschter <code>kid</code>-Werte nicht zu einer Flut von Requests gegen Keycloak wird. Ein Snapshot läuft nach dem doppelten Refresh-Intervall ab (begrenzt auf eine Minute bis 24 Stunden). Danach liefert <code>/readyz</code> 503 und Kubernetes schickt keinen Traffic mehr, statt dass der Proxy endlos Tokens gegen veraltete Schlüssel akzeptiert.</p>
<h3 id="welche-tokens-er-ablehnt">Welche Tokens er ablehnt</h3>
<p>Die Algorithmus-Allowlist steht standardmäßig auf <code>RS256,ES256</code> und akzeptiert nur asymmetrische Algorithmen: RS256/384/512, PS256/384/512, ES256/384/512 und EdDSA mit Ed25519. <code>none</code> und die HMAC-Familie <code>HS*</code> werden sowohl beim Konfigurieren als auch beim Prüfen abgelehnt. Damit ist der klassische Algorithm-Confusion-Angriff erledigt, bei dem ein mit HS256 signiertes Token, das den öffentlichen RSA-Schlüssel als HMAC-Secret nutzt, einen naiven Verifier passiert. Außerdem lehnt der Proxy Tokens mit den Headern <code>crit</code>, <code>b64</code>, <code>jku</code>, <code>x5u</code> oder <code>jwk</code> ab, weil ein Token damit dem Verifier vorschreiben kann, wo er seinen Schlüssel holen soll. <a href="https://www.rfc-editor.org/rfc/rfc8725" rel="noopener">RFC 8725</a>, die JWT Best Current Practices, empfiehlt beides.</p>
<p>Die relevanten Standards, falls du tiefer einsteigen willst: <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> und Keycloaks <a href="https://www.keycloak.org/docs/latest/authorization_services/index.html#_service_uma_authorization_process" rel="noopener">UMA-2.0-Doku</a>.</p>
<h3 id="verwirrung-um-header-und-pfade">Verwirrung um Header und Pfade</h3>
<p>Um die Kryptografie kümmert sich die JWT-Library. Die meisten Schwachstellen in Auth-Proxys entstehen woanders: Proxy und Backend sind sich über einen Header oder einen Pfad uneinig. Die Advisory-Liste von oauth2-proxy aus den letzten 16 Monaten ist ein guter Katalog dessen, was schiefgehen kann:</p>
<table>
	<thead>
			<tr>
					<th>Advisory</th>
					<th>Schweregrad</th>
					<th>Was passiert ist</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> wurde gegen die URL samt Query-String abgeglichen</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-Header mit Unterstrichen gingen durch, und WSGI/CGI-Backends lasen sie als Variante mit Bindestrich</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>Authentifizierung umgangen über einen gefälschten <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-Verwechslung in <code>skip_auth_routes</code> und <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>Ein Healthcheck-Abgleich auf den User-Agent übersprang im <code>auth_request</code>-Modus die Authentifizierung</td>
			</tr>
	</tbody>
</table>
<p>Alle sind in aktuellen Releases behoben. Ich liste sie auf, weil gocloak-auth-proxy genau für diese Fälle Tests hat: Er entfernt Identity-Header in beiden Schreibweisen (<code>X-Auth-Request-User</code> und <code>X_Auth_Request_User</code>), vertraut <code>X-Forwarded-*</code> vom Client nie, lehnt kodierte Trennzeichen und Fragmente in Pfaden ab und kennt keine Ausnahmen anhand des User-Agents. Tests decken nur die Fälle ab, an die jemand gedacht hat. Die Bugs in oauth2-proxy wurden gefunden, weil viele ihn betreiben und Forscher ihn untersuchen. gocloak-auth-proxy hatte diese Prüfung noch nicht, Reviews sind willkommen.</p>
<h2 id="benchmarks">Benchmarks</h2>
<h3 id="direkter-vergleich-v020">Direkter Vergleich (v0.2.0)</h3>
<p>v0.2.0 bringt seine Benchmark-Suite ins Repository: <code>make bench-compare</code> startet <a href="https://github.com/Nerzal/gocloak-auth-proxy/blob/main/bench/run_compare.sh" rel="noopener"><code>bench/run_compare.sh</code></a>. Alle Proxys liefen auf demselben Linux-Host (Intel Xeon 6985P-C) mit <code>GOMAXPROCS=2</code>, 32 gleichzeitigen Verbindungen über 5 Sekunden, einem 2048-Bit-RS256-Bearer-Token, einem lokalen Mock-JWKS und einem lokalen Mock-Upstream, mit null Fehlern in jedem Lauf.</p>
<p>Zum Lesen der Zahlen sind zwei Token-Modi wichtig. &ldquo;Warm&rdquo; schickt bei jedem Request dasselbe Token. &ldquo;Cold&rdquo; rotiert durch 2.000 verschiedene Tokens, und bei gocloak-auth-proxy ist der Token-Cache abgeschaltet (<code>TOKEN_CACHE_SIZE=0</code>), jeder Request bezahlt also eine vollständige Signaturprüfung.</p>
<p><img src="/img/gocloak/auth-proxy/benchmark-overview.svg" alt="Durchsatz, Latenz und Speicher von gocloak-auth-proxy v0.2.0 gegen oauth2-proxy und Referenz-Proxys in Rust, Java und 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">Speicher unter Last</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 bis 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 bis 16,2 MB</td>
			</tr>
			<tr>
					<td>Rust-Referenz (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 bis 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 bis 27,8 MB</td>
			</tr>
			<tr>
					<td>Java-17-Referenz (<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 bis 114,4 MB</td>
			</tr>
			<tr>
					<td>Python-3.11-Referenz (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-Prozess)</td>
			</tr>
	</tbody>
</table>
<p>Die Proxys in Rust, Java und Python sind keine Produkte. Es sind kleine Referenzimplementierungen in <a href="https://github.com/Nerzal/gocloak-auth-proxy/tree/main/bench/competitors" rel="noopener"><code>bench/competitors/</code></a>, geschrieben für diesen Benchmark, um zu zeigen, was jede Runtime mit derselben Aufgabe macht. oauth2-proxy ist das einzige etablierte Produkt im Lauf; gogatekeeper ist nicht mehr dabei.</p>
<p>Fair verglichen, mit vollständiger Signaturprüfung bei jedem Request, schafft gocloak-auth-proxy im Forward-Auth-Modus den 2,5-fachen Durchsatz von oauth2-proxy und im Reverse-Proxy-Modus den 3,4-fachen. Mit Cache sind es das 4,2- und 5-Fache. Die Rust-Referenz ist schneller als gocloak-auth-proxy ohne Cache (21.616 gegen 15.002 Forward-Auth-Requests pro Sekunde) und braucht ein Viertel des Speichers; erst mit Cache liegt der Go-Proxy vorn. Das &ldquo;#1&rdquo;-Badge im Chart zählt den Warm-Lauf. Das beschreibt Produktion ganz gut, wo ein Client dasselbe Token bis zum Ablauf schickt, ist aber nicht dieselbe Last wie die Cold-Läufe der Referenz-Proxys.</p>
<h3 id="was-v020-geändert-hat">Was v0.2.0 geändert hat</h3>
<p><img src="/img/gocloak/auth-proxy/pprof-progression.svg" alt="Bytes und Allokationen pro Token-Prüfung von v0.1.0 bis v0.2.0"></p>
<p>Zwei Änderungen kamen aus dem Profiling. Der JWS-Verifier liest die Claims jetzt mit dem Streaming-Paket <code>jsontext</code> aus <code>encoding/json/v2</code>, statt sie in Maps zu dekodieren. Bei einer kalten RS256-Prüfung sinken die Bytes damit von 6.412 auf 1.728 (-73 %) und die Allokationen von 79 auf 33 (-58 %); bei EdDSA mit eigenen Claim-Headern von 8.540 auf 624 Bytes (-93 %) und von 104 auf 24 Allokationen (-77 %). Die zweite Änderung ist ein geshardeter Cache geprüfter Tokens, mit dem SHA-256 des Tokens als Schlüssel. Ein Treffer dauert 463 ns und allokiert nichts. Er prüft bei jedem Treffer weiterhin <code>exp</code> und <code>nbf</code> und ob der Signaturschlüssel noch im aktuellen JWKS-Snapshot steht. Eine Schlüsselrotation in Keycloak macht also auch gecachte Tokens ungültig. Das Lesen eines OIDC-Session-Cookies wurde zusätzlich 40 % schneller.</p>
<p>Die Micro-Benchmarks aus dem README von v0.2.0 (Go 1.27.4, gocloak v15.1.0, Intel Xeon 6985P-C):</p>
<table>
	<thead>
			<tr>
					<th>Benchmark</th>
					<th style="text-align: right">Zeit pro Op</th>
					<th style="text-align: right">Bytes pro Op</th>
					<th style="text-align: right">Allocs pro 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>Sie stammen von einer anderen Maschine als mein Lauf mit v0.1.0 weiter unten, vergleiche also die Allokationsspalten, nicht die Zeiten.</p>
<h3 id="der-vergleich-von-v010">Der Vergleich von v0.1.0</h3>
<p>Der Vergleich für v0.1.0 stammt aus dem README des Projekts. Alle drei Proxys liefen auf demselben Linux-Host (Intel Xeon 6985P-C) mit <code>GOMAXPROCS=2</code>, gegen ein lokales Mock-Keycloak-JWKS und einen lokalen Mock-Upstream, mit einem 2048-Bit-RS256-Bearer-Token bei jedem Request, 32 gleichzeitigen Verbindungen, abgeschaltetem Access-Log und null Fehlern in jedem Lauf. Die Lastskripte und die Konfigurationen der Konkurrenz lagen damals noch nicht im Repository, eins zu eins nachstellen lässt sich dieser Lauf also nicht.</p>










<figure class="diagram">
    <div class="diagram-canvas" role="img" aria-label="Forward-Auth ist der Modus, den Traefik und nginx nutzen. Dort ist der Abstand am größten, weil der Proxy nur prüft und antwortet; im Reverse-Proxy-Modus leitet er zusätzlich den Request-Body an den Upstream weiter.">
        <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 ist der Modus, den Traefik und nginx nutzen. Dort ist der Abstand am größten, weil der Proxy nur prüft und antwortet; im Reverse-Proxy-Modus leitet er zusätzlich den Request-Body an den Upstream weiter.</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-Größe und Speicherverbrauch im Leerlauf aus dem README. Dependencies sind die require-Einträge in der go.mod jedes Projekts am Release-Tag, direkt und indirekt.">
        <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-Größe und Speicherverbrauch im Leerlauf aus dem README. Dependencies sind die require-Einträge in der go.mod jedes Projekts am Release-Tag, direkt und indirekt.</figcaption>
</figure>

<p>Am Tag v0.1.0 hat die go.mod von gocloak-auth-proxy 12 require-Einträge (2 direkte, 10 indirekte; das README nennt noch 8). gogatekeeper 5.1.0 hat 104, oauth2-proxy v7.15.6 hat 73. Weniger Dependencies machen Code nicht korrekt, aber jedes Modul ist Code, dem du vertrauen und den du aktualisieren musst.</p>
<p>Im Forward-Auth-Modus kostet ein gültiger Request eine Signaturprüfung und ein paar Map-Lookups, wie die Micro-Benchmarks unten zeigen. Ein Benchmark gegen Mock-Services misst nur den Proxy; in Produktion dominiert meist der Upstream.</p>
<h3 id="micro-benchmarks-auf-meiner-maschine">Micro-Benchmarks auf meiner Maschine</h3>
<p>Ich habe <code>make bench</code> für v0.1.0 auf meinem Rechner laufen lassen (AMD Ryzen AI 9 HX 370, Windows 11, Go 1.26.2):</p>
<table>
	<thead>
			<tr>
					<th>Benchmark</th>
					<th style="text-align: right">Zeit pro Op</th>
					<th style="text-align: right">Bytes pro Op</th>
					<th style="text-align: right">Allocs pro Op</th>
					<th>Was er abdeckt</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>Treffer im Introspection-Cache, SHA-256 als Schlüssel</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>Pfadnormalisierung, ACL, Rollenprüfung pro Route</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-Bereinigung und Identity-Header (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>Entschlüsseln und Dekodieren des AES-256-GCM-Session-Cookies</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>Vollständige RS256-Prüfung mit Claims und Rollen</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>Dasselbe mit 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 eigene Claim-Header</td>
			</tr>
	</tbody>
</table>
<p>Die Zahlen im README von v0.1.0 stammen von Go 1.27 unter Linux und zeigen weniger Allokationen (zum Beispiel 79 für RS256), die Toolchain spielt hier also eine Rolle; vergleiche Läufe nur auf demselben Setup. Das Bild ist auf beiden gleich: Die Signaturprüfung macht etwa 95 Prozent der CPU-Zeit eines Requests aus, alles drumherum fällt kaum ins Gewicht. RS256 ist der am schnellsten zu prüfende Algorithmus, was praktisch ist, weil er der Standard in Keycloak ist.</p>
<h2 id="einrichtung">Einrichtung</h2>
<h3 id="1-ein-client-in-keycloak">1. Ein Client in Keycloak</h3>
<p>Für Forward-Auth und den Reverse-Proxy-Modus braucht der Proxy nur die Issuer-URL und eine Audience, ein Client-Secret ist nicht nötig. Keycloak schreibt standardmäßig <code>account</code> in <code>aud</code>. Füge deshalb einem Client-Scope der Clients, die deine API aufrufen, einen <em>Audience</em>-Mapper hinzu, mit dem Namen deiner API (zum Beispiel <code>my-api</code>) als Included Audience.</p>
<p>Für Browser-SSO legst du einen Confidential Client mit aktiviertem Standard Flow und <code>https://app.example.com/oauth2/callback</code> als Redirect-URI an.</p>
<h3 id="2-starten">2. Starten</h3>
<p>Es gibt statische Binaries für Linux und macOS (amd64 und arm64) mit Checksummen und GitHub Build Provenance sowie ein Multi-Arch-Distroless-Image auf GHCR. Unter Windows nimmst du den Container oder <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"># oder das Binary, mit Provenance-Prüfung</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"># oder mit Go 1.27+ bauen</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>Ohne <code>UPSTREAM_URL</code> läuft der Proxy im Forward-Auth-Modus unter <code>/auth</code>. <code>/healthz</code> und <code>/readyz</code> sind öffentlich, und Prometheus-Metriken landen auf einem eigenen Port, wenn du <code>METRICS_ADDR</code> setzt. Docker bekommst du unter <a href="https://docs.docker.com/get-started/get-docker/" rel="noopener">docs.docker.com</a>, die GitHub CLI für <code>gh attestation</code> unter <a href="https://cli.github.com/" rel="noopener">cli.github.com</a>.</p>
<p>Teste ihn mit einem Token aus 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>Eine 200 mit den Headern <code>X-Auth-Request-User</code>, <code>-Email</code>, <code>-Groups</code> und <code>-Roles</code> heißt, dass es funktioniert. Eine 401 heißt, dass das Token abgelehnt wurde; das JSON-Log auf stderr sagt dir warum, ohne das Token zu loggen.</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> ist wichtig. Mit <code>true</code> reicht Traefik die <code>X-Forwarded-*</code>-Header des Clients an den Auth-Service durch. Der Proxy entfernt sie zwar ohnehin, aber es gibt keinen Grund, sie mitzuschicken.</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"># keine Client-Header weiterleiten, auch keine gefälschten Identity-Header
</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> wirkt drastisch, ist aber der einzige verlässliche Weg, damit das Backend nie ein vom Client erfundenes <code>X_Auth_Request_User</code> zu sehen bekommt. Die Header, die deine App braucht, fügst du explizit wieder hinzu.</p>
<h3 id="5-route-regeln-step-up-und-sso">5. Route-Regeln, Step-up und SSO</h3>
<p>Im Reverse-Proxy-Modus legt <code>ROUTE_RULES</code> Anforderungen pro Methode und Pfad fest. Diese Regel verlangt für jeden <code>POST</code> unter <code>/admin</code> die Realm-Rolle <code>admin</code>, das Authentifizierungslevel <code>aal2</code> und einen MFA-Login:</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>Für Browser-SSO setzt du <code>OIDC_ENABLED=true</code>, <code>CLIENT_ID</code>, <code>CLIENT_SECRET</code>, <code>OIDC_REDIRECT_URL</code> und ein Cookie-Secret mit mindestens 32 Zeichen:</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>Die Session liegt in AES-256-GCM-verschlüsselten Cookies, aufgeteilt in Chunks, weil Keycloak-Tokens mit vielen Rollen das Cookie-Limit von 4 KB schnell überschreiten. Der Standardname des Cookies hat das Präfix <code>__Host-</code>, das Browser nur mit <code>Secure</code>, ohne <code>Domain</code> und mit <code>Path=/</code> akzeptieren. Refresh-Tokens werden genau einmal rotiert, auch wenn mehrere Tabs gleichzeitig anfragen, <code>/oauth2/logout</code> ist gegen CSRF geschützt, und Keycloak kann Sessions per Back-Channel-Logout beenden. Der Proxy entfernt seine eigenen Cookies vor dem Weiterleiten, der Upstream sieht die Session also nie.</p>
<p>Wenn du Keycloak hinter einer eigenen CA oder mit Mutual TLS betreibst, decken <code>KEYCLOAK_CA_FILE</code> und <code>KEYCLOAK_CLIENT_CERT_FILE</code>/<code>KEYCLOAK_CLIENT_KEY_FILE</code> das ab. In meinem <span class="link-upcoming">Neuroclip-Artikel über Kubernetes hinter Keycloak</span> beschreibe ich dasselbe Muster aus Sicht der Anwendung: Die App weiß nichts von OIDC, das übernimmt der Proxy davor.</p>
<h2 id="welchen-solltest-du-nehmen">Welchen solltest du nehmen?</h2>
<ul>
<li>Mehrere Identity Provider, oder GitHub- und Google-Login neben Keycloak: oauth2-proxy.</li>
<li>Identity-Aware Access für ein ganzes Unternehmen, mit Device Posture und einer Policy-Sprache: Pomerium.</li>
<li>Nur Keycloak, und du willst OPA-Policies, Sessions in Redis und ein Projekt mit jahrelanger Geschichte: gogatekeeper.</li>
<li>Nur Keycloak, Forward-Auth in Traefik oder nginx mit möglichst niedriger Latenz und kleinem Dependency-Baum, und v0.2.0 ist für dich in Ordnung: gocloak-auth-proxy.</li>
</ul>
<p>Erfahrungen mit echtem Traffic, vor allem dazu, wo er kaputtgeht, sind als Issues oder vertrauliche Security-Reports im <a href="https://github.com/Nerzal/gocloak-auth-proxy" rel="noopener">Repository</a> willkommen.</p>
]]></content:encoded></item></channel></rss>