# Hono adapter

> Hono 4 middleware: the proxy, `authenticate`, `need`, and `approvable`, on Node, Bun, Deno, and Workers.

From `sdk/hono/README.md` in tensile-tools/tensile-auth at commit `e66760d` (v0.4.1-7-ge66760d). The HTML page is https://auth.tensile.tools/docs/sdk/hono.

The Hono 4 adapter of `@tensile-tools/auth`. Hono is a peer dependency: the app installs it.

## Use it

```ts
import { Hono } from 'hono'
import { tensile, type TensileEnv } from '@tensile-tools/auth/hono'

const auth = tensile({ url: process.env.TENSILE_AUTH_URL!, appKey: process.env.TENSILE_AUTH_APP_KEY! })
const app = new Hono<TensileEnv>()

app.all('/auth/*', auth.proxy())
app.get('/me', auth.authenticate(), c => c.json({ user: c.get('principal')?.user ?? null }))
app.delete('/repos/:id',
  auth.approvable({ action: 'repo.delete', human: true },
    c => ({ kind: 'repo.delete', resource: c.req.param('id')!, summary: `Delete the repository ${c.req.param('id')}` })),
  c => c.body(null, 204))
```

| Part | What it does |
|---|---|
| `auth.proxy(routes?)` | A handler that forwards the browser routes to the server. Mount it at `/auth/*`, and at `/admin/*`, `/login/oauth/*`, `/login/device/*`, or `/.well-known/*` with the matching `routes` option. Any path outside them answers 404 |
| `auth.authenticate()` | Sets `c.get('principal')`, or leaves it `undefined` when the request carries no credential or a session cookie the server no longer knows. A bad bearer token gets a 401 problem |
| `auth.need(need)` | Lets the request through only when the principal meets `need`. Otherwise answers the server's problem |
| `auth.approvable(need, describe, options?)` | Like `need`, and an agent may ask its human to approve the action `describe(c)` returns, then retry with `Tensile-Approval`. With `Prefer: wait=N` on the retry, the route holds it until the person decides, for at most `approvalWait` seconds, or `options.wait` for this route; see [wait for an approval](/docs/sdk/core#wait-for-an-approval). The wait ends when `c.req.raw.signal` aborts. On Cloudflare Workers, Deno Deploy, and other edge runtimes, `approvalWait` defaults to 0 |
| `auth.as(c)` | Calls on the person's behalf with the request's credential. A refusal throws a `ProblemError`; return `err.toResponse()` |
| `auth.client` | The core client from `createTensile` |

## The client's address

The server uses the client's address for rate limits and the audit log. Anyone who connects to the app directly can send `X-Forwarded-For` and `X-Real-IP`, so the adapter reads a forwarded header only when you name it in `trustProxy` and, where the socket's address is known, that address is in `trustedProxies`, as `authhttp.WithClientIP` does:

| `trustProxy` | The header the adapter reads |
|---|---|
| Unset or `false` | None |
| `{ header: 'x-forwarded-for', hops: n }` | The `n`th `X-Forwarded-For` entry from the right, for `n` proxies in a chain |
| `{ header: 'x-real-ip' }` | `X-Real-IP`, for a proxy that sets it |
| `true` | Shorthand for `x-forwarded-for` with 1 hop |
| A number `n` | Shorthand for `x-forwarded-for` with `n` hops |

The adapter reads only the header named. It never falls back from one header to the other, so a client-sent `X-Forwarded-For` can't override the `X-Real-IP` your proxy sets.

`trustedProxies` lists your proxies' own addresses as CIDRs, for example `['10.0.0.0/8', 'fd00::/8']`. It defaults to none.

- Under `@hono/node-server` or Bun, the adapter knows the socket's address. It reads the header only when that address is in `trustedProxies`, and sends the socket's address otherwise, so `trustProxy` alone has no effect.
- On runtimes that expose no socket address, such as Deno Deploy or Cloudflare Workers, the adapter sends no address without `trustProxy`, and reads the header on every request with it. Set `trustProxy` there only when the platform overwrites that header on every request.

When you set `trustedProxies`, the walk along `X-Forwarded-For` also stops at the first entry outside them, and an entry that isn't an IP address stops it too, as in `authhttp`. A configuration that isn't valid, such as `hops: 0` or a malformed CIDR, throws when you create the adapter.

To read the address another way, pass `clientIp`, which overrides both options.

The adapter's tests pass on Node and on Deno 2.9. Bun isn't tested yet.
