@canopy-io/node 0.13.0 → 0.15.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 +24 -0
- package/README.md +79 -3
- package/dist/index.cjs +166 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5078 -1137
- package/dist/index.d.ts +5078 -1137
- package/dist/index.js +166 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -946,6 +946,30 @@ var Organizations = class {
|
|
|
946
946
|
`/api/v1/organizations/${enc(id)}/directory/activity`
|
|
947
947
|
);
|
|
948
948
|
}
|
|
949
|
+
/**
|
|
950
|
+
* Every time your staff entered the organization or were refused, newest
|
|
951
|
+
* first: who, with which roles held at the Environment root, and why a
|
|
952
|
+
* refusal happened (`staff_entry_refused` while the organization's policy
|
|
953
|
+
* says `staff_entry: "refused"`).
|
|
954
|
+
*
|
|
955
|
+
* Paginated (20 per page by default, `take` up to 100), so this returns a
|
|
956
|
+
* `Paginator`. `kind` narrows to `entered` or `refused`; `q` searches who
|
|
957
|
+
* and the role names; `sort_by` is `created` (the default) or `actor`.
|
|
958
|
+
*
|
|
959
|
+
* ```ts
|
|
960
|
+
* for await (const entry of canopy.organizations.listStaffActivity(id)) { … }
|
|
961
|
+
* ```
|
|
962
|
+
*/
|
|
963
|
+
listStaffActivity(id, query = {}) {
|
|
964
|
+
return paginate(
|
|
965
|
+
(params) => this.client.request(
|
|
966
|
+
"GET",
|
|
967
|
+
`/api/v1/organizations/${enc(id)}/staff-activity`,
|
|
968
|
+
{ query: params }
|
|
969
|
+
),
|
|
970
|
+
{ ...query }
|
|
971
|
+
);
|
|
972
|
+
}
|
|
949
973
|
/**
|
|
950
974
|
* The groups the provider has pushed, each with its role mapping.
|
|
951
975
|
*
|
|
@@ -1185,7 +1209,8 @@ function understood(page, path) {
|
|
|
1185
1209
|
}
|
|
1186
1210
|
return page;
|
|
1187
1211
|
}
|
|
1188
|
-
var GRANTS_PATH = (
|
|
1212
|
+
var GRANTS_PATH = (type, id) => type === "service" ? `/api/v1/oauth-clients/${encodeURIComponent(id)}/grants` : `/api/v1/identities/${encodeURIComponent(id)}/grants`;
|
|
1213
|
+
var subjectKey = (type, id) => `${type}:${id}`;
|
|
1189
1214
|
var PARENTS_PATH = "/api/v1/nodes/parents";
|
|
1190
1215
|
function inEffect(window, now) {
|
|
1191
1216
|
return (window.from === null || window.from <= now) && (window.to === null || window.to > now);
|
|
@@ -1294,9 +1319,12 @@ var LocalAuthorizer = class {
|
|
|
1294
1319
|
* root the readable hierarchy does not contain raises
|
|
1295
1320
|
* `authorizer.hierarchy_incomplete`, as a check at such a node does.
|
|
1296
1321
|
*/
|
|
1297
|
-
async grantRoots(identityId, permission) {
|
|
1322
|
+
async grantRoots(identityId, permission, subject = {}) {
|
|
1298
1323
|
const now = this.now();
|
|
1299
|
-
const held = (await this.grantRootsFor(
|
|
1324
|
+
const held = (await this.grantRootsFor(
|
|
1325
|
+
identityId,
|
|
1326
|
+
subject.principalType ?? "identity"
|
|
1327
|
+
)).get(permission) ?? [];
|
|
1300
1328
|
const granted = new Set(
|
|
1301
1329
|
held.filter((window) => inEffect(window, now)).map((w) => w.node)
|
|
1302
1330
|
);
|
|
@@ -1343,7 +1371,9 @@ var LocalAuthorizer = class {
|
|
|
1343
1371
|
*/
|
|
1344
1372
|
async accessibleNodes(identityId, permission, options = {}) {
|
|
1345
1373
|
const cap = options.maxNodes ?? this.maxNodes;
|
|
1346
|
-
const { roots, envWide } = await this.grantRoots(identityId, permission
|
|
1374
|
+
const { roots, envWide } = await this.grantRoots(identityId, permission, {
|
|
1375
|
+
...options.principalType ? { principalType: options.principalType } : {}
|
|
1376
|
+
});
|
|
1347
1377
|
if (envWide) {
|
|
1348
1378
|
return { roots, envWide: true, nodes: null, complete: true };
|
|
1349
1379
|
}
|
|
@@ -1368,13 +1398,73 @@ var LocalAuthorizer = class {
|
|
|
1368
1398
|
}
|
|
1369
1399
|
return { roots, envWide: false, nodes, complete: true };
|
|
1370
1400
|
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Whether the identity holds the permission anywhere at or beneath a node,
|
|
1403
|
+
* and where: the question a menu inside an organization asks ("show
|
|
1404
|
+
* Invoices to someone who approves invoices for any team in Acme"), which
|
|
1405
|
+
* `app_wide` cannot answer because it covers the whole Environment.
|
|
1406
|
+
*
|
|
1407
|
+
* `roots` is `[subtreeRootId]` when a grant at the node or above it reaches
|
|
1408
|
+
* the whole subtree; otherwise the topmost granted nodes inside it, the
|
|
1409
|
+
* ones no other granted node inside it already covers. A list query filters
|
|
1410
|
+
* by "beneath one of these".
|
|
1411
|
+
*
|
|
1412
|
+
* **A read for menus and lists, never a guard.** Holding the permission
|
|
1413
|
+
* somewhere in the subtree says nothing about the record a write touches:
|
|
1414
|
+
* check the node the action writes to, with `evaluate`. No `scope` value
|
|
1415
|
+
* asks this question, on purpose.
|
|
1416
|
+
*
|
|
1417
|
+
* Computed from the same cached grant roots and tree a check uses. A grant
|
|
1418
|
+
* or a subtree root the readable hierarchy does not contain raises
|
|
1419
|
+
* `authorizer.hierarchy_incomplete`, as a check at such a node does.
|
|
1420
|
+
*/
|
|
1421
|
+
async holdsAnywhereUnder(identityId, permission, subtreeRootId, subject = {}) {
|
|
1422
|
+
const now = this.now();
|
|
1423
|
+
const held = (await this.grantRootsFor(
|
|
1424
|
+
identityId,
|
|
1425
|
+
subject.principalType ?? "identity"
|
|
1426
|
+
)).get(permission) ?? [];
|
|
1427
|
+
const granted = new Set(
|
|
1428
|
+
held.filter((window) => inEffect(window, now)).map((w) => w.node)
|
|
1429
|
+
);
|
|
1430
|
+
const { parents } = await this.treeEntry();
|
|
1431
|
+
if (!parents.has(subtreeRootId)) {
|
|
1432
|
+
throw this.hierarchyIncomplete(subtreeRootId);
|
|
1433
|
+
}
|
|
1434
|
+
if (granted.size === 0) {
|
|
1435
|
+
return { held: false, roots: [] };
|
|
1436
|
+
}
|
|
1437
|
+
if (this.lineage(parents, subtreeRootId).some((n) => granted.has(n))) {
|
|
1438
|
+
return { held: true, roots: [subtreeRootId] };
|
|
1439
|
+
}
|
|
1440
|
+
const roots = [];
|
|
1441
|
+
for (const node of granted) {
|
|
1442
|
+
if (!parents.has(node)) {
|
|
1443
|
+
throw this.hierarchyIncomplete(node);
|
|
1444
|
+
}
|
|
1445
|
+
const above = this.lineage(parents, node).slice(1);
|
|
1446
|
+
if (above.includes(subtreeRootId) && !above.some((n) => granted.has(n))) {
|
|
1447
|
+
roots.push(node);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
return { held: roots.length > 0, roots };
|
|
1451
|
+
}
|
|
1452
|
+
/** The node and every ancestor up to the root, nearest first. */
|
|
1453
|
+
lineage(parents, nodeId) {
|
|
1454
|
+
const path = [];
|
|
1455
|
+
let depth = 0;
|
|
1456
|
+
for (let current = nodeId; current && depth < MAX_WALK_DEPTH; current = parents.get(current), depth += 1) {
|
|
1457
|
+
path.push(current);
|
|
1458
|
+
}
|
|
1459
|
+
return path;
|
|
1460
|
+
}
|
|
1371
1461
|
/**
|
|
1372
1462
|
* Every permission the identity holds at the node, by grant or by
|
|
1373
1463
|
* inheritance: the list a screen renders its controls from, answered from
|
|
1374
1464
|
* the same cached grant roots and tree a check uses.
|
|
1375
1465
|
*/
|
|
1376
|
-
async permissionsAt(identityId, nodeId) {
|
|
1377
|
-
const answers = await this.permissionsAtMany(identityId, [nodeId]);
|
|
1466
|
+
async permissionsAt(identityId, nodeId, subject = {}) {
|
|
1467
|
+
const answers = await this.permissionsAtMany(identityId, [nodeId], subject);
|
|
1378
1468
|
return answers.get(nodeId) ?? [];
|
|
1379
1469
|
}
|
|
1380
1470
|
/**
|
|
@@ -1382,9 +1472,12 @@ var LocalAuthorizer = class {
|
|
|
1382
1472
|
* the rows of a list each with their own controls, answered from one read
|
|
1383
1473
|
* of the grant roots and one walk per node.
|
|
1384
1474
|
*/
|
|
1385
|
-
async permissionsAtMany(identityId, nodeIds) {
|
|
1475
|
+
async permissionsAtMany(identityId, nodeIds, subject = {}) {
|
|
1386
1476
|
const now = this.now();
|
|
1387
|
-
const roots = await this.grantRootsFor(
|
|
1477
|
+
const roots = await this.grantRootsFor(
|
|
1478
|
+
identityId,
|
|
1479
|
+
subject.principalType ?? "identity"
|
|
1480
|
+
);
|
|
1388
1481
|
const answers = /* @__PURE__ */ new Map();
|
|
1389
1482
|
for (const nodeId of new Set(nodeIds)) {
|
|
1390
1483
|
const path = await this.lineageOf(nodeId);
|
|
@@ -1402,9 +1495,10 @@ var LocalAuthorizer = class {
|
|
|
1402
1495
|
}
|
|
1403
1496
|
async decide(query) {
|
|
1404
1497
|
const now = this.now();
|
|
1405
|
-
const held = (await this.grantRootsFor(
|
|
1406
|
-
query.
|
|
1407
|
-
|
|
1498
|
+
const held = (await this.grantRootsFor(
|
|
1499
|
+
query.identity_id,
|
|
1500
|
+
query.principal_type ?? "identity"
|
|
1501
|
+
)).get(query.permission);
|
|
1408
1502
|
if (!held || held.length === 0) {
|
|
1409
1503
|
return { allowed: false, reason: "permission_not_held" };
|
|
1410
1504
|
}
|
|
@@ -1440,6 +1534,8 @@ var LocalAuthorizer = class {
|
|
|
1440
1534
|
* cached hierarchy and every other identity's entries stay warm. This is
|
|
1441
1535
|
* the shape an assignment webhook wants: the event names the identity
|
|
1442
1536
|
* whose authority moved, and nothing else needs to pay a refetch for it.
|
|
1537
|
+
* An event naming a machine client (`oauth_client_id`, with `identity_id`
|
|
1538
|
+
* null) passes that client's `client_id` with `principalType: "service"`.
|
|
1443
1539
|
*
|
|
1444
1540
|
* With no argument, everything goes: grants and the hierarchy tree. Not
|
|
1445
1541
|
* needed in normal operation, where entries expire on their own; useful in
|
|
@@ -1455,9 +1551,11 @@ var LocalAuthorizer = class {
|
|
|
1455
1551
|
* the TTL, and webhook-driven invalidation is a latency optimization on
|
|
1456
1552
|
* top of it — size the TTL to the revocation latency you can promise.
|
|
1457
1553
|
*/
|
|
1458
|
-
invalidate(identityId) {
|
|
1554
|
+
invalidate(identityId, subject = {}) {
|
|
1459
1555
|
if (identityId !== void 0) {
|
|
1460
|
-
this.grants.delete(
|
|
1556
|
+
this.grants.delete(
|
|
1557
|
+
subjectKey(subject.principalType ?? "identity", identityId)
|
|
1558
|
+
);
|
|
1461
1559
|
return;
|
|
1462
1560
|
}
|
|
1463
1561
|
this.grants.clear();
|
|
@@ -1500,19 +1598,20 @@ var LocalAuthorizer = class {
|
|
|
1500
1598
|
`The hierarchy this client can read does not contain node "${nodeId}", so authorization at that node cannot be decided. A scoped API key needs the \`hierarchy.view\` scope to read the tree.`
|
|
1501
1599
|
);
|
|
1502
1600
|
}
|
|
1503
|
-
async grantRootsFor(identityId) {
|
|
1504
|
-
const
|
|
1601
|
+
async grantRootsFor(identityId, type) {
|
|
1602
|
+
const key = subjectKey(type, identityId);
|
|
1603
|
+
const cached = this.grants.get(key);
|
|
1505
1604
|
if (cached && cached.expiresAt > this.now()) {
|
|
1506
1605
|
return cached.roots;
|
|
1507
1606
|
}
|
|
1508
|
-
const pending = this.pendingGrants.get(
|
|
1607
|
+
const pending = this.pendingGrants.get(key);
|
|
1509
1608
|
if (pending) {
|
|
1510
1609
|
return pending;
|
|
1511
1610
|
}
|
|
1512
|
-
const read = this.fetchGrantRoots(identityId).finally(() => {
|
|
1513
|
-
this.pendingGrants.delete(
|
|
1611
|
+
const read = this.fetchGrantRoots(identityId, type).finally(() => {
|
|
1612
|
+
this.pendingGrants.delete(key);
|
|
1514
1613
|
});
|
|
1515
|
-
this.pendingGrants.set(
|
|
1614
|
+
this.pendingGrants.set(key, read);
|
|
1516
1615
|
return read;
|
|
1517
1616
|
}
|
|
1518
1617
|
/**
|
|
@@ -1530,9 +1629,9 @@ var LocalAuthorizer = class {
|
|
|
1530
1629
|
* window boundary when that comes before the TTL, so a grant takes effect
|
|
1531
1630
|
* or ends on time rather than up to `ttlMs` late.
|
|
1532
1631
|
*/
|
|
1533
|
-
async fetchGrantRoots(identityId) {
|
|
1632
|
+
async fetchGrantRoots(identityId, type) {
|
|
1534
1633
|
this.stats.grantFetches += 1;
|
|
1535
|
-
const path = GRANTS_PATH(identityId);
|
|
1634
|
+
const path = GRANTS_PATH(type, identityId);
|
|
1536
1635
|
const rows = paginate(
|
|
1537
1636
|
(query) => {
|
|
1538
1637
|
this.stats.grantPages += 1;
|
|
@@ -1565,7 +1664,7 @@ var LocalAuthorizer = class {
|
|
|
1565
1664
|
roots.set(row.permission, [window]);
|
|
1566
1665
|
}
|
|
1567
1666
|
}
|
|
1568
|
-
this.grants.set(identityId, {
|
|
1667
|
+
this.grants.set(subjectKey(type, identityId), {
|
|
1569
1668
|
roots,
|
|
1570
1669
|
expiresAt: Math.min(now + this.ttlMs, boundary)
|
|
1571
1670
|
});
|
|
@@ -1752,7 +1851,13 @@ var HOSTED_ORIGIN = "https://auth.canopy-io.com";
|
|
|
1752
1851
|
var DEFAULT_JWKS_CACHE_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
1753
1852
|
var DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS = 30 * 1e3;
|
|
1754
1853
|
var DEFAULT_JWKS_TIMEOUT_MS = 5e3;
|
|
1755
|
-
var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
1854
|
+
var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
1855
|
+
"user",
|
|
1856
|
+
"identity",
|
|
1857
|
+
"service",
|
|
1858
|
+
"api_key",
|
|
1859
|
+
"platform"
|
|
1860
|
+
]);
|
|
1756
1861
|
var DEFAULT_CLOCK_TOLERANCE_SEC = 60;
|
|
1757
1862
|
function resolveIssuer(options) {
|
|
1758
1863
|
const { environmentId, issuer } = options;
|
|
@@ -1772,11 +1877,17 @@ function resolveIssuer(options) {
|
|
|
1772
1877
|
);
|
|
1773
1878
|
}
|
|
1774
1879
|
function orgContext(claims) {
|
|
1775
|
-
const { org_id, org_roles } = claims;
|
|
1776
|
-
|
|
1880
|
+
const { org_id, org_roles, org_role_ids } = claims;
|
|
1881
|
+
const isNameList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string" && entry !== "");
|
|
1882
|
+
if (typeof org_id !== "string" || org_id === "" || !isNameList(org_roles) || !isNameList(org_role_ids) || org_role_ids.length !== org_roles.length) {
|
|
1777
1883
|
return null;
|
|
1778
1884
|
}
|
|
1779
|
-
return {
|
|
1885
|
+
return {
|
|
1886
|
+
orgId: org_id,
|
|
1887
|
+
orgRoles: [...org_roles],
|
|
1888
|
+
orgRoleIds: [...org_role_ids],
|
|
1889
|
+
staff: claims.org_scope === "all"
|
|
1890
|
+
};
|
|
1780
1891
|
}
|
|
1781
1892
|
function decodeBase64Url(value) {
|
|
1782
1893
|
const padded = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
@@ -1801,6 +1912,7 @@ function decodeJsonSegment(segment, what) {
|
|
|
1801
1912
|
var TokenVerifier = class {
|
|
1802
1913
|
issuer;
|
|
1803
1914
|
audience;
|
|
1915
|
+
serviceAudience;
|
|
1804
1916
|
jwksUri;
|
|
1805
1917
|
jwksCacheMaxAgeMs;
|
|
1806
1918
|
jwksMinRefetchIntervalMs;
|
|
@@ -1820,6 +1932,7 @@ var TokenVerifier = class {
|
|
|
1820
1932
|
constructor(options = {}) {
|
|
1821
1933
|
this.issuer = resolveIssuer(options);
|
|
1822
1934
|
this.audience = options.audience;
|
|
1935
|
+
this.serviceAudience = options.serviceAudience;
|
|
1823
1936
|
this.jwksUri = options.jwksUri ?? `${this.issuer}/.well-known/jwks.json`;
|
|
1824
1937
|
this.jwksCacheMaxAgeMs = options.jwksCacheMaxAgeMs ?? DEFAULT_JWKS_CACHE_MAX_AGE_MS;
|
|
1825
1938
|
this.jwksMinRefetchIntervalMs = options.jwksMinRefetchIntervalMs ?? DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS;
|
|
@@ -1842,7 +1955,7 @@ var TokenVerifier = class {
|
|
|
1842
1955
|
* Order matters: the signature is checked before any claim is believed, so
|
|
1843
1956
|
* nothing downstream ever reads an unverified payload.
|
|
1844
1957
|
*/
|
|
1845
|
-
async verify(token) {
|
|
1958
|
+
async verify(token, options = {}) {
|
|
1846
1959
|
const parts = token.split(".");
|
|
1847
1960
|
if (parts.length !== 3) {
|
|
1848
1961
|
throw new CanopyTokenError(
|
|
@@ -1887,11 +2000,11 @@ var TokenVerifier = class {
|
|
|
1887
2000
|
encodedPayload,
|
|
1888
2001
|
"payload"
|
|
1889
2002
|
);
|
|
1890
|
-
this.assertClaims(claims);
|
|
2003
|
+
this.assertClaims(claims, options);
|
|
1891
2004
|
return claims;
|
|
1892
2005
|
}
|
|
1893
2006
|
/** Everything checked after the signature is known good. */
|
|
1894
|
-
assertClaims(claims) {
|
|
2007
|
+
assertClaims(claims, options) {
|
|
1895
2008
|
if (typeof claims.sub !== "string" || claims.sub === "") {
|
|
1896
2009
|
throw new CanopyTokenError(
|
|
1897
2010
|
"token.malformed",
|
|
@@ -1904,11 +2017,15 @@ var TokenVerifier = class {
|
|
|
1904
2017
|
`Token has an unrecognised \`type\` claim: ${JSON.stringify(claims.type)}.`
|
|
1905
2018
|
);
|
|
1906
2019
|
}
|
|
1907
|
-
|
|
2020
|
+
const accepted = /* @__PURE__ */ new Set([
|
|
2021
|
+
...this.allowedTypes,
|
|
2022
|
+
...options.alsoAllow ?? []
|
|
2023
|
+
]);
|
|
2024
|
+
if (!accepted.has(claims.type)) {
|
|
1908
2025
|
throw new CanopyTokenError(
|
|
1909
2026
|
"token.type_not_allowed",
|
|
1910
2027
|
`Token is for a "${claims.type}" principal; this verifier accepts ${[
|
|
1911
|
-
...
|
|
2028
|
+
...accepted
|
|
1912
2029
|
].map((type) => `"${type}"`).join(", ")}. Set \`allowedTypes\` to accept another.`
|
|
1913
2030
|
);
|
|
1914
2031
|
}
|
|
@@ -1950,9 +2067,27 @@ var TokenVerifier = class {
|
|
|
1950
2067
|
* token being verified by something that never said which client it is, and
|
|
1951
2068
|
* ignoring it would accept a token minted for a different client. So it
|
|
1952
2069
|
* throws and names the option.
|
|
2070
|
+
*
|
|
2071
|
+
* A service token names the API it was minted for, never a client, so it
|
|
2072
|
+
* is held to `serviceAudience` alone, which it must name.
|
|
1953
2073
|
*/
|
|
1954
2074
|
assertAudience(claims) {
|
|
1955
2075
|
const audiences = claims.aud === void 0 ? [] : Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
2076
|
+
if (claims.type === "service") {
|
|
2077
|
+
if (this.serviceAudience === void 0) {
|
|
2078
|
+
throw new CanopyTokenError(
|
|
2079
|
+
"token.audience_unverified",
|
|
2080
|
+
"This is a machine client's service token, and the verifier has no `serviceAudience` configured. Set `serviceAudience` to the audience your API answers to."
|
|
2081
|
+
);
|
|
2082
|
+
}
|
|
2083
|
+
if (!audiences.includes(this.serviceAudience)) {
|
|
2084
|
+
throw new CanopyTokenError(
|
|
2085
|
+
"token.audience_mismatch",
|
|
2086
|
+
`Token audience does not include "${this.serviceAudience}".`
|
|
2087
|
+
);
|
|
2088
|
+
}
|
|
2089
|
+
return;
|
|
2090
|
+
}
|
|
1956
2091
|
if (this.audience === void 0) {
|
|
1957
2092
|
if (audiences.length > 0) {
|
|
1958
2093
|
throw new CanopyTokenError(
|