@canopy-io/node 0.16.0 → 0.18.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 +25 -0
- package/README.md +110 -25
- package/dist/index.cjs +320 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +603 -142
- package/dist/index.d.ts +603 -142
- package/dist/index.js +319 -36
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -93,11 +93,18 @@ function withoutTrailingSlashes(value) {
|
|
|
93
93
|
return value.slice(0, end);
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
// src/origin.ts
|
|
97
|
+
var HOSTED_ORIGIN = "https://auth.canopy-io.com";
|
|
98
|
+
function hostedOrigin() {
|
|
99
|
+
const env = globalThis.process?.env;
|
|
100
|
+
const configured = env?.["CANOPY_BASE_URL"]?.trim();
|
|
101
|
+
return withoutTrailingSlashes(configured || HOSTED_ORIGIN);
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
// src/client.ts
|
|
97
105
|
function isCursorPagination(pagination) {
|
|
98
106
|
return "next_cursor" in pagination;
|
|
99
107
|
}
|
|
100
|
-
var DEFAULT_BASE_URL = "https://auth.canopy-io.com";
|
|
101
108
|
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
102
109
|
var DEFAULT_MAX_RETRIES = 2;
|
|
103
110
|
var DEFAULT_MAX_BACKOFF_MS = 3e4;
|
|
@@ -117,7 +124,7 @@ var CanopyClient = class {
|
|
|
117
124
|
"CanopyClient requires either `apiKey` (server-to-server) or `accessToken` (a user or identity JWT)."
|
|
118
125
|
);
|
|
119
126
|
}
|
|
120
|
-
this.baseUrl = withoutTrailingSlashes(options.baseUrl ??
|
|
127
|
+
this.baseUrl = withoutTrailingSlashes(options.baseUrl ?? hostedOrigin());
|
|
121
128
|
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
122
129
|
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
123
130
|
this.maxBackoffMs = options.maxBackoffMs ?? DEFAULT_MAX_BACKOFF_MS;
|
|
@@ -286,13 +293,13 @@ var CanopyClient = class {
|
|
|
286
293
|
if (response.status === 204) {
|
|
287
294
|
return void 0;
|
|
288
295
|
}
|
|
289
|
-
const
|
|
290
|
-
if (
|
|
296
|
+
const text2 = await response.text();
|
|
297
|
+
if (text2 === "") {
|
|
291
298
|
return void 0;
|
|
292
299
|
}
|
|
293
300
|
let parsed;
|
|
294
301
|
try {
|
|
295
|
-
parsed = JSON.parse(
|
|
302
|
+
parsed = JSON.parse(text2);
|
|
296
303
|
} catch {
|
|
297
304
|
throw new CanopyError(
|
|
298
305
|
{
|
|
@@ -328,15 +335,15 @@ var CanopyClient = class {
|
|
|
328
335
|
return new CanopyError(body, { method, path }, retryAfter ?? void 0);
|
|
329
336
|
}
|
|
330
337
|
};
|
|
331
|
-
function parseRetryAfter(
|
|
332
|
-
if (!
|
|
338
|
+
function parseRetryAfter(header2) {
|
|
339
|
+
if (!header2) {
|
|
333
340
|
return null;
|
|
334
341
|
}
|
|
335
|
-
const seconds = Number(
|
|
342
|
+
const seconds = Number(header2);
|
|
336
343
|
if (Number.isFinite(seconds)) {
|
|
337
344
|
return Math.max(0, seconds * 1e3);
|
|
338
345
|
}
|
|
339
|
-
const date = Date.parse(
|
|
346
|
+
const date = Date.parse(header2);
|
|
340
347
|
if (Number.isNaN(date)) {
|
|
341
348
|
return null;
|
|
342
349
|
}
|
|
@@ -1191,6 +1198,113 @@ var Canopy = class {
|
|
|
1191
1198
|
}
|
|
1192
1199
|
};
|
|
1193
1200
|
|
|
1201
|
+
// src/invalidation.ts
|
|
1202
|
+
var NO_ACCESS_CHANGE = /* @__PURE__ */ new Set([
|
|
1203
|
+
"identity.created",
|
|
1204
|
+
"identity.updated",
|
|
1205
|
+
"account_member.added",
|
|
1206
|
+
"account_member.removed",
|
|
1207
|
+
"session.all_revoked",
|
|
1208
|
+
"oauth_client.secret_rotated",
|
|
1209
|
+
"organization.updated",
|
|
1210
|
+
"organization.policy.updated",
|
|
1211
|
+
"organization.sso_connection.bound",
|
|
1212
|
+
"organization.sso_connection.unbound",
|
|
1213
|
+
// A pushed group grants nothing until it is mapped, which is its own event.
|
|
1214
|
+
"scim.group.created"
|
|
1215
|
+
]);
|
|
1216
|
+
var IDENTITY_RESOURCE = /* @__PURE__ */ new Set([
|
|
1217
|
+
"identity.status_set",
|
|
1218
|
+
"identity.erased",
|
|
1219
|
+
"identity.env_membership.added",
|
|
1220
|
+
"identity.env_membership.removed",
|
|
1221
|
+
"scim.user.provisioned",
|
|
1222
|
+
"scim.user.updated",
|
|
1223
|
+
"scim.user.deprovisioned",
|
|
1224
|
+
"scim.user.reactivated"
|
|
1225
|
+
]);
|
|
1226
|
+
var ONE_ASSIGNMENT = /* @__PURE__ */ new Set([
|
|
1227
|
+
"assignment.created",
|
|
1228
|
+
"assignment.updated",
|
|
1229
|
+
"assignment.removed",
|
|
1230
|
+
"assignment.activated",
|
|
1231
|
+
"assignment.expired"
|
|
1232
|
+
]);
|
|
1233
|
+
var AFFECTED_ASSIGNMENTS = /* @__PURE__ */ new Set([
|
|
1234
|
+
"assignment.bulk_removed",
|
|
1235
|
+
"assignment.bulk_role_changed"
|
|
1236
|
+
]);
|
|
1237
|
+
var IDENTITY_IN_METADATA = /* @__PURE__ */ new Set([
|
|
1238
|
+
"organization.member.added",
|
|
1239
|
+
"organization.member.role_changed",
|
|
1240
|
+
"organization.member.removed",
|
|
1241
|
+
"scim.group.member_added",
|
|
1242
|
+
"scim.group.member_removed"
|
|
1243
|
+
]);
|
|
1244
|
+
var MACHINE_CLIENT = /* @__PURE__ */ new Set([
|
|
1245
|
+
"oauth_client.updated",
|
|
1246
|
+
"oauth_client.deleted"
|
|
1247
|
+
]);
|
|
1248
|
+
function holderOf(metadata) {
|
|
1249
|
+
const identityId = text(metadata["identity_id"]);
|
|
1250
|
+
if (identityId) {
|
|
1251
|
+
return { principalType: "identity", id: identityId };
|
|
1252
|
+
}
|
|
1253
|
+
const clientId = text(metadata["client_id"]);
|
|
1254
|
+
if (clientId) {
|
|
1255
|
+
return { principalType: "service", id: clientId };
|
|
1256
|
+
}
|
|
1257
|
+
return void 0;
|
|
1258
|
+
}
|
|
1259
|
+
function invalidationFor(change) {
|
|
1260
|
+
const { event, data } = change;
|
|
1261
|
+
const metadata = data.metadata ?? {};
|
|
1262
|
+
if (NO_ACCESS_CHANGE.has(event)) {
|
|
1263
|
+
return "none";
|
|
1264
|
+
}
|
|
1265
|
+
if (IDENTITY_RESOURCE.has(event)) {
|
|
1266
|
+
const id = data.resource_type === "identity" ? text(data.resource_id) : "";
|
|
1267
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1268
|
+
}
|
|
1269
|
+
if (ONE_ASSIGNMENT.has(event)) {
|
|
1270
|
+
const holder = holderOf(metadata);
|
|
1271
|
+
return holder ? [holder] : "all";
|
|
1272
|
+
}
|
|
1273
|
+
if (AFFECTED_ASSIGNMENTS.has(event)) {
|
|
1274
|
+
const affected = Array.isArray(metadata["affected"]) ? metadata["affected"] : [];
|
|
1275
|
+
const holders = affected.map(
|
|
1276
|
+
(entry) => entry !== null && typeof entry === "object" ? holderOf(entry) : void 0
|
|
1277
|
+
);
|
|
1278
|
+
if (holders.length === 0 || holders.some((holder) => !holder)) {
|
|
1279
|
+
return "all";
|
|
1280
|
+
}
|
|
1281
|
+
return unique(holders);
|
|
1282
|
+
}
|
|
1283
|
+
if (IDENTITY_IN_METADATA.has(event)) {
|
|
1284
|
+
const id = text(metadata["identity_id"]);
|
|
1285
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1286
|
+
}
|
|
1287
|
+
if (MACHINE_CLIENT.has(event)) {
|
|
1288
|
+
const clientId = text(metadata["client_id"]);
|
|
1289
|
+
return clientId ? [{ principalType: "service", id: clientId }] : "all";
|
|
1290
|
+
}
|
|
1291
|
+
if (event === "deny.created" || event === "deny.deleted") {
|
|
1292
|
+
const id = metadata["target_type"] === "identity" ? text(metadata["identity_id"]) : "";
|
|
1293
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1294
|
+
}
|
|
1295
|
+
return "all";
|
|
1296
|
+
}
|
|
1297
|
+
function text(value) {
|
|
1298
|
+
return typeof value === "string" ? value : "";
|
|
1299
|
+
}
|
|
1300
|
+
function unique(subjects) {
|
|
1301
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1302
|
+
for (const subject of subjects) {
|
|
1303
|
+
seen.set(`${subject.principalType}:${subject.id}`, subject);
|
|
1304
|
+
}
|
|
1305
|
+
return [...seen.values()];
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1194
1308
|
// src/authorizer.ts
|
|
1195
1309
|
var DEFAULT_TTL_MS = 6e4;
|
|
1196
1310
|
var MAX_TTL_MS = 3e5;
|
|
@@ -1199,6 +1313,8 @@ var MAX_WALK_DEPTH = 256;
|
|
|
1199
1313
|
var PAGE_SIZE = 100;
|
|
1200
1314
|
var MAX_PAGES = 1e4;
|
|
1201
1315
|
var MAX_TREE_WALKS = 3;
|
|
1316
|
+
var CHANGES_PATH = "/api/v1/changes";
|
|
1317
|
+
var CHANGES_PAGE_SIZE = 200;
|
|
1202
1318
|
var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1, 2]);
|
|
1203
1319
|
function understood(page, path) {
|
|
1204
1320
|
if (!KNOWN_MODEL_VERSIONS.has(page.model_version)) {
|
|
@@ -1249,6 +1365,13 @@ var LocalAuthorizer = class {
|
|
|
1249
1365
|
*/
|
|
1250
1366
|
pendingGrants = /* @__PURE__ */ new Map();
|
|
1251
1367
|
pendingTree;
|
|
1368
|
+
/**
|
|
1369
|
+
* Bumped by every full invalidation. A hierarchy read that began before
|
|
1370
|
+
* the bump answers the caller that was waiting on it but is not kept, so
|
|
1371
|
+
* a read already in flight when a change lands cannot cache what the
|
|
1372
|
+
* change replaced. Grants use the pending map for the same purpose.
|
|
1373
|
+
*/
|
|
1374
|
+
treeGeneration = 0;
|
|
1252
1375
|
stats = {
|
|
1253
1376
|
grantFetches: 0,
|
|
1254
1377
|
grantPages: 0,
|
|
@@ -1669,13 +1792,99 @@ var LocalAuthorizer = class {
|
|
|
1669
1792
|
*/
|
|
1670
1793
|
invalidate(identityId, subject = {}) {
|
|
1671
1794
|
if (identityId !== void 0) {
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
);
|
|
1795
|
+
const key = subjectKey(subject.principalType ?? "identity", identityId);
|
|
1796
|
+
this.grants.delete(key);
|
|
1797
|
+
this.pendingGrants.delete(key);
|
|
1675
1798
|
return;
|
|
1676
1799
|
}
|
|
1677
1800
|
this.grants.clear();
|
|
1801
|
+
this.pendingGrants.clear();
|
|
1678
1802
|
this.tree = void 0;
|
|
1803
|
+
this.pendingTree = void 0;
|
|
1804
|
+
this.treeGeneration += 1;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Drop what one change makes stale, from a webhook delivery or a
|
|
1808
|
+
* change-feed entry (the two share one shape). Answers what it dropped.
|
|
1809
|
+
*
|
|
1810
|
+
* A change naming whose access moved (an assignment, an Environment or
|
|
1811
|
+
* organization membership, an identity's status, a deny on one person,
|
|
1812
|
+
* a machine client disabled or deleted) drops those subjects' grants and
|
|
1813
|
+
* keeps everyone else's and the hierarchy warm. A change whose reach it
|
|
1814
|
+
* cannot name (a role, a permission, a node, the hierarchy schema, an
|
|
1815
|
+
* organization created or deleted, a deny on a role or a machine client,
|
|
1816
|
+
* a directory group mapped) drops everything. A change that moves no
|
|
1817
|
+
* one's access (a profile edit, a sign-in policy, a session revoked)
|
|
1818
|
+
* drops nothing. An event this SDK does not know drops everything.
|
|
1819
|
+
*
|
|
1820
|
+
* Check the delivery's signature first (`verifyWebhookSignature`): this
|
|
1821
|
+
* trusts what it is given. Like {@link invalidate}, it reaches this
|
|
1822
|
+
* process only; see there for what that means across several instances.
|
|
1823
|
+
*/
|
|
1824
|
+
applyEvent(change) {
|
|
1825
|
+
const reach = invalidationFor(change);
|
|
1826
|
+
if (reach === "none") {
|
|
1827
|
+
return { event: change.event, invalidated: "none" };
|
|
1828
|
+
}
|
|
1829
|
+
if (reach === "all") {
|
|
1830
|
+
this.invalidate();
|
|
1831
|
+
return { event: change.event, invalidated: "all" };
|
|
1832
|
+
}
|
|
1833
|
+
for (const subject of reach) {
|
|
1834
|
+
this.invalidate(subject.id, { principalType: subject.principalType });
|
|
1835
|
+
}
|
|
1836
|
+
return { event: change.event, invalidated: "subjects", subjects: reach };
|
|
1837
|
+
}
|
|
1838
|
+
/**
|
|
1839
|
+
* Read `GET /api/v1/changes` from `cursor` to the newest change and apply
|
|
1840
|
+
* each one, as {@link applyEvent} would a webhook. Answers the cursor to
|
|
1841
|
+
* resume from next time.
|
|
1842
|
+
*
|
|
1843
|
+
* Two uses. After a restart, to catch up on deliveries a process missed
|
|
1844
|
+
* while it was down, before it serves from a cache warmed elsewhere. And
|
|
1845
|
+
* in place of webhooks: every instance polling on an interval shorter
|
|
1846
|
+
* than `ttlMs` sees every change, which a webhook (delivered to one
|
|
1847
|
+
* instance) does not give a fleet.
|
|
1848
|
+
*
|
|
1849
|
+
* The feed holds what the audit log retains, so a cursor older than that
|
|
1850
|
+
* resumes at the oldest change still held. Each page is one request; a
|
|
1851
|
+
* replay with no cursor walks the whole retained window once.
|
|
1852
|
+
*/
|
|
1853
|
+
async replayChanges(options = {}) {
|
|
1854
|
+
let cursor = options.cursor ?? null;
|
|
1855
|
+
let changes = 0;
|
|
1856
|
+
let invalidatedAll = false;
|
|
1857
|
+
let subjectsInvalidated = 0;
|
|
1858
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
1859
|
+
const read = await this.client.request(
|
|
1860
|
+
"GET",
|
|
1861
|
+
CHANGES_PATH,
|
|
1862
|
+
{
|
|
1863
|
+
...this.readOptions,
|
|
1864
|
+
query: {
|
|
1865
|
+
limit: CHANGES_PAGE_SIZE,
|
|
1866
|
+
...cursor === null ? {} : { cursor }
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
);
|
|
1870
|
+
for (const change of read.items) {
|
|
1871
|
+
const applied = this.applyEvent(change);
|
|
1872
|
+
changes += 1;
|
|
1873
|
+
if (applied.invalidated === "all") {
|
|
1874
|
+
invalidatedAll = true;
|
|
1875
|
+
}
|
|
1876
|
+
if (applied.invalidated === "subjects") {
|
|
1877
|
+
subjectsInvalidated += applied.subjects.length;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
const next = read.pagination.next_cursor;
|
|
1881
|
+
const caughtUp = read.items.length < CHANGES_PAGE_SIZE || next === null || next === cursor;
|
|
1882
|
+
cursor = next ?? cursor;
|
|
1883
|
+
if (caughtUp) {
|
|
1884
|
+
break;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
return { cursor, changes, invalidatedAll, subjectsInvalidated };
|
|
1679
1888
|
}
|
|
1680
1889
|
/** Fire the decision hooks, after the verdict and without waiting on them. */
|
|
1681
1890
|
announce(event) {
|
|
@@ -1724,8 +1933,14 @@ var LocalAuthorizer = class {
|
|
|
1724
1933
|
if (pending) {
|
|
1725
1934
|
return pending;
|
|
1726
1935
|
}
|
|
1727
|
-
const read = this.fetchGrantRoots(
|
|
1728
|
-
|
|
1936
|
+
const read = this.fetchGrantRoots(
|
|
1937
|
+
identityId,
|
|
1938
|
+
type,
|
|
1939
|
+
() => this.pendingGrants.get(key) === read
|
|
1940
|
+
).finally(() => {
|
|
1941
|
+
if (this.pendingGrants.get(key) === read) {
|
|
1942
|
+
this.pendingGrants.delete(key);
|
|
1943
|
+
}
|
|
1729
1944
|
});
|
|
1730
1945
|
this.pendingGrants.set(key, read);
|
|
1731
1946
|
return read;
|
|
@@ -1748,7 +1963,7 @@ var LocalAuthorizer = class {
|
|
|
1748
1963
|
* Every page carries the subject's deny roots whole, beside its rows; they
|
|
1749
1964
|
* are gathered from each page, so a read is one snapshot of both.
|
|
1750
1965
|
*/
|
|
1751
|
-
async fetchGrantRoots(identityId, type) {
|
|
1966
|
+
async fetchGrantRoots(identityId, type, stillCurrent = () => true) {
|
|
1752
1967
|
this.stats.grantFetches += 1;
|
|
1753
1968
|
const path = GRANTS_PATH(type, identityId);
|
|
1754
1969
|
const denies = /* @__PURE__ */ new Map();
|
|
@@ -1799,10 +2014,12 @@ var LocalAuthorizer = class {
|
|
|
1799
2014
|
}
|
|
1800
2015
|
}
|
|
1801
2016
|
const holdings = { roots, denies };
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
2017
|
+
if (stillCurrent()) {
|
|
2018
|
+
this.grants.set(subjectKey(type, identityId), {
|
|
2019
|
+
holdings,
|
|
2020
|
+
expiresAt: Math.min(now + this.ttlMs, boundary)
|
|
2021
|
+
});
|
|
2022
|
+
}
|
|
1806
2023
|
return holdings;
|
|
1807
2024
|
}
|
|
1808
2025
|
async treeEntry() {
|
|
@@ -1813,7 +2030,9 @@ var LocalAuthorizer = class {
|
|
|
1813
2030
|
return this.pendingTree;
|
|
1814
2031
|
}
|
|
1815
2032
|
const read = this.fetchTree().finally(() => {
|
|
1816
|
-
this.pendingTree
|
|
2033
|
+
if (this.pendingTree === read) {
|
|
2034
|
+
this.pendingTree = void 0;
|
|
2035
|
+
}
|
|
1817
2036
|
});
|
|
1818
2037
|
this.pendingTree = read;
|
|
1819
2038
|
return read;
|
|
@@ -1839,26 +2058,30 @@ var LocalAuthorizer = class {
|
|
|
1839
2058
|
async fetchTree() {
|
|
1840
2059
|
this.stats.treeRequests += 1;
|
|
1841
2060
|
const held = this.tree;
|
|
2061
|
+
const generation = this.treeGeneration;
|
|
1842
2062
|
let first = await this.parentsFirstPage(held?.etag ?? void 0);
|
|
1843
2063
|
if (!first.modified) {
|
|
1844
2064
|
if (held) {
|
|
1845
2065
|
this.stats.treeNotModified += 1;
|
|
1846
|
-
|
|
1847
|
-
|
|
2066
|
+
const renewed = { ...held, expiresAt: this.now() + this.ttlMs };
|
|
2067
|
+
if (generation === this.treeGeneration) {
|
|
2068
|
+
this.tree = renewed;
|
|
2069
|
+
}
|
|
2070
|
+
return renewed;
|
|
1848
2071
|
}
|
|
1849
|
-
return this.keepTree([], null);
|
|
2072
|
+
return this.keepTree([], null, generation);
|
|
1850
2073
|
}
|
|
1851
2074
|
for (let walk = 1; ; walk++) {
|
|
1852
2075
|
const edges = await this.parentsRemainingPages(first.data);
|
|
1853
2076
|
if (!spansPages(first.data) || first.etag === null) {
|
|
1854
|
-
return this.keepTree(edges, first.etag);
|
|
2077
|
+
return this.keepTree(edges, first.etag, generation);
|
|
1855
2078
|
}
|
|
1856
2079
|
const confirm = await this.parentsFirstPage(first.etag);
|
|
1857
2080
|
if (!confirm.modified) {
|
|
1858
|
-
return this.keepTree(edges, first.etag);
|
|
2081
|
+
return this.keepTree(edges, first.etag, generation);
|
|
1859
2082
|
}
|
|
1860
2083
|
if (walk >= MAX_TREE_WALKS) {
|
|
1861
|
-
return this.keepTree(edges, null);
|
|
2084
|
+
return this.keepTree(edges, null, generation);
|
|
1862
2085
|
}
|
|
1863
2086
|
first = confirm;
|
|
1864
2087
|
}
|
|
@@ -1899,7 +2122,7 @@ var LocalAuthorizer = class {
|
|
|
1899
2122
|
}
|
|
1900
2123
|
return edges;
|
|
1901
2124
|
}
|
|
1902
|
-
keepTree(edges, etag) {
|
|
2125
|
+
keepTree(edges, etag, generation) {
|
|
1903
2126
|
const ids = /* @__PURE__ */ new Map();
|
|
1904
2127
|
for (const edge of edges) {
|
|
1905
2128
|
ids.set(edge.id, edge.id);
|
|
@@ -1918,13 +2141,16 @@ var LocalAuthorizer = class {
|
|
|
1918
2141
|
}
|
|
1919
2142
|
}
|
|
1920
2143
|
}
|
|
1921
|
-
|
|
2144
|
+
const entry = {
|
|
1922
2145
|
parents,
|
|
1923
2146
|
children,
|
|
1924
2147
|
etag,
|
|
1925
2148
|
expiresAt: this.now() + this.ttlMs
|
|
1926
2149
|
};
|
|
1927
|
-
|
|
2150
|
+
if (generation === this.treeGeneration) {
|
|
2151
|
+
this.tree = entry;
|
|
2152
|
+
}
|
|
2153
|
+
return entry;
|
|
1928
2154
|
}
|
|
1929
2155
|
};
|
|
1930
2156
|
function spansPages(first) {
|
|
@@ -1982,7 +2208,6 @@ function authorizationErrorStatus(error) {
|
|
|
1982
2208
|
}
|
|
1983
2209
|
|
|
1984
2210
|
// src/verify.ts
|
|
1985
|
-
var HOSTED_ORIGIN = "https://auth.canopy-io.com";
|
|
1986
2211
|
var DEFAULT_JWKS_CACHE_MAX_AGE_MS = 10 * 60 * 1e3;
|
|
1987
2212
|
var DEFAULT_JWKS_MIN_REFETCH_INTERVAL_MS = 30 * 1e3;
|
|
1988
2213
|
var DEFAULT_JWKS_TIMEOUT_MS = 5e3;
|
|
@@ -2005,7 +2230,7 @@ function resolveIssuer(options) {
|
|
|
2005
2230
|
return withoutTrailingSlashes(issuer);
|
|
2006
2231
|
}
|
|
2007
2232
|
if (environmentId) {
|
|
2008
|
-
return `${
|
|
2233
|
+
return `${hostedOrigin()}/environments/${environmentId}`;
|
|
2009
2234
|
}
|
|
2010
2235
|
throw new TypeError(
|
|
2011
2236
|
"TokenVerifier needs environmentId (the Environment your tokens come from) or, on a custom auth domain or self-hosted instance, issuer."
|
|
@@ -2099,14 +2324,14 @@ var TokenVerifier = class {
|
|
|
2099
2324
|
);
|
|
2100
2325
|
}
|
|
2101
2326
|
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
2102
|
-
const
|
|
2103
|
-
if (
|
|
2327
|
+
const header2 = decodeJsonSegment(encodedHeader, "header");
|
|
2328
|
+
if (header2.alg !== "RS256") {
|
|
2104
2329
|
throw new CanopyTokenError(
|
|
2105
2330
|
"token.unsupported_algorithm",
|
|
2106
|
-
`Token is signed with "${
|
|
2331
|
+
`Token is signed with "${header2.alg ?? "none"}"; Canopy issues RS256.`
|
|
2107
2332
|
);
|
|
2108
2333
|
}
|
|
2109
|
-
const key = await this.resolveKey(
|
|
2334
|
+
const key = await this.resolveKey(header2.kid);
|
|
2110
2335
|
const signed = new TextEncoder().encode(
|
|
2111
2336
|
`${encodedHeader}.${encodedPayload}`
|
|
2112
2337
|
);
|
|
@@ -2395,7 +2620,65 @@ var TokenVerifier = class {
|
|
|
2395
2620
|
function isWebhookEvent(delivery, name) {
|
|
2396
2621
|
return delivery.event === name;
|
|
2397
2622
|
}
|
|
2623
|
+
var WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 300;
|
|
2624
|
+
var SIGNATURE_PREFIX = "v1,";
|
|
2625
|
+
var SIGNATURE_HEX = /^[0-9a-f]{64}$/i;
|
|
2626
|
+
async function verifyWebhookSignature(input) {
|
|
2627
|
+
const id = header(input.headers, "x-canopy-webhook-id");
|
|
2628
|
+
const timestamp = header(input.headers, "x-canopy-timestamp");
|
|
2629
|
+
const signature = header(input.headers, "x-canopy-signature");
|
|
2630
|
+
if (!id || !timestamp || !signature || !/^\d+$/.test(timestamp)) {
|
|
2631
|
+
return false;
|
|
2632
|
+
}
|
|
2633
|
+
const tolerance = input.toleranceSeconds ?? WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS;
|
|
2634
|
+
const nowSeconds = Math.floor((input.now ?? Date.now)() / 1e3);
|
|
2635
|
+
if (Math.abs(nowSeconds - Number(timestamp)) > tolerance) {
|
|
2636
|
+
return false;
|
|
2637
|
+
}
|
|
2638
|
+
const secrets = (typeof input.secret === "string" ? [input.secret] : input.secret).filter((secret) => typeof secret === "string" && secret.length > 0);
|
|
2639
|
+
const received = signature.split(/\s+/).filter((value) => value.startsWith(SIGNATURE_PREFIX)).map((value) => value.slice(SIGNATURE_PREFIX.length)).filter((hex) => SIGNATURE_HEX.test(hex)).map(hexToBytes);
|
|
2640
|
+
if (secrets.length === 0 || received.length === 0) {
|
|
2641
|
+
return false;
|
|
2642
|
+
}
|
|
2643
|
+
const encoder = new TextEncoder();
|
|
2644
|
+
const body = typeof input.rawBody === "string" ? encoder.encode(input.rawBody) : input.rawBody;
|
|
2645
|
+
const prefix = encoder.encode(`${id}.${timestamp}.`);
|
|
2646
|
+
const signed = new Uint8Array(new ArrayBuffer(prefix.length + body.length));
|
|
2647
|
+
signed.set(prefix, 0);
|
|
2648
|
+
signed.set(body, prefix.length);
|
|
2649
|
+
const checks = await Promise.all(
|
|
2650
|
+
secrets.flatMap((secret) => {
|
|
2651
|
+
const key = crypto.subtle.importKey(
|
|
2652
|
+
"raw",
|
|
2653
|
+
encoder.encode(secret),
|
|
2654
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
2655
|
+
false,
|
|
2656
|
+
["verify"]
|
|
2657
|
+
);
|
|
2658
|
+
return received.map(
|
|
2659
|
+
async (value) => crypto.subtle.verify("HMAC", await key, value, signed)
|
|
2660
|
+
);
|
|
2661
|
+
})
|
|
2662
|
+
);
|
|
2663
|
+
return checks.some(Boolean);
|
|
2664
|
+
}
|
|
2665
|
+
function header(headers, name) {
|
|
2666
|
+
if (typeof headers.get === "function") {
|
|
2667
|
+
return headers.get(name) ?? void 0;
|
|
2668
|
+
}
|
|
2669
|
+
const record = headers;
|
|
2670
|
+
const key = Object.keys(record).find((k) => k.toLowerCase() === name);
|
|
2671
|
+
const value = key === void 0 ? void 0 : record[key];
|
|
2672
|
+
return Array.isArray(value) ? value.join(" ") : value;
|
|
2673
|
+
}
|
|
2674
|
+
function hexToBytes(hex) {
|
|
2675
|
+
const bytes = new Uint8Array(new ArrayBuffer(hex.length / 2));
|
|
2676
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2677
|
+
bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
2678
|
+
}
|
|
2679
|
+
return bytes;
|
|
2680
|
+
}
|
|
2398
2681
|
|
|
2399
|
-
export { Assignments, Canopy, CanopyAuthorizerError, CanopyClient, CanopyConnectionError, CanopyError, CanopyTokenError, DEFAULT_MAX_NODES, Identities, LocalAuthorizer, MAX_TTL_MS, Organizations, Paginator, Permissions, Roles, TokenVerifier, authorizationErrorStatus, isCanopyAuthorizerError, isCanopyConnectionError, isCanopyError, isCanopyTokenError, isCursorPagination, isWebhookEvent, orgContext, paginate, tokenErrorStatus, withConcurrency };
|
|
2682
|
+
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 };
|
|
2400
2683
|
//# sourceMappingURL=index.js.map
|
|
2401
2684
|
//# sourceMappingURL=index.js.map
|