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