# Issue Token

> 签发新的 JWT 访问令牌或会话令牌。

Source: https://docs.sommatic.ai/zh/nodes/token.issue/

- 标识: `token.issue`
- 类别: 身份
- 版本: 2.0.0
- 状态: 可用
- 执行方式: sync

签发新的 JWT 访问令牌或会话令牌。

## 配置

- `connection_id` (string, 可选): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `jwt-signing`.
- `secret_key` (string, 可选): 签名密钥
- `payload` (object, 必填): 令牌声明（Claims）
- `ttl_sec` (integer, 可选): 默认值: `3600`.
- `algorithm` (string, 可选): 默认值: `HS256`. 可选值: `HS256`, `HS384`, `HS512`, `RS256`, `RS384`, `RS512`, `ES256`, `ES384`, `ES512`.

## 输出端口

- `out` (默认): 已签发的令牌
- `error`: 签发失败

## 输入契约

- `connection_id` (string, 可选)
- `secret_key` (string, 可选)
- `payload` (object, 必填)
- `ttl_sec` (integer, 可选)
- `algorithm` (string, 可选)

## 输出契约

- `token` (string, 可选)
- `expires_at` (string, 可选)

## 在表达式中

后续节点可通过 `$("Issue Token").item.json.<路径>` 读取此节点输出中的内容（请把 `Issue Token` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Issue Token").item.json.token` (string) = `"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWI…"`
- `$("Issue Token").item.json.expires_at` (string) = `"2026-11-26T15:19:05.000Z"`


## 工作原理

用你提供的声明签发 JWT，有效期为 `ttl_sec` 秒。支持 HS256/384/512、RS256/384/512 和 ES256/384/512；密钥来自 `jwt-signing` 凭据或字面量 `secret_key`。

