# Inspect Token

> 解码并验证 JWT。

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

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

解码并验证 JWT。

## 配置

- `token` (string, 必填)
- `connection_id` (string, 可选): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `jwt-signing`.
- `secret_key` (string, 可选): 可选的验证密钥
- `algorithm` (string, 可选): 默认值: `HS256`. 可选值: `HS256`, `HS384`, `HS512`, `RS256`, `RS384`, `RS512`, `ES256`, `ES384`, `ES512`.

## 输出端口

- `out` (默认): 令牌有效或已解码
- `invalid`: 验证失败
- `error`: 处理错误

## 输入契约

- `token` (string, 必填)
- `connection_id` (string, 可选)
- `secret_key` (string, 可选)
- `algorithm` (string, 可选)

## 输出契约

- `valid` (boolean, 可选)
- `verified` (boolean, 可选)
- `payload` (object, 可选)
- `header` (object, 可选)
- `error` (string, 可选)

## 在表达式中

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

- `$("Inspect Token").item.json.valid` (boolean) = `true`
- `$("Inspect Token").item.json.verified` (boolean) = `true`
- `$("Inspect Token").item.json.payload` (object) = `{…}`
- `$("Inspect Token").item.json.header` (object) = `{…}`
- `$("Inspect Token").item.json.error` (string) **worker 不返回：读取结果为 undefined.**
- `$("Inspect Token").item.json.payload.sub` (string) = `"usr_123"` (编辑器不提示)
- `$("Inspect Token").item.json.payload.role` (string) = `"approver"` (编辑器不提示)
- `$("Inspect Token").item.json.payload.iat` (number) = `1795724213` (编辑器不提示)
- `$("Inspect Token").item.json.payload.exp` (number) = `1795725113` (编辑器不提示)
- `$("Inspect Token").item.json.header.alg` (string) = `"HS256"` (编辑器不提示)
- `$("Inspect Token").item.json.header.typ` (string) = `"JWT"` (编辑器不提示)


## 工作原理

使用你提供的密钥校验 JWT 签名及时间声明，并返回声明和头部。未配置密钥时只解码令牌，并从 `invalid` 输出。

