honojs-middleware/packages/cloudflare-access
github-actions[bot] 7007f19734
Version Packages (#1002)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-05 09:51:46 +09:00
..
src feat(cloudflare-access): Add support to read JWT from Cookie (#1001) 2025-03-05 09:20:59 +09:00
CHANGELOG.md Version Packages (#1002) 2025-03-05 09:51:46 +09:00
README.md feat(cloudflare-access): Handle Access organization does not exist and Access not available cases (#898) 2024-12-23 11:19:56 +09:00
package.json Version Packages (#1002) 2025-03-05 09:51:46 +09:00
tsconfig.json feat: Add Cloudflare Access middleware (#880) 2024-12-13 17:16:11 +09:00
vitest.config.ts feat: Add Cloudflare Access middleware (#880) 2024-12-13 17:16:11 +09:00

README.md

Cloudflare Access middleware for Hono

This is a Cloudflare Access third-party middleware for Hono.

This middleware can be used to validate that your application is being served behind Cloudflare Access by verifying the JWT received, User details from the JWT are also available inside the request context.

This middleware will also ensure the Access policy serving the application is from a specific Access Team.

Usage

import { cloudflareAccess } from '@hono/cloudflare-access'
import { Hono } from 'hono'

const app = new Hono()

app.use('*', cloudflareAccess('my-access-team-name'))
app.get('/', (c) => c.text('foo'))

export default app

Access JWT payload

import { cloudflareAccess, CloudflareAccessVariables } from '@hono/cloudflare-access'
import { Hono } from 'hono'

type myVariables = {
  user: number
}

const app = new Hono<{ Variables: myVariables & CloudflareAccessVariables }>()

app.use('*', cloudflareAccess('my-access-team-name'))
app.get('/', (c) => {
  const payload = c.get('accessPayload')

  return c.text(`You just authenticated with the email ${payload.email}`)
})

export default app

Errors throw by the middleware

Error HTTP Code
Authentication error: Missing bearer token 401
Authentication error: Unable to decode Bearer token 401
Authentication error: Token is expired 401
Authentication error: Expected team name {your-team-name}, but received ${different-team-signed-token} 401
Authentication error: Invalid Token 401
Authentication error: The Access Organization 'my-team-name' does not exist 500
Authentication error: Received unexpected HTTP code 500 from Cloudflare Access 500

Author

Gabriel Massadas https://github.com/g4brym

License

MIT