@canopy-io/node 0.16.0 → 0.17.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 +15 -0
- package/README.md +105 -25
- package/dist/index.cjs +310 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +576 -137
- package/dist/index.d.ts +576 -137
- package/dist/index.js +309 -32
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -286,13 +286,13 @@ var CanopyClient = class {
|
|
|
286
286
|
if (response.status === 204) {
|
|
287
287
|
return void 0;
|
|
288
288
|
}
|
|
289
|
-
const
|
|
290
|
-
if (
|
|
289
|
+
const text2 = await response.text();
|
|
290
|
+
if (text2 === "") {
|
|
291
291
|
return void 0;
|
|
292
292
|
}
|
|
293
293
|
let parsed;
|
|
294
294
|
try {
|
|
295
|
-
parsed = JSON.parse(
|
|
295
|
+
parsed = JSON.parse(text2);
|
|
296
296
|
} catch {
|
|
297
297
|
throw new CanopyError(
|
|
298
298
|
{
|
|
@@ -328,15 +328,15 @@ var CanopyClient = class {
|
|
|
328
328
|
return new CanopyError(body, { method, path }, retryAfter ?? void 0);
|
|
329
329
|
}
|
|
330
330
|
};
|
|
331
|
-
function parseRetryAfter(
|
|
332
|
-
if (!
|
|
331
|
+
function parseRetryAfter(header2) {
|
|
332
|
+
if (!header2) {
|
|
333
333
|
return null;
|
|
334
334
|
}
|
|
335
|
-
const seconds = Number(
|
|
335
|
+
const seconds = Number(header2);
|
|
336
336
|
if (Number.isFinite(seconds)) {
|
|
337
337
|
return Math.max(0, seconds * 1e3);
|
|
338
338
|
}
|
|
339
|
-
const date = Date.parse(
|
|
339
|
+
const date = Date.parse(header2);
|
|
340
340
|
if (Number.isNaN(date)) {
|
|
341
341
|
return null;
|
|
342
342
|
}
|
|
@@ -1191,6 +1191,113 @@ var Canopy = class {
|
|
|
1191
1191
|
}
|
|
1192
1192
|
};
|
|
1193
1193
|
|
|
1194
|
+
// src/invalidation.ts
|
|
1195
|
+
var NO_ACCESS_CHANGE = /* @__PURE__ */ new Set([
|
|
1196
|
+
"identity.created",
|
|
1197
|
+
"identity.updated",
|
|
1198
|
+
"account_member.added",
|
|
1199
|
+
"account_member.removed",
|
|
1200
|
+
"session.all_revoked",
|
|
1201
|
+
"oauth_client.secret_rotated",
|
|
1202
|
+
"organization.updated",
|
|
1203
|
+
"organization.policy.updated",
|
|
1204
|
+
"organization.sso_connection.bound",
|
|
1205
|
+
"organization.sso_connection.unbound",
|
|
1206
|
+
// A pushed group grants nothing until it is mapped, which is its own event.
|
|
1207
|
+
"scim.group.created"
|
|
1208
|
+
]);
|
|
1209
|
+
var IDENTITY_RESOURCE = /* @__PURE__ */ new Set([
|
|
1210
|
+
"identity.status_set",
|
|
1211
|
+
"identity.erased",
|
|
1212
|
+
"identity.env_membership.added",
|
|
1213
|
+
"identity.env_membership.removed",
|
|
1214
|
+
"scim.user.provisioned",
|
|
1215
|
+
"scim.user.updated",
|
|
1216
|
+
"scim.user.deprovisioned",
|
|
1217
|
+
"scim.user.reactivated"
|
|
1218
|
+
]);
|
|
1219
|
+
var ONE_ASSIGNMENT = /* @__PURE__ */ new Set([
|
|
1220
|
+
"assignment.created",
|
|
1221
|
+
"assignment.updated",
|
|
1222
|
+
"assignment.removed",
|
|
1223
|
+
"assignment.activated",
|
|
1224
|
+
"assignment.expired"
|
|
1225
|
+
]);
|
|
1226
|
+
var AFFECTED_ASSIGNMENTS = /* @__PURE__ */ new Set([
|
|
1227
|
+
"assignment.bulk_removed",
|
|
1228
|
+
"assignment.bulk_role_changed"
|
|
1229
|
+
]);
|
|
1230
|
+
var IDENTITY_IN_METADATA = /* @__PURE__ */ new Set([
|
|
1231
|
+
"organization.member.added",
|
|
1232
|
+
"organization.member.role_changed",
|
|
1233
|
+
"organization.member.removed",
|
|
1234
|
+
"scim.group.member_added",
|
|
1235
|
+
"scim.group.member_removed"
|
|
1236
|
+
]);
|
|
1237
|
+
var MACHINE_CLIENT = /* @__PURE__ */ new Set([
|
|
1238
|
+
"oauth_client.updated",
|
|
1239
|
+
"oauth_client.deleted"
|
|
1240
|
+
]);
|
|
1241
|
+
function holderOf(metadata) {
|
|
1242
|
+
const identityId = text(metadata["identity_id"]);
|
|
1243
|
+
if (identityId) {
|
|
1244
|
+
return { principalType: "identity", id: identityId };
|
|
1245
|
+
}
|
|
1246
|
+
const clientId = text(metadata["client_id"]);
|
|
1247
|
+
if (clientId) {
|
|
1248
|
+
return { principalType: "service", id: clientId };
|
|
1249
|
+
}
|
|
1250
|
+
return void 0;
|
|
1251
|
+
}
|
|
1252
|
+
function invalidationFor(change) {
|
|
1253
|
+
const { event, data } = change;
|
|
1254
|
+
const metadata = data.metadata ?? {};
|
|
1255
|
+
if (NO_ACCESS_CHANGE.has(event)) {
|
|
1256
|
+
return "none";
|
|
1257
|
+
}
|
|
1258
|
+
if (IDENTITY_RESOURCE.has(event)) {
|
|
1259
|
+
const id = data.resource_type === "identity" ? text(data.resource_id) : "";
|
|
1260
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1261
|
+
}
|
|
1262
|
+
if (ONE_ASSIGNMENT.has(event)) {
|
|
1263
|
+
const holder = holderOf(metadata);
|
|
1264
|
+
return holder ? [holder] : "all";
|
|
1265
|
+
}
|
|
1266
|
+
if (AFFECTED_ASSIGNMENTS.has(event)) {
|
|
1267
|
+
const affected = Array.isArray(metadata["affected"]) ? metadata["affected"] : [];
|
|
1268
|
+
const holders = affected.map(
|
|
1269
|
+
(entry) => entry !== null && typeof entry === "object" ? holderOf(entry) : void 0
|
|
1270
|
+
);
|
|
1271
|
+
if (holders.length === 0 || holders.some((holder) => !holder)) {
|
|
1272
|
+
return "all";
|
|
1273
|
+
}
|
|
1274
|
+
return unique(holders);
|
|
1275
|
+
}
|
|
1276
|
+
if (IDENTITY_IN_METADATA.has(event)) {
|
|
1277
|
+
const id = text(metadata["identity_id"]);
|
|
1278
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1279
|
+
}
|
|
1280
|
+
if (MACHINE_CLIENT.has(event)) {
|
|
1281
|
+
const clientId = text(metadata["client_id"]);
|
|
1282
|
+
return clientId ? [{ principalType: "service", id: clientId }] : "all";
|
|
1283
|
+
}
|
|
1284
|
+
if (event === "deny.created" || event === "deny.deleted") {
|
|
1285
|
+
const id = metadata["target_type"] === "identity" ? text(metadata["identity_id"]) : "";
|
|
1286
|
+
return id ? [{ principalType: "identity", id }] : "all";
|
|
1287
|
+
}
|
|
1288
|
+
return "all";
|
|
1289
|
+
}
|
|
1290
|
+
function text(value) {
|
|
1291
|
+
return typeof value === "string" ? value : "";
|
|
1292
|
+
}
|
|
1293
|
+
function unique(subjects) {
|
|
1294
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1295
|
+
for (const subject of subjects) {
|
|
1296
|
+
seen.set(`${subject.principalType}:${subject.id}`, subject);
|
|
1297
|
+
}
|
|
1298
|
+
return [...seen.values()];
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1194
1301
|
// src/authorizer.ts
|
|
1195
1302
|
var DEFAULT_TTL_MS = 6e4;
|
|
1196
1303
|
var MAX_TTL_MS = 3e5;
|
|
@@ -1199,6 +1306,8 @@ var MAX_WALK_DEPTH = 256;
|
|
|
1199
1306
|
var PAGE_SIZE = 100;
|
|
1200
1307
|
var MAX_PAGES = 1e4;
|
|
1201
1308
|
var MAX_TREE_WALKS = 3;
|
|
1309
|
+
var CHANGES_PATH = "/api/v1/changes";
|
|
1310
|
+
var CHANGES_PAGE_SIZE = 200;
|
|
1202
1311
|
var KNOWN_MODEL_VERSIONS = /* @__PURE__ */ new Set([1, 2]);
|
|
1203
1312
|
function understood(page, path) {
|
|
1204
1313
|
if (!KNOWN_MODEL_VERSIONS.has(page.model_version)) {
|
|
@@ -1249,6 +1358,13 @@ var LocalAuthorizer = class {
|
|
|
1249
1358
|
*/
|
|
1250
1359
|
pendingGrants = /* @__PURE__ */ new Map();
|
|
1251
1360
|
pendingTree;
|
|
1361
|
+
/**
|
|
1362
|
+
* Bumped by every full invalidation. A hierarchy read that began before
|
|
1363
|
+
* the bump answers the caller that was waiting on it but is not kept, so
|
|
1364
|
+
* a read already in flight when a change lands cannot cache what the
|
|
1365
|
+
* change replaced. Grants use the pending map for the same purpose.
|
|
1366
|
+
*/
|
|
1367
|
+
treeGeneration = 0;
|
|
1252
1368
|
stats = {
|
|
1253
1369
|
grantFetches: 0,
|
|
1254
1370
|
grantPages: 0,
|
|
@@ -1669,13 +1785,99 @@ var LocalAuthorizer = class {
|
|
|
1669
1785
|
*/
|
|
1670
1786
|
invalidate(identityId, subject = {}) {
|
|
1671
1787
|
if (identityId !== void 0) {
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
);
|
|
1788
|
+
const key = subjectKey(subject.principalType ?? "identity", identityId);
|
|
1789
|
+
this.grants.delete(key);
|
|
1790
|
+
this.pendingGrants.delete(key);
|
|
1675
1791
|
return;
|
|
1676
1792
|
}
|
|
1677
1793
|
this.grants.clear();
|
|
1794
|
+
this.pendingGrants.clear();
|
|
1678
1795
|
this.tree = void 0;
|
|
1796
|
+
this.pendingTree = void 0;
|
|
1797
|
+
this.treeGeneration += 1;
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* Drop what one change makes stale, from a webhook delivery or a
|
|
1801
|
+
* change-feed entry (the two share one shape). Answers what it dropped.
|
|
1802
|
+
*
|
|
1803
|
+
* A change naming whose access moved (an assignment, an Environment or
|
|
1804
|
+
* organization membership, an identity's status, a deny on one person,
|
|
1805
|
+
* a machine client disabled or deleted) drops those subjects' grants and
|
|
1806
|
+
* keeps everyone else's and the hierarchy warm. A change whose reach it
|
|
1807
|
+
* cannot name (a role, a permission, a node, the hierarchy schema, an
|
|
1808
|
+
* organization created or deleted, a deny on a role or a machine client,
|
|
1809
|
+
* a directory group mapped) drops everything. A change that moves no
|
|
1810
|
+
* one's access (a profile edit, a sign-in policy, a session revoked)
|
|
1811
|
+
* drops nothing. An event this SDK does not know drops everything.
|
|
1812
|
+
*
|
|
1813
|
+
* Check the delivery's signature first (`verifyWebhookSignature`): this
|
|
1814
|
+
* trusts what it is given. Like {@link invalidate}, it reaches this
|
|
1815
|
+
* process only; see there for what that means across several instances.
|
|
1816
|
+
*/
|
|
1817
|
+
applyEvent(change) {
|
|
1818
|
+
const reach = invalidationFor(change);
|
|
1819
|
+
if (reach === "none") {
|
|
1820
|
+
return { event: change.event, invalidated: "none" };
|
|
1821
|
+
}
|
|
1822
|
+
if (reach === "all") {
|
|
1823
|
+
this.invalidate();
|
|
1824
|
+
return { event: change.event, invalidated: "all" };
|
|
1825
|
+
}
|
|
1826
|
+
for (const subject of reach) {
|
|
1827
|
+
this.invalidate(subject.id, { principalType: subject.principalType });
|
|
1828
|
+
}
|
|
1829
|
+
return { event: change.event, invalidated: "subjects", subjects: reach };
|
|
1830
|
+
}
|
|
1831
|
+
/**
|
|
1832
|
+
* Read `GET /api/v1/changes` from `cursor` to the newest change and apply
|
|
1833
|
+
* each one, as {@link applyEvent} would a webhook. Answers the cursor to
|
|
1834
|
+
* resume from next time.
|
|
1835
|
+
*
|
|
1836
|
+
* Two uses. After a restart, to catch up on deliveries a process missed
|
|
1837
|
+
* while it was down, before it serves from a cache warmed elsewhere. And
|
|
1838
|
+
* in place of webhooks: every instance polling on an interval shorter
|
|
1839
|
+
* than `ttlMs` sees every change, which a webhook (delivered to one
|
|
1840
|
+
* instance) does not give a fleet.
|
|
1841
|
+
*
|
|
1842
|
+
* The feed holds what the audit log retains, so a cursor older than that
|
|
1843
|
+
* resumes at the oldest change still held. Each page is one request; a
|
|
1844
|
+
* replay with no cursor walks the whole retained window once.
|
|
1845
|
+
*/
|
|
1846
|
+
async replayChanges(options = {}) {
|
|
1847
|
+
let cursor = options.cursor ?? null;
|
|
1848
|
+
let changes = 0;
|
|
1849
|
+
let invalidatedAll = false;
|
|
1850
|
+
let subjectsInvalidated = 0;
|
|
1851
|
+
for (let page = 0; page < MAX_PAGES; page++) {
|
|
1852
|
+
const read = await this.client.request(
|
|
1853
|
+
"GET",
|
|
1854
|
+
CHANGES_PATH,
|
|
1855
|
+
{
|
|
1856
|
+
...this.readOptions,
|
|
1857
|
+
query: {
|
|
1858
|
+
limit: CHANGES_PAGE_SIZE,
|
|
1859
|
+
...cursor === null ? {} : { cursor }
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
);
|
|
1863
|
+
for (const change of read.items) {
|
|
1864
|
+
const applied = this.applyEvent(change);
|
|
1865
|
+
changes += 1;
|
|
1866
|
+
if (applied.invalidated === "all") {
|
|
1867
|
+
invalidatedAll = true;
|
|
1868
|
+
}
|
|
1869
|
+
if (applied.invalidated === "subjects") {
|
|
1870
|
+
subjectsInvalidated += applied.subjects.length;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
const next = read.pagination.next_cursor;
|
|
1874
|
+
const caughtUp = read.items.length < CHANGES_PAGE_SIZE || next === null || next === cursor;
|
|
1875
|
+
cursor = next ?? cursor;
|
|
1876
|
+
if (caughtUp) {
|
|
1877
|
+
break;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
return { cursor, changes, invalidatedAll, subjectsInvalidated };
|
|
1679
1881
|
}
|
|
1680
1882
|
/** Fire the decision hooks, after the verdict and without waiting on them. */
|
|
1681
1883
|
announce(event) {
|
|
@@ -1724,8 +1926,14 @@ var LocalAuthorizer = class {
|
|
|
1724
1926
|
if (pending) {
|
|
1725
1927
|
return pending;
|
|
1726
1928
|
}
|
|
1727
|
-
const read = this.fetchGrantRoots(
|
|
1728
|
-
|
|
1929
|
+
const read = this.fetchGrantRoots(
|
|
1930
|
+
identityId,
|
|
1931
|
+
type,
|
|
1932
|
+
() => this.pendingGrants.get(key) === read
|
|
1933
|
+
).finally(() => {
|
|
1934
|
+
if (this.pendingGrants.get(key) === read) {
|
|
1935
|
+
this.pendingGrants.delete(key);
|
|
1936
|
+
}
|
|
1729
1937
|
});
|
|
1730
1938
|
this.pendingGrants.set(key, read);
|
|
1731
1939
|
return read;
|
|
@@ -1748,7 +1956,7 @@ var LocalAuthorizer = class {
|
|
|
1748
1956
|
* Every page carries the subject's deny roots whole, beside its rows; they
|
|
1749
1957
|
* are gathered from each page, so a read is one snapshot of both.
|
|
1750
1958
|
*/
|
|
1751
|
-
async fetchGrantRoots(identityId, type) {
|
|
1959
|
+
async fetchGrantRoots(identityId, type, stillCurrent = () => true) {
|
|
1752
1960
|
this.stats.grantFetches += 1;
|
|
1753
1961
|
const path = GRANTS_PATH(type, identityId);
|
|
1754
1962
|
const denies = /* @__PURE__ */ new Map();
|
|
@@ -1799,10 +2007,12 @@ var LocalAuthorizer = class {
|
|
|
1799
2007
|
}
|
|
1800
2008
|
}
|
|
1801
2009
|
const holdings = { roots, denies };
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
2010
|
+
if (stillCurrent()) {
|
|
2011
|
+
this.grants.set(subjectKey(type, identityId), {
|
|
2012
|
+
holdings,
|
|
2013
|
+
expiresAt: Math.min(now + this.ttlMs, boundary)
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
1806
2016
|
return holdings;
|
|
1807
2017
|
}
|
|
1808
2018
|
async treeEntry() {
|
|
@@ -1813,7 +2023,9 @@ var LocalAuthorizer = class {
|
|
|
1813
2023
|
return this.pendingTree;
|
|
1814
2024
|
}
|
|
1815
2025
|
const read = this.fetchTree().finally(() => {
|
|
1816
|
-
this.pendingTree
|
|
2026
|
+
if (this.pendingTree === read) {
|
|
2027
|
+
this.pendingTree = void 0;
|
|
2028
|
+
}
|
|
1817
2029
|
});
|
|
1818
2030
|
this.pendingTree = read;
|
|
1819
2031
|
return read;
|
|
@@ -1839,26 +2051,30 @@ var LocalAuthorizer = class {
|
|
|
1839
2051
|
async fetchTree() {
|
|
1840
2052
|
this.stats.treeRequests += 1;
|
|
1841
2053
|
const held = this.tree;
|
|
2054
|
+
const generation = this.treeGeneration;
|
|
1842
2055
|
let first = await this.parentsFirstPage(held?.etag ?? void 0);
|
|
1843
2056
|
if (!first.modified) {
|
|
1844
2057
|
if (held) {
|
|
1845
2058
|
this.stats.treeNotModified += 1;
|
|
1846
|
-
|
|
1847
|
-
|
|
2059
|
+
const renewed = { ...held, expiresAt: this.now() + this.ttlMs };
|
|
2060
|
+
if (generation === this.treeGeneration) {
|
|
2061
|
+
this.tree = renewed;
|
|
2062
|
+
}
|
|
2063
|
+
return renewed;
|
|
1848
2064
|
}
|
|
1849
|
-
return this.keepTree([], null);
|
|
2065
|
+
return this.keepTree([], null, generation);
|
|
1850
2066
|
}
|
|
1851
2067
|
for (let walk = 1; ; walk++) {
|
|
1852
2068
|
const edges = await this.parentsRemainingPages(first.data);
|
|
1853
2069
|
if (!spansPages(first.data) || first.etag === null) {
|
|
1854
|
-
return this.keepTree(edges, first.etag);
|
|
2070
|
+
return this.keepTree(edges, first.etag, generation);
|
|
1855
2071
|
}
|
|
1856
2072
|
const confirm = await this.parentsFirstPage(first.etag);
|
|
1857
2073
|
if (!confirm.modified) {
|
|
1858
|
-
return this.keepTree(edges, first.etag);
|
|
2074
|
+
return this.keepTree(edges, first.etag, generation);
|
|
1859
2075
|
}
|
|
1860
2076
|
if (walk >= MAX_TREE_WALKS) {
|
|
1861
|
-
return this.keepTree(edges, null);
|
|
2077
|
+
return this.keepTree(edges, null, generation);
|
|
1862
2078
|
}
|
|
1863
2079
|
first = confirm;
|
|
1864
2080
|
}
|
|
@@ -1899,7 +2115,7 @@ var LocalAuthorizer = class {
|
|
|
1899
2115
|
}
|
|
1900
2116
|
return edges;
|
|
1901
2117
|
}
|
|
1902
|
-
keepTree(edges, etag) {
|
|
2118
|
+
keepTree(edges, etag, generation) {
|
|
1903
2119
|
const ids = /* @__PURE__ */ new Map();
|
|
1904
2120
|
for (const edge of edges) {
|
|
1905
2121
|
ids.set(edge.id, edge.id);
|
|
@@ -1918,13 +2134,16 @@ var LocalAuthorizer = class {
|
|
|
1918
2134
|
}
|
|
1919
2135
|
}
|
|
1920
2136
|
}
|
|
1921
|
-
|
|
2137
|
+
const entry = {
|
|
1922
2138
|
parents,
|
|
1923
2139
|
children,
|
|
1924
2140
|
etag,
|
|
1925
2141
|
expiresAt: this.now() + this.ttlMs
|
|
1926
2142
|
};
|
|
1927
|
-
|
|
2143
|
+
if (generation === this.treeGeneration) {
|
|
2144
|
+
this.tree = entry;
|
|
2145
|
+
}
|
|
2146
|
+
return entry;
|
|
1928
2147
|
}
|
|
1929
2148
|
};
|
|
1930
2149
|
function spansPages(first) {
|
|
@@ -2099,14 +2318,14 @@ var TokenVerifier = class {
|
|
|
2099
2318
|
);
|
|
2100
2319
|
}
|
|
2101
2320
|
const [encodedHeader, encodedPayload, encodedSignature] = parts;
|
|
2102
|
-
const
|
|
2103
|
-
if (
|
|
2321
|
+
const header2 = decodeJsonSegment(encodedHeader, "header");
|
|
2322
|
+
if (header2.alg !== "RS256") {
|
|
2104
2323
|
throw new CanopyTokenError(
|
|
2105
2324
|
"token.unsupported_algorithm",
|
|
2106
|
-
`Token is signed with "${
|
|
2325
|
+
`Token is signed with "${header2.alg ?? "none"}"; Canopy issues RS256.`
|
|
2107
2326
|
);
|
|
2108
2327
|
}
|
|
2109
|
-
const key = await this.resolveKey(
|
|
2328
|
+
const key = await this.resolveKey(header2.kid);
|
|
2110
2329
|
const signed = new TextEncoder().encode(
|
|
2111
2330
|
`${encodedHeader}.${encodedPayload}`
|
|
2112
2331
|
);
|
|
@@ -2395,7 +2614,65 @@ var TokenVerifier = class {
|
|
|
2395
2614
|
function isWebhookEvent(delivery, name) {
|
|
2396
2615
|
return delivery.event === name;
|
|
2397
2616
|
}
|
|
2617
|
+
var WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 300;
|
|
2618
|
+
var SIGNATURE_PREFIX = "v1,";
|
|
2619
|
+
var SIGNATURE_HEX = /^[0-9a-f]{64}$/i;
|
|
2620
|
+
async function verifyWebhookSignature(input) {
|
|
2621
|
+
const id = header(input.headers, "x-canopy-webhook-id");
|
|
2622
|
+
const timestamp = header(input.headers, "x-canopy-timestamp");
|
|
2623
|
+
const signature = header(input.headers, "x-canopy-signature");
|
|
2624
|
+
if (!id || !timestamp || !signature || !/^\d+$/.test(timestamp)) {
|
|
2625
|
+
return false;
|
|
2626
|
+
}
|
|
2627
|
+
const tolerance = input.toleranceSeconds ?? WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS;
|
|
2628
|
+
const nowSeconds = Math.floor((input.now ?? Date.now)() / 1e3);
|
|
2629
|
+
if (Math.abs(nowSeconds - Number(timestamp)) > tolerance) {
|
|
2630
|
+
return false;
|
|
2631
|
+
}
|
|
2632
|
+
const secrets = (typeof input.secret === "string" ? [input.secret] : input.secret).filter((secret) => typeof secret === "string" && secret.length > 0);
|
|
2633
|
+
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);
|
|
2634
|
+
if (secrets.length === 0 || received.length === 0) {
|
|
2635
|
+
return false;
|
|
2636
|
+
}
|
|
2637
|
+
const encoder = new TextEncoder();
|
|
2638
|
+
const body = typeof input.rawBody === "string" ? encoder.encode(input.rawBody) : input.rawBody;
|
|
2639
|
+
const prefix = encoder.encode(`${id}.${timestamp}.`);
|
|
2640
|
+
const signed = new Uint8Array(new ArrayBuffer(prefix.length + body.length));
|
|
2641
|
+
signed.set(prefix, 0);
|
|
2642
|
+
signed.set(body, prefix.length);
|
|
2643
|
+
const checks = await Promise.all(
|
|
2644
|
+
secrets.flatMap((secret) => {
|
|
2645
|
+
const key = crypto.subtle.importKey(
|
|
2646
|
+
"raw",
|
|
2647
|
+
encoder.encode(secret),
|
|
2648
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
2649
|
+
false,
|
|
2650
|
+
["verify"]
|
|
2651
|
+
);
|
|
2652
|
+
return received.map(
|
|
2653
|
+
async (value) => crypto.subtle.verify("HMAC", await key, value, signed)
|
|
2654
|
+
);
|
|
2655
|
+
})
|
|
2656
|
+
);
|
|
2657
|
+
return checks.some(Boolean);
|
|
2658
|
+
}
|
|
2659
|
+
function header(headers, name) {
|
|
2660
|
+
if (typeof headers.get === "function") {
|
|
2661
|
+
return headers.get(name) ?? void 0;
|
|
2662
|
+
}
|
|
2663
|
+
const record = headers;
|
|
2664
|
+
const key = Object.keys(record).find((k) => k.toLowerCase() === name);
|
|
2665
|
+
const value = key === void 0 ? void 0 : record[key];
|
|
2666
|
+
return Array.isArray(value) ? value.join(" ") : value;
|
|
2667
|
+
}
|
|
2668
|
+
function hexToBytes(hex) {
|
|
2669
|
+
const bytes = new Uint8Array(new ArrayBuffer(hex.length / 2));
|
|
2670
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
2671
|
+
bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
2672
|
+
}
|
|
2673
|
+
return bytes;
|
|
2674
|
+
}
|
|
2398
2675
|
|
|
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 };
|
|
2676
|
+
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
2677
|
//# sourceMappingURL=index.js.map
|
|
2401
2678
|
//# sourceMappingURL=index.js.map
|