1. 解析配置；当 `token` 为空时，以 `token is required` 从 `error` 输出。
2. 选择密钥：优先 `connection_id`，否则使用字面量 `secret_key`。
3. 没有密钥时，不做校验直接解码令牌，从 `invalid` 返回其声明和头部，绝不会标记为有效。
4. 否则仅允许 `algorithm`（默认 `HS256`）校验签名，并检查 `exp` 和 `nbf`。
5. 有效时从 `out` 返回 `{ valid: true, verified: true, payload, header }`；无效时从 `invalid` 返回 `{ valid: false, verified: false, error }`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-verify-approval-token",
  "label": "Verify approval token",
  "operator_slug": "token.inspect",
  "operator_version": "2.0.0",
  "config": {
    "token": "{{ $json.token }}",
    "connection_id": "pi-credential-xxxx",
    "algorithm": "HS256"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "valid": true,
    "verified": true,
    "payload": {
      "sub": "usr_123",
      "role": "approver",
      "iat": 1795724213,
      "exp": 1795725113
    },
    "header": {
      "alg": "HS256",
      "typ": "JWT"
    }
  },
  "meta": {
    "crypto": {
      "op": "jwtVerify",
      "key_source": "credential",
      "alias": "connection",
      "credential_id": "pi-credential-xxxx",
      "algorithm": "HS256",
      "ok": true
    },
    "warnings": []
  }
}
```

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

### `out`

签名和时间声明均有效。

```json
{
  "valid": true,
  "verified": true,
  "payload": {
    "sub": "usr_123",
    "role": "approver",
    "iat": 1795724213,
    "exp": 1795725113
  },
  "header": {
    "alg": "HS256",
    "typ": "JWT"
  }
}
```

### `invalid`

校验失败（签名错误、已过期、格式错误、算法不同），或未配置密钥。未配置密钥时，数据还会包含未经校验的 `payload` 和 `header`，且没有 `meta`。

```json
{
  "valid": false,
  "verified": false,
  "error": "jwt expired"
}
```

### `error`

`token` 为空，或凭据无法解析。

```json
{
  "error": "token is required"
}
```

## 代码中的行为

- `connection_id`: 优先于 `secret_key`，在节点运行时于服务端解析。字符串密钥直接使用；对象密钥依次读取 `public_key`、`secret`、`key`。
- `secret_key`: 字面量密钥可以使用，但会保存在流程、版本历史和 Git 中，并且每次运行都会在 `meta.warnings` 中报告。建议使用 `connection_id`。
- `algorithm`: 只接受一个算法。使用其他算法签名的令牌会走 `invalid`。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `token is required` | `error` | `token` 为空。 |
| `invalid signature` | `invalid` | 令牌使用了其他密钥签名。 |
| `jwt expired` | `invalid` | `exp` 声明的时间已过。 |
| `jwt malformed` | `invalid` | 该值不是 JWT（三段 base64url）。 |
| `invalid algorithm` | `invalid` | 令牌头部使用的算法与 `algorithm` 不同。请把 `algorithm` 设为签发方使用的算法。 |
| `Signature not verified: no key configured` | `invalid` | `connection_id` 和 `secret_key` 都未设置，因此只解码了令牌。 |
| `credential binding 'connection' has no key usable for verify` | `invalid` | 凭据已解析，但其密钥为空，或是缺少可用字段的对象。请将密钥存为字符串，或存为包含本操作所读字段的对象（见 `connection_id`）。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据 id 不存在，或该工作流无权使用。请检查 id 和凭据类型。 |

## 说明与限制

- 定义中把 `out` 描述为“有效或已解码”，但仅解码（无密钥）的令牌总是走 `invalid`。
- 不会校验受众（`aud`）和签发者（`iss`）：请在 `out` 之后用 `flow.if` 对 `payload` 自行比较。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（JSON Schema）

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

```json
{
  "slug": "token.inspect",
  "name": "Inspect Token",
  "category": "identity",
  "version": "2.0.0",
  "description": "Verifies a JWT with a Sommatic AI credential (recommended) or a literal key. Without a key the token is never reported as valid.",
  "capabilities": [
    "authn",
    "jwt",
    "crypto"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "token": {
        "type": "string"
      },
      "connection_id": {
        "type": "string",
        "title": "Verification 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": "Verification secret or public key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
      },
      "algorithm": {
        "type": "string",
        "default": "HS256",
        "enum": [
          "HS256",
          "HS384",
          "HS512",
          "RS256",
          "RS384",
          "RS512",
          "ES256",
          "ES384",
          "ES512"
        ]
      }
    },
    "required": [
      "token"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "token": {
        "type": "string"
      },
      "connection_id": {
        "type": "string"
      },
      "secret_key": {
        "type": "string"
      },
      "algorithm": {
        "type": "string"
      }
    },
    "required": [
      "token"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "valid": {
        "type": "boolean"
      },
      "verified": {
        "type": "boolean"
      },
      "payload": {
        "type": "object"
      },
      "header": {
        "type": "object"
      },
      "error": {
        "type": "string"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Token Data",
      "description": "Token is valid or decoded",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "invalid",
      "label": "Invalid",
      "description": "Verification failed",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Processing error",
      "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": {
    "token": {
      "type": "string"
    },
    "connection_id": {
      "type": "string",
      "title": "Verification 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": "Verification secret or public key as a literal. Allowed, but it is stored in the flow, its version history and Git: prefer connection_id."
    },
    "algorithm": {
      "type": "string",
      "default": "HS256",
      "enum": [
        "HS256",
        "HS384",
        "HS512",
        "RS256",
        "RS384",
        "RS512",
        "ES256",
        "ES384",
        "ES512"
      ]
    }
  },
  "required": [
    "token"
  ]
}
```

### input_contract_schema

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

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "valid": {
      "type": "boolean"
    },
    "verified": {
      "type": "boolean"
    },
    "payload": {
      "type": "object"
    },
    "header": {
      "type": "object"
    },
    "error": {
      "type": "string"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Token Data",
    "description": "Token is valid or decoded",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "invalid",
    "label": "Invalid",
    "description": "Verification failed",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Processing error",
    "is_default": false,
    "supports_condition": false
  }
]
```
