@canopy-io/node 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +39 -0
- package/dist/index.cjs +133 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23448 -9697
- package/dist/index.d.ts +23448 -9697
- package/dist/index.js +130 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -117,6 +117,7 @@ var CanopyClient = class {
|
|
|
117
117
|
maxBackoffMs;
|
|
118
118
|
authHeaders;
|
|
119
119
|
extraHeaders;
|
|
120
|
+
environmentId;
|
|
120
121
|
fetchImpl;
|
|
121
122
|
constructor(options = {}) {
|
|
122
123
|
if (!options.apiKey && !options.accessToken) {
|
|
@@ -129,6 +130,7 @@ var CanopyClient = class {
|
|
|
129
130
|
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
130
131
|
this.maxBackoffMs = options.maxBackoffMs ?? DEFAULT_MAX_BACKOFF_MS;
|
|
131
132
|
this.extraHeaders = options.headers ?? {};
|
|
133
|
+
this.environmentId = options.environmentId;
|
|
132
134
|
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
133
135
|
if (typeof this.fetchImpl !== "function") {
|
|
134
136
|
throw new TypeError(
|
|
@@ -262,6 +264,10 @@ var CanopyClient = class {
|
|
|
262
264
|
if (options.body !== void 0) {
|
|
263
265
|
headers["Content-Type"] = "application/json";
|
|
264
266
|
}
|
|
267
|
+
const environmentId = options.environmentId === void 0 ? this.environmentId : options.environmentId;
|
|
268
|
+
if (environmentId) {
|
|
269
|
+
headers["X-Canopy-Environment"] = environmentId;
|
|
270
|
+
}
|
|
265
271
|
Object.assign(headers, options.headers, this.authHeaders);
|
|
266
272
|
const init = { method, headers, signal: controller.signal };
|
|
267
273
|
if (options.body !== void 0) {
|
|
@@ -1703,6 +1709,29 @@ var LocalAuthorizer = class {
|
|
|
1703
1709
|
return answers;
|
|
1704
1710
|
}
|
|
1705
1711
|
async decide(query) {
|
|
1712
|
+
const decision = await this.decideFor(query);
|
|
1713
|
+
if (!decision.allowed || query.acting_client_id === void 0) {
|
|
1714
|
+
return decision;
|
|
1715
|
+
}
|
|
1716
|
+
if (query.acting_client_id === "") {
|
|
1717
|
+
return { allowed: false, reason: "acting_client_not_granted" };
|
|
1718
|
+
}
|
|
1719
|
+
const its = await this.decideFor({
|
|
1720
|
+
identity_id: query.acting_client_id,
|
|
1721
|
+
principal_type: "service",
|
|
1722
|
+
permission: query.permission,
|
|
1723
|
+
...query.scope ? { scope: query.scope } : {},
|
|
1724
|
+
...query.node_id ? { node_id: query.node_id } : {}
|
|
1725
|
+
});
|
|
1726
|
+
if (its.allowed) {
|
|
1727
|
+
return its;
|
|
1728
|
+
}
|
|
1729
|
+
return {
|
|
1730
|
+
allowed: false,
|
|
1731
|
+
reason: its.reason === "denied" ? "denied" : "acting_client_not_granted"
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
async decideFor(query) {
|
|
1706
1735
|
const now = this.now();
|
|
1707
1736
|
const holdings = await this.grantRootsFor(
|
|
1708
1737
|
query.identity_id,
|
|
@@ -2210,6 +2239,84 @@ function authorizationErrorStatus(error) {
|
|
|
2210
2239
|
return { status: 500, message: "Authorization could not be evaluated." };
|
|
2211
2240
|
}
|
|
2212
2241
|
|
|
2242
|
+
// src/exchange.ts
|
|
2243
|
+
var TOKEN_EXCHANGE_GRANT = "urn:ietf:params:oauth:grant-type:token-exchange";
|
|
2244
|
+
var ACCESS_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token";
|
|
2245
|
+
var TOKEN_PATH = "/oauth/token";
|
|
2246
|
+
async function exchangeToken(options) {
|
|
2247
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
2248
|
+
const origin = withoutTrailingSlashes(options.baseUrl ?? hostedOrigin());
|
|
2249
|
+
const request = { method: "POST", path: TOKEN_PATH };
|
|
2250
|
+
const body = new URLSearchParams({
|
|
2251
|
+
grant_type: TOKEN_EXCHANGE_GRANT,
|
|
2252
|
+
subject_token: options.subjectToken,
|
|
2253
|
+
subject_token_type: ACCESS_TOKEN_TYPE
|
|
2254
|
+
});
|
|
2255
|
+
if (options.audience !== void 0) {
|
|
2256
|
+
body.set("audience", options.audience);
|
|
2257
|
+
}
|
|
2258
|
+
if (options.scope !== void 0) {
|
|
2259
|
+
body.set("scope", options.scope);
|
|
2260
|
+
}
|
|
2261
|
+
const credentials = btoa(
|
|
2262
|
+
`${encodeURIComponent(options.clientId)}:${encodeURIComponent(options.clientSecret)}`
|
|
2263
|
+
);
|
|
2264
|
+
let response;
|
|
2265
|
+
try {
|
|
2266
|
+
response = await fetchImpl(`${origin}${TOKEN_PATH}`, {
|
|
2267
|
+
method: "POST",
|
|
2268
|
+
headers: {
|
|
2269
|
+
Authorization: `Basic ${credentials}`,
|
|
2270
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
2271
|
+
Accept: "application/json"
|
|
2272
|
+
},
|
|
2273
|
+
body: body.toString(),
|
|
2274
|
+
...options.signal ? { signal: options.signal } : {}
|
|
2275
|
+
});
|
|
2276
|
+
} catch (error) {
|
|
2277
|
+
if (options.signal?.aborted) {
|
|
2278
|
+
throw error;
|
|
2279
|
+
}
|
|
2280
|
+
throw new CanopyConnectionError(
|
|
2281
|
+
`Could not reach ${origin}${TOKEN_PATH}.`,
|
|
2282
|
+
request,
|
|
2283
|
+
{ cause: error }
|
|
2284
|
+
);
|
|
2285
|
+
}
|
|
2286
|
+
const payload = await response.json().catch(() => null);
|
|
2287
|
+
if (!response.ok) {
|
|
2288
|
+
const error = typeof payload?.["error"] === "string" ? payload["error"] : null;
|
|
2289
|
+
const description = typeof payload?.["error_description"] === "string" ? payload["error_description"] : `The token endpoint answered ${response.status}.`;
|
|
2290
|
+
const retryAfter = Number(response.headers.get("retry-after"));
|
|
2291
|
+
throw new CanopyError(
|
|
2292
|
+
{
|
|
2293
|
+
statusCode: response.status,
|
|
2294
|
+
code: error ? `oauth.${error}` : null,
|
|
2295
|
+
message: description
|
|
2296
|
+
},
|
|
2297
|
+
request,
|
|
2298
|
+
Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter * 1e3 : void 0
|
|
2299
|
+
);
|
|
2300
|
+
}
|
|
2301
|
+
if (typeof payload?.["access_token"] !== "string" || typeof payload["expires_in"] !== "number") {
|
|
2302
|
+
throw new CanopyError(
|
|
2303
|
+
{
|
|
2304
|
+
statusCode: response.status,
|
|
2305
|
+
code: null,
|
|
2306
|
+
message: "The token endpoint answered without an access token."
|
|
2307
|
+
},
|
|
2308
|
+
request
|
|
2309
|
+
);
|
|
2310
|
+
}
|
|
2311
|
+
return {
|
|
2312
|
+
accessToken: payload["access_token"],
|
|
2313
|
+
tokenType: "Bearer",
|
|
2314
|
+
expiresIn: payload["expires_in"],
|
|
2315
|
+
...typeof payload["scope"] === "string" ? { scope: payload["scope"] } : {},
|
|
2316
|
+
issuedTokenType: typeof payload["issued_token_type"] === "string" ? payload["issued_token_type"] : ACCESS_TOKEN_TYPE
|
|
2317
|
+
};
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2213
2320
|
// src/verify.ts
|
|
2214
2321
|
var DEFAULT_JWKS_CACHE_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
2215
2322
|
var DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS = 30 * 1e3;
|
|
@@ -2218,6 +2325,7 @@ var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
|
2218
2325
|
"user",
|
|
2219
2326
|
"identity",
|
|
2220
2327
|
"service",
|
|
2328
|
+
"delegated",
|
|
2221
2329
|
"api_key",
|
|
2222
2330
|
"platform"
|
|
2223
2331
|
]);
|
|
@@ -2252,6 +2360,16 @@ function orgContext(claims) {
|
|
|
2252
2360
|
staff: claims.org_scope === "all"
|
|
2253
2361
|
};
|
|
2254
2362
|
}
|
|
2363
|
+
function delegation(claims) {
|
|
2364
|
+
if (claims.type !== "delegated") {
|
|
2365
|
+
return null;
|
|
2366
|
+
}
|
|
2367
|
+
const actingClientId = claims.act?.sub;
|
|
2368
|
+
if (typeof actingClientId !== "string" || actingClientId === "") {
|
|
2369
|
+
return null;
|
|
2370
|
+
}
|
|
2371
|
+
return { identityId: claims.sub, actingClientId };
|
|
2372
|
+
}
|
|
2255
2373
|
function decodeBase64Url(value) {
|
|
2256
2374
|
const padded = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
2257
2375
|
const binary = atob(padded.padEnd(Math.ceil(padded.length / 4) * 4, "="));
|
|
@@ -2386,6 +2504,12 @@ var TokenVerifier = class {
|
|
|
2386
2504
|
`Token has an unrecognised \`type\` claim: ${JSON.stringify(claims.type)}.`
|
|
2387
2505
|
);
|
|
2388
2506
|
}
|
|
2507
|
+
if (claims.type === "delegated" && (typeof claims.act?.sub !== "string" || claims.act.sub === "")) {
|
|
2508
|
+
throw new CanopyTokenError(
|
|
2509
|
+
"token.malformed",
|
|
2510
|
+
"Token is delegated but has no `act.sub` claim naming the client acting for its subject."
|
|
2511
|
+
);
|
|
2512
|
+
}
|
|
2389
2513
|
const accepted = /* @__PURE__ */ new Set([
|
|
2390
2514
|
...this.allowedTypes,
|
|
2391
2515
|
...options.alsoAllow ?? []
|
|
@@ -2437,16 +2561,17 @@ var TokenVerifier = class {
|
|
|
2437
2561
|
* ignoring it would accept a token minted for a different client. So it
|
|
2438
2562
|
* throws and names the option.
|
|
2439
2563
|
*
|
|
2440
|
-
* A
|
|
2441
|
-
* is held to `serviceAudience` alone,
|
|
2564
|
+
* A machine client's token (`service` or `delegated`) names the API it was
|
|
2565
|
+
* minted for, never a client, so it is held to `serviceAudience` alone,
|
|
2566
|
+
* which it must name.
|
|
2442
2567
|
*/
|
|
2443
2568
|
assertAudience(claims) {
|
|
2444
2569
|
const audiences = claims.aud === void 0 ? [] : Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
2445
|
-
if (claims.type === "service") {
|
|
2570
|
+
if (claims.type === "service" || claims.type === "delegated") {
|
|
2446
2571
|
if (this.serviceAudience === void 0) {
|
|
2447
2572
|
throw new CanopyTokenError(
|
|
2448
2573
|
"token.audience_unverified",
|
|
2449
|
-
|
|
2574
|
+
`This is a machine client's ${claims.type} token, and the verifier has no \`serviceAudience\` configured. Set \`serviceAudience\` to the audience your API answers to.`
|
|
2450
2575
|
);
|
|
2451
2576
|
}
|
|
2452
2577
|
if (!audiences.includes(this.serviceAudience)) {
|
|
@@ -2688,6 +2813,6 @@ function hexToBytes(hex) {
|
|
|
2688
2813
|
return bytes;
|
|
2689
2814
|
}
|
|
2690
2815
|
|
|
2691
|
-
export { Assignments, Canopy, CanopyAuthorizerError, CanopyClient, CanopyConnectionError, CanopyError, CanopyTokenError, DEFAULT_MAX_NODES, Identities, LocalAuthorizer, MAX_TTL_MS, Organizations, Paginator, Permissions, Roles, TokenVerifier, WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS, authorizationErrorStatus, isCanopyAuthorizerError, isCanopyConnectionError, isCanopyError, isCanopyTokenError, isCursorPagination, isWebhookEvent, orgContext, paginate, tokenErrorStatus, verifyWebhookSignature, withConcurrency };
|
|
2816
|
+
export { ACCESS_TOKEN_TYPE, Assignments, Canopy, CanopyAuthorizerError, CanopyClient, CanopyConnectionError, CanopyError, CanopyTokenError, DEFAULT_MAX_NODES, Identities, LocalAuthorizer, MAX_TTL_MS, Organizations, Paginator, Permissions, Roles, TOKEN_EXCHANGE_GRANT, TokenVerifier, WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS, authorizationErrorStatus, delegation, exchangeToken, isCanopyAuthorizerError, isCanopyConnectionError, isCanopyError, isCanopyTokenError, isCursorPagination, isWebhookEvent, orgContext, paginate, tokenErrorStatus, verifyWebhookSignature, withConcurrency };
|
|
2692
2817
|
//# sourceMappingURL=index.js.map
|
|
2693
2818
|
//# sourceMappingURL=index.js.map
|