@authup/server-adapter-web

中文文档
1.0.0-beta.73发布于 2026/11/02Apache-2.0TypeScriptES Module近 30 天下载 3101

Verify the Authup access token of a Web Request, in any framework built on it.

原始使用文档
文档目录10 节
  1. Why @authup/server-adapter-web?
  2. Installation
  3. Usage
  4. Fetch handler
  5. Hand the claims to your handler
  6. Read the token from elsewhere
  7. Options
  8. API
  9. Notes
  10. License

Authup logo

@authup/server-adapter-web

Verify the Authup access token of a Web Request, in any framework built on it.
One function you wrap in your framework's middleware shape: Hono, routup, Bun.serve or a plain fetch handler.

npm version CI License: Apache-2.0

Installation · Usage · API · Documentation


Why @authup/server-adapter-web?

Frameworks built on the Web Request API each have their own middleware signature, so an adapter per framework would never keep up. This package ships the part they share: read the bearer token from a Request, verify it with @authup/server-adapter-kit and return the claims. The few lines that store the result are yours.

ConcernWithout itWith it
Header parsingSplit Bearer <token> yourself and handle malformed valuesParsed for you; a malformed header throws
VerificationFetch keys or call introspection per frameworkOne TokenVerifier, local or remote, shared with your other transports
Tokens outside the headerHand-written cookie or query fallbackstokenByRequest reads a fallback when no Authorization header is sent
Bound tokensCompare certificate thumbprints yourselfcertificateThumbprintByRequest feeds the RFC 8705 check

Installation

npm install @authup/server-adapter-web @authup/server-adapter-kit

TokenVerifier comes from @authup/server-adapter-kit, which you import directly. The package is ESM-only and has no peer dependencies. Its engines field asks for Node.js >=26.0.0; the verifier package imports node:crypto, so another runtime must resolve Node built-ins.

Usage

Fetch handler

import { TokenVerifier } from '@authup/server-adapter-kit';
import { verifyRequest } from '@authup/server-adapter-web';

const tokenVerifier = new TokenVerifier({ baseURL: 'https://auth.example.com' });

export async function handle(request: Request): Promise<Response> {
    let data;
    try {
        data = await verifyRequest(request, { tokenVerifier });
    } catch {
        return new Response('invalid token', { status: 401 });
    }

    if (!data) {
        return new Response('unauthenticated', { status: 401 });
    }

    return new Response(`hello ${data.sub}`);
}

verifyRequest resolves the verification data, resolves undefined when the request carries no token, and throws for a malformed Authorization header or a token the verifier rejects.

See @authup/server-adapter-kit for local versus remote verification, caching and permissions.

Hand the claims to your handler

import type { TokenVerificationData } from '@authup/server-adapter-kit';

type AuthedHandler = (request: Request, auth: TokenVerificationData) => Response | Promise<Response>;

function withAuth(handler: AuthedHandler) {
    return async (request: Request): Promise<Response> => {
        const auth = await verifyRequest(request, { tokenVerifier }).catch(() => undefined);
        if (!auth) {
            return new Response(null, { status: 401 });
        }

        return handler(request, auth);
    };
}

export const orders = withAuth((_request, auth) => new Response(`orders of ${auth.sub}`));

The claims travel as an argument, typed once, instead of being attached to the Request. In a framework with its own context object, pass the framework's underlying Request (Hono: c.req.raw) and store the result in that context.

Read the token from elsewhere

const data = await verifyRequest(request, {
    tokenVerifier,
    tokenByRequest: (req) => new URL(req.url).searchParams.get('access_token') ?? undefined,
});

tokenByRequest is only called when the request has no Authorization header. Its value may carry a Bearer prefix or not. The adapter parses no cookies itself.

Options

OptionTypeDefaultDescription
tokenVerifierITokenVerifier(required)Usually a TokenVerifier from @authup/server-adapter-kit
tokenByRequest(request) => string | undefinedundefinedFallback token source when no Authorization header is present
certificateThumbprintByRequest(request) => string | undefined | Promise<string | undefined>undefinedSHA-256 thumbprint of the client certificate, for certificate-bound tokens

API

ExportKindPurpose
verifyRequestfunctionVerifies one Request
VerifyRequestOptionstypeThe option object above

Notes

  • Only Bearer is accepted in the header. A Basic or other scheme in Authorization throws BearerTokenMalformedError (bearer_token_malformed), and tokenByRequest is not consulted.
  • Verification errors are JWTErrors (invalid_token, expired_token, inactive_token). All of them, and the malformed header, map to HTTP 401.
  • A query string token is logged by proxies. Prefer a header or a cookie for tokenByRequest where you can.

License

Made with 💚

Published under the Apache 2.0 License.

版本与发布记录

@authup/server-adapter-web 共发布 32 个版本,首次发布于 2026/05/08;过去 12 个月发布了 32 个版本。

版本发布日期距今
1.0.0-beta.73latest2026/11/025 天前
1.0.0-beta.722026/10/2711 天前
1.0.0-beta.712026/10/2018 天前
1.0.0-beta.702026/10/1919 天前
1.0.0-beta.692026/10/1424 天前
1.0.0-beta.682026/09/251 个月前

关于 @authup/server-adapter-web 的常见问题

@authup/server-adapter-web 是什么?

Verify the Authup access token of a Web Request, in any framework built on it.

如何安装 @authup/server-adapter-web?

在项目目录运行 npm install @authup/server-adapter-web,或使用 pnpm add / yarn add / bun add。

@authup/server-adapter-web 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@authup/server-adapter-web 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@authup/server-adapter-web 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@authup/server-adapter-web 可以商用吗?

@authup/server-adapter-web 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@authup/server-adapter-web 的下载量有多少?

根据 npm 官方下载统计,@authup/server-adapter-web 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,101 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@authup/server-adapter-web 还在维护吗?

最新版本 1.0.0-beta.73 发布于 2026/11/02(5 天前);至今共发布 32 个版本,其中过去 12 个月发布了 32 个;首次发布于 2026/05/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。