@canopy-io/node 0.13.0 → 0.14.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 +50 -3
- package/dist/index.cjs +81 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1065 -98
- package/dist/index.d.ts +1065 -98
- package/dist/index.js +81 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1185,7 +1185,8 @@ function understood(page, path) {
|
|
|
1185
1185
|
}
|
|
1186
1186
|
return page;
|
|
1187
1187
|
}
|
|
1188
|
-
var GRANTS_PATH = (
|
|
1188
|
+
var GRANTS_PATH = (type, id) => type === "service" ? `/api/v1/oauth-clients/${encodeURIComponent(id)}/grants` : `/api/v1/identities/${encodeURIComponent(id)}/grants`;
|
|
1189
|
+
var subjectKey = (type, id) => `${type}:${id}`;
|
|
1189
1190
|
var PARENTS_PATH = "/api/v1/nodes/parents";
|
|
1190
1191
|
function inEffect(window, now) {
|
|
1191
1192
|
return (window.from === null || window.from <= now) && (window.to === null || window.to > now);
|
|
@@ -1294,9 +1295,12 @@ var LocalAuthorizer = class {
|
|
|
1294
1295
|
* root the readable hierarchy does not contain raises
|
|
1295
1296
|
* `authorizer.hierarchy_incomplete`, as a check at such a node does.
|
|
1296
1297
|
*/
|
|
1297
|
-
async grantRoots(identityId, permission) {
|
|
1298
|
+
async grantRoots(identityId, permission, subject = {}) {
|
|
1298
1299
|
const now = this.now();
|
|
1299
|
-
const held = (await this.grantRootsFor(
|
|
1300
|
+
const held = (await this.grantRootsFor(
|
|
1301
|
+
identityId,
|
|
1302
|
+
subject.principalType ?? "identity"
|
|
1303
|
+
)).get(permission) ?? [];
|
|
1300
1304
|
const granted = new Set(
|
|
1301
1305
|
held.filter((window) => inEffect(window, now)).map((w) => w.node)
|
|
1302
1306
|
);
|
|
@@ -1343,7 +1347,9 @@ var LocalAuthorizer = class {
|
|
|
1343
1347
|
*/
|
|
1344
1348
|
async accessibleNodes(identityId, permission, options = {}) {
|
|
1345
1349
|
const cap = options.maxNodes ?? this.maxNodes;
|
|
1346
|
-
const { roots, envWide } = await this.grantRoots(identityId, permission
|
|
1350
|
+
const { roots, envWide } = await this.grantRoots(identityId, permission, {
|
|
1351
|
+
...options.principalType ? { principalType: options.principalType } : {}
|
|
1352
|
+
});
|
|
1347
1353
|
if (envWide) {
|
|
1348
1354
|
return { roots, envWide: true, nodes: null, complete: true };
|
|
1349
1355
|
}
|
|
@@ -1373,8 +1379,8 @@ var LocalAuthorizer = class {
|
|
|
1373
1379
|
* inheritance: the list a screen renders its controls from, answered from
|
|
1374
1380
|
* the same cached grant roots and tree a check uses.
|
|
1375
1381
|
*/
|
|
1376
|
-
async permissionsAt(identityId, nodeId) {
|
|
1377
|
-
const answers = await this.permissionsAtMany(identityId, [nodeId]);
|
|
1382
|
+
async permissionsAt(identityId, nodeId, subject = {}) {
|
|
1383
|
+
const answers = await this.permissionsAtMany(identityId, [nodeId], subject);
|
|
1378
1384
|
return answers.get(nodeId) ?? [];
|
|
1379
1385
|
}
|
|
1380
1386
|
/**
|
|
@@ -1382,9 +1388,12 @@ var LocalAuthorizer = class {
|
|
|
1382
1388
|
* the rows of a list each with their own controls, answered from one read
|
|
1383
1389
|
* of the grant roots and one walk per node.
|
|
1384
1390
|
*/
|
|
1385
|
-
async permissionsAtMany(identityId, nodeIds) {
|
|
1391
|
+
async permissionsAtMany(identityId, nodeIds, subject = {}) {
|
|
1386
1392
|
const now = this.now();
|
|
1387
|
-
const roots = await this.grantRootsFor(
|
|
1393
|
+
const roots = await this.grantRootsFor(
|
|
1394
|
+
identityId,
|
|
1395
|
+
subject.principalType ?? "identity"
|
|
1396
|
+
);
|
|
1388
1397
|
const answers = /* @__PURE__ */ new Map();
|
|
1389
1398
|
for (const nodeId of new Set(nodeIds)) {
|
|
1390
1399
|
const path = await this.lineageOf(nodeId);
|
|
@@ -1402,9 +1411,10 @@ var LocalAuthorizer = class {
|
|
|
1402
1411
|
}
|
|
1403
1412
|
async decide(query) {
|
|
1404
1413
|
const now = this.now();
|
|
1405
|
-
const held = (await this.grantRootsFor(
|
|
1406
|
-
query.
|
|
1407
|
-
|
|
1414
|
+
const held = (await this.grantRootsFor(
|
|
1415
|
+
query.identity_id,
|
|
1416
|
+
query.principal_type ?? "identity"
|
|
1417
|
+
)).get(query.permission);
|
|
1408
1418
|
if (!held || held.length === 0) {
|
|
1409
1419
|
return { allowed: false, reason: "permission_not_held" };
|
|
1410
1420
|
}
|
|
@@ -1440,6 +1450,8 @@ var LocalAuthorizer = class {
|
|
|
1440
1450
|
* cached hierarchy and every other identity's entries stay warm. This is
|
|
1441
1451
|
* the shape an assignment webhook wants: the event names the identity
|
|
1442
1452
|
* whose authority moved, and nothing else needs to pay a refetch for it.
|
|
1453
|
+
* An event naming a machine client (`oauth_client_id`, with `identity_id`
|
|
1454
|
+
* null) passes that client's `client_id` with `principalType: "service"`.
|
|
1443
1455
|
*
|
|
1444
1456
|
* With no argument, everything goes: grants and the hierarchy tree. Not
|
|
1445
1457
|
* needed in normal operation, where entries expire on their own; useful in
|
|
@@ -1455,9 +1467,11 @@ var LocalAuthorizer = class {
|
|
|
1455
1467
|
* the TTL, and webhook-driven invalidation is a latency optimization on
|
|
1456
1468
|
* top of it — size the TTL to the revocation latency you can promise.
|
|
1457
1469
|
*/
|
|
1458
|
-
invalidate(identityId) {
|
|
1470
|
+
invalidate(identityId, subject = {}) {
|
|
1459
1471
|
if (identityId !== void 0) {
|
|
1460
|
-
this.grants.delete(
|
|
1472
|
+
this.grants.delete(
|
|
1473
|
+
subjectKey(subject.principalType ?? "identity", identityId)
|
|
1474
|
+
);
|
|
1461
1475
|
return;
|
|
1462
1476
|
}
|
|
1463
1477
|
this.grants.clear();
|
|
@@ -1500,19 +1514,20 @@ var LocalAuthorizer = class {
|
|
|
1500
1514
|
`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
1515
|
);
|
|
1502
1516
|
}
|
|
1503
|
-
async grantRootsFor(identityId) {
|
|
1504
|
-
const
|
|
1517
|
+
async grantRootsFor(identityId, type) {
|
|
1518
|
+
const key = subjectKey(type, identityId);
|
|
1519
|
+
const cached = this.grants.get(key);
|
|
1505
1520
|
if (cached && cached.expiresAt > this.now()) {
|
|
1506
1521
|
return cached.roots;
|
|
1507
1522
|
}
|
|
1508
|
-
const pending = this.pendingGrants.get(
|
|
1523
|
+
const pending = this.pendingGrants.get(key);
|
|
1509
1524
|
if (pending) {
|
|
1510
1525
|
return pending;
|
|
1511
1526
|
}
|
|
1512
|
-
const read = this.fetchGrantRoots(identityId).finally(() => {
|
|
1513
|
-
this.pendingGrants.delete(
|
|
1527
|
+
const read = this.fetchGrantRoots(identityId, type).finally(() => {
|
|
1528
|
+
this.pendingGrants.delete(key);
|
|
1514
1529
|
});
|
|
1515
|
-
this.pendingGrants.set(
|
|
1530
|
+
this.pendingGrants.set(key, read);
|
|
1516
1531
|
return read;
|
|
1517
1532
|
}
|
|
1518
1533
|
/**
|
|
@@ -1530,9 +1545,9 @@ var LocalAuthorizer = class {
|
|
|
1530
1545
|
* window boundary when that comes before the TTL, so a grant takes effect
|
|
1531
1546
|
* or ends on time rather than up to `ttlMs` late.
|
|
1532
1547
|
*/
|
|
1533
|
-
async fetchGrantRoots(identityId) {
|
|
1548
|
+
async fetchGrantRoots(identityId, type) {
|
|
1534
1549
|
this.stats.grantFetches += 1;
|
|
1535
|
-
const path = GRANTS_PATH(identityId);
|
|
1550
|
+
const path = GRANTS_PATH(type, identityId);
|
|
1536
1551
|
const rows = paginate(
|
|
1537
1552
|
(query) => {
|
|
1538
1553
|
this.stats.grantPages += 1;
|
|
@@ -1565,7 +1580,7 @@ var LocalAuthorizer = class {
|
|
|
1565
1580
|
roots.set(row.permission, [window]);
|
|
1566
1581
|
}
|
|
1567
1582
|
}
|
|
1568
|
-
this.grants.set(identityId, {
|
|
1583
|
+
this.grants.set(subjectKey(type, identityId), {
|
|
1569
1584
|
roots,
|
|
1570
1585
|
expiresAt: Math.min(now + this.ttlMs, boundary)
|
|
1571
1586
|
});
|
|
@@ -1752,7 +1767,13 @@ var HOSTED_ORIGIN = "https://auth.canopy-io.com";
|
|
|
1752
1767
|
var DEFAULT_JWKS_CACHE_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
1753
1768
|
var DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS = 30 * 1e3;
|
|
1754
1769
|
var DEFAULT_JWKS_TIMEOUT_MS = 5e3;
|
|
1755
|
-
var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
1770
|
+
var PRINCIPAL_TYPES = /* @__PURE__ */ new Set([
|
|
1771
|
+
"user",
|
|
1772
|
+
"identity",
|
|
1773
|
+
"service",
|
|
1774
|
+
"api_key",
|
|
1775
|
+
"platform"
|
|
1776
|
+
]);
|
|
1756
1777
|
var DEFAULT_CLOCK_TOLERANCE_SEC = 60;
|
|
1757
1778
|
function resolveIssuer(options) {
|
|
1758
1779
|
const { environmentId, issuer } = options;
|
|
@@ -1772,11 +1793,16 @@ function resolveIssuer(options) {
|
|
|
1772
1793
|
);
|
|
1773
1794
|
}
|
|
1774
1795
|
function orgContext(claims) {
|
|
1775
|
-
const { org_id, org_roles } = claims;
|
|
1776
|
-
|
|
1796
|
+
const { org_id, org_roles, org_role_ids } = claims;
|
|
1797
|
+
const isNameList = (value) => Array.isArray(value) && value.every((entry) => typeof entry === "string" && entry !== "");
|
|
1798
|
+
if (typeof org_id !== "string" || org_id === "" || !isNameList(org_roles) || !isNameList(org_role_ids) || org_role_ids.length !== org_roles.length) {
|
|
1777
1799
|
return null;
|
|
1778
1800
|
}
|
|
1779
|
-
return {
|
|
1801
|
+
return {
|
|
1802
|
+
orgId: org_id,
|
|
1803
|
+
orgRoles: [...org_roles],
|
|
1804
|
+
orgRoleIds: [...org_role_ids]
|
|
1805
|
+
};
|
|
1780
1806
|
}
|
|
1781
1807
|
function decodeBase64Url(value) {
|
|
1782
1808
|
const padded = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
@@ -1801,6 +1827,7 @@ function decodeJsonSegment(segment, what) {
|
|
|
1801
1827
|
var TokenVerifier = class {
|
|
1802
1828
|
issuer;
|
|
1803
1829
|
audience;
|
|
1830
|
+
serviceAudience;
|
|
1804
1831
|
jwksUri;
|
|
1805
1832
|
jwksCacheMaxAgeMs;
|
|
1806
1833
|
jwksMinRefetchIntervalMs;
|
|
@@ -1820,6 +1847,7 @@ var TokenVerifier = class {
|
|
|
1820
1847
|
constructor(options = {}) {
|
|
1821
1848
|
this.issuer = resolveIssuer(options);
|
|
1822
1849
|
this.audience = options.audience;
|
|
1850
|
+
this.serviceAudience = options.serviceAudience;
|
|
1823
1851
|
this.jwksUri = options.jwksUri ?? `${this.issuer}/.well-known/jwks.json`;
|
|
1824
1852
|
this.jwksCacheMaxAgeMs = options.jwksCacheMaxAgeMs ?? DEFAULT_JWKS_CACHE_MAX_AGE_MS;
|
|
1825
1853
|
this.jwksMinRefetchIntervalMs = options.jwksMinRefetchIntervalMs ?? DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS;
|
|
@@ -1842,7 +1870,7 @@ var TokenVerifier = class {
|
|
|
1842
1870
|
* Order matters: the signature is checked before any claim is believed, so
|
|
1843
1871
|
* nothing downstream ever reads an unverified payload.
|
|
1844
1872
|
*/
|
|
1845
|
-
async verify(token) {
|
|
1873
|
+
async verify(token, options = {}) {
|
|
1846
1874
|
const parts = token.split(".");
|
|
1847
1875
|
if (parts.length !== 3) {
|
|
1848
1876
|
throw new CanopyTokenError(
|
|
@@ -1887,11 +1915,11 @@ var TokenVerifier = class {
|
|
|
1887
1915
|
encodedPayload,
|
|
1888
1916
|
"payload"
|
|
1889
1917
|
);
|
|
1890
|
-
this.assertClaims(claims);
|
|
1918
|
+
this.assertClaims(claims, options);
|
|
1891
1919
|
return claims;
|
|
1892
1920
|
}
|
|
1893
1921
|
/** Everything checked after the signature is known good. */
|
|
1894
|
-
assertClaims(claims) {
|
|
1922
|
+
assertClaims(claims, options) {
|
|
1895
1923
|
if (typeof claims.sub !== "string" || claims.sub === "") {
|
|
1896
1924
|
throw new CanopyTokenError(
|
|
1897
1925
|
"token.malformed",
|
|
@@ -1904,11 +1932,15 @@ var TokenVerifier = class {
|
|
|
1904
1932
|
`Token has an unrecognised \`type\` claim: ${JSON.stringify(claims.type)}.`
|
|
1905
1933
|
);
|
|
1906
1934
|
}
|
|
1907
|
-
|
|
1935
|
+
const accepted = /* @__PURE__ */ new Set([
|
|
1936
|
+
...this.allowedTypes,
|
|
1937
|
+
...options.alsoAllow ?? []
|
|
1938
|
+
]);
|
|
1939
|
+
if (!accepted.has(claims.type)) {
|
|
1908
1940
|
throw new CanopyTokenError(
|
|
1909
1941
|
"token.type_not_allowed",
|
|
1910
1942
|
`Token is for a "${claims.type}" principal; this verifier accepts ${[
|
|
1911
|
-
...
|
|
1943
|
+
...accepted
|
|
1912
1944
|
].map((type) => `"${type}"`).join(", ")}. Set \`allowedTypes\` to accept another.`
|
|
1913
1945
|
);
|
|
1914
1946
|
}
|
|
@@ -1950,9 +1982,27 @@ var TokenVerifier = class {
|
|
|
1950
1982
|
* token being verified by something that never said which client it is, and
|
|
1951
1983
|
* ignoring it would accept a token minted for a different client. So it
|
|
1952
1984
|
* throws and names the option.
|
|
1985
|
+
*
|
|
1986
|
+
* A service token names the API it was minted for, never a client, so it
|
|
1987
|
+
* is held to `serviceAudience` alone, which it must name.
|
|
1953
1988
|
*/
|
|
1954
1989
|
assertAudience(claims) {
|
|
1955
1990
|
const audiences = claims.aud === void 0 ? [] : Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
1991
|
+
if (claims.type === "service") {
|
|
1992
|
+
if (this.serviceAudience === void 0) {
|
|
1993
|
+
throw new CanopyTokenError(
|
|
1994
|
+
"token.audience_unverified",
|
|
1995
|
+
"This is a machine client's service token, and the verifier has no `serviceAudience` configured. Set `serviceAudience` to the audience your API answers to."
|
|
1996
|
+
);
|
|
1997
|
+
}
|
|
1998
|
+
if (!audiences.includes(this.serviceAudience)) {
|
|
1999
|
+
throw new CanopyTokenError(
|
|
2000
|
+
"token.audience_mismatch",
|
|
2001
|
+
`Token audience does not include "${this.serviceAudience}".`
|
|
2002
|
+
);
|
|
2003
|
+
}
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
1956
2006
|
if (this.audience === void 0) {
|
|
1957
2007
|
if (audiences.length > 0) {
|
|
1958
2008
|
throw new CanopyTokenError(
|