@thirdweb-dev/service-utils 0.4.38 → 0.4.39
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/dist/cjs/cf-worker/index.js +155 -0
- package/dist/cjs/cf-worker/index.js.map +1 -0
- package/dist/cjs/cf-worker/usage.js +55 -0
- package/dist/cjs/cf-worker/usage.js.map +1 -0
- package/dist/cjs/core/api.js +65 -0
- package/dist/cjs/core/api.js.map +1 -0
- package/dist/cjs/core/authorize/client.js +115 -0
- package/dist/cjs/core/authorize/client.js.map +1 -0
- package/dist/cjs/core/authorize/index.js +215 -0
- package/dist/cjs/core/authorize/index.js.map +1 -0
- package/dist/cjs/core/authorize/service.js +58 -0
- package/dist/cjs/core/authorize/service.js.map +1 -0
- package/dist/cjs/core/authorize/types.js +3 -0
- package/dist/cjs/core/authorize/types.js.map +1 -0
- package/dist/cjs/core/rateLimit/index.js +64 -0
- package/dist/cjs/core/rateLimit/index.js.map +1 -0
- package/dist/cjs/core/rateLimit/types.js +3 -0
- package/dist/cjs/core/rateLimit/types.js.map +1 -0
- package/dist/cjs/core/services.js +71 -0
- package/dist/cjs/core/services.js.map +1 -0
- package/dist/cjs/core/types.js +3 -0
- package/dist/cjs/core/types.js.map +1 -0
- package/dist/cjs/core/usage.js +93 -0
- package/dist/cjs/core/usage.js.map +1 -0
- package/dist/cjs/core/usageLimit/index.js +45 -0
- package/dist/cjs/core/usageLimit/index.js.map +1 -0
- package/dist/cjs/core/usageLimit/types.js +3 -0
- package/dist/cjs/core/usageLimit/types.js.map +1 -0
- package/dist/cjs/index.js +10 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mocks.js +53 -0
- package/dist/cjs/mocks.js.map +1 -0
- package/dist/cjs/node/index.js +173 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/cf-worker/index.js +147 -0
- package/dist/esm/cf-worker/index.js.map +1 -0
- package/dist/esm/cf-worker/usage.js +54 -0
- package/dist/esm/cf-worker/usage.js.map +1 -0
- package/dist/esm/core/api.js +60 -0
- package/dist/esm/core/api.js.map +1 -0
- package/dist/esm/core/authorize/client.js +110 -0
- package/dist/esm/core/authorize/client.js.map +1 -0
- package/dist/esm/core/authorize/index.js +212 -0
- package/dist/esm/core/authorize/index.js.map +1 -0
- package/dist/esm/core/authorize/service.js +55 -0
- package/dist/esm/core/authorize/service.js.map +1 -0
- package/dist/esm/core/authorize/types.js +2 -0
- package/dist/esm/core/authorize/types.js.map +1 -0
- package/dist/esm/core/rateLimit/index.js +61 -0
- package/dist/esm/core/rateLimit/index.js.map +1 -0
- package/dist/esm/core/rateLimit/types.js +2 -0
- package/dist/esm/core/rateLimit/types.js.map +1 -0
- package/dist/esm/core/services.js +67 -0
- package/dist/esm/core/services.js.map +1 -0
- package/dist/esm/core/types.js +2 -0
- package/dist/esm/core/types.js.map +1 -0
- package/dist/esm/core/usage.js +90 -0
- package/dist/esm/core/usage.js.map +1 -0
- package/dist/esm/core/usageLimit/index.js +42 -0
- package/dist/esm/core/usageLimit/index.js.map +1 -0
- package/dist/esm/core/usageLimit/types.js +2 -0
- package/dist/esm/core/usageLimit/types.js.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mocks.js +50 -0
- package/dist/esm/mocks.js.map +1 -0
- package/dist/esm/node/index.js +165 -0
- package/dist/esm/node/index.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/{declarations/src → types}/cf-worker/index.d.ts +8 -8
- package/dist/types/cf-worker/index.d.ts.map +1 -0
- package/dist/{declarations/src → types}/cf-worker/usage.d.ts +3 -2
- package/dist/types/cf-worker/usage.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/api.d.ts +1 -1
- package/dist/types/core/api.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/client.d.ts +2 -2
- package/dist/types/core/authorize/client.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/index.d.ts +2 -2
- package/dist/types/core/authorize/index.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/service.d.ts +2 -2
- package/dist/types/core/authorize/service.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/types.d.ts +1 -1
- package/dist/types/core/authorize/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/rateLimit/index.d.ts +3 -3
- package/dist/types/core/rateLimit/index.d.ts.map +1 -0
- package/dist/types/core/rateLimit/types.d.ts.map +1 -0
- package/dist/types/core/services.d.ts.map +1 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/usage.d.ts +2 -2
- package/dist/types/core/usage.d.ts.map +1 -0
- package/dist/types/core/usageLimit/index.d.ts +5 -0
- package/dist/types/core/usageLimit/index.d.ts.map +1 -0
- package/dist/types/core/usageLimit/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/mocks.d.ts +5 -0
- package/dist/types/mocks.d.ts.map +1 -0
- package/dist/{declarations/src → types}/node/index.d.ts +8 -8
- package/dist/types/node/index.d.ts.map +1 -0
- package/package.json +36 -29
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts +0 -2
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts.map +0 -1
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +0 -226
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.js +0 -7
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +0 -226
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +0 -212
- package/cf-worker/package.json +0 -4
- package/dist/client-8440b8fb.esm.js +0 -195
- package/dist/client-84e46164.cjs.prod.js +0 -203
- package/dist/client-a5cc5822.cjs.dev.js +0 -203
- package/dist/declarations/src/cf-worker/index.d.ts.map +0 -1
- package/dist/declarations/src/cf-worker/usage.d.ts.map +0 -1
- package/dist/declarations/src/core/api.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/client.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/index.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/service.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/types.d.ts.map +0 -1
- package/dist/declarations/src/core/rateLimit/index.d.ts.map +0 -1
- package/dist/declarations/src/core/rateLimit/types.d.ts.map +0 -1
- package/dist/declarations/src/core/services.d.ts.map +0 -1
- package/dist/declarations/src/core/types.d.ts.map +0 -1
- package/dist/declarations/src/core/usage.d.ts.map +0 -1
- package/dist/declarations/src/core/usageLimit/index.d.ts +0 -5
- package/dist/declarations/src/core/usageLimit/index.d.ts.map +0 -1
- package/dist/declarations/src/core/usageLimit/types.d.ts.map +0 -1
- package/dist/declarations/src/index.d.ts +0 -3
- package/dist/declarations/src/index.d.ts.map +0 -1
- package/dist/declarations/src/node/index.d.ts.map +0 -1
- package/dist/index-23f268d8.cjs.prod.js +0 -540
- package/dist/index-5dc16842.esm.js +0 -535
- package/dist/index-88f1ffb6.cjs.dev.js +0 -540
- package/dist/thirdweb-dev-service-utils.cjs.d.ts +0 -2
- package/dist/thirdweb-dev-service-utils.cjs.d.ts.map +0 -1
- package/dist/thirdweb-dev-service-utils.cjs.dev.js +0 -14
- package/dist/thirdweb-dev-service-utils.cjs.js +0 -7
- package/dist/thirdweb-dev-service-utils.cjs.prod.js +0 -14
- package/dist/thirdweb-dev-service-utils.esm.js +0 -1
- package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts +0 -2
- package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts.map +0 -1
- package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +0 -190
- package/node/dist/thirdweb-dev-service-utils-node.cjs.js +0 -7
- package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +0 -190
- package/node/dist/thirdweb-dev-service-utils-node.esm.js +0 -176
- package/node/package.json +0 -4
- /package/dist/{declarations/src → types}/core/rateLimit/types.d.ts +0 -0
- /package/dist/{declarations/src → types}/core/services.d.ts +0 -0
- /package/dist/{declarations/src → types}/core/types.d.ts +0 -0
- /package/dist/{declarations/src → types}/core/usageLimit/types.d.ts +0 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.authorizeService = authorizeService;
|
4
|
+
function authorizeService(apiKeyMetadata, serviceConfig, authorizationPayload) {
|
5
|
+
const { services } = apiKeyMetadata;
|
6
|
+
// validate services
|
7
|
+
const service = services.find((srv) => srv.name === serviceConfig.serviceScope);
|
8
|
+
if (!service) {
|
9
|
+
return {
|
10
|
+
authorized: false,
|
11
|
+
errorMessage: `Invalid request: Unauthorized service: ${serviceConfig.serviceScope}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
12
|
+
errorCode: "SERVICE_UNAUTHORIZED",
|
13
|
+
status: 403,
|
14
|
+
};
|
15
|
+
}
|
16
|
+
// validate service actions
|
17
|
+
if (serviceConfig.serviceAction) {
|
18
|
+
const isActionAllowed = service.actions.includes(serviceConfig.serviceAction);
|
19
|
+
if (!isActionAllowed) {
|
20
|
+
return {
|
21
|
+
authorized: false,
|
22
|
+
errorMessage: `Invalid request: Unauthorized action: ${serviceConfig.serviceScope} ${serviceConfig.serviceAction}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
23
|
+
errorCode: "SERVICE_ACTION_UNAUTHORIZED",
|
24
|
+
status: 403,
|
25
|
+
};
|
26
|
+
}
|
27
|
+
}
|
28
|
+
// validate service target addresses
|
29
|
+
// the service has to pass in the target address for this to be validated
|
30
|
+
if (authorizationPayload?.targetAddress) {
|
31
|
+
const checkedAddresses = Array.isArray(authorizationPayload.targetAddress)
|
32
|
+
? authorizationPayload.targetAddress
|
33
|
+
: [authorizationPayload.targetAddress];
|
34
|
+
const allAllowed = service.targetAddresses.includes("*");
|
35
|
+
if (!allAllowed &&
|
36
|
+
checkedAddresses.some((ta) => !service.targetAddresses.includes(ta))) {
|
37
|
+
return {
|
38
|
+
authorized: false,
|
39
|
+
errorMessage: `Invalid request: Unauthorized address: ${serviceConfig.serviceScope} ${checkedAddresses}. You can view the restrictions on this API key in your dashboard: https://thirdweb.com/create-api-key`,
|
40
|
+
errorCode: "SERVICE_TARGET_ADDRESS_UNAUTHORIZED",
|
41
|
+
status: 403,
|
42
|
+
};
|
43
|
+
}
|
44
|
+
}
|
45
|
+
return {
|
46
|
+
authorized: true,
|
47
|
+
apiKeyMeta: apiKeyMetadata,
|
48
|
+
accountMeta: {
|
49
|
+
id: apiKeyMetadata.accountId,
|
50
|
+
name: "",
|
51
|
+
creatorWalletAddress: apiKeyMetadata.creatorWalletAddress,
|
52
|
+
limits: apiKeyMetadata.limits,
|
53
|
+
rateLimits: apiKeyMetadata.rateLimits,
|
54
|
+
usage: apiKeyMetadata.usage,
|
55
|
+
},
|
56
|
+
};
|
57
|
+
}
|
58
|
+
//# sourceMappingURL=service.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"service.js","sourceRoot":"","sources":["../../../../src/core/authorize/service.ts"],"names":[],"mappings":";;AAKA,4CAoEC;AApED,SAAgB,gBAAgB,CAC9B,cAA8B,EAC9B,aAAgC,EAChC,oBAAkD;IAElD,MAAM,EAAE,QAAQ,EAAE,GAAG,cAAc,CAAC;IACpC,oBAAoB;IACpB,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAC3B,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,aAAa,CAAC,YAAY,CACjD,CAAC;IACF,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,UAAU,EAAE,KAAK;YACjB,YAAY,EAAE,0CAA0C,aAAa,CAAC,YAAY,yGAAyG;YAC3L,SAAS,EAAE,sBAAsB;YACjC,MAAM,EAAE,GAAG;SACZ,CAAC;IACJ,CAA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C,oBAAoB;YACzD,MAAM,EAAE,cAAc,CAAC,MAAM;YAC7B,UAAU,EAAE,cAAc,CAAC,UAAU;YACrC,KAAK,EAAE,cAAc,CAAC,KAAK;SAC5B;KACF,CAAC;AACJ,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/core/authorize/types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.rateLimit = rateLimit;
|
4
|
+
const api_js_1 = require("../api.js");
|
5
|
+
const RATE_LIMIT_WINDOW_SECONDS = 10;
|
6
|
+
async function rateLimit(args) {
|
7
|
+
const { authzResult, serviceConfig, redis, sampleRate = 1.0 } = args;
|
8
|
+
const shouldSampleRequest = Math.random() < sampleRate;
|
9
|
+
if (!shouldSampleRequest || !authzResult.authorized) {
|
10
|
+
return {
|
11
|
+
rateLimited: false,
|
12
|
+
requestCount: 0,
|
13
|
+
rateLimit: 0,
|
14
|
+
};
|
15
|
+
}
|
16
|
+
const { apiKeyMeta, accountMeta } = authzResult;
|
17
|
+
const accountId = apiKeyMeta?.accountId || accountMeta?.id;
|
18
|
+
const { serviceScope } = serviceConfig;
|
19
|
+
const limitPerSecond = apiKeyMeta?.rateLimits?.[serviceScope] ??
|
20
|
+
accountMeta?.rateLimits?.[serviceScope];
|
21
|
+
if (!limitPerSecond) {
|
22
|
+
// No rate limit is provided. Assume the request is not rate limited.
|
23
|
+
return {
|
24
|
+
rateLimited: false,
|
25
|
+
requestCount: 0,
|
26
|
+
rateLimit: 0,
|
27
|
+
};
|
28
|
+
}
|
29
|
+
// Gets the 10-second window for the current timestamp.
|
30
|
+
const timestampWindow = Math.floor(Date.now() / (1000 * RATE_LIMIT_WINDOW_SECONDS)) *
|
31
|
+
RATE_LIMIT_WINDOW_SECONDS;
|
32
|
+
const key = `rate-limit:${serviceScope}:${accountId}:${timestampWindow}`;
|
33
|
+
// Increment and get the current request count in this window.
|
34
|
+
const requestCount = await redis.incr(key);
|
35
|
+
if (requestCount === 1) {
|
36
|
+
// For the first increment, set an expiration to clean up this key.
|
37
|
+
await redis.expire(key, RATE_LIMIT_WINDOW_SECONDS);
|
38
|
+
}
|
39
|
+
// Get the limit for this window accounting for the sample rate.
|
40
|
+
const limitPerWindow = limitPerSecond * sampleRate * RATE_LIMIT_WINDOW_SECONDS;
|
41
|
+
if (requestCount > limitPerWindow) {
|
42
|
+
// Report rate limit hits.
|
43
|
+
if (apiKeyMeta?.id) {
|
44
|
+
await (0, api_js_1.updateRateLimitedAt)(apiKeyMeta.id, serviceConfig);
|
45
|
+
}
|
46
|
+
// Reject requests when they've exceeded 2x the rate limit.
|
47
|
+
if (requestCount > 2 * limitPerWindow) {
|
48
|
+
return {
|
49
|
+
rateLimited: true,
|
50
|
+
requestCount,
|
51
|
+
rateLimit: limitPerWindow,
|
52
|
+
status: 429,
|
53
|
+
errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
|
54
|
+
errorCode: "RATE_LIMIT_EXCEEDED",
|
55
|
+
};
|
56
|
+
}
|
57
|
+
}
|
58
|
+
return {
|
59
|
+
rateLimited: false,
|
60
|
+
requestCount,
|
61
|
+
rateLimit: limitPerWindow,
|
62
|
+
};
|
63
|
+
}
|
64
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/core/rateLimit/index.ts"],"names":[],"mappings":";;AAYA,8BAgFC;AA5FD,sCAAwE;AAIxE,MAAM,yBAAyB,GAAG,EAAE,CAAC;AAQ9B,KAAK,UAAU,SAAS,CAAC,IAU/B;IACC,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,EAAE,UAAU,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC;IAErE,MAAM,mBA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AAG,cAAc,EAAE,CAAC;QAClC,0BAA0B;QAC1B,IAAI,UAAU,EAAE,EAAE,EAAE,CAAC;YACnB,MAAM,IAAA,4BAAmB,EAAC,UAAU,CAAC,EAAE,EAAE,aAAa,CAAC,CAAC;QAC1D,CAAC;QAED,2DAA2D;QAC3D,IAAI,YAAY,GAAG,CAAC,GAAG,cAAc,EAAE,CAAC;YACtC,OAAO;gBACL,WAAW,EAAE,IAAI;gBACjB,YAAY;gBACZ,SAAS,EAAE,cAAc;gBACzB,MAAM,EAAE,GAAG;gBACX,YAAY,EAAE,wBAAwB,YAAY,kBAAkB,cAAc,sFAAsF;gBACxK,SAAS,EAAE,qBAAqB;aACjC,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO;QACL,WAAW,EAAE,KAAK;QAClB,YAAY;QACZ,SAAS,EAAE,cAAc;KAC1B,CAAC;AACJ,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/core/rateLimit/types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,71 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.SERVICES = exports.SERVICE_NAMES = exports.SERVICE_DEFINITIONS = void 0;
|
4
|
+
exports.getServiceByName = getServiceByName;
|
5
|
+
exports.SERVICE_DEFINITIONS = {
|
6
|
+
storage: {
|
7
|
+
name: "storage",
|
8
|
+
title: "Storage",
|
9
|
+
description: "IPFS Upload and Download",
|
10
|
+
actions: [
|
11
|
+
{
|
12
|
+
name: "read",
|
13
|
+
title: "Download",
|
14
|
+
description: "Download a file from Storage",
|
15
|
+
},
|
16
|
+
{
|
17
|
+
name: "write",
|
18
|
+
title: "Upload",
|
19
|
+
description: "Upload a file to Storage",
|
20
|
+
},
|
21
|
+
],
|
22
|
+
},
|
23
|
+
rpc: {
|
24
|
+
name: "rpc",
|
25
|
+
title: "RPC",
|
26
|
+
description: "Accelerated RPC Edge",
|
27
|
+
// all actions allowed
|
28
|
+
actions: [],
|
29
|
+
},
|
30
|
+
bundler: {
|
31
|
+
name: "bundler",
|
32
|
+
title: "Account Abstraction",
|
33
|
+
description: "Bundler & Paymaster services",
|
34
|
+
// all actions allowed
|
35
|
+
actions: [],
|
36
|
+
},
|
37
|
+
relayer: {
|
38
|
+
name: "relayer",
|
39
|
+
title: "Gasless Relayer",
|
40
|
+
description: "Enable gasless transactions",
|
41
|
+
// all actions allowed
|
42
|
+
actions: [],
|
43
|
+
},
|
44
|
+
embeddedWallets: {
|
45
|
+
name: "embeddedWallets",
|
46
|
+
title: "In-App Wallets",
|
47
|
+
description: "E-mail and social login wallets for easy web3 onboarding",
|
48
|
+
// all actions allowed
|
49
|
+
actions: [],
|
50
|
+
},
|
51
|
+
pay: {
|
52
|
+
name: "pay",
|
53
|
+
title: "Pay",
|
54
|
+
description: "Pay for a blockchain transaction with any currency",
|
55
|
+
// all actions allowed
|
56
|
+
actions: [],
|
57
|
+
},
|
58
|
+
chainsaw: {
|
59
|
+
name: "chainsaw",
|
60
|
+
title: "Chainsaw",
|
61
|
+
description: "Indexed data for any EVM chain",
|
62
|
+
// all actions allowed
|
63
|
+
actions: [],
|
64
|
+
},
|
65
|
+
};
|
66
|
+
exports.SERVICE_NAMES = Object.keys(exports.SERVICE_DEFINITIONS);
|
67
|
+
exports.SERVICES = Object.values(exports.SERVICE_DEFINITIONS);
|
68
|
+
function getServiceByName(name) {
|
69
|
+
return exports.SERVICE_DEFINITIONS[name];
|
70
|
+
}
|
71
|
+
//# sourceMappingURL=services.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"services.js","sourceRoot":"","sources":["../../../src/core/services.ts"],"names":[],"mappings":";;;AA+EA,4CAEC;AAjFY,QAAA,mBAAmB,GAAG;IACjC,OAAO,EAAE;QACP,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,SAAS;QAChB,WAAW,EAAE,0BAA0B;QACvC,OAAO,EAAE;Y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C;AACnC,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/core/types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.usageEventSchema = void 0;
|
4
|
+
const zod_1 = require("zod");
|
5
|
+
exports.usageEventSchema = zod_1.z.object({
|
6
|
+
source: zod_1.z.enum([
|
7
|
+
"ecosystemWallets",
|
8
|
+
"embeddedWallets",
|
9
|
+
"rpc",
|
10
|
+
"storage",
|
11
|
+
"bundler",
|
12
|
+
"paymaster",
|
13
|
+
"relayer",
|
14
|
+
"connectWallet",
|
15
|
+
"checkout",
|
16
|
+
"engine",
|
17
|
+
"pay",
|
18
|
+
"rpcV2",
|
19
|
+
]),
|
20
|
+
action: zod_1.z.string(),
|
21
|
+
/**
|
22
|
+
* The following fields are optional.
|
23
|
+
*/
|
24
|
+
accountId: zod_1.z.string().optional(),
|
25
|
+
isClientEvent: zod_1.z.boolean().optional(),
|
26
|
+
apiKeyId: zod_1.z.string().optional(),
|
27
|
+
creatorWalletAddress: zod_1.z.string().optional(),
|
28
|
+
clientId: zod_1.z.string().optional(),
|
29
|
+
walletAddress: zod_1.z.string().optional(),
|
30
|
+
walletType: zod_1.z.string().optional(),
|
31
|
+
chainId: zod_1.z.number().int().positive().optional(),
|
32
|
+
provider: zod_1.z.string().optional(),
|
33
|
+
mimeType: zod_1.z.string().optional(),
|
34
|
+
fileSize: zod_1.z.number().int().nonnegative().optional(),
|
35
|
+
fileCid: zod_1.z.string().optional(),
|
36
|
+
evmMethod: zod_1.z.string().optional(),
|
37
|
+
userOpHash: zod_1.z.string().optional(),
|
38
|
+
gasLimit: zod_1.z.number().nonnegative().optional(),
|
39
|
+
gasPricePerUnit: zod_1.z.string().optional(),
|
40
|
+
transactionFeeUsd: zod_1.z.number().optional(),
|
41
|
+
transactionHash: zod_1.z.string().optional(),
|
42
|
+
sdkName: zod_1.z.string().optional(),
|
43
|
+
sdkVersion: zod_1.z.string().optional(),
|
44
|
+
sdkPlatform: zod_1.z.string().optional(),
|
45
|
+
sdkOS: zod_1.z.string().optional(),
|
46
|
+
productName: zod_1.z.string().optional(),
|
47
|
+
transactionValue: zod_1.z.string().optional(),
|
48
|
+
pathname: zod_1.z.string().optional(),
|
49
|
+
contractAddress: zod_1.z.string().optional(),
|
50
|
+
errorCode: zod_1.z.string().optional(),
|
51
|
+
httpStatusCode: zod_1.z.number().int().nonnegative().optional(),
|
52
|
+
functionName: zod_1.z.string().optional(),
|
53
|
+
extension: zod_1.z.string().optional(),
|
54
|
+
retryCount: zod_1.z.number().int().nonnegative().optional(),
|
55
|
+
policyId: zod_1.z.string().optional(),
|
56
|
+
msSinceQueue: zod_1.z.number().nonnegative().optional(),
|
57
|
+
msSinceSend: zod_1.z.number().nonnegative().optional(),
|
58
|
+
msTotalDuration: zod_1.z.number().nonnegative().optional(),
|
59
|
+
swapId: zod_1.z.string().optional(),
|
60
|
+
tokenAddress: zod_1.z.string().optional(),
|
61
|
+
amountWei: zod_1.z.string().optional(),
|
62
|
+
amountUSDCents: zod_1.z.number().nonnegative().optional(),
|
63
|
+
httpMethod: zod_1.z
|
64
|
+
.enum([
|
65
|
+
"GET",
|
66
|
+
"POST",
|
67
|
+
"PUT",
|
68
|
+
"DELETE",
|
69
|
+
"PATCH",
|
70
|
+
"HEAD",
|
71
|
+
"CONNECT",
|
72
|
+
"OPTIONS",
|
73
|
+
"TRACE",
|
74
|
+
])
|
75
|
+
.optional(),
|
76
|
+
// Used to identify the ecosystem that the an ecosystem wallet belongs too
|
77
|
+
ecosystemId: zod_1.z.string().optional(),
|
78
|
+
ecosystemPartnerId: zod_1.z.string().optional(),
|
79
|
+
authenticationMethod: zod_1.z.string().optional(),
|
80
|
+
chainName: zod_1.z.string().optional(),
|
81
|
+
tokenSymbol: zod_1.z.string().optional(),
|
82
|
+
dstChainId: zod_1.z.number().optional(),
|
83
|
+
dstTokenAddress: zod_1.z.string().optional(),
|
84
|
+
dstChainName: zod_1.z.string().optional(),
|
85
|
+
dstTokenSymbol: zod_1.z.string().optional(),
|
86
|
+
msLatency: zod_1.z.number().optional(),
|
87
|
+
toAmountUSDCents: zod_1.z.number().optional(),
|
88
|
+
secondaryProvider: zod_1.z.string().optional(),
|
89
|
+
onRampId: zod_1.z.string().optional(),
|
90
|
+
evmRequestParams: zod_1.z.string().optional(),
|
91
|
+
providerIp: zod_1.z.string().optional(),
|
92
|
+
});
|
93
|
+
//# sourceMappingURL=usage.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"usage.js","sourceRoot":"","sources":["../../../src/core/usage.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAEX,QAAA,gBAAgB,GAAG,OAAC,CAAC,MAAM,CAAC;IACvC,MAAM,EAAE,OAAC,CAAC,IAAI,CAAC;QACb,kBAAkB;QAClB,iBAAiB;QACjB,KAAK;QACL,SAAS;QACT,SAAS;QACT,WAAW;QACX,SAAS;QACT,eAAe;QACf,UAAU;QACV,QAAQ;QACR,KAAK;QACL,OAAO;KACR,CAAC;IACF,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE;IAElB;;OAEG;IAEH,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAChC,aAAa,EAAE,OAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE;IACrC,QAAQ,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC/B,oBAAoB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC3C,QAAQ,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC/B,aAAa,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACpC,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACjC,OAAO,EAA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kB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACzC,oBAAoB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC3C,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAChC,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAClC,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACjC,eAAe,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACtC,YAAY,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACnC,cAAc,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACrC,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAChC,gBAAgB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACvC,iBAAiB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACxC,QAAQ,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC/B,gBAAgB,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACvC,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAClC,CAAC,CAAC"}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.usageLimit = usageLimit;
|
4
|
+
async function usageLimit(authzResult, serviceConfig) {
|
5
|
+
if (!authzResult.authorized) {
|
6
|
+
return {
|
7
|
+
usageLimited: false,
|
8
|
+
};
|
9
|
+
}
|
10
|
+
const { apiKeyMeta, accountMeta } = authzResult;
|
11
|
+
const { limits, usage } = apiKeyMeta || accountMeta || {};
|
12
|
+
const { serviceScope } = serviceConfig;
|
13
|
+
if (!usage ||
|
14
|
+
!(serviceScope in usage) ||
|
15
|
+
!limits ||
|
16
|
+
!(serviceScope in limits)) {
|
17
|
+
// No usage limit is provided. Assume the request is not limited.
|
18
|
+
return {
|
19
|
+
usageLimited: false,
|
20
|
+
};
|
21
|
+
}
|
22
|
+
if (serviceScope === "storage" &&
|
23
|
+
(usage.storage?.sumFileSizeBytes ?? 0) > (limits.storage ?? 0)) {
|
24
|
+
return {
|
25
|
+
usageLimited: true,
|
26
|
+
status: 402,
|
27
|
+
errorMessage: `You've used all of your total usage credits for Storage Pinning. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
|
28
|
+
errorCode: "PAYMENT_METHOD_REQUIRED",
|
29
|
+
};
|
30
|
+
}
|
31
|
+
if (serviceScope === "embeddedWallets" &&
|
32
|
+
(usage.embeddedWallets?.countWalletAddresses ?? 0) >
|
33
|
+
(limits.embeddedWallets ?? 0)) {
|
34
|
+
return {
|
35
|
+
usageLimited: true,
|
36
|
+
status: 402,
|
37
|
+
errorMessage: `You've used all of your total usage credits for Embedded Wallets. Please add your payment method at https://thirdweb.com/dashboard/settings/billing.`,
|
38
|
+
errorCode: "PAYMENT_METHOD_REQUIRED",
|
39
|
+
};
|
40
|
+
}
|
41
|
+
return {
|
42
|
+
usageLimited: false,
|
43
|
+
};
|
44
|
+
}
|
45
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/core/usageLimit/index.ts"],"names":[],"mappings":";;AAIA,gCAsDC;AAtDM,KAAK,UAAU,UAAU,CAC9B,WAAgC,EAChC,aAAgC;IAEhC,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;QAC5B,OAAO;YACL,YAAY,EAAE,KAAK;SACpB,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,WAAW,CAAC;IAChD,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,UAAU,IAAI,WAAW,IAAI,EAAE,CAAC;IAC1D,MAAM,EAAE,YAAY,EAAE,GAAG,aAAa,CAAC;IAEvC,IACE,CAAC,KAAK;QACN,CAAC,CAAC,YAAY,IAAI,KAAK,CAAC;QACxB,CAAC,MAAM;QACP,CAAC,CAAC,YAAY,IAAI,MAAM,CAAC,EACzB,CAAC;QACD,iEAAiE;QACjE,OAAO;YACL,YAAY,EAAE,KAAK;SACpB,CAAC;IACJ,CAAC;IAED,IACE,YAAY,KAAK,SAAS;QAC1B,CAAC,KAAK,CAAC,OAAO,EAAE,gBAAgB,IAAI,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC,EAC9D,CAAC;QACD,OAAO;YACL,YAAY,EAAE,IAAI;YAClB,MAAM,EAAE,GAAG;YACX,YAAY,EAAE,qJAAqJ;YACnK,SAAS,EAAE,yBAAyB;SACrC,CAAC;IACJ,CAAC;IAED,IACE,YAAY,KAAK,iBAAiB;QAClC,CAAC,KAAK,CAAC,eAAe,EAAE,oBAAoB,IAAI,CAAC,CAAC;YAChD,CAAC,MAAM,CAAC,eAAe,IAAI,CAAC,CAAC,EAC/B,CAAC;QACD,OAAO;YACL,YAAY,EAAE,IAAI;YAClB,MAAM,EAAE,GAAG;YACX,YAAY,EAAE,sJAAsJ;YACpK,SAAS,EAAE,yBAAyB;SACrC,CAAC;IACJ,CAAC;IAED,OAAO;QACL,YAAY,EAAE,KAAK;KACpB,CAAC;AACJ,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/core/usageLimit/types.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.authorizeDomain = exports.authorizeBundleId = void 0;
|
4
|
+
const tslib_1 = require("tslib");
|
5
|
+
// Exports the public service definitions.
|
6
|
+
tslib_1.__exportStar(require("./core/services.js"), exports);
|
7
|
+
var client_js_1 = require("./core/authorize/client.js");
|
8
|
+
Object.defineProperty(exports, "authorizeBundleId", { enumerable: true, get: function () { return client_js_1.authorizeBundleId; } });
|
9
|
+
Object.defineProperty(exports, "authorizeDomain", { enumerable: true, get: function () { return client_js_1.authorizeDomain; } });
|
10
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;AAAA,0CAA0C;AAC1C,6DAAmC;AAEnC,wDAGoC;AAFlC,8GAAA,iBAAiB,OAAA;AACjB,4GAAA,eAAe,OAAA"}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.validBundlerServiceConfig = exports.validServiceConfig = exports.validApiKeyMeta = void 0;
|
4
|
+
exports.validApiKeyMeta = {
|
5
|
+
id: "1",
|
6
|
+
key: "your-api-key",
|
7
|
+
creatorWalletAddress: "creator-address",
|
8
|
+
secretHash: "secret-hash",
|
9
|
+
walletAddresses: [],
|
10
|
+
domains: ["example.com", "*.example.com"],
|
11
|
+
bundleIds: [],
|
12
|
+
redirectUrls: [],
|
13
|
+
accountId: "test-account-id",
|
14
|
+
accountStatus: "noCustomer",
|
15
|
+
accountPlan: "free",
|
16
|
+
services: [
|
17
|
+
{
|
18
|
+
name: "storage",
|
19
|
+
targetAddresses: ["target1", "target2"],
|
20
|
+
actions: ["action1", "action2"],
|
21
|
+
},
|
22
|
+
{
|
23
|
+
name: "service2",
|
24
|
+
targetAddresses: ["target3"],
|
25
|
+
actions: ["action3"],
|
26
|
+
},
|
27
|
+
{
|
28
|
+
name: "bundler",
|
29
|
+
targetAddresses: ["*"],
|
30
|
+
actions: ["action3"],
|
31
|
+
},
|
32
|
+
],
|
33
|
+
limits: {
|
34
|
+
storage: 100,
|
35
|
+
},
|
36
|
+
rateLimits: {
|
37
|
+
rpc: 25,
|
38
|
+
},
|
39
|
+
};
|
40
|
+
exports.validServiceConfig = {
|
41
|
+
apiUrl: "https://api.example.com",
|
42
|
+
serviceScope: "storage",
|
43
|
+
serviceApiKey: "service-api-key",
|
44
|
+
serviceAction: "action1",
|
45
|
+
enforceAuth: true,
|
46
|
+
};
|
47
|
+
exports.validBundlerServiceConfig = {
|
48
|
+
apiUrl: "https://api.example.com",
|
49
|
+
serviceScope: "bundler",
|
50
|
+
serviceApiKey: "service-api-key",
|
51
|
+
enforceAuth: true,
|
52
|
+
};
|
53
|
+
//# sourceMappingURL=mocks.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"mocks.js","sourceRoot":"","sources":["../../src/mocks.ts"],"names":[],"mappings":";;;AAEa,QAAA,eAAe,GAAmB;IAC7C,EAAE,EAAE,GAAG;IACP,GAAG,EAAE,cAAc;IACnB,oBAAoB,EAAE,iBAAiB;IACvC,UAAU,EAAE,aAAa;IACzB,eAAe,EAAE,EAAE;IACnB,OAAO,EAAE,CAAC,aAAa,EAAE,eAAe,CAAC;IACzC,SAAS,EAAE,EAAE;IACb,YAAY,EAAE,EAAE;IAChB,SAAS,EAAE,iBAAiB;IAC5B,aAAa,EAAE,YAAY;IAC3B,WAAW,EAAE,MAAM;IACnB,QAAQ,EAAE;QACR;YACE,IAAI,EAAE,SAAS;YACf,eAAe,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC;YACvC,OAAO,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC;SAChC;QACD;YACE,IAAI,EAAE,UAAU;YAChB,eAAe,EAAE,CAAC,SAAS,CAAC;YAC5B,OAAO,EAAE,CAAC,SAAS,CAAC;SACrB;QACD;YACE,IAAI,EAAE,SAAS;YACf,eAAe,EAAE,CAAC,GAAG,CAAC;YACtB,OAAO,EAAE,CAAC,SAAS,CAAC;SACrB;KACF;IACD,MAAM,EAAE;QACN,OAAO,EAAE,GAAG;KACb;IACD,UAAU,EAAE;QACV,GAAG,EAAE,EAAE;KACR;CACF,CAAC;AAEW,QAAA,kBAAkB,GAAsB;IACnD,MAAM,EAAE,yBAAyB;IACjC,YAAY,EAAE,SAAS;IACvB,aAAa,EAAE,iBAAiB;IAChC,aAAa,EAAE,SAAS;IACxB,WAAW,EAAE,IAAI;CAClB,CAAC;AAEW,QAAA,yBAAyB,GAAsB;IAC1D,MAAM,EAAE,yBAAyB;IACjC,YAAY,EAAE,SAAS;IACvB,aAAa,EAAE,iBAAiB;IAChC,WAAW,EAAE,IAAI;CAClB,CAAC"}
|
@@ -0,0 +1,173 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.authorizeNode = authorizeNode;
|
4
|
+
exports.extractAuthorizationData = extractAuthorizationData;
|
5
|
+
exports.hashSecretKey = hashSecretKey;
|
6
|
+
exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
|
7
|
+
exports.logHttpRequest = logHttpRequest;
|
8
|
+
const tslib_1 = require("tslib");
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
10
|
+
const index_js_1 = require("../core/authorize/index.js");
|
11
|
+
tslib_1.__exportStar(require("../core/usage.js"), exports);
|
12
|
+
tslib_1.__exportStar(require("../core/rateLimit/index.js"), exports);
|
13
|
+
tslib_1.__exportStar(require("../core/services.js"), exports);
|
14
|
+
tslib_1.__exportStar(require("../core/usageLimit/index.js"), exports);
|
15
|
+
/**
|
16
|
+
*
|
17
|
+
* @param {AuthInput['req']} authInput.req - The incoming request from which information will be pulled from. These information includes (checks are in order and terminates on first match):
|
18
|
+
* - clientId: Checks header `x-client-id`, search param `clientId`
|
19
|
+
* - bundleId: Checks header `x-bundle-id`, search param `bundleId`
|
20
|
+
* - secretKey: Checks header `x-secret-key`
|
21
|
+
* - origin (the requesting domain): Checks header `origin`, `referer`
|
22
|
+
* @param {AuthInput['clientId']} authInput.clientId - Overrides any clientId found on the `req` object
|
23
|
+
* @param {AuthInput['targetAddress']} authInput.targetAddress - Only used in smart wallets to determine if the request is authorized to interact with the target address.
|
24
|
+
* @param {NodeServiceConfig['enforceAuth']} serviceConfig - Always `true` unless you need to turn auth off. Tells the service whether or not to enforce auth.
|
25
|
+
* @param {NodeServiceConfig['apiUrl']} serviceConfig.apiUrl - The url of the api server to fetch information for verification. `https://api.thirdweb.com` for production and `https://api.staging.thirdweb.com` for staging
|
26
|
+
* @param {NodeServiceConfig['serviceApiKey']} serviceConfig.serviceApiKey - secret key to be used authenticate the caller of the api-server. Check the api-server's env variable for the keys.
|
27
|
+
* @param {NodeServiceConfig['serviceScope']} serviceConfig.serviceScope - The service that we are requesting authorization for. E.g. `relayer`, `rpc`, 'bundler', 'storage' etc.
|
28
|
+
* @param {NodeServiceConfig['serviceAction']} serviceConfig.serviceAction - Needed when the `serviceScope` is `storage`. Can be either `read` or `write`.
|
29
|
+
* @param {NodeServiceConfig['useWalletAuth']} serviceConfig.useWalletAuth - If true it pings the `wallet/me` or else, `account/me`. You most likely can leave this as false.
|
30
|
+
* @returns {AuthorizationResult} authorizationResult - contains if the request is authorized, and information about the account if it is authorized. Otherwise, it contains the error message and status code.
|
31
|
+
*/
|
32
|
+
async function authorizeNode(authInput, serviceConfig) {
|
33
|
+
let authData;
|
34
|
+
try {
|
35
|
+
authData = extractAuthorizationData(authInput);
|
36
|
+
}
|
37
|
+
catch (e) {
|
38
|
+
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
39
|
+
return {
|
40
|
+
authorized: false,
|
41
|
+
status: 400,
|
42
|
+
errorMessage: "Please pass either a client id or a secret key.",
|
43
|
+
errorCode: "KEY_CONFLICT",
|
44
|
+
};
|
45
|
+
}
|
46
|
+
return {
|
47
|
+
authorized: false,
|
48
|
+
status: 500,
|
49
|
+
errorMessage: "Internal Server Error",
|
50
|
+
errorCode: "INTERNAL_SERVER_ERROR",
|
51
|
+
};
|
52
|
+
}
|
53
|
+
return await (0, index_js_1.authorize)(authData, serviceConfig);
|
54
|
+
}
|
55
|
+
function getHeader(headers, headerName) {
|
56
|
+
const header = headers[headerName];
|
57
|
+
if (Array.isArray(header)) {
|
58
|
+
return header?.[0] ?? null;
|
59
|
+
}
|
60
|
+
return header ?? null;
|
61
|
+
}
|
62
|
+
function extractAuthorizationData(authInput) {
|
63
|
+
let requestUrl;
|
64
|
+
try {
|
65
|
+
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
66
|
+
}
|
67
|
+
catch (error) {
|
68
|
+
console.log("** Node URL Error **", error);
|
69
|
+
throw error;
|
70
|
+
}
|
71
|
+
const headers = authInput.req.headers;
|
72
|
+
const secretKey = getHeader(headers, "x-secret-key");
|
73
|
+
// prefer clientId that is explicitly passed in
|
74
|
+
let clientId = authInput.clientId ?? null;
|
75
|
+
if (!clientId) {
|
76
|
+
// next preference is clientId from header
|
77
|
+
clientId = getHeader(headers, "x-client-id");
|
78
|
+
}
|
79
|
+
// next preference is search param
|
80
|
+
if (!clientId) {
|
81
|
+
clientId = requestUrl.searchParams.get("clientId");
|
82
|
+
}
|
83
|
+
// bundle id from header is first preference
|
84
|
+
let bundleId = getHeader(headers, "x-bundle-id");
|
85
|
+
// next preference is search param
|
86
|
+
if (!bundleId) {
|
87
|
+
bundleId = requestUrl.searchParams.get("bundleId");
|
88
|
+
}
|
89
|
+
let origin = getHeader(headers, "origin");
|
90
|
+
// if origin header is not available we'll fall back to referrer;
|
91
|
+
if (!origin) {
|
92
|
+
origin = getHeader(headers, "referer");
|
93
|
+
}
|
94
|
+
// if we have an origin at this point, normalize it
|
95
|
+
if (origin) {
|
96
|
+
try {
|
97
|
+
origin = new URL(origin).host;
|
98
|
+
}
|
99
|
+
catch (e) {
|
100
|
+
console.warn("failed to parse origin", origin, e);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
// handle if we a secret key is passed in the headers
|
104
|
+
let secretKeyHash = null;
|
105
|
+
if (secretKey) {
|
106
|
+
// hash the secret key
|
107
|
+
secretKeyHash = hashSecretKey(secretKey);
|
108
|
+
// derive the client id from the secret key hash
|
109
|
+
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
110
|
+
// if we already have a client id passed in we need to make sure they match
|
111
|
+
if (clientId && clientId !== derivedClientId) {
|
112
|
+
throw new Error("KEY_CONFLICT");
|
113
|
+
}
|
114
|
+
// otherwise set the client id to the derived client id (client id based off of secret key)
|
115
|
+
clientId = derivedClientId;
|
116
|
+
}
|
117
|
+
let jwt = null;
|
118
|
+
let useWalletAuth = null;
|
119
|
+
// check for authorization header on the request
|
120
|
+
const authorizationHeader = getHeader(headers, "authorization");
|
121
|
+
if (authorizationHeader) {
|
122
|
+
const [type, token] = authorizationHeader.split(" ");
|
123
|
+
if (type?.toLowerCase() === "bearer" && !!token) {
|
124
|
+
jwt = token;
|
125
|
+
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
126
|
+
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
127
|
+
if (walletAuthHeader?.toLowerCase() === "true") {
|
128
|
+
useWalletAuth = walletAuthHeader;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
return {
|
133
|
+
jwt,
|
134
|
+
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
135
|
+
secretKeyHash,
|
136
|
+
secretKey,
|
137
|
+
clientId,
|
138
|
+
origin,
|
139
|
+
bundleId,
|
140
|
+
targetAddress: authInput.targetAddress,
|
141
|
+
useWalletAuth,
|
142
|
+
};
|
143
|
+
}
|
144
|
+
function hashSecretKey(secretKey) {
|
145
|
+
return (0, node_crypto_1.createHash)("sha256").update(secretKey).digest("hex");
|
146
|
+
}
|
147
|
+
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
148
|
+
return secretKeyHash.slice(0, 32);
|
149
|
+
}
|
150
|
+
function logHttpRequest({ clientId, req, res, isAuthed, statusMessage, latencyMs, }) {
|
151
|
+
try {
|
152
|
+
const authorizationData = extractAuthorizationData({ req, clientId });
|
153
|
+
const headers = req.headers;
|
154
|
+
console.log(JSON.stringify({
|
155
|
+
method: req.method,
|
156
|
+
pathname: req.url,
|
157
|
+
hasSecretKey: !!authorizationData.secretKey,
|
158
|
+
hasClientId: !!authorizationData.clientId,
|
159
|
+
hasJwt: !!authorizationData.jwt,
|
160
|
+
clientId: authorizationData.clientId,
|
161
|
+
isAuthed,
|
162
|
+
status: res.statusCode,
|
163
|
+
statusMessage,
|
164
|
+
sdkName: headers["x-sdk-name"] ?? undefined,
|
165
|
+
sdkVersion: headers["x-sdk-version"] ?? undefined,
|
166
|
+
platform: headers["x-sdk-platform"] ?? undefined,
|
167
|
+
os: headers["x-sdk-os"] ?? undefined,
|
168
|
+
latencyMs,
|
169
|
+
}));
|
170
|
+
}
|
171
|
+
catch { }
|
172
|
+
}
|
173
|
+
//# sourceMappingURL=index.js.map
|