Skip to content

Commit 1a040da

Browse files
authored
docs: document script runtime web APIs (#1490)
1 parent 49ebb19 commit 1a040da

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

docs/developers/actions/README.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ The payload contains:
6262
- `event`: The production authentication event. Its shape depends on the action type.
6363
- `environmentVariables`: The string values configured for this action. These values are passed through the function payload; they are not available through `process.env`.
6464

65-
The editor provides type information, but the saved script is executed as JavaScript. The script may be asynchronous and can use the injected `fetch` function to call external HTTPS APIs. It cannot import packages or access Node.js globals such as `require` or `process`.
65+
The editor provides type information, but the saved script is executed as JavaScript. The script may be asynchronous and can use these standard Web APIs in both Logto Cloud and self-hosted Logto:
66+
67+
- `fetch`, `Request`, `Response`, and `Headers`
68+
- Web Crypto through `crypto` and `crypto.subtle`
69+
- `TextEncoder` and `TextDecoder`
70+
- `URL` and `URLSearchParams`
71+
72+
Scripts cannot import packages. Avoid Node.js-specific globals and modules because they are not portable between self-hosted Logto and Logto Cloud and are not part of the supported script contract.
6673

6774
The supported result is different for each action type; see the corresponding reference page before enabling an Action.
6875

docs/developers/custom-token-claims/create-script.mdx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,43 @@ api.denyAccess(message?: string): void
257257

258258
The `api.denyAccess()` function allows you to deny the token issuing process with a custom message. You may use this function to enforce additional access validation over the token issuing process.
259259

260+
## Runtime APIs \{#runtime-apis}
261+
262+
Custom access token scripts can use these standard Web APIs in both Logto Cloud and self-hosted Logto:
263+
264+
- `fetch`, `Request`, `Response`, and `Headers`
265+
- Web Crypto through `crypto` and `crypto.subtle`
266+
- `TextEncoder` and `TextDecoder`
267+
- `URL` and `URLSearchParams`
268+
269+
For example, you can compute SHA-256 and HMAC-SHA-256 values with Web Crypto:
270+
271+
```js
272+
const toHex = (buffer) =>
273+
[...new Uint8Array(buffer)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
274+
275+
const sha256 = async (input) =>
276+
toHex(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input)));
277+
278+
const hmacSha256 = async (key, input) => {
279+
const encoder = new TextEncoder();
280+
const cryptoKey = await crypto.subtle.importKey(
281+
'raw',
282+
encoder.encode(key),
283+
{ name: 'HMAC', hash: 'SHA-256' },
284+
false,
285+
['sign']
286+
);
287+
288+
return toHex(await crypto.subtle.sign('HMAC', cryptoKey, encoder.encode(input)));
289+
};
290+
```
291+
292+
Scripts cannot import packages. Avoid Node.js-specific globals and modules because they are not portable between self-hosted Logto and Logto Cloud and are not part of the supported script contract.
293+
260294
## Step 3: Fetch external data \{#step-3-fetch-external-data}
261295

262-
You may use the node built-in `fetch` function to fetch external data in your script. The `fetch` function is a promise-based function that allows you to make HTTP requests to external APIs.
296+
You may use the standard `fetch` function to fetch external data in your script. The `fetch` function is a promise-based function that allows you to make HTTP requests to external APIs.
263297

264298
```jsx
265299
const getCustomJwtClaims = async ({ environmentVariables }) => {

0 commit comments

Comments
 (0)