- 概述
- JavaScript 函数
- Python 函数
- 部署并运行
函数上下文
运行时传递给每个 JavaScript 函数处理程序的身份、平台坐标和请求数据。
每个处理程序都会收到一个上下文对象作为其第二个参数。它包含函数的调用者、函数自己的平台身份以及出站平台调用的发送位置,因此不必将这些内容作为输入传递。
handler: async (input, ctx) => {
ctx.user?.accessToken // the caller's OAuth token
ctx.user?.sub // the caller's user id
ctx.robot?.accessToken // the function's own platform token
ctx.robot?.key // the robot key
ctx.platform?.baseUrl // e.g. "https://<automation-suite-fqdn>"
ctx.platform?.orgId // organization id
ctx.platform?.tenantId // tenant id
ctx.platform?.folderKey // folder of the invocation, if any
ctx.params // path parameters, as strings
ctx.headers // request headers, lowercase keys
}
handler: async (input, ctx) => {
ctx.user?.accessToken // the caller's OAuth token
ctx.user?.sub // the caller's user id
ctx.robot?.accessToken // the function's own platform token
ctx.robot?.key // the robot key
ctx.platform?.baseUrl // e.g. "https://<automation-suite-fqdn>"
ctx.platform?.orgId // organization id
ctx.platform?.tenantId // tenant id
ctx.platform?.folderKey // folder of the invocation, if any
ctx.params // path parameters, as strings
ctx.headers // request headers, lowercase keys
}
两个身份
一个函数每次调用时都会被赋予两个身份,在这两种身份之间进行选择即为安全决策。
ctx.user | ctx.robot | |
|---|---|---|
| 所有权归属 | 调用函数的调用者 | 函数自己的平台标识 |
| 适用的权限 | 调用者自己的 | 函数的服务帐户 |
| 用于 | 代表已登录的用户执行操作 | 正在读取调用者不得直接访问的资源 |
ctx.user.accessToken 适用于调用者希望仅查看其自身权限允许的内容的情况。当函数必须访问调用者无法访问的内容(例如,受限文件夹中的凭据)时,适用 ctx.robot.accessToken。请参阅访问平台服务。
ctx.robot is the more privileged identity. A handler that reads a caller-supplied resource name is acting as a deputy for the caller's request with the function's own privileges. Fetching must be constrained to a fixed set in code.
平台坐标
ctx.platform 为出站平台调用提供 baseUrl、orgId 和 tenantId。这些来自运行时,而非调用者,因此请求无法重定向函数将其流量发送到的位置,并且绝不会将其接受为输入字段。
ctx.platform is null unless all three are available. folderKey can be null on its own, for a folderless invocation.
可用位置
| HTTP 触发器 | 作业 | 本地 serve | |
|---|---|---|---|
ctx.user | 调用者的身份 | 无调用者身份 | 当请求包含持有者令牌时显示 |
ctx.robot | 是 | 是 | null |
ctx.platform | 是 | 是 | 仅适用于设置了 UIPATH_BASE_URL、UIPATH_ORG_ID 和 UIPATH_TENANT_ID 的情况 |
ctx.params, ctx.headers | 是 | 空 — 没有 HTTP 请求 | 是 |
由于 ctx.robot 在本地为 null,因此需要平台令牌的代码应回退到环境变量,以进行本地开发:
const token = ctx.robot?.accessToken || process.env["UIPATH_ACCESS_TOKEN"] || "";
const token = ctx.robot?.accessToken || process.env["UIPATH_ACCESS_TOKEN"] || "";