@digicatapult/tsoa-oauth-express

中文文档
2.0.88发布于 2026/11/01Apache-2.0TypeScriptES Module构建来源已验证

Authentication handler for TSOA using OAuth2 JWT tokens

原始使用文档

tsoa-oauth-express

This library enables easy implementation of OAuth2 authentication for tsoa on express. To use this library first add it as an npm dependency to your existing tsoa application.

npm i @digicatapult/tsoa-oauth-express

Your authentication file (see tsoa's documentation on authentication setup here) will then need to contain something like:

import type express from 'express'
import type * as jwt from 'jsonwebtoken'

import mkExpressAuthentication, { AuthOptions } from '@digicatapult/tsoa-oauth-express'

// module options
const options: AuthOptions = {
  ...
}

export const expressAuthentication = mkExpressAuthentication(options)

where the options value is an object with the following type and properties:

interface AuthOptions {
  securityName?: string
  verifyOptions?: jwt.VerifyOptions & { complete?: false }
  jwksUri: () => Promise<string>
  getAccessToken: (req: express.Request) => Promise<string | undefined>
  getScopesFromToken: (decoded: string | jwt.JwtPayload) => Promise<string[]>
  tryRefreshTokens: (req: express.Request) => Promise<boolean>
}
propertyrequireddescription
jwksUritrueResolves to the uri for your identity providers jwks configuration
getAccessTokentrueResolves a JWT from the Request. For example this may come from an authorization header or a cookie based on requirements
getScopesFromTokentrueResolves to an array of OAuth scopes that the provided token supports
securityNamefalseOpenAPI Security scheme name to be secured
verifyOptionsfalsejsonwebtoken verification options. See the jsonwebtoken documentation for details
tryRefreshTokensfalsePerforms a token refresh. Resolves to true if the refressh succeeded otherwise false. By default this always resolves to false

Example

An example implementation of using the OAuth client-credential flow to authenticate a machine-to-machine (m2m) API is included in the package repository. This consists of two parts, a pre-configured Keycloak instance which can be instantiated with docker compose to act as our identity provider and an example application under ./example which implements to tsoa server. For further information please see the tsoa authentication documentation and our authentication handler example at ./example/authentication.ts.

To get started first bring up the Keycloak instance by executing from the repository root:

docker compose up -d

This will automatically pre-configure a client called example with client secret example. We can then fetch a m2m token by executing:

curl --request POST \
  --url 'http://localhost:3080/realms/example/protocol/openid-connect/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=client_credentials \
  --data client_id=example \
  --data client_secret=example

This will return a JSON object with a JWT auth token in the access_token property that takes the form like eyJhbGci...r8TIOfRQ.

The example server can then be run with:

npm run example

This server listens on port 3000 and exposes two routes /authenticated and /unauthenticated where only the first route requires an auth token. So we can for example call

curl http://localhost:3000/unauthenticated

which should return something like

{ "message": "This route is unauthenticated", "authenticated": false }

But calling the authenticated route (curl http://localhost:3000/authenticated) in the same was will return an error

{ "message": "unauthenticated" }

Providing the JWT auth token as part of an authentication header:

curl http://localhost:3000/authenticated --header 'authorization: bearer eyJhbGci...r8TIOfRQ'

causes the call to succeed:

{ "message": "This route is authenticated", "authenticated": true }

版本与发布记录

@digicatapult/tsoa-oauth-express 共发布 214 个版本,首次发布于 2024/05/14;过去 12 个月发布了 29 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.88latest2026/11/012 天前
2.0.872026/10/1519 天前
2.0.862026/10/1321 天前
2.0.852026/10/1321 天前
2.0.842026/10/0430 天前
2.0.832026/10/0430 天前

关于 @digicatapult/tsoa-oauth-express 的常见问题

@digicatapult/tsoa-oauth-express 是什么?

使用OAuth2 JWT令牌的TSOA身份验证处理程序

如何安装 @digicatapult/tsoa-oauth-express?

在项目目录运行 npm install @digicatapult/tsoa-oauth-express,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:express(^5.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@digicatapult/tsoa-oauth-express 支持 TypeScript 吗?

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

@digicatapult/tsoa-oauth-express 支持 ESM 还是 CommonJS?

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

@digicatapult/tsoa-oauth-express 可以商用吗?

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

@digicatapult/tsoa-oauth-express 还在维护吗?

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