# Veripass Policy Evaluation

> 依据 Veripass 身份契约评估访问策略。

Source: https://docs.sommatic.ai/zh/nodes/veripass.evaluate/

- 标识: `veripass.evaluate`
- 类别: 身份
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

依据 Veripass 身份契约评估访问策略。

## 配置

- `connection_id` (string, 必填): 用于 Veripass 策略评估的凭据。接受 `api-key` 类型的凭据（Veripass API 密钥）。
- `policy_set` (string, 可选): 策略集 ID 或名称
- `resource` (string, 可选): 资源标识符
- `action` (string, 必填): 操作（read、write 等）
- `context` (object, 可选): 附加上下文（user_id、IP 等）

## 输出端口

- `allow` (默认): 已授予访问
- `deny`: 已拒绝访问
- `challenge`: 需要 MFA 或升级验证
- `error`: 评估错误

## 输入契约

- `connection_id` (string, 必填)
- `policy_set` (string, 可选)
- `resource` (string, 可选)
- `action` (string, 必填)
- `context` (object, 可选)

## 输出契约

- `decision` (string, 可选): 可选值: `allow`, `deny`, `challenge`.
- `reason` (string, 可选)
- `obligations` (array, 可选)

## 在表达式中

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

- `$("Veripass Policy Evaluation").item.json.decision` (string) = `"allow"`
- `$("Veripass Policy Evaluation").item.json.reason` (string) = `"Role approver may approve invoices"`
- `$("Veripass Policy Evaluation").item.json.obligations` (array) = `[…] (0)`


## 工作原理

向 Veripass 请求针对某资源上某操作的策略决策，并根据响应中的 `decision` 路由到 `allow`、`challenge` 或 `deny`。

1. 解析配置；当 `connection_id` 或 `action` 为空时从 `error` 输出。
2. 在服务端解析凭据：其配置提供 Veripass 基础 URL（`url` 或 `baseUrl`）和可选的额外 `headers`，其密钥用于认证。
3. 发送 `POST <基础 URL>/api/identity/contract/evaluate`，请求体为 `{ policy_set, resource, action, context }`（`context` 默认为 `{}`）。
4. 从响应体读取 `decision`、`reason` 和 `obligations`。
5. `allow` 路由到 `allow`，`challenge` 路由到 `challenge`。其他任何值（包括缺失）都走 `deny`，`decision` 为 `"deny"`，`reason` 默认为 `Policy denied`。
6. 网络错误或非 2xx 响应会从 `error` 输出，附带错误信息和响应体。

## 在工作流中

### 节点

```json
{
  "node_id": "node-authorize-approval",
  "label": "Authorize invoice approval",
  "operator_slug": "veripass.evaluate",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "policy_set": "finance-approvals",
    "resource": "invoice:{{ $json.invoice_id }}",
    "action": "approve",
    "context": "{{ $json.request_context }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "allow",
  "data": {
    "decision": "allow",
    "reason": "Role approver may approve invoices",
    "obligations": []
  }
}
```

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

### `allow`

Veripass 返回 `decision: "allow"`。

```json
{
  "decision": "allow",
  "reason": "Role approver may approve invoices",
  "obligations": []
}
```

### `challenge`

Veripass 返回 `decision: "challenge"`（例如需要升级认证）。

```json
{
  "decision": "challenge",
  "reason": "Step-up authentication required",
  "obligations": [
    "mfa"
  ]
}
```

### `deny`

Veripass 返回 `deny`、其他任何值，或未返回 `decision`。

```json
{
  "decision": "deny",
  "reason": "Policy denied"
}
```

### `error`

缺少 `connection_id` 或 `action`、凭据无法解析，或 HTTP 调用失败。

```json
{
  "error": "Request failed with status code 401",
  "response": {
    "message": "Unauthorized"
  }
}
```

## 代码中的行为

