Validate JWTs
When Cloudflare sends a request to your origin, the request will include an application token as a Cf-Access-Jwt-Assertion
request header and as a CF_Authorization
cookie.
Cloudflare signs the token with a key pair unique to your account. You should validate the token with your public key to ensure that the request came from Access and not a malicious third party.
Access signing keys
The public key for the signing key pair is located at https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/certs
, where <your-team-name>
is your Zero Trust team name.
By default, Access rotates the signing key every 6 weeks. This means you will need to programmatically or manually update your keys as they rotate. Previous keys remain valid for 7 days after rotation to allow time for you to make the update.
You can also manually rotate the key using the API. This can be done for testing or security purposes.
As shown in the example below, https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/certs
contains two public keys: the current key used to sign all new tokens, and the previous key that has been rotated out.
keys
: both keys in JWK formatpublic_cert
: current key in PEM formatpublic_certs
: both keys in PEM format
Verify the JWT manually
To verify the token manually:
-
Copy the JWT from the
CF_Authorization
cookie or from theCf-Access-Jwt-Assertion
request header. -
Go to jwt.io.
-
Select the RS256 algorithm.
-
Paste the JWT into the Encoded box.
-
Get the
kid
value located in the Header box. -
Get your public key:
- Go to
https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/certs
. - Under
public_certs
, locate the entry with thekid
value you found in Step 5. - Copy the
cert
value.
- Go to
-
In the Verify Signature box, paste the
cert
value into the Public Key field. -
Ensure that the signature says verified.
You can now trust that this request was sent by Access.
Programmatic verification
You can run an automated script on your origin server to validate incoming requests. The provided sample code gets the application token from a request and checks its signature against your public key. You will need to insert your own team domain and Application Audience (AUD) tag into the sample code.
Get your AUD tag
- In Zero Trust, go to Access > Applications.
- Select Configure for your application.
- On the Overview tab, copy the Application Audience (AUD) Tag.
You can now paste the AUD into your token validation script.
Golang example
Python example
pip
install the following:
- flask
- requests
- PyJWT
- cryptography