@canopy-io/node 0.14.0 → 0.16.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/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
  *
@@ -1175,7 +1199,7 @@ var MAX_WALK_DEPTH = 256;
1175
1199
  var PAGE_SIZE = 100;
1176
1200
  var MAX_PAGES = 1e4;
1177
1201
  var MAX_TREE_WALKS = 3;
1178
- var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1]);
1202
+ var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1, 2]);
1179
1203
  function understood(page, path) {
1180
1204
  if (!KNOWN_MODEL_VERSIONS.has(page.model_version)) {
1181
1205
  throw new CanopyAuthorizerError(
@@ -1194,6 +1218,17 @@ function inEffect(window, now) {
1194
1218
  function reasonAmong(windows, now) {
1195
1219
  return windows.some((window) => window.to !== null && window.to <= now) ? "expired" : "not_yet_effective";
1196
1220
  }
1221
+ function refusedOn(denied, lineage) {
1222
+ if (!denied) {
1223
+ return false;
1224
+ }
1225
+ for (const node of lineage) {
1226
+ if (denied.has(node)) {
1227
+ return true;
1228
+ }
1229
+ }
1230
+ return false;
1231
+ }
1197
1232
  var LocalAuthorizer = class {
1198
1233
  client;
1199
1234
  ttlMs;
@@ -1291,55 +1326,63 @@ var LocalAuthorizer = class {
1291
1326
  * that keeps a closure table or a materialised path: it filters by "beneath
1292
1327
  * one of these" without an expanded set.
1293
1328
  *
1329
+ * A grant a deny refuses (a deny root at its node or above it) is left out,
1330
+ * and a deny root beneath a root is named in `excludedRoots`, so the filter
1331
+ * becomes "beneath one of the roots and not beneath an excluded one".
1332
+ * `envWide` holds only while nothing is excluded.
1333
+ *
1294
1334
  * Reads the tree, since minimality and `envWide` are questions about it; a
1295
1335
  * root the readable hierarchy does not contain raises
1296
1336
  * `authorizer.hierarchy_incomplete`, as a check at such a node does.
1297
1337
  */
1298
1338
  async grantRoots(identityId, permission, subject = {}) {
1299
1339
  const now = this.now();
1300
- const held = (await this.grantRootsFor(
1340
+ const holdings = await this.grantRootsFor(
1301
1341
  identityId,
1302
1342
  subject.principalType ?? "identity"
1303
- )).get(permission) ?? [];
1343
+ );
1304
1344
  const granted = new Set(
1305
- held.filter((window) => inEffect(window, now)).map((w) => w.node)
1345
+ (holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
1306
1346
  );
1307
1347
  if (granted.size === 0) {
1308
- return { roots: [], envWide: false };
1348
+ return { roots: [], excludedRoots: [], envWide: false };
1309
1349
  }
1350
+ const denied = holdings.denies.get(permission);
1310
1351
  const { parents } = await this.treeEntry();
1311
1352
  const roots = [];
1312
- let envWide = false;
1353
+ let rootGranted = false;
1313
1354
  for (const node of granted) {
1314
1355
  if (!parents.has(node)) {
1315
1356
  throw this.hierarchyIncomplete(node);
1316
1357
  }
1317
- if (parents.get(node) === null) {
1318
- envWide = true;
1358
+ const path = this.lineage(parents, node);
1359
+ if (refusedOn(denied, path)) {
1360
+ continue;
1319
1361
  }
1320
- let covered = false;
1321
- let depth = 0;
1322
- for (let current = parents.get(node); current && depth < MAX_WALK_DEPTH; current = parents.get(current), depth += 1) {
1323
- if (granted.has(current)) {
1324
- covered = true;
1325
- break;
1326
- }
1362
+ if (parents.get(node) === null) {
1363
+ rootGranted = true;
1327
1364
  }
1328
- if (!covered) {
1365
+ if (!path.slice(1).some((n) => granted.has(n))) {
1329
1366
  roots.push(node);
1330
1367
  }
1331
1368
  }
1332
- return { roots, envWide };
1369
+ const excludedRoots = this.excludedBeneath(parents, denied, roots);
1370
+ return {
1371
+ roots,
1372
+ excludedRoots,
1373
+ envWide: rootGranted && excludedRoots.length === 0
1374
+ };
1333
1375
  }
1334
1376
  /**
1335
1377
  * Where the identity may act with the permission, as the set a list query
1336
- * filters by: {@link grantRoots} expanded through their descendants, up to
1337
- * `maxNodes` (the authorizer's default unless given here).
1378
+ * filters by: {@link grantRoots} expanded through their descendants, the
1379
+ * subtrees beneath `excludedRoots` left out, up to `maxNodes` (the
1380
+ * authorizer's default unless given here).
1338
1381
  *
1339
1382
  * Past the cap `nodes` is null and `complete` false: the set is too large
1340
- * to be worth an `IN (...)` list, and the caller filters by `roots`
1341
- * instead. An `envWide` answer carries no set either, since every node
1342
- * qualifies and a filter would only slow the query down.
1383
+ * to be worth an `IN (...)` list, and the caller filters by `roots` and
1384
+ * `excludedRoots` instead. An `envWide` answer carries no set either, since
1385
+ * every node qualifies and a filter would only slow the query down.
1343
1386
  *
1344
1387
  * Computed from the same cached grant roots and tree a check uses, so a
1345
1388
  * list filtered by it and the check on a write cannot disagree, and it goes
@@ -1347,37 +1390,158 @@ var LocalAuthorizer = class {
1347
1390
  */
1348
1391
  async accessibleNodes(identityId, permission, options = {}) {
1349
1392
  const cap = options.maxNodes ?? this.maxNodes;
1350
- const { roots, envWide } = await this.grantRoots(identityId, permission, {
1351
- ...options.principalType ? { principalType: options.principalType } : {}
1352
- });
1393
+ const { roots, excludedRoots, envWide } = await this.grantRoots(
1394
+ identityId,
1395
+ permission,
1396
+ {
1397
+ ...options.principalType ? { principalType: options.principalType } : {}
1398
+ }
1399
+ );
1353
1400
  if (envWide) {
1354
- return { roots, envWide: true, nodes: null, complete: true };
1401
+ return {
1402
+ roots,
1403
+ excludedRoots,
1404
+ envWide: true,
1405
+ nodes: null,
1406
+ complete: true
1407
+ };
1355
1408
  }
1356
1409
  if (roots.length === 0) {
1357
- return { roots, envWide: false, nodes: /* @__PURE__ */ new Set(), complete: true };
1410
+ return {
1411
+ roots,
1412
+ excludedRoots,
1413
+ envWide: false,
1414
+ nodes: /* @__PURE__ */ new Set(),
1415
+ complete: true
1416
+ };
1358
1417
  }
1359
1418
  const { children } = await this.treeEntry();
1419
+ const excluded = new Set(excludedRoots);
1360
1420
  const nodes = /* @__PURE__ */ new Set();
1361
1421
  const pending = [...roots];
1362
1422
  while (pending.length > 0) {
1363
1423
  const node = pending.pop();
1364
- if (nodes.has(node)) {
1424
+ if (nodes.has(node) || excluded.has(node)) {
1365
1425
  continue;
1366
1426
  }
1367
1427
  nodes.add(node);
1368
1428
  if (nodes.size > cap) {
1369
- return { roots, envWide: false, nodes: null, complete: false };
1429
+ return {
1430
+ roots,
1431
+ excludedRoots,
1432
+ envWide: false,
1433
+ nodes: null,
1434
+ complete: false
1435
+ };
1370
1436
  }
1371
1437
  for (const child of children.get(node) ?? []) {
1372
1438
  pending.push(child);
1373
1439
  }
1374
1440
  }
1375
- return { roots, envWide: false, nodes, complete: true };
1441
+ return { roots, excludedRoots, envWide: false, nodes, complete: true };
1442
+ }
1443
+ /**
1444
+ * Whether the identity holds the permission anywhere at or beneath a node,
1445
+ * and where: the question a menu inside an organization asks ("show
1446
+ * Invoices to someone who approves invoices for any team in Acme"), which
1447
+ * `app_wide` cannot answer because it covers the whole Environment.
1448
+ *
1449
+ * `roots` is `[subtreeRootId]` when a grant at the node or above it reaches
1450
+ * the whole subtree; otherwise the topmost granted nodes inside it, the
1451
+ * ones no other granted node inside it already covers. A list query filters
1452
+ * by "beneath one of these and not beneath one of `excludedRoots`", the
1453
+ * deny roots inside them. A deny at the node or above it refuses the whole
1454
+ * subtree, and a grant a deny refuses counts for nothing.
1455
+ *
1456
+ * **A read for menus and lists, never a guard.** Holding the permission
1457
+ * somewhere in the subtree says nothing about the record a write touches:
1458
+ * check the node the action writes to, with `evaluate`. No `scope` value
1459
+ * asks this question, on purpose.
1460
+ *
1461
+ * Computed from the same cached grant roots and tree a check uses. A grant
1462
+ * or a subtree root the readable hierarchy does not contain raises
1463
+ * `authorizer.hierarchy_incomplete`, as a check at such a node does.
1464
+ */
1465
+ async holdsAnywhereUnder(identityId, permission, subtreeRootId, subject = {}) {
1466
+ const now = this.now();
1467
+ const holdings = await this.grantRootsFor(
1468
+ identityId,
1469
+ subject.principalType ?? "identity"
1470
+ );
1471
+ const granted = new Set(
1472
+ (holdings.roots.get(permission) ?? []).filter((window) => inEffect(window, now)).map((w) => w.node)
1473
+ );
1474
+ const denied = holdings.denies.get(permission);
1475
+ const { parents } = await this.treeEntry();
1476
+ if (!parents.has(subtreeRootId)) {
1477
+ throw this.hierarchyIncomplete(subtreeRootId);
1478
+ }
1479
+ const above = this.lineage(parents, subtreeRootId);
1480
+ if (granted.size === 0 || refusedOn(denied, above)) {
1481
+ return { held: false, roots: [], excludedRoots: [] };
1482
+ }
1483
+ if (above.some((n) => granted.has(n))) {
1484
+ return {
1485
+ held: true,
1486
+ roots: [subtreeRootId],
1487
+ excludedRoots: this.excludedBeneath(parents, denied, [subtreeRootId])
1488
+ };
1489
+ }
1490
+ const roots = [];
1491
+ for (const node of granted) {
1492
+ if (!parents.has(node)) {
1493
+ throw this.hierarchyIncomplete(node);
1494
+ }
1495
+ const path = this.lineage(parents, node);
1496
+ if (refusedOn(denied, path)) {
1497
+ continue;
1498
+ }
1499
+ const ancestors = path.slice(1);
1500
+ if (ancestors.includes(subtreeRootId) && !ancestors.some((n) => granted.has(n))) {
1501
+ roots.push(node);
1502
+ }
1503
+ }
1504
+ return {
1505
+ held: roots.length > 0,
1506
+ roots,
1507
+ excludedRoots: this.excludedBeneath(parents, denied, roots)
1508
+ };
1509
+ }
1510
+ /** The node and every ancestor up to the root, nearest first. */
1511
+ lineage(parents, nodeId) {
1512
+ const path = [];
1513
+ let depth = 0;
1514
+ for (let current = nodeId; current && depth < MAX_WALK_DEPTH; current = parents.get(current), depth += 1) {
1515
+ path.push(current);
1516
+ }
1517
+ return path;
1518
+ }
1519
+ /**
1520
+ * The deny roots strictly beneath one of `roots`: the subtrees a filter by
1521
+ * those roots has to cut out. A deny root at a root or above it never
1522
+ * reaches here, because that root was refused and left out.
1523
+ */
1524
+ excludedBeneath(parents, denied, roots) {
1525
+ if (!denied || roots.length === 0) {
1526
+ return [];
1527
+ }
1528
+ const rootSet = new Set(roots);
1529
+ const excluded = [];
1530
+ for (const node of denied) {
1531
+ if (!parents.has(node)) {
1532
+ throw this.hierarchyIncomplete(node);
1533
+ }
1534
+ if (this.lineage(parents, node).slice(1).some((n) => rootSet.has(n))) {
1535
+ excluded.push(node);
1536
+ }
1537
+ }
1538
+ return excluded;
1376
1539
  }
1377
1540
  /**
1378
1541
  * Every permission the identity holds at the node, by grant or by
1379
- * inheritance: the list a screen renders its controls from, answered from
1380
- * the same cached grant roots and tree a check uses.
1542
+ * inheritance, less those a deny refuses there: the list a screen renders
1543
+ * its controls from, answered from the same cached grant roots and tree a
1544
+ * check uses.
1381
1545
  */
1382
1546
  async permissionsAt(identityId, nodeId, subject = {}) {
1383
1547
  const answers = await this.permissionsAtMany(identityId, [nodeId], subject);
@@ -1390,7 +1554,7 @@ var LocalAuthorizer = class {
1390
1554
  */
1391
1555
  async permissionsAtMany(identityId, nodeIds, subject = {}) {
1392
1556
  const now = this.now();
1393
- const roots = await this.grantRootsFor(
1557
+ const holdings = await this.grantRootsFor(
1394
1558
  identityId,
1395
1559
  subject.principalType ?? "identity"
1396
1560
  );
@@ -1398,7 +1562,10 @@ var LocalAuthorizer = class {
1398
1562
  for (const nodeId of new Set(nodeIds)) {
1399
1563
  const path = await this.lineageOf(nodeId);
1400
1564
  const held = [];
1401
- for (const [permission, windows] of roots) {
1565
+ for (const [permission, windows] of holdings.roots) {
1566
+ if (refusedOn(holdings.denies.get(permission), path)) {
1567
+ continue;
1568
+ }
1402
1569
  if (windows.some(
1403
1570
  (window) => path.has(window.node) && inEffect(window, now)
1404
1571
  )) {
@@ -1411,19 +1578,34 @@ var LocalAuthorizer = class {
1411
1578
  }
1412
1579
  async decide(query) {
1413
1580
  const now = this.now();
1414
- const held = (await this.grantRootsFor(
1581
+ const holdings = await this.grantRootsFor(
1415
1582
  query.identity_id,
1416
1583
  query.principal_type ?? "identity"
1417
- )).get(query.permission);
1418
- if (!held || held.length === 0) {
1419
- return { allowed: false, reason: "permission_not_held" };
1420
- }
1584
+ );
1585
+ const held = holdings.roots.get(query.permission) ?? [];
1586
+ const denied = holdings.denies.get(query.permission);
1421
1587
  if ((query.scope ?? "node") === "app_wide") {
1422
- return held.some((window) => inEffect(window, now)) ? { allowed: true, reason: null } : { allowed: false, reason: reasonAmong(held, now) };
1588
+ if (held.length === 0) {
1589
+ return { allowed: false, reason: "permission_not_held" };
1590
+ }
1591
+ const standing = held.filter((window) => inEffect(window, now));
1592
+ if (standing.length === 0) {
1593
+ return { allowed: false, reason: reasonAmong(held, now) };
1594
+ }
1595
+ return await this.anyClearOf(denied, standing) ? { allowed: true, reason: null } : { allowed: false, reason: "denied" };
1596
+ }
1597
+ if (held.length === 0 && !denied) {
1598
+ return { allowed: false, reason: "permission_not_held" };
1423
1599
  }
1424
1600
  if (!query.node_id) {
1425
1601
  return { allowed: false, reason: "node_missing" };
1426
1602
  }
1603
+ if (denied && refusedOn(denied, await this.lineageOf(query.node_id))) {
1604
+ return { allowed: false, reason: "denied" };
1605
+ }
1606
+ if (held.length === 0) {
1607
+ return { allowed: false, reason: "permission_not_held" };
1608
+ }
1427
1609
  if (held.some(
1428
1610
  (window) => window.node === query.node_id && inEffect(window, now)
1429
1611
  )) {
@@ -1439,6 +1621,22 @@ var LocalAuthorizer = class {
1439
1621
  }
1440
1622
  return { allowed: false, reason: reasonAmong(reachable, now) };
1441
1623
  }
1624
+ /**
1625
+ * Whether one of the grants stands clear of every deny root: none at its
1626
+ * node or above it. A grant with a deny strictly beneath it still reaches
1627
+ * the rest of its subtree. Reads the tree only when there is a deny.
1628
+ */
1629
+ async anyClearOf(denied, windows) {
1630
+ if (!denied) {
1631
+ return true;
1632
+ }
1633
+ for (const window of windows) {
1634
+ if (!refusedOn(denied, await this.lineageOf(window.node))) {
1635
+ return true;
1636
+ }
1637
+ }
1638
+ return false;
1639
+ }
1442
1640
  /** Counters for observability — how much traffic the cache is actually saving. */
1443
1641
  snapshot() {
1444
1642
  return { ...this.stats };
@@ -1456,7 +1654,9 @@ var LocalAuthorizer = class {
1456
1654
  * With no argument, everything goes: grants and the hierarchy tree. Not
1457
1655
  * needed in normal operation, where entries expire on their own; useful in
1458
1656
  * tests and after a change whose reach you cannot name (a role's
1459
- * permissions edited, a role deactivated or reactivated, a node moved). A
1657
+ * permissions edited, a role deactivated or reactivated, a node moved, a
1658
+ * deny created or deleted, since a deny on a role binds everyone holding
1659
+ * it). A
1460
1660
  * deactivated role grants nothing to anyone holding it, so until the cache
1461
1661
  * is cleared or expires, its holders keep being allowed locally.
1462
1662
  *
@@ -1518,7 +1718,7 @@ var LocalAuthorizer = class {
1518
1718
  const key = subjectKey(type, identityId);
1519
1719
  const cached = this.grants.get(key);
1520
1720
  if (cached && cached.expiresAt > this.now()) {
1521
- return cached.roots;
1721
+ return cached.holdings;
1522
1722
  }
1523
1723
  const pending = this.pendingGrants.get(key);
1524
1724
  if (pending) {
@@ -1544,17 +1744,35 @@ var LocalAuthorizer = class {
1544
1744
  * `expired` or `not_yet_effective`, and the entry expires at the nearest
1545
1745
  * window boundary when that comes before the TTL, so a grant takes effect
1546
1746
  * or ends on time rather than up to `ttlMs` late.
1747
+ *
1748
+ * Every page carries the subject's deny roots whole, beside its rows; they
1749
+ * are gathered from each page, so a read is one snapshot of both.
1547
1750
  */
1548
1751
  async fetchGrantRoots(identityId, type) {
1549
1752
  this.stats.grantFetches += 1;
1550
1753
  const path = GRANTS_PATH(type, identityId);
1754
+ const denies = /* @__PURE__ */ new Map();
1551
1755
  const rows = paginate(
1552
1756
  (query) => {
1553
1757
  this.stats.grantPages += 1;
1554
1758
  return this.client.request("GET", path, {
1555
1759
  ...this.readOptions,
1556
1760
  query: { ...query, include: "windows" }
1557
- }).then((page) => understood(page, path));
1761
+ }).then((page) => {
1762
+ const read = understood(
1763
+ page,
1764
+ path
1765
+ ).denies;
1766
+ for (const deny of read ?? []) {
1767
+ const nodes = denies.get(deny.permission);
1768
+ if (nodes) {
1769
+ nodes.add(deny.node_id);
1770
+ } else {
1771
+ denies.set(deny.permission, /* @__PURE__ */ new Set([deny.node_id]));
1772
+ }
1773
+ }
1774
+ return page;
1775
+ });
1558
1776
  },
1559
1777
  { page: 1, take: PAGE_SIZE },
1560
1778
  { maxPages: MAX_PAGES }
@@ -1580,11 +1798,12 @@ var LocalAuthorizer = class {
1580
1798
  roots.set(row.permission, [window]);
1581
1799
  }
1582
1800
  }
1801
+ const holdings = { roots, denies };
1583
1802
  this.grants.set(subjectKey(type, identityId), {
1584
- roots,
1803
+ holdings,
1585
1804
  expiresAt: Math.min(now + this.ttlMs, boundary)
1586
1805
  });
1587
- return roots;
1806
+ return holdings;
1588
1807
  }
1589
1808
  async treeEntry() {
1590
1809
  if (this.tree && this.tree.expiresAt > this.now()) {
@@ -1801,7 +2020,8 @@ function orgContext(claims) {
1801
2020
  return {
1802
2021
  orgId: org_id,
1803
2022
  orgRoles: [...org_roles],
1804
- orgRoleIds: [...org_role_ids]
2023
+ orgRoleIds: [...org_role_ids],
2024
+ staff: claims.org_scope === "all"
1805
2025
  };
1806
2026
  }
1807
2027
  function decodeBase64Url(value) {