Cloudflare Worker Authentication

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up cloudflare worker authentication by checking tokens or keys on every edge request. The edge script reads headers, checks user proof, and blocks bad traffic before it hits backends. Teams often start by storing shared keys in worker secrets or using basic zero trust rules. When roles or keys stay too broad, any valid user can run risky calls across services. A single leaked token can give full read and write power to your internal data stores. How cloudflare worker authentication wor

Multi-Factor Authentication (MFA): the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You set up cloudflare worker authentication by checking tokens or keys on every edge request. The edge script reads headers, checks user proof, and blocks bad traffic before it hits backends. Teams often start by storing shared keys in worker secrets or using basic zero trust rules. When roles or keys stay too broad, any valid user can run risky calls across services. A single leaked token can give full read and write power to your internal data stores.

How cloudflare worker authentication works at the edge

An edge worker runs small pieces of code across global servers close to your users. When a client sends a request, the worker catches it before it reaches your main app. The script inspects the request headers to find a session cookie, key, or signed token. If the user runs a browser app, the worker can redirect them to a login page. After the user logs in, the identity provider sends back a signed token with user details.

For machine traffic, teams often use service tokens or mutual TLS certificates at the edge. The worker checks the token signature against public keys fetched from your identity provider. You can cache these public keys in edge storage so checks run fast on every call. Once the check passes, the worker forwards the clean request to your backend API or database. If the check fails, the worker drops the call right away and returns an error code.

Choosing between edge login rules and custom code

Teams can set up login checks through platform access rules or write custom check logic. Platform rules let you put a login wall in front of worker routes without writing code. This approach works well when human users need to log in through a standard company portal. The platform checks the user identity and passes a header with user info to your script. Your team saves time because you do not need to write token check logic by hand.

Custom script logic fits better when you build public APIs for external apps or partner systems. Your code can read custom headers, look up hashed keys in edge storage, and check scopes. You can also rate limit bad actors at the edge before they waste your backend compute. Still, managing custom login code across dozens of tiny scripts creates hard work for your team. Every time you change a token format, you must update and deploy every single worker script.

Multi-Factor Authentication (MFA): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What edge login solves and what it leaves open

Edge checks do a great job of stopping bad traffic far away from your core servers. They verify who is calling your app and keep public bots from hitting your internal endpoints. They also take the heavy load of checking cryptographic signatures off your main backend apps. Your internal services only see traffic that has already passed the initial edge door check.

Yet edge checks only decide who gets through the front door at the start of a request. Once the worker forwards the call, it often uses a single shared secret to reach backends. The backend database or service sees the worker identity, not the real human or service user. Edge login also cannot inspect or govern the exact database queries or commands that run next. If a valid user sends a bad query or reads sensitive rows, the edge lets it pass. Raw results with emails, phone numbers, or card numbers flow right back to the caller.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Where should we store secret keys for edge workers?

Store sensitive keys in encrypted worker secrets rather than plain text files or code repos. Your script can read these secrets at runtime without showing them in your dashboard logs.

How do we pass user identity from the edge to backend servers?

Have the worker attach a signed token header to the request before forwarding it upstream. The backend app should check that signature so attackers cannot spoof headers by bypassing the edge.

How can we handle expired tokens at the edge?

Check the expiration timestamp inside the token payload during every single request at the edge. Reject expired tokens right away with an unauthorized status code so the client fetches a fresh token.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.