# Inspect Token

> Verifies a JWT with a Sommatic AI credential (recommended) or a literal key.

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

- Slug: `token.inspect`
- Family: Identity
- Version: 2.0.0
- Status: Available
- Execution: sync

Verifies a JWT with a Sommatic AI credential (recommended) or a literal key. Without a key the token is never reported as valid.

## Configuration

- `token` (string, required)
- `connection_id` (string, optional): Sommatic AI credential resolved server-side (recommended). Accepts credentials of type `jwt-signing`.
- `secret_key` (string, optional): 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` (string, optional): Default: `HS256`. Allowed values: `HS256`, `HS384`, `HS512`, `RS256`, `RS384`, `RS512`, `ES256`, `ES384`, `ES512`.

## Output ports

- `out` (default): Token is valid or decoded
- `invalid`: Verification failed
- `error`: Processing error

## Input contract

- `token` (string, required)
- `connection_id` (string, optional)
- `secret_key` (string, optional)
- `algorithm` (string, optional)

## Output contract

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

## In expressions

What a later node can read from this node's output with `$("Inspect Token").item.json.<path>` (write your node's label instead of `Inspect Token`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("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) **The worker does not return it: reads as undefined.**
- `$("Inspect Token").item.json.payload.sub` (string) = `"usr_123"` (Not suggested by the editor)
- `$("Inspect Token").item.json.payload.role` (string) = `"approver"` (Not suggested by the editor)
- `$("Inspect Token").item.json.payload.iat` (number) = `1795724213` (Not suggested by the editor)
- `$("Inspect Token").item.json.payload.exp` (number) = `1795725113` (Not suggested by the editor)
- `$("Inspect Token").item.json.header.alg` (string) = `"HS256"` (Not suggested by the editor)
- `$("Inspect Token").item.json.header.typ` (string) = `"JWT"` (Not suggested by the editor)


## How it works

Verifies a JWT signature and its time claims with the key you provide, and returns the claims and header. Without a key it only decodes the token and sends it to `invalid`.

1. Resolves the config; leaves through `error` with `token is required` when `token` is empty.
2. Picks the key: `connection_id` first, otherwise the literal `secret_key`.
3. With no key, decodes the token without checking it and returns its claims and header on `invalid`, never as valid.
4. Otherwise verifies the signature allowing only `algorithm` (default `HS256`), and checks `exp` and `nbf`.
5. Valid: returns `{ valid: true, verified: true, payload, header }` on `out`. Not valid: returns `{ valid: false, verified: false, error }` on `invalid`.

## In a workflow

### Node

```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": []
  }
}
```

## Output ports (behaviour in the code)

### `out`

The signature and time claims are valid.

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

### `invalid`

Verification failed (bad signature, expired, malformed, other algorithm), or no key is configured. Without a key the data also carries the unverified `payload` and `header`, and there is no `meta`.

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

### `error`

`token` is empty or the credential cannot be resolved.

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

## Behaviour in the code

- `connection_id`: Takes precedence over `secret_key`. Resolved on the server when the node runs. A string secret is used as the key; an object secret is read as `public_key`, then `secret`, then `key`.
- `secret_key`: A literal key works, but it is stored in the flow, its version history and Git, and `meta.warnings` reports it on every run. Prefer `connection_id`.
- `algorithm`: Exactly one algorithm is accepted. A token signed with any other algorithm goes to `invalid`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `token is required` | `error` | `token` is empty. |
| `invalid signature` | `invalid` | The token was signed with a different key. |
| `jwt expired` | `invalid` | The `exp` claim is in the past. |
| `jwt malformed` | `invalid` | The value is not a JWT (three base64url parts). |
| `invalid algorithm` | `invalid` | The token header uses an algorithm other than `algorithm`. Set `algorithm` to the one the issuer uses. |
| `Signature not verified: no key configured` | `invalid` | Neither `connection_id` nor `secret_key` is set, so the token was only decoded. |
| `credential binding 'connection' has no key usable for verify` | `invalid` | The credential resolved, but its secret is empty or is an object without a usable field. Store the key as a string, or as an object with the field this operation reads (see `connection_id`). |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | The credential id does not exist or is not available to this workflow. Check the id and the credential type. |

## Notes and limits

- The definition describes `out` as "valid or decoded", but decoded-only tokens (no key) always go to `invalid`.
- Audience (`aud`) and issuer (`iss`) are not checked: compare them yourself with a `flow.if` on `payload` after `out`.
- Leaving through `error` does not fail the run: the engine follows the edges wired to `error`, and if there are none the branch simply ends.


## Contracts (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
  }
]
```
