@thirdweb-dev/service-utils 0.5.0-nightly-6cf298a29-20240308012322 → 0.5.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/dist/cjs/cf-worker/index.js +164 -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 +42 -0
- package/dist/cjs/core/api.js.map +1 -0
- package/dist/cjs/core/authorize/client.js +98 -0
- package/dist/cjs/core/authorize/client.js.map +1 -0
- package/dist/cjs/core/authorize/index.js +109 -0
- package/dist/cjs/core/authorize/index.js.map +1 -0
- package/dist/cjs/core/authorize/service.js +50 -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 +60 -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 +85 -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 +85 -0
- package/dist/cjs/core/usage.js.map +1 -0
- package/dist/cjs/index.js +13 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/mocks.js +60 -0
- package/dist/cjs/mocks.js.map +1 -0
- package/dist/cjs/node/index.js +182 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/cf-worker/index.js +156 -0
- package/dist/esm/cf-worker/index.js.map +1 -0
- package/dist/esm/cf-worker/usage.js +53 -0
- package/dist/esm/cf-worker/usage.js.map +1 -0
- package/dist/esm/core/api.js +38 -0
- package/dist/esm/core/api.js.map +1 -0
- package/dist/esm/core/authorize/client.js +93 -0
- package/dist/esm/core/authorize/client.js.map +1 -0
- package/dist/esm/core/authorize/index.js +106 -0
- package/dist/esm/core/authorize/index.js.map +1 -0
- package/dist/esm/core/authorize/service.js +47 -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 +57 -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 +81 -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 +82 -0
- package/dist/esm/core/usage.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/mocks.js +57 -0
- package/dist/esm/mocks.js.map +1 -0
- package/dist/esm/node/index.js +174 -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 -9
- package/dist/types/cf-worker/index.d.ts.map +1 -0
- package/dist/types/cf-worker/usage.d.ts +24 -0
- package/dist/types/cf-worker/usage.d.ts.map +1 -0
- package/dist/types/core/api.d.ts +121 -0
- package/dist/types/core/api.d.ts.map +1 -0
- package/dist/types/core/authorize/client.d.ts +17 -0
- package/dist/types/core/authorize/client.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/authorize/index.d.ts +6 -5
- package/dist/types/core/authorize/index.d.ts.map +1 -0
- package/dist/types/core/authorize/service.d.ts +4 -0
- package/dist/types/core/authorize/service.d.ts.map +1 -0
- package/dist/types/core/authorize/types.d.ts +10 -0
- package/dist/types/core/authorize/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/rateLimit/index.d.ts +4 -4
- package/dist/types/core/rateLimit/index.d.ts.map +1 -0
- package/dist/types/core/rateLimit/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/services.d.ts +55 -23
- package/dist/types/core/services.d.ts.map +1 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/{declarations/src/cf-worker → types/core}/usage.d.ts +84 -30
- package/dist/types/core/usage.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/mocks.d.ts +7 -0
- package/dist/types/mocks.d.ts.map +1 -0
- package/dist/{declarations/src → types}/node/index.d.ts +10 -12
- package/dist/types/node/index.d.ts.map +1 -0
- package/package.json +42 -45
- 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 -272
- 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 -272
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +0 -258
- package/cf-worker/package.json +0 -4
- 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 +0 -84
- package/dist/declarations/src/core/api.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/client.d.ts +0 -9
- 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 +0 -7
- package/dist/declarations/src/core/authorize/service.d.ts.map +0 -1
- package/dist/declarations/src/core/authorize/types.d.ts +0 -12
- 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/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 +0 -9
- package/dist/declarations/src/core/usageLimit/types.d.ts.map +0 -1
- package/dist/declarations/src/index.d.ts +0 -2
- package/dist/declarations/src/index.d.ts.map +0 -1
- package/dist/declarations/src/node/index.d.ts.map +0 -1
- package/dist/index-3b9a0743.esm.js +0 -572
- package/dist/index-62b88cac.cjs.dev.js +0 -576
- package/dist/index-aa324361.cjs.prod.js +0 -576
- package/dist/services-2aecbda8.esm.js +0 -65
- package/dist/services-508322f3.cjs.dev.js +0 -70
- package/dist/services-5c4d6977.cjs.prod.js +0 -70
- 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 -12
- package/dist/thirdweb-dev-service-utils.cjs.js +0 -7
- package/dist/thirdweb-dev-service-utils.cjs.prod.js +0 -12
- 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 -193
- package/node/dist/thirdweb-dev-service-utils-node.cjs.js +0 -7
- package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +0 -193
- package/node/dist/thirdweb-dev-service-utils-node.esm.js +0 -180
- package/node/package.json +0 -4
- /package/dist/{declarations/src → types}/core/rateLimit/types.d.ts +0 -0
- /package/dist/{declarations/src → types}/core/types.d.ts +0 -0
@@ -1,70 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
const SERVICE_DEFINITIONS = {
|
4
|
-
storage: {
|
5
|
-
name: "storage",
|
6
|
-
title: "Storage",
|
7
|
-
description: "IPFS Upload and Download",
|
8
|
-
actions: [{
|
9
|
-
name: "read",
|
10
|
-
title: "Download",
|
11
|
-
description: "Download a file from Storage"
|
12
|
-
}, {
|
13
|
-
name: "write",
|
14
|
-
title: "Upload",
|
15
|
-
description: "Upload a file to Storage"
|
16
|
-
}]
|
17
|
-
},
|
18
|
-
rpc: {
|
19
|
-
name: "rpc",
|
20
|
-
title: "RPC",
|
21
|
-
description: "Accelerated RPC Edge",
|
22
|
-
// all actions allowed
|
23
|
-
actions: []
|
24
|
-
},
|
25
|
-
bundler: {
|
26
|
-
name: "bundler",
|
27
|
-
title: "Smart Wallets",
|
28
|
-
description: "Bundler & Paymaster services",
|
29
|
-
// all actions allowed
|
30
|
-
actions: []
|
31
|
-
},
|
32
|
-
relayer: {
|
33
|
-
name: "relayer",
|
34
|
-
title: "Gasless Relayer",
|
35
|
-
description: "Enable gasless transactions",
|
36
|
-
// all actions allowed
|
37
|
-
actions: []
|
38
|
-
},
|
39
|
-
embeddedWallets: {
|
40
|
-
name: "embeddedWallets",
|
41
|
-
title: "Embedded Wallets",
|
42
|
-
description: "E-mail and social login wallets for easy web3 onboarding",
|
43
|
-
// all actions allowed
|
44
|
-
actions: []
|
45
|
-
},
|
46
|
-
checkout: {
|
47
|
-
name: "checkout",
|
48
|
-
title: "Checkouts",
|
49
|
-
description: "NFT Checkouts for easy web3 onboarding",
|
50
|
-
// all actions allowed
|
51
|
-
actions: []
|
52
|
-
},
|
53
|
-
pay: {
|
54
|
-
name: "pay",
|
55
|
-
title: "Pay",
|
56
|
-
description: "Pay for a blockchain transaction with any currency",
|
57
|
-
// all actions allowed
|
58
|
-
actions: []
|
59
|
-
}
|
60
|
-
};
|
61
|
-
const SERVICE_NAMES = Object.keys(SERVICE_DEFINITIONS);
|
62
|
-
const SERVICES = Object.values(SERVICE_DEFINITIONS);
|
63
|
-
function getServiceByName(name) {
|
64
|
-
return SERVICE_DEFINITIONS[name];
|
65
|
-
}
|
66
|
-
|
67
|
-
exports.SERVICES = SERVICES;
|
68
|
-
exports.SERVICE_DEFINITIONS = SERVICE_DEFINITIONS;
|
69
|
-
exports.SERVICE_NAMES = SERVICE_NAMES;
|
70
|
-
exports.getServiceByName = getServiceByName;
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"thirdweb-dev-service-utils.cjs.d.ts","sourceRoot":"","sources":["./declarations/src/index.d.ts"],"names":[],"mappings":"AAAA"}
|
@@ -1,12 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
-
|
5
|
-
var services = require('./services-508322f3.cjs.dev.js');
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
exports.SERVICES = services.SERVICES;
|
10
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
11
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
12
|
-
exports.getServiceByName = services.getServiceByName;
|
@@ -1,12 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
-
|
5
|
-
var services = require('./services-5c4d6977.cjs.prod.js');
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
exports.SERVICES = services.SERVICES;
|
10
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
11
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
12
|
-
exports.getServiceByName = services.getServiceByName;
|
@@ -1 +0,0 @@
|
|
1
|
-
export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from './services-2aecbda8.esm.js';
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"thirdweb-dev-service-utils-node.cjs.d.ts","sourceRoot":"","sources":["../../dist/declarations/src/node/index.d.ts"],"names":[],"mappings":"AAAA"}
|
@@ -1,193 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
-
|
5
|
-
var node_crypto = require('node:crypto');
|
6
|
-
var index = require('../../dist/index-62b88cac.cjs.dev.js');
|
7
|
-
var services = require('../../dist/services-508322f3.cjs.dev.js');
|
8
|
-
|
9
|
-
/**
|
10
|
-
*
|
11
|
-
* @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):
|
12
|
-
* - clientId: Checks header `x-client-id`, search param `clientId`
|
13
|
-
* - bundleId: Checks header `x-bundle-id`, search param `bundleId`
|
14
|
-
* - secretKey: Checks header `x-secret-key`
|
15
|
-
* - origin (the requesting domain): Checks header `origin`, `referer`
|
16
|
-
* @param {AuthInput['clientId']} authInput.clientId - Overrides any clientId found on the `req` object
|
17
|
-
* @param {AuthInput['targetAddress']} authInput.targetAddress - Only used in smart wallets to determine if the request is authorized to interact with the target address.
|
18
|
-
* @param {NodeServiceConfig['enforceAuth']} serviceConfig - Always `true` unless you need to turn auth off. Tells the service whether or not to enforce auth.
|
19
|
-
* @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
|
20
|
-
* @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.
|
21
|
-
* @param {NodeServiceConfig['serviceScope']} serviceConfig.serviceScope - The service that we are requesting authorization for. E.g. `relayer`, `rpc`, 'bundler', 'storage' etc.
|
22
|
-
* @param {NodeServiceConfig['serviceAction']} serviceConfig.serviceAction - Needed when the `serviceScope` is `storage`. Can be either `read` or `write`.
|
23
|
-
* @param {NodeServiceConfig['useWalletAuth']} serviceConfig.useWalletAuth - If true it pings the `wallet/me` or else, `account/me`. You most likely can leave this as false.
|
24
|
-
* @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.
|
25
|
-
*/
|
26
|
-
async function authorizeNode(authInput, serviceConfig) {
|
27
|
-
let authData;
|
28
|
-
try {
|
29
|
-
authData = extractAuthorizationData(authInput);
|
30
|
-
} catch (e) {
|
31
|
-
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
32
|
-
return {
|
33
|
-
authorized: false,
|
34
|
-
status: 400,
|
35
|
-
errorMessage: "Please pass either a client id or a secret key.",
|
36
|
-
errorCode: "KEY_CONFLICT"
|
37
|
-
};
|
38
|
-
}
|
39
|
-
return {
|
40
|
-
authorized: false,
|
41
|
-
status: 500,
|
42
|
-
errorMessage: "Internal Server Error",
|
43
|
-
errorCode: "INTERNAL_SERVER_ERROR"
|
44
|
-
};
|
45
|
-
}
|
46
|
-
return await index.authorize(authData, serviceConfig);
|
47
|
-
}
|
48
|
-
function getHeader(headers, headerName) {
|
49
|
-
const header = headers[headerName];
|
50
|
-
if (Array.isArray(header)) {
|
51
|
-
return header[0];
|
52
|
-
}
|
53
|
-
return header ?? null;
|
54
|
-
}
|
55
|
-
function extractAuthorizationData(authInput) {
|
56
|
-
let requestUrl;
|
57
|
-
try {
|
58
|
-
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
59
|
-
} catch (error) {
|
60
|
-
console.log("** Node URL Error **", error);
|
61
|
-
throw error;
|
62
|
-
}
|
63
|
-
const headers = authInput.req.headers;
|
64
|
-
const secretKey = getHeader(headers, "x-secret-key");
|
65
|
-
// prefer clientId that is explicitly passed in
|
66
|
-
let clientId = authInput.clientId ?? null;
|
67
|
-
if (!clientId) {
|
68
|
-
// next preference is clientId from header
|
69
|
-
clientId = getHeader(headers, "x-client-id");
|
70
|
-
}
|
71
|
-
|
72
|
-
// next preference is search param
|
73
|
-
if (!clientId) {
|
74
|
-
clientId = requestUrl.searchParams.get("clientId");
|
75
|
-
}
|
76
|
-
// bundle id from header is first preference
|
77
|
-
let bundleId = getHeader(headers, "x-bundle-id");
|
78
|
-
|
79
|
-
// next preference is search param
|
80
|
-
if (!bundleId) {
|
81
|
-
bundleId = requestUrl.searchParams.get("bundleId");
|
82
|
-
}
|
83
|
-
let origin = getHeader(headers, "origin");
|
84
|
-
// if origin header is not available we'll fall back to referrer;
|
85
|
-
if (!origin) {
|
86
|
-
origin = getHeader(headers, "referer");
|
87
|
-
}
|
88
|
-
// if we have an origin at this point, normalize it
|
89
|
-
if (origin) {
|
90
|
-
try {
|
91
|
-
origin = new URL(origin).host;
|
92
|
-
} catch (e) {
|
93
|
-
console.warn("failed to parse origin", origin, e);
|
94
|
-
}
|
95
|
-
}
|
96
|
-
|
97
|
-
// handle if we a secret key is passed in the headers
|
98
|
-
let secretKeyHash = null;
|
99
|
-
if (secretKey) {
|
100
|
-
// hash the secret key
|
101
|
-
secretKeyHash = hashSecretKey(secretKey);
|
102
|
-
// derive the client id from the secret key hash
|
103
|
-
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
104
|
-
// if we already have a client id passed in we need to make sure they match
|
105
|
-
if (clientId && clientId !== derivedClientId) {
|
106
|
-
throw new Error("KEY_CONFLICT");
|
107
|
-
}
|
108
|
-
// otherwise set the client id to the derived client id (client id based off of secret key)
|
109
|
-
clientId = derivedClientId;
|
110
|
-
}
|
111
|
-
let jwt = null;
|
112
|
-
let useWalletAuth = null;
|
113
|
-
// check for authorization header on the request
|
114
|
-
const authorizationHeader = getHeader(headers, "authorization");
|
115
|
-
if (authorizationHeader) {
|
116
|
-
const [type, token] = authorizationHeader.split(" ");
|
117
|
-
if (type.toLowerCase() === "bearer" && !!token) {
|
118
|
-
jwt = token;
|
119
|
-
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
120
|
-
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
121
|
-
if (walletAuthHeader?.toLowerCase() === "true") {
|
122
|
-
useWalletAuth = walletAuthHeader;
|
123
|
-
}
|
124
|
-
}
|
125
|
-
}
|
126
|
-
return {
|
127
|
-
jwt,
|
128
|
-
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
129
|
-
secretKeyHash,
|
130
|
-
secretKey,
|
131
|
-
clientId,
|
132
|
-
origin,
|
133
|
-
bundleId,
|
134
|
-
targetAddress: authInput.targetAddress,
|
135
|
-
useWalletAuth
|
136
|
-
};
|
137
|
-
}
|
138
|
-
function hashSecretKey(secretKey) {
|
139
|
-
return node_crypto.createHash("sha256").update(secretKey).digest("hex");
|
140
|
-
}
|
141
|
-
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
142
|
-
return secretKeyHash.slice(0, 32);
|
143
|
-
}
|
144
|
-
function logHttpRequest(_ref) {
|
145
|
-
let {
|
146
|
-
source,
|
147
|
-
clientId,
|
148
|
-
req,
|
149
|
-
res,
|
150
|
-
isAuthed,
|
151
|
-
statusMessage,
|
152
|
-
latencyMs
|
153
|
-
} = _ref;
|
154
|
-
try {
|
155
|
-
const authorizationData = extractAuthorizationData({
|
156
|
-
req,
|
157
|
-
clientId
|
158
|
-
});
|
159
|
-
const headers = req.headers;
|
160
|
-
const _statusMessage = statusMessage ?? res.statusMessage;
|
161
|
-
console.log(JSON.stringify({
|
162
|
-
source,
|
163
|
-
pathname: req.url,
|
164
|
-
hasSecretKey: !!authorizationData.secretKey,
|
165
|
-
hasClientId: !!authorizationData.clientId,
|
166
|
-
hasJwt: !!authorizationData.jwt,
|
167
|
-
clientId: authorizationData.clientId,
|
168
|
-
isAuthed: !!isAuthed ?? null,
|
169
|
-
status: res.statusCode,
|
170
|
-
statusMessage: _statusMessage,
|
171
|
-
sdkName: headers["x-sdk-name"] ?? "unknown",
|
172
|
-
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
173
|
-
platform: headers["x-sdk-platform"] ?? "unknown",
|
174
|
-
os: headers["x-sdk-os"] ?? "unknown",
|
175
|
-
latencyMs: latencyMs ?? null
|
176
|
-
}));
|
177
|
-
console.log(`statusMessage=${_statusMessage}`);
|
178
|
-
} catch (err) {
|
179
|
-
console.error("Failed to log HTTP request:", err);
|
180
|
-
}
|
181
|
-
}
|
182
|
-
|
183
|
-
exports.rateLimit = index.rateLimit;
|
184
|
-
exports.usageLimit = index.usageLimit;
|
185
|
-
exports.SERVICES = services.SERVICES;
|
186
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
187
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
188
|
-
exports.getServiceByName = services.getServiceByName;
|
189
|
-
exports.authorizeNode = authorizeNode;
|
190
|
-
exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
|
191
|
-
exports.extractAuthorizationData = extractAuthorizationData;
|
192
|
-
exports.hashSecretKey = hashSecretKey;
|
193
|
-
exports.logHttpRequest = logHttpRequest;
|
@@ -1,193 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
4
|
-
|
5
|
-
var node_crypto = require('node:crypto');
|
6
|
-
var index = require('../../dist/index-aa324361.cjs.prod.js');
|
7
|
-
var services = require('../../dist/services-5c4d6977.cjs.prod.js');
|
8
|
-
|
9
|
-
/**
|
10
|
-
*
|
11
|
-
* @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):
|
12
|
-
* - clientId: Checks header `x-client-id`, search param `clientId`
|
13
|
-
* - bundleId: Checks header `x-bundle-id`, search param `bundleId`
|
14
|
-
* - secretKey: Checks header `x-secret-key`
|
15
|
-
* - origin (the requesting domain): Checks header `origin`, `referer`
|
16
|
-
* @param {AuthInput['clientId']} authInput.clientId - Overrides any clientId found on the `req` object
|
17
|
-
* @param {AuthInput['targetAddress']} authInput.targetAddress - Only used in smart wallets to determine if the request is authorized to interact with the target address.
|
18
|
-
* @param {NodeServiceConfig['enforceAuth']} serviceConfig - Always `true` unless you need to turn auth off. Tells the service whether or not to enforce auth.
|
19
|
-
* @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
|
20
|
-
* @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.
|
21
|
-
* @param {NodeServiceConfig['serviceScope']} serviceConfig.serviceScope - The service that we are requesting authorization for. E.g. `relayer`, `rpc`, 'bundler', 'storage' etc.
|
22
|
-
* @param {NodeServiceConfig['serviceAction']} serviceConfig.serviceAction - Needed when the `serviceScope` is `storage`. Can be either `read` or `write`.
|
23
|
-
* @param {NodeServiceConfig['useWalletAuth']} serviceConfig.useWalletAuth - If true it pings the `wallet/me` or else, `account/me`. You most likely can leave this as false.
|
24
|
-
* @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.
|
25
|
-
*/
|
26
|
-
async function authorizeNode(authInput, serviceConfig) {
|
27
|
-
let authData;
|
28
|
-
try {
|
29
|
-
authData = extractAuthorizationData(authInput);
|
30
|
-
} catch (e) {
|
31
|
-
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
32
|
-
return {
|
33
|
-
authorized: false,
|
34
|
-
status: 400,
|
35
|
-
errorMessage: "Please pass either a client id or a secret key.",
|
36
|
-
errorCode: "KEY_CONFLICT"
|
37
|
-
};
|
38
|
-
}
|
39
|
-
return {
|
40
|
-
authorized: false,
|
41
|
-
status: 500,
|
42
|
-
errorMessage: "Internal Server Error",
|
43
|
-
errorCode: "INTERNAL_SERVER_ERROR"
|
44
|
-
};
|
45
|
-
}
|
46
|
-
return await index.authorize(authData, serviceConfig);
|
47
|
-
}
|
48
|
-
function getHeader(headers, headerName) {
|
49
|
-
const header = headers[headerName];
|
50
|
-
if (Array.isArray(header)) {
|
51
|
-
return header[0];
|
52
|
-
}
|
53
|
-
return header ?? null;
|
54
|
-
}
|
55
|
-
function extractAuthorizationData(authInput) {
|
56
|
-
let requestUrl;
|
57
|
-
try {
|
58
|
-
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
59
|
-
} catch (error) {
|
60
|
-
console.log("** Node URL Error **", error);
|
61
|
-
throw error;
|
62
|
-
}
|
63
|
-
const headers = authInput.req.headers;
|
64
|
-
const secretKey = getHeader(headers, "x-secret-key");
|
65
|
-
// prefer clientId that is explicitly passed in
|
66
|
-
let clientId = authInput.clientId ?? null;
|
67
|
-
if (!clientId) {
|
68
|
-
// next preference is clientId from header
|
69
|
-
clientId = getHeader(headers, "x-client-id");
|
70
|
-
}
|
71
|
-
|
72
|
-
// next preference is search param
|
73
|
-
if (!clientId) {
|
74
|
-
clientId = requestUrl.searchParams.get("clientId");
|
75
|
-
}
|
76
|
-
// bundle id from header is first preference
|
77
|
-
let bundleId = getHeader(headers, "x-bundle-id");
|
78
|
-
|
79
|
-
// next preference is search param
|
80
|
-
if (!bundleId) {
|
81
|
-
bundleId = requestUrl.searchParams.get("bundleId");
|
82
|
-
}
|
83
|
-
let origin = getHeader(headers, "origin");
|
84
|
-
// if origin header is not available we'll fall back to referrer;
|
85
|
-
if (!origin) {
|
86
|
-
origin = getHeader(headers, "referer");
|
87
|
-
}
|
88
|
-
// if we have an origin at this point, normalize it
|
89
|
-
if (origin) {
|
90
|
-
try {
|
91
|
-
origin = new URL(origin).host;
|
92
|
-
} catch (e) {
|
93
|
-
console.warn("failed to parse origin", origin, e);
|
94
|
-
}
|
95
|
-
}
|
96
|
-
|
97
|
-
// handle if we a secret key is passed in the headers
|
98
|
-
let secretKeyHash = null;
|
99
|
-
if (secretKey) {
|
100
|
-
// hash the secret key
|
101
|
-
secretKeyHash = hashSecretKey(secretKey);
|
102
|
-
// derive the client id from the secret key hash
|
103
|
-
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
104
|
-
// if we already have a client id passed in we need to make sure they match
|
105
|
-
if (clientId && clientId !== derivedClientId) {
|
106
|
-
throw new Error("KEY_CONFLICT");
|
107
|
-
}
|
108
|
-
// otherwise set the client id to the derived client id (client id based off of secret key)
|
109
|
-
clientId = derivedClientId;
|
110
|
-
}
|
111
|
-
let jwt = null;
|
112
|
-
let useWalletAuth = null;
|
113
|
-
// check for authorization header on the request
|
114
|
-
const authorizationHeader = getHeader(headers, "authorization");
|
115
|
-
if (authorizationHeader) {
|
116
|
-
const [type, token] = authorizationHeader.split(" ");
|
117
|
-
if (type.toLowerCase() === "bearer" && !!token) {
|
118
|
-
jwt = token;
|
119
|
-
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
120
|
-
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
121
|
-
if (walletAuthHeader?.toLowerCase() === "true") {
|
122
|
-
useWalletAuth = walletAuthHeader;
|
123
|
-
}
|
124
|
-
}
|
125
|
-
}
|
126
|
-
return {
|
127
|
-
jwt,
|
128
|
-
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
129
|
-
secretKeyHash,
|
130
|
-
secretKey,
|
131
|
-
clientId,
|
132
|
-
origin,
|
133
|
-
bundleId,
|
134
|
-
targetAddress: authInput.targetAddress,
|
135
|
-
useWalletAuth
|
136
|
-
};
|
137
|
-
}
|
138
|
-
function hashSecretKey(secretKey) {
|
139
|
-
return node_crypto.createHash("sha256").update(secretKey).digest("hex");
|
140
|
-
}
|
141
|
-
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
142
|
-
return secretKeyHash.slice(0, 32);
|
143
|
-
}
|
144
|
-
function logHttpRequest(_ref) {
|
145
|
-
let {
|
146
|
-
source,
|
147
|
-
clientId,
|
148
|
-
req,
|
149
|
-
res,
|
150
|
-
isAuthed,
|
151
|
-
statusMessage,
|
152
|
-
latencyMs
|
153
|
-
} = _ref;
|
154
|
-
try {
|
155
|
-
const authorizationData = extractAuthorizationData({
|
156
|
-
req,
|
157
|
-
clientId
|
158
|
-
});
|
159
|
-
const headers = req.headers;
|
160
|
-
const _statusMessage = statusMessage ?? res.statusMessage;
|
161
|
-
console.log(JSON.stringify({
|
162
|
-
source,
|
163
|
-
pathname: req.url,
|
164
|
-
hasSecretKey: !!authorizationData.secretKey,
|
165
|
-
hasClientId: !!authorizationData.clientId,
|
166
|
-
hasJwt: !!authorizationData.jwt,
|
167
|
-
clientId: authorizationData.clientId,
|
168
|
-
isAuthed: !!isAuthed ?? null,
|
169
|
-
status: res.statusCode,
|
170
|
-
statusMessage: _statusMessage,
|
171
|
-
sdkName: headers["x-sdk-name"] ?? "unknown",
|
172
|
-
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
173
|
-
platform: headers["x-sdk-platform"] ?? "unknown",
|
174
|
-
os: headers["x-sdk-os"] ?? "unknown",
|
175
|
-
latencyMs: latencyMs ?? null
|
176
|
-
}));
|
177
|
-
console.log(`statusMessage=${_statusMessage}`);
|
178
|
-
} catch (err) {
|
179
|
-
console.error("Failed to log HTTP request:", err);
|
180
|
-
}
|
181
|
-
}
|
182
|
-
|
183
|
-
exports.rateLimit = index.rateLimit;
|
184
|
-
exports.usageLimit = index.usageLimit;
|
185
|
-
exports.SERVICES = services.SERVICES;
|
186
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
187
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
188
|
-
exports.getServiceByName = services.getServiceByName;
|
189
|
-
exports.authorizeNode = authorizeNode;
|
190
|
-
exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
|
191
|
-
exports.extractAuthorizationData = extractAuthorizationData;
|
192
|
-
exports.hashSecretKey = hashSecretKey;
|
193
|
-
exports.logHttpRequest = logHttpRequest;
|