@canopy-io/node 0.21.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 +6 -0
- package/README.md +39 -0
- package/dist/index.cjs +127 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +186 -42
- package/dist/index.d.ts +186 -42
- package/dist/index.js +124 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1709,6 +1709,29 @@ var LocalAuthorizer = class {
|
|
|
1709
1709
|
return answers;
|
|
1710
1710
|
}
|
|
1711
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) {
|
|
1712
1735
|
const now = this.now();
|
|
1713
1736
|
const holdings = await this.grantRootsFor(
|
|
1714
1737
|
query.identity_id,
|
|
@@ -2216,6 +2239,84 @@ function authorizationErrorStatus(error) {
|
|
|
2216
2239
|
return { status: 500, message: "Authorization could not be evaluated." };
|
|
2217
2240
|
}
|
|
2218
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
|
+
|
|
2219
2320
|
// src/verify.ts
|
|
2220
2321
|
var DEFAULT_JWKS_CACHE_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
2221
2322
|
var DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS = 30 * 1e3;
|
|
@@ -2224,6 +2325,7 @@ var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
|
2224
2325
|
"user",
|
|
2225
2326
|
"identity",
|
|
2226
2327
|
"service",
|
|
2328
|
+
"delegated",
|
|
2227
2329
|
"api_key",
|
|
2228
2330
|
"platform"
|
|
2229
2331
|
]);
|
|
@@ -2258,6 +2360,16 @@ function orgContext(claims) {
|
|
|
2258
2360
|
staff: claims.org_scope === "all"
|
|
2259
2361
|
};
|
|
2260
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
|
+
}
|
|
2261
2373
|
function decodeBase64Url(value) {
|
|
2262
2374
|
const padded = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
2263
2375
|
const binary = atob(padded.padEnd(Math.ceil(padded.length / 4) * 4, "="));
|
|
@@ -2392,6 +2504,12 @@ var TokenVerifier = class {
|
|
|
2392
2504
|
`Token has an unrecognised \`type\` claim: ${JSON.stringify(claims.type)}.`
|
|
2393
2505
|
);
|
|
2394
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
|
+
}
|
|
2395
2513
|
const accepted = /* @__PURE__ */ new Set([
|
|
2396
2514
|
...this.allowedTypes,
|
|
2397
2515
|
...options.alsoAllow ?? []
|
|
@@ -2443,16 +2561,17 @@ var TokenVerifier = class {
|
|
|
2443
2561
|
* ignoring it would accept a token minted for a different client. So it
|
|
2444
2562
|
* throws and names the option.
|
|
2445
2563
|
*
|
|
2446
|
-
* A
|
|
2447
|
-
* 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.
|
|
2448
2567
|
*/
|
|
2449
2568
|
assertAudience(claims) {
|
|
2450
2569
|
const audiences = claims.aud === void 0 ? [] : Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
2451
|
-
if (claims.type === "service") {
|
|
2570
|
+
if (claims.type === "service" || claims.type === "delegated") {
|
|
2452
2571
|
if (this.serviceAudience === void 0) {
|
|
2453
2572
|
throw new CanopyTokenError(
|
|
2454
2573
|
"token.audience_unverified",
|
|
2455
|
-
|
|
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.`
|
|
2456
2575
|
);
|
|
2457
2576
|
}
|
|
2458
2577
|
if (!audiences.includes(this.serviceAudience)) {
|
|
@@ -2694,6 +2813,6 @@ function hexToBytes(hex) {
|
|
|
2694
2813
|
return bytes;
|
|
2695
2814
|
}
|
|
2696
2815
|
|
|
2697
|
-
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 };
|
|
2698
2817
|
//# sourceMappingURL=index.js.map
|
|
2699
2818
|
//# sourceMappingURL=index.js.map
|