The Simplest Way to Make Keycloak and Netlify Edge Functions Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

In production, keycloak and netlify edge functions work together to check user identity close to the browser. Keycloak issues signed tokens after login, and your edge scripts inspect those tokens before serving content. This setup stops bad requests before they ever reach your core databases or backend services. Teams use this pattern to block unpaid users, enforce role rules, and personalize pages fast. Your origin servers stay quiet because the edge node rejects unverified traffic right away.

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

In production, keycloak and netlify edge functions work together to check user identity close to the browser. Keycloak issues signed tokens after login, and your edge scripts inspect those tokens before serving content.

This setup stops bad requests before they ever reach your core databases or backend services. Teams use this pattern to block unpaid users, enforce role rules, and personalize pages fast. Your origin servers stay quiet because the edge node rejects unverified traffic right away.

How keycloak and netlify edge functions share identity data

When a user logs in, Keycloak checks their password and two-factor code against your user store. After a good login, Keycloak signs a JSON Web Token with a private key. The browser stores this token in a secure cookie or sends it in a request header.

Next, the user asks for a protected web page or API route on your site. Your edge script intercepts this request at the nearest network node before loading the page. The script reads the token from the request header and checks the digital signature.

To check the signature, the edge script needs the public keys from your Keycloak realm. Fetching keys from Keycloak on every single web request adds too much latency. Teams cache the public keys at the edge or store them in edge variables. Once the signature passes the check, the script reads the user roles inside the token. The edge script then lets the user through or sends them back to the login screen.

Common failure modes and how to scope roles

One common mistake is putting huge role lists inside the user token. Keycloak can attach realm roles, client roles, and group names to every single token. If a token grows too big, it exceeds the size limit for browser cookies and headers. Edge requests will fail with header size errors that frustrate your users. You should set up Keycloak client scopes to include only the roles that the edge script needs.

Another big risk is using shared service accounts with broad tokens for backend calls. Sometimes an edge script needs to fetch extra data from a database or internal API. Teams often embed a long-lived static key in the edge settings to make this work. If that static key leaks, an attacker gets wide access to your backend data. Instead, scope each service role to read only the exact tables or routes required.

Keycloak + Cloud Functions IAM: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Token expiration also causes trouble at the network edge. If tokens live for hours, a banned user can keep reading data until the clock runs out. Keep access token lifespans short, such as five minutes, and refresh them in the background. You must also log every failed token check and role denial at the edge. Unlogged edge queries leave your security team blind when attackers probe your routes.

Protecting backend data and sensitive fields from the edge

Edge scripts check who the user is, but backend databases still need strong protection. Even when an edge script checks a token, downstream queries still touch real user records. Those records often hold sensitive details like emails, phone numbers, card numbers, or national IDs.

You should apply data masking to rewrite matched fields in query results before returning them. Also, lock down your network paths so backend services only accept traffic from trusted nodes. Combine edge token checks with strict database roles to keep your production data safe.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

How should an edge script check a Keycloak token without adding latency?

Do not call the Keycloak server on every single request to verify a token. Instead, check the token signature locally at the edge using cached public keys from Keycloak.

How do we handle token revocations when checking tokens at the edge?

Keep your Keycloak access token lifespan very short, around five minutes or less. When you revoke a session in Keycloak, the user loses access as soon as the short token expires.

Why do requests fail after adding many Keycloak groups to a user?

Every group and role increases the size of the token stored in the cookie or header. Use Keycloak mappers to filter the token so it only holds roles needed by the edge app.

The query runs. The emails stay hidden.