Skip to content

Commit bb06149

Browse files
[CF1] verifying JWTs example
1 parent d6c5ece commit bb06149

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

src/content/docs/cloudflare-one/identity/authorization-cookie/validating-json.mdx

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ pcx_content_type: how-to
33
title: Validate JWTs
44
sidebar:
55
order: 1
6-
76
---
87

9-
import { GlossaryTooltip } from "~/components"
8+
import { GlossaryTooltip } from "~/components";
109

1110
When Cloudflare sends a request to your origin, the request will include an [application token](/cloudflare-one/identity/authorization-cookie/application-token/) as a `Cf-Access-Jwt-Assertion` request header and as a `CF_Authorization` cookie.
1211

@@ -22,9 +21,9 @@ You can also manually rotate the key using the [API](/api/resources/zero_trust/s
2221

2322
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.
2423

25-
* `keys`: both keys in JWK format
26-
* `public_cert`: current key in PEM format
27-
* `public_certs`: both keys in PEM format
24+
- `keys`: both keys in JWK format
25+
- `public_cert`: current key in PEM format
26+
- `public_certs`: both keys in PEM format
2827

2928
```txt
3029
{
@@ -65,9 +64,8 @@ As shown in the example below, `https://<your-team-name>.cloudflareaccess.com/cd
6564

6665
:::note[Avoid key rotation issues]
6766

68-
69-
* Validate tokens using the external endpoint rather than saving the public key as a hard-coded value.
70-
* Do not fetch the current key from `public_cert`, since your origin may inadvertently read an expired value from an outdated cache. Instead, match the `kid` value in the JWT to the corresponding certificate in `public_certs`.
67+
- Validate tokens using the external endpoint rather than saving the public key as a hard-coded value.
68+
- Do not fetch the current key from `public_cert`, since your origin may inadvertently read an expired value from an outdated cache. Instead, match the `kid` value in the JWT to the corresponding certificate in `public_certs`.
7169
:::
7270

7371
## Verify the JWT manually
@@ -175,10 +173,10 @@ func main() {
175173

176174
`pip` install the following:
177175

178-
* flask
179-
* requests
180-
* PyJWT
181-
* cryptography
176+
- flask
177+
- requests
178+
- PyJWT
179+
- cryptography
182180

183181
```python
184182
from flask import Flask, request
@@ -251,8 +249,8 @@ if __name__ == '__main__':
251249
### JavaScript example
252250

253251
```javascript
254-
const express = require('express');
255-
const jose = require('jose');
252+
const express = require("express");
253+
const jose = require("jose");
256254

257255
// The Application Audience (AUD) tag for your application
258256
const AUD = process.env.POLICY_AUD;
@@ -265,33 +263,36 @@ const JWKS = jose.createRemoteJWKSet(new URL(CERTS_URL));
265263

266264
// verifyToken is a middleware to verify a CF authorization token
267265
const verifyToken = async (req, res, next) => {
268-
const token = req.headers['cf-access-jwt-assertion'];
269-
270-
// Make sure that the incoming request has our token header
271-
if (!token) {
272-
return res.status(403).send({
273-
status: false,
274-
message: 'missing required cf authorization token',
275-
});
276-
}
277-
278-
const result = await jose.jwtVerify(token, JWKS, {
279-
issuer: TEAM_DOMAIN,
280-
audience: AUD,
281-
});
282-
283-
req.user = result.payload;
284-
next();
266+
const token = req.headers["cf-access-jwt-assertion"];
267+
268+
// Make sure that the incoming request has our token header
269+
if (!token) {
270+
return res.status(403).send({
271+
status: false,
272+
message: "missing required cf authorization token",
273+
});
274+
}
275+
276+
const result = await jose.jwtVerify(token, JWKS, {
277+
issuer: TEAM_DOMAIN,
278+
audience: AUD,
279+
});
280+
281+
req.user = result.payload;
282+
next();
285283
};
286284

287285
const app = express();
288286

289287
app.use(verifyToken);
290288

291-
app.get('/', (req, res) => {
292-
res.send('Hello World!');
289+
app.get("/", (req, res) => {
290+
res.send("Hello World!");
293291
});
294292

295293
app.listen(3333);
296-
297294
```
295+
296+
## Related resources
297+
298+
- [Verifying JWTs in Cloudflare Workers](https://kinde.com/blog/engineering/verifying-jwts-in-cloudflare-workers/) - Implement JWT verification in Cloudflare Workers.

0 commit comments

Comments
 (0)