1. 解析配置。声明取自 `payload`；若节点未配置 `payload`，则把节点输入项作为声明。
2. 当声明缺失或不是对象时，以 `payload is required` 从 `error` 输出。
3. `ttl_sec` 为正数时使用该值，否则使用 3600 秒。
4. 选择密钥：优先 `connection_id`，否则使用字面量 `secret_key`。
5. 使用 `algorithm`（默认 `HS256`）签名；令牌会带上 `iat` 和 `exp` 声明。
6. 从 `out` 返回 `{ token, expires_at }`，并附带 `meta.crypto` 和 `meta.warnings`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-issue-approval-token",
  "label": "Issue approval token",
  "operator_slug": "token.issue",
  "operator_version": "2.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "payload": {
      "sub": "{{ $json.user_id }}",
      "role": "approver"
    },
    "ttl_sec": 900,
    "algorithm": "HS256"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c3JfMTIzIiwicm9sZSI6ImFwcHJvdmVyIiwiaWF0IjoxNzk1NzI0MjEzLCJleHAiOjE3OTU3MjUxMTN9.<signature>",
    "expires_at": "2026-11-26T15:19:05.000Z"
  },
  "meta": {
    "crypto": {
      "op": "jwtSign",
      "key_source": "credential",
      "alias": "connection",
      "credential_id": "pi-credential-xxxx",
      "algorithm": "HS256",
      "ok": true
    },
    "warnings": []
  }
}
```

## 输出端口 (代码中的行为)

### `out`

令牌已签发。

```json
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c3JfMTIzIiwicm9sZSI6ImFwcHJvdmVyIiwiaWF0IjoxNzk1NzI0MjEzLCJleHAiOjE3OTU3MjUxMTN9.<signature>",
  "expires_at": "2026-11-26T15:19:05.000Z"
}
```

### `error`

声明缺失、算法或密钥无效，或凭据无法解析。

```json
{
  "error": "Unsupported JWT algorithm: PS256"
}
```

## 代码中的行为

- `connection_id`: 优先于 `secret_key`，在节点运行时于服务端解析。字符串密钥直接使用；对象密钥依次读取 `private_key`、`secret`、`key`。
- `secret_key`: 字面量密钥可以使用，但会保存在流程、版本历史和 Git 中，并且每次运行都会在 `meta.warnings` 中报告。建议使用 `connection_id`。
- `payload`: 实际上可不填：未配置时，整个输入项会成为声明。建议显式配置，以免签入不想要的字段。
- `ttl_sec`: 零、负数或非数字值会回退为 3600。所有令牌都会过期，不支持不带 `exp` 的令牌。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `payload is required` | `error` | `payload` 解析结果不是对象（例如混合文本得到的字符串）。 |
| `Unsupported JWT algorithm: <algorithm>` | `error` | `algorithm` 不在列出的九个值中。 |
| `Bad "options.expiresIn" option the payload already has an "exp" property.` | `error` | 声明中已包含 `exp`。请删除它：过期时间始终由 `ttl_sec` 决定。 |
| `secretOrPrivateKey must be an asymmetric key when using <algorithm>` | `error` | 选择了 RS* 或 ES* 算法却使用共享密钥。请使用对应类型的 PEM 私钥。 |
| `A key is required: pass { credential: "<alias>" } (recommended) or { key }` | `error` | `connection_id` 和字面量密钥字段都未设置。请在 `connection_id` 中选择一个凭据。 |
| `credential binding 'connection' has no key usable for sign` | `error` | 凭据已解析，但其密钥为空，或是缺少可用字段的对象。请将密钥存为字符串，或存为包含本操作所读字段的对象（见 `connection_id`）。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据 id 不存在，或该工作流无权使用。请检查 id 和凭据类型。 |

## 说明与限制

- `expires_at` 在签名后按 worker 时钟计算，可能与令牌的 `exp` 相差几毫秒。
- 受众、签发者和主题无法作为选项设置：如有需要，请把 `aud`、`iss` 或 `sub` 放入声明中。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "token.issue",
  "name": "Issue Token",
  "category": "identity",
  "version": "2.0.0",
  "description": "Issues a JWT signed with a Sommatic AI credential (recommended) or a literal secret.",
  "capabilities": [
    "authn",
    "jwt",
    "crypto"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Signing credential",
        "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `jwt-signing`.",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "jwt-signing"
          ]
        }
      },
      "secret_key": {
        "type": "string",
        "ui_widget": "password",
        "x-literal-secret": true,
        "description": "Signing secret or private key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
      },
      "payload": {
        "type": "object",
        "description": "Token claims"
      },
      "ttl_sec": {
        "type": "integer",
        "default": 3600
      },
      "algorithm": {
        "type": "string",
        "default": "HS256",
        "enum": [
          "HS256",
          "HS384",
          "HS512",
          "RS256",
          "RS384",
          "RS512",
          "ES256",
          "ES384",
          "ES512"
        ]
      }
    },
    "required": [
      "payload"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "secret_key": {
        "type": "string"
      },
      "payload": {
        "type": "object"
      },
      "ttl_sec": {
        "type": "integer"
      },
      "algorithm": {
        "type": "string"
      }
    },
    "required": [
      "payload"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "token": {
        "type": "string"
      },
      "expires_at": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Token",
      "description": "Issued Token",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Issuance failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "sync",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard"
  ],
  "tags": [
    "jwt",
    "auth"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_veripass",
    "profile": {
      "name": "Veripass",
      "verified": true,
      "url": "https://me.veripass.com/veripass",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "Signing credential",
      "description": "Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `jwt-signing`.",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "jwt-signing"
        ]
      }
    },
    "secret_key": {
      "type": "string",
      "ui_widget": "password",
      "x-literal-secret": true,
      "description": "Signing secret or private key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
    },
    "payload": {
      "type": "object",
      "description": "Token claims"
    },
    "ttl_sec": {
      "type": "integer",
      "default": 3600
    },
    "algorithm": {
      "type": "string",
      "default": "HS256",
      "enum": [
        "HS256",
        "HS384",
        "HS512",
        "RS256",
        "RS384",
        "RS512",
        "ES256",
        "ES384",
        "ES512"
      ]
    }
  },
  "required": [
    "payload"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "secret_key": {
      "type": "string"
    },
    "payload": {
      "type": "object"
    },
    "ttl_sec": {
      "type": "integer"
    },
    "algorithm": {
      "type": "string"
    }
  },
  "required": [
    "payload"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "token": {
      "type": "string"
    },
    "expires_at": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Token",
    "description": "Issued Token",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Issuance failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