- `connection_id`: 凭据配置中必须以 `url`（或 `baseUrl`）提供 Veripass 基础 URL；其中的 `headers` 会添加到请求中。认证来自密钥：包含 `token` 的对象会发送 `Authorization: Bearer <token>`，包含 `apiKey` 的对象会发送 `x-api-key`。纯字符串密钥不会添加认证头。
- `context`: 原样发送。使用纯表达式（如 `{{ $json.request_context }}`）传入对象；混合文本会以字符串形式发送。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `connection_id is required` | `error` | 未选择 Veripass 凭据。 |
| `action is required` | `error` | `action` 为空，或其表达式未解析出任何值。 |
| `Request failed with status code <status>` | `error` | Veripass 拒绝了调用：401/403 通常表示凭据密钥中没有可用的 `token` 或 `apiKey`；404 表示基础 URL 不正确。`response` 中是 Veripass 返回的响应体。 |
| `Failed to resolve connection <connection_id>: <detail>` | `error` | 凭据 id 不存在，或该工作流无权使用。 |

## 说明与限制

- 未知或缺失的决策都走 `deny`，即默认拒绝。
- 返回的信封不包含 `ok` 字段，且该调用未设置请求超时。
- 从 `error` 端口输出不会使运行失败：引擎沿 `error` 上的连线继续；若没有连线，该分支直接结束。


## 契约（JSON Schema）

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

```json
{
  "slug": "veripass.evaluate",
  "name": "Veripass Policy Evaluation",
  "category": "identity",
  "version": "1.0.0",
  "description": "Evaluates access policies against Veripass Identity Contract.",
  "capabilities": [
    "authz",
    "policy",
    "veripass",
    "identity"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "Veripass Credential",
        "description": "Credential for Veripass policy evaluation. Accepts credentials of type `api-key` (Veripass API key).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "api-key"
          ],
          "types": [
            "veripass",
            "http"
          ]
        }
      },
      "policy_set": {
        "type": "string",
        "description": "Policy Set ID or Name"
      },
      "resource": {
        "type": "string",
        "description": "Resource identifier"
      },
      "action": {
        "type": "string",
        "description": "Action (read, write, etc.)"
      },
      "context": {
        "type": "object",
        "description": "Additional context (user_id, IP, etc.)"
      }
    },
    "required": [
      "connection_id",
      "action"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "policy_set": {
        "type": "string"
      },
      "resource": {
        "type": "string"
      },
      "action": {
        "type": "string"
      },
      "context": {
        "type": "object"
      }
    },
    "required": [
      "connection_id",
      "action"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "decision": {
        "type": "string",
        "enum": [
          "allow",
          "deny",
          "challenge"
        ]
      },
      "reason": {
        "type": "string"
      },
      "obligations": {
        "type": "array"
      }
    }
  },
  "ports": [
    {
      "port_id": "allow",
      "label": "Allow",
      "description": "Access granted",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "deny",
      "label": "Deny",
      "description": "Access denied",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "challenge",
      "label": "Challenge",
      "description": "MFA or step-up required",
      "is_default": false,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Evaluation error",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "identity-worker",
    "standard"
  ],
  "tags": [
    "veripass",
    "security",
    "policy"
  ],
  "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": "Veripass Credential",
      "description": "Credential for Veripass policy evaluation. Accepts credentials of type `api-key` (Veripass API key).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "api-key"
        ],
        "types": [
          "veripass",
          "http"
        ]
      }
    },
    "policy_set": {
      "type": "string",
      "description": "Policy Set ID or Name"
    },
    "resource": {
      "type": "string",
      "description": "Resource identifier"
    },
    "action": {
      "type": "string",
      "description": "Action (read, write, etc.)"
    },
    "context": {
      "type": "object",
      "description": "Additional context (user_id, IP, etc.)"
    }
  },
  "required": [
    "connection_id",
    "action"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "policy_set": {
      "type": "string"
    },
    "resource": {
      "type": "string"
    },
    "action": {
      "type": "string"
    },
    "context": {
      "type": "object"
    }
  },
  "required": [
    "connection_id",
    "action"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "decision": {
      "type": "string",
      "enum": [
        "allow",
        "deny",
        "challenge"
      ]
    },
    "reason": {
      "type": "string"
    },
    "obligations": {
      "type": "array"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "allow",
    "label": "Allow",
    "description": "Access granted",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "deny",
    "label": "Deny",
    "description": "Access denied",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "challenge",
    "label": "Challenge",
    "description": "MFA or step-up required",
    "is_default": false,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Evaluation error",
    "is_default": false,
    "supports_condition": false
  }
]
```
