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