Skip to content

Commit 2720ac7

Browse files
authored
feat: Add Cloudflare Access middleware (#880)
1 parent 9150550 commit 2720ac7

File tree

11 files changed

+588
-0
lines changed

11 files changed

+588
-0
lines changed

.changeset/sharp-moles-knock.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hono/cloudflare-access': minor
3+
---
4+
5+
Initial release
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: ci-cloudflare-access
2+
on:
3+
push:
4+
branches: [main]
5+
paths:
6+
- 'packages/cloudflare-access/**'
7+
pull_request:
8+
branches: ['*']
9+
paths:
10+
- 'packages/cloudflare-access/**'
11+
12+
jobs:
13+
ci:
14+
runs-on: ubuntu-latest
15+
defaults:
16+
run:
17+
working-directory: ./packages/cloudflare-access
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: actions/setup-node@v4
21+
with:
22+
node-version: 20.x
23+
- run: yarn install --frozen-lockfile
24+
- run: yarn build
25+
- run: yarn test

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"build:casbin": "yarn workspace @hono/casbin build",
4141
"build:ajv-validator": "yarn workspace @hono/ajv-validator build",
4242
"build:tsyringe": "yarn workspace @hono/tsyringe build",
43+
"build:cloudflare-access": "yarn workspace @hono/cloudflare-access build",
4344
"build": "run-p 'build:*'",
4445
"lint": "eslint 'packages/**/*.{ts,tsx}'",
4546
"lint:fix": "eslint --fix 'packages/**/*.{ts,tsx}'",

packages/cloudflare-access/CHANGELOG.md

Whitespace-only changes.

packages/cloudflare-access/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Cloudflare Access middleware for Hono
2+
3+
This is a [Cloudflare Access](https://www.cloudflare.com/zero-trust/products/access/) third-party middleware
4+
for [Hono](https://github.com/honojs/hono).
5+
6+
This middleware can be used to validate that your application is being served behind Cloudflare Access by verifying the
7+
JWT received, User details from the JWT are also available inside the request context.
8+
9+
This middleware will also ensure the Access policy serving the application is from a
10+
specific [Access Team](https://developers.cloudflare.com/cloudflare-one/faq/getting-started-faq/#whats-a-team-domainteam-name).
11+
12+
## Usage
13+
14+
```ts
15+
import { cloudflareAccess } from '@hono/cloudflare-access'
16+
import { Hono } from 'hono'
17+
18+
const app = new Hono()
19+
20+
app.use('*', cloudflareAccess('my-access-team-name'))
21+
app.get('/', (c) => c.text('foo'))
22+
23+
export default app
24+
```
25+
26+
## Access JWT payload
27+
28+
```ts
29+
import { cloudflareAccess, CloudflareAccessVariables } from '@hono/cloudflare-access'
30+
import { Hono } from 'hono'
31+
32+
type myVariables = {
33+
user: number
34+
}
35+
36+
const app = new Hono<{ Variables: myVariables & CloudflareAccessVariables }>()
37+
38+
app.use('*', cloudflareAccess('my-access-team-name'))
39+
app.get('/', (c) => {
40+
const payload = c.get('accessPayload')
41+
42+
return c.text(`You just authenticated with the email ${payload.email}`)
43+
})
44+
45+
export default app
46+
```
47+
48+
49+
## Errors throw by the middleware
50+
51+
| Error | HTTP Code |
52+
|--------------------------------------------------------------------------------------------------------|-----------|
53+
| Authentication error: Missing bearer token | 401 |
54+
| Authentication error: Unable to decode Bearer token | 401 |
55+
| Authentication error: Token is expired | 401 |
56+
| Authentication error: Expected team name {your-team-name}, but received ${different-team-signed-token} | 401 |
57+
| Authentication error: Invalid Token | 401 |
58+
59+
## Author
60+
61+
Gabriel Massadas <https://github.com/g4brym>
62+
63+
## License
64+
65+
MIT
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@hono/cloudflare-access",
3+
"version": "0.0.0",
4+
"description": "A third-party Cloudflare Access auth middleware for Hono",
5+
"type": "module",
6+
"module": "dist/index.js",
7+
"types": "dist/index.d.ts",
8+
"files": [
9+
"dist"
10+
],
11+
"scripts": {
12+
"test": "vitest --run",
13+
"build": "tsup ./src/index.ts --format esm,cjs --dts",
14+
"publint": "publint",
15+
"release": "yarn build && yarn test && yarn publint && yarn publish"
16+
},
17+
"exports": {
18+
".": {
19+
"import": {
20+
"types": "./dist/index.d.ts",
21+
"default": "./dist/index.js"
22+
},
23+
"require": {
24+
"types": "./dist/index.d.cts",
25+
"default": "./dist/index.cjs"
26+
}
27+
}
28+
},
29+
"license": "MIT",
30+
"publishConfig": {
31+
"registry": "https://registry.npmjs.org",
32+
"access": "public"
33+
},
34+
"repository": {
35+
"type": "git",
36+
"url": "https://github.com/honojs/middleware.git"
37+
},
38+
"homepage": "https://github.com/honojs/middleware",
39+
"peerDependencies": {
40+
"hono": "*"
41+
},
42+
"devDependencies": {
43+
"hono": "^4.4.12",
44+
"tsup": "^8.1.0",
45+
"vitest": "^1.6.0"
46+
}
47+
}

0 commit comments

Comments
 (0)