Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Zero Trust

Cloudflare Access: Add Login and Policies to Any App

Protect an application with Cloudflare Access: create a self-hosted app and policy, use login methods, validate the Access JWT in your origin and use service tokens for machines.

Cloudflare Access puts a login screen and a policy check in front of an application without changing the application. A visitor requests https://admin.example.com; Cloudflare checks for a valid Access session; if there is none, the visitor is sent to log in through your identity provider. Only after the policy allows them does the request reach your server.

The dashboard and the network are Cloudflare's, so everything below is read-only here, apart from stand-ins that parse and check the token your origin receives.

Self-hosted applications

You protect your own application by creating a self-hosted application:

  1. In the Zero Trust dashboard open Access controls, Applications (the menu names move over time), choose Add an application, then Self-hosted.
  2. Enter the application domain, for example admin.example.com, optionally with a path such as /admin/*.
  3. Choose which login methods apply and the session duration (how long a login stays valid).
  4. Attach one or more policies.

The hostname must be proxied through Cloudflare (orange cloud) or reach your origin through a tunnel. If your DNS record is not proxied, Access cannot sit in front of it.

You can protect more than websites: SSH, RDP, VNC and arbitrary TCP services via the client or browser-rendered terminals; SaaS apps through SSO; and private networks reached from WARP.

Writing good policies

A policy is a reusable set of rules. Practical guidance:

  • Start with Allow policies built from IdP groups instead of single emails, so people join and leave through your identity system.
  • Add Require rules for what must always be true: device posture (managed, disk encrypted), country, MFA used.
  • Use Exclude sparingly for named exceptions.
  • Put Block policies first, because evaluation order matters.
  • Use Bypass only for genuinely public paths, such as /health or /.well-known/*, and place the protected path separately so the more specific application wins.
  • Choose the session duration to match risk: eight hours for normal tools, shorter for admin panels.

What your origin receives

After a successful login, Cloudflare adds two things to each request it forwards:

  • A cookie CF_Authorization (a signed JWT).
  • A header Cf-Access-Jwt-Assertion with the same JWT.

The JWT contains who the user is (email, sub), the issuer (https://<team>.cloudflareaccess.com), an audience tag (aud) that identifies your application, issue and expiry times, and a signature. Your origin can trust that the request passed Access, but only if it verifies the token. Otherwise anyone who reaches the origin directly (bypassing Cloudflare) could forge a header.

The checks an origin must perform:

  1. Fetch Cloudflare's public keys from https://<team>.cloudflareaccess.com/cdn-cgi/access/certs and verify the signature (RS256).
  2. Check iss equals your team domain.
  3. Check aud contains your application's AUD tag (shown in the app settings).
  4. Check exp has not passed (and nbf if present).

A Worker doing this uses a JWT library such as jose:

JavaScript
import { createRemoteJWKSet, jwtVerify } from "jose";

export default {
  async fetch(request, env) {
    const token = request.headers.get("Cf-Access-Jwt-Assertion");
    if (!token) return new Response("Missing Access token", { status: 403 });

    const jwks = createRemoteJWKSet(new URL(`https://${env.TEAM}.cloudflareaccess.com/cdn-cgi/access/certs`));
    try {
      const { payload } = await jwtVerify(token, jwks, {
        issuer: `https://${env.TEAM}.cloudflareaccess.com`,
        audience: env.POLICY_AUD,
      });
      return new Response(`Hello ${payload.email}`);
    } catch {
      return new Response("Invalid token", { status: 403 });
    }
  },
};

A claims checker stand-in

Signature verification needs the real keys, which are not available here. This stand-in decodes a token's payload and runs the claim checks (issuer, audience, expiry) on a sample token it builds itself. It does not verify signatures, which is why production code must use a library.

JS Check Access JWT claims (no signature)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The audience check is the one people forget: a valid token for a different Access application, signed by the same team, must still be refused.

Service tokens for machines

Scripts, CI jobs and other services cannot complete a browser login. Create a service token (Access controls, Service credentials): you get a Client ID and Client Secret (shown once). The machine sends them as headers:

Terminal
curl https://admin.example.com/api/report \
  -H "CF-Access-Client-Id: <client id>.access" \
  -H "CF-Access-Client-Secret: <client secret>"

To let them in, add a policy with action Service Auth that includes that token. Keep secrets in a secret manager and give each integration its own token so you can revoke one without breaking others.

Check your policy for gaps

JS Policy review checklist

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Operating Access

  • Logs: Access request logs (who, which app, allowed or denied, why) are in the dashboard; push them to storage or a SIEM with Logpush.
  • Revoke sessions for a user when they leave: remove them from the group and revoke active sessions.
  • App launcher: users can open https://<team>.cloudflareaccess.com to see every app they may open.
  • Test policies with the policy tester in the dashboard before enforcing, then keep one tested break-glass path.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must an origin do with the Cf-Access-Jwt-Assertion header?

    Anyone who can reach the origin directly could forge the header. Verifying the JWT proves it came from Access for your app.
  2. 2. What is the AUD tag used for?

    The aud claim must equal your application's AUD tag, so tokens issued for other applications are rejected.
  3. 3. How should a CI job call an Access-protected API?

    Service tokens (client id and secret headers) let machines authenticate, and a Service Auth policy admits them.
  4. 4. Which Access policy action skips authentication for a path?

    Bypass lets matching requests through without any login, so use it only for truly public paths.

Summary

  • Access sits in front of an app (a self-hosted application on a proxied hostname or a tunnel) and checks identity and device posture before forwarding.
  • Policies use Allow, Block, Bypass or Service Auth with Include, Require and Exclude rules; build them from groups and require MFA and device posture.
  • Origins must verify the Cf-Access-Jwt-Assertion JWT: signature (team certs), issuer, audience tag and expiry.
  • Machines use service tokens with a Service Auth policy; review logs and sessions regularly.

Next: Tunnel, WARP and Gateway