@tesouro/embedded-components-widget-token

中文文档
0.11.69发布于 2026/11/02Apache-2.0TypeScriptES Module

Server-side helpers for minting widget tokens used by Tesouro's embedded components. Mints an RFC 8693 token-exchange request and encrypts it as a JWE (A256KW + A256GCM) using your shared widget secret.

原始使用文档
文档目录11 节
  1. Installation
  2. Usage
  3. One-shot
  4. Pre-configured factory
  5. In a route handler
  6. API
  7. createWidgetToken(params): Promise<WidgetTokenResult>
  8. configureCreateWidgetToken(creds)
  9. DEFAULT_EXPIRATION_IN_SECONDS
  10. Token format
  11. License

@tesouro/embedded-components-widget-token

Server-side helpers for minting widget tokens used by Tesouro's embedded components. Mints an RFC 8693 token-exchange request and encrypts it as a JWE (A256KW + A256GCM) using your shared widget secret.

Server-only. This package handles your clientSecret and widgetSecret, so keep it on the server and never bundle it into client code. It runs in any server runtime — a Next.js Route Handler, an Express server, or a Lambda handler.

Installation

yarn add @tesouro/embedded-components-widget-token
# or
npm install @tesouro/embedded-components-widget-token

Usage

One-shot

import { createWidgetToken } from '@tesouro/embedded-components-widget-token';

const { widgetToken, exp } = await createWidgetToken({
  clientId: process.env.TESOURO_CLIENT_ID!,
  clientSecret: process.env.TESOURO_CLIENT_SECRET!,
  widgetSecret: process.env.TESOURO_WIDGET_SECRET!,
  userId: 'user_123',
  userEmail: '[email protected]',
  organizationReference: 'partner-org-123',
});

exp is the token's expiration time in seconds since epoch — return it to the client so it can refresh before expiry.

Pre-configured factory

When the same credentials are reused across many requests, bind them once with configureCreateWidgetToken:

import { configureCreateWidgetToken } from '@tesouro/embedded-components-widget-token';

const mintWidgetToken = configureCreateWidgetToken({
  clientId: process.env.TESOURO_CLIENT_ID!,
  clientSecret: process.env.TESOURO_CLIENT_SECRET!,
  widgetSecret: process.env.TESOURO_WIDGET_SECRET!,
  organizationReference: 'partner-org-123',
});

const { widgetToken, exp } = await mintWidgetToken({
  userId: 'user_123',
  userEmail: '[email protected]',
});

In a route handler

Call the factory from an authenticated route on your server and return { widgetToken, exp } to the browser. Getting started has a complete Next.js route handler and the provider that calls it.

Identity must come from the server, not the caller. Resolve userId, userEmail, and organizationReference from your authenticated session, never from the request body. organizationReference names the business the user belongs to, so bind it once in configureCreateWidgetToken only when every user of that server belongs to the same business. Your route mints a valid token for whatever identity it receives, so trusting client-supplied identity lets any caller impersonate another user.

API

createWidgetToken(params): Promise<WidgetTokenResult>

ParamTypeRequiredDescription
clientIdstringyesTesouro-issued client identifier. Used as JWE kid and as iss/client_id in the payload.
clientSecretstringyesTesouro-issued client secret. Carried as client_secret inside the encrypted payload.
widgetSecretstringyesShared symmetric key used to encrypt the JWE.
userIdstringyesSubject (sub) of the inner identity token.
userEmailstringyesEmail claim of the inner identity token.
organizationReferencestringyesPartner-resolved organization/business reference. Carried as organization_reference in the outer encrypted payload. Resolve this server-side, not from request body input.
expirationInSecondsnumbernoToken TTL in seconds. Defaults to DEFAULT_EXPIRATION_IN_SECONDS (480 — 8 minutes).

Returns:

interface WidgetTokenResult {
  widgetToken: string; // compact-serialized JWE
  exp: number; // expiration in seconds since epoch (UTC)
}

Throws if clientId, clientSecret, widgetSecret, or organizationReference is missing or empty.

configureCreateWidgetToken(creds)

Binds clientId, clientSecret, widgetSecret, and optional defaults for organizationReference and expirationInSeconds, and returns a function that only takes per-request userId / userEmail plus optional per-call overrides for organizationReference and expirationInSeconds. organizationReference is still required: the returned function throws unless it is bound here or passed per call.

DEFAULT_EXPIRATION_IN_SECONDS

480 — the default token lifetime in seconds (8 minutes).

Token format

createWidgetToken produces a JWE with the following protected header:

{
  "alg": "A256KW",
  "enc": "A256GCM",
  "kid": "<clientId>",
  "typ": "JWT",
  "cty": "JWT"
}

The encrypted payload is an RFC 8693 token-exchange request whose subject_token is an unsigned identity JWT (alg: none) bearing the user's sub, email, iss, aud, iat, and exp claims. organizationReference is minted at the outer encrypted payload level as organization_reference; it is not added to the inner subject_token.

Backends that don't run Node build the same token from the widget token protocol in Authentication › User token, in the Embedded Banking or FinOps docs.

License

Apache-2.0

版本与发布记录

@tesouro/embedded-components-widget-token 共发布 192 个版本,首次发布于 2026/07/24;过去 12 个月发布了 192 个版本。

版本发布日期距今
0.11.69latest2026/11/02今天
0.11.682026/11/02今天
0.11.622026/10/313 天前
0.11.552026/10/313 天前
0.11.462026/10/313 天前
0.11.412026/10/304 天前

其他发布通道:bootstrap → 0.0.1。

关于 @tesouro/embedded-components-widget-token 的常见问题

@tesouro/embedded-components-widget-token 是什么?

用于生成Tesouro嵌入组件使用的小部件令牌的服务器端帮助程序。薄荷糖的 RFC 8693 令牌交换请求,并将其加密为JWE (A256KW + A256GCM)使用您共享的小部件秘密。

如何安装 @tesouro/embedded-components-widget-token?

在项目目录运行 npm install @tesouro/embedded-components-widget-token,或使用 pnpm add / yarn add / bun add。

@tesouro/embedded-components-widget-token 支持 TypeScript 吗?

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

@tesouro/embedded-components-widget-token 支持 ESM 还是 CommonJS?

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

@tesouro/embedded-components-widget-token 可以商用吗?

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

@tesouro/embedded-components-widget-token 还在维护吗?

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