@thirdweb-dev/service-utils 0.5.0-nightly-f5aed34d-20230829001006 → 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 +10 -8
- 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/types/core/rateLimit/index.d.ts +20 -0
- package/dist/types/core/rateLimit/index.d.ts.map +1 -0
- package/dist/types/core/rateLimit/types.d.ts +13 -0
- package/dist/types/core/rateLimit/types.d.ts.map +1 -0
- package/dist/{declarations/src → types}/core/services.d.ts +84 -4
- package/dist/types/core/services.d.ts.map +1 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/core/usage.d.ts +196 -0
- 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/types/node/index.d.ts +41 -0
- package/dist/types/node/index.d.ts.map +1 -0
- package/package.json +42 -44
- 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 -240
- 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 -240
- package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +0 -227
- 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 +0 -79
- package/dist/declarations/src/cf-worker/usage.d.ts.map +0 -1
- package/dist/declarations/src/core/api.d.ts +0 -48
- 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/services.d.ts.map +0 -1
- package/dist/declarations/src/core/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 +0 -23
- package/dist/declarations/src/node/index.d.ts.map +0 -1
- package/dist/index-6e0ecc5f.cjs.prod.js +0 -424
- package/dist/index-cd4f96ef.cjs.dev.js +0 -424
- package/dist/index-ffddf746.esm.js +0 -422
- package/dist/services-86283509.esm.js +0 -44
- package/dist/services-9e185105.cjs.prod.js +0 -49
- package/dist/services-a3f36057.cjs.dev.js +0 -49
- 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 -167
- package/node/dist/thirdweb-dev-service-utils-node.cjs.js +0 -7
- package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +0 -167
- package/node/dist/thirdweb-dev-service-utils-node.esm.js +0 -155
- package/node/package.json +0 -4
- /package/dist/{declarations/src → types}/core/types.d.ts +0 -0
@@ -1,167 +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-cd4f96ef.cjs.dev.js');
|
7
|
-
var services = require('../../dist/services-a3f36057.cjs.dev.js');
|
8
|
-
|
9
|
-
async function authorizeNode(authInput, serviceConfig) {
|
10
|
-
let authData;
|
11
|
-
try {
|
12
|
-
authData = extractAuthorizationData(authInput);
|
13
|
-
} catch (e) {
|
14
|
-
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
15
|
-
return {
|
16
|
-
authorized: false,
|
17
|
-
status: 400,
|
18
|
-
errorMessage: "Please pass either a client id or a secret key.",
|
19
|
-
errorCode: "KEY_CONFLICT"
|
20
|
-
};
|
21
|
-
}
|
22
|
-
return {
|
23
|
-
authorized: false,
|
24
|
-
status: 500,
|
25
|
-
errorMessage: "Internal Server Error",
|
26
|
-
errorCode: "INTERNAL_SERVER_ERROR"
|
27
|
-
};
|
28
|
-
}
|
29
|
-
return await index.authorize(authData, serviceConfig);
|
30
|
-
}
|
31
|
-
function getHeader(headers, headerName) {
|
32
|
-
const header = headers[headerName];
|
33
|
-
if (Array.isArray(header)) {
|
34
|
-
return header[0];
|
35
|
-
}
|
36
|
-
return header ?? null;
|
37
|
-
}
|
38
|
-
function extractAuthorizationData(authInput) {
|
39
|
-
let requestUrl;
|
40
|
-
try {
|
41
|
-
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
42
|
-
} catch (error) {
|
43
|
-
console.log("** Node URL Error **", error);
|
44
|
-
throw error;
|
45
|
-
}
|
46
|
-
const headers = authInput.req.headers;
|
47
|
-
const secretKey = getHeader(headers, "x-secret-key");
|
48
|
-
// prefer clientId that is explicitly passed in
|
49
|
-
let clientId = authInput.clientId ?? null;
|
50
|
-
if (!clientId) {
|
51
|
-
// next preference is clientId from header
|
52
|
-
clientId = getHeader(headers, "x-client-id");
|
53
|
-
}
|
54
|
-
|
55
|
-
// next preference is search param
|
56
|
-
if (!clientId) {
|
57
|
-
clientId = requestUrl.searchParams.get("clientId");
|
58
|
-
}
|
59
|
-
// bundle id from header is first preference
|
60
|
-
let bundleId = getHeader(headers, "x-bundle-id");
|
61
|
-
|
62
|
-
// next preference is search param
|
63
|
-
if (!bundleId) {
|
64
|
-
bundleId = requestUrl.searchParams.get("bundleId");
|
65
|
-
}
|
66
|
-
let origin = getHeader(headers, "origin");
|
67
|
-
// if origin header is not available we'll fall back to referrer;
|
68
|
-
if (!origin) {
|
69
|
-
origin = getHeader(headers, "referer");
|
70
|
-
}
|
71
|
-
// if we have an origin at this point, normalize it
|
72
|
-
if (origin) {
|
73
|
-
try {
|
74
|
-
origin = new URL(origin).host;
|
75
|
-
} catch (e) {
|
76
|
-
console.warn("failed to parse origin", origin, e);
|
77
|
-
}
|
78
|
-
}
|
79
|
-
|
80
|
-
// handle if we a secret key is passed in the headers
|
81
|
-
let secretKeyHash = null;
|
82
|
-
if (secretKey) {
|
83
|
-
// hash the secret key
|
84
|
-
secretKeyHash = hashSecretKey(secretKey);
|
85
|
-
// derive the client id from the secret key hash
|
86
|
-
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
87
|
-
// if we already have a client id passed in we need to make sure they match
|
88
|
-
if (clientId && clientId !== derivedClientId) {
|
89
|
-
throw new Error("KEY_CONFLICT");
|
90
|
-
}
|
91
|
-
// otherwise set the client id to the derived client id (client id based off of secret key)
|
92
|
-
clientId = derivedClientId;
|
93
|
-
}
|
94
|
-
let jwt = null;
|
95
|
-
let useWalletAuth = null;
|
96
|
-
// check for authorization header on the request
|
97
|
-
const authorizationHeader = getHeader(headers, "authorization");
|
98
|
-
if (authorizationHeader) {
|
99
|
-
const [type, token] = authorizationHeader.split(" ");
|
100
|
-
if (type.toLowerCase() === "bearer" && !!token) {
|
101
|
-
jwt = token;
|
102
|
-
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
103
|
-
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
104
|
-
if (walletAuthHeader?.toLowerCase() === "true") {
|
105
|
-
useWalletAuth = walletAuthHeader;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
return {
|
110
|
-
jwt,
|
111
|
-
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
112
|
-
secretKeyHash,
|
113
|
-
secretKey,
|
114
|
-
clientId,
|
115
|
-
origin,
|
116
|
-
bundleId,
|
117
|
-
targetAddress: authInput.targetAddress,
|
118
|
-
useWalletAuth
|
119
|
-
};
|
120
|
-
}
|
121
|
-
function hashSecretKey(secretKey) {
|
122
|
-
return node_crypto.createHash("sha256").update(secretKey).digest("hex");
|
123
|
-
}
|
124
|
-
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
125
|
-
return secretKeyHash.slice(0, 32);
|
126
|
-
}
|
127
|
-
function logHttpRequest(_ref) {
|
128
|
-
let {
|
129
|
-
source,
|
130
|
-
clientId,
|
131
|
-
req,
|
132
|
-
res,
|
133
|
-
isAuthed,
|
134
|
-
statusMessage
|
135
|
-
} = _ref;
|
136
|
-
const authorizationData = extractAuthorizationData({
|
137
|
-
req,
|
138
|
-
clientId
|
139
|
-
});
|
140
|
-
const headers = req.headers;
|
141
|
-
const _statusMessage = statusMessage ?? res.statusMessage;
|
142
|
-
console.log(JSON.stringify({
|
143
|
-
source,
|
144
|
-
pathname: req.url,
|
145
|
-
hasSecretKey: !!authorizationData.secretKey,
|
146
|
-
hasClientId: !!authorizationData.clientId,
|
147
|
-
hasJwt: !!authorizationData.jwt,
|
148
|
-
clientId: authorizationData.clientId,
|
149
|
-
isAuthed: !!isAuthed ?? null,
|
150
|
-
status: res.statusCode,
|
151
|
-
statusMessage: _statusMessage,
|
152
|
-
sdkName: headers["x-sdk-name"] ?? "unknown",
|
153
|
-
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
154
|
-
platform: headers["x-sdk-platform"] ?? "unknown"
|
155
|
-
}));
|
156
|
-
console.log(`statusMessage=${_statusMessage}`);
|
157
|
-
}
|
158
|
-
|
159
|
-
exports.SERVICES = services.SERVICES;
|
160
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
161
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
162
|
-
exports.getServiceByName = services.getServiceByName;
|
163
|
-
exports.authorizeNode = authorizeNode;
|
164
|
-
exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
|
165
|
-
exports.extractAuthorizationData = extractAuthorizationData;
|
166
|
-
exports.hashSecretKey = hashSecretKey;
|
167
|
-
exports.logHttpRequest = logHttpRequest;
|
@@ -1,167 +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-6e0ecc5f.cjs.prod.js');
|
7
|
-
var services = require('../../dist/services-9e185105.cjs.prod.js');
|
8
|
-
|
9
|
-
async function authorizeNode(authInput, serviceConfig) {
|
10
|
-
let authData;
|
11
|
-
try {
|
12
|
-
authData = extractAuthorizationData(authInput);
|
13
|
-
} catch (e) {
|
14
|
-
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
15
|
-
return {
|
16
|
-
authorized: false,
|
17
|
-
status: 400,
|
18
|
-
errorMessage: "Please pass either a client id or a secret key.",
|
19
|
-
errorCode: "KEY_CONFLICT"
|
20
|
-
};
|
21
|
-
}
|
22
|
-
return {
|
23
|
-
authorized: false,
|
24
|
-
status: 500,
|
25
|
-
errorMessage: "Internal Server Error",
|
26
|
-
errorCode: "INTERNAL_SERVER_ERROR"
|
27
|
-
};
|
28
|
-
}
|
29
|
-
return await index.authorize(authData, serviceConfig);
|
30
|
-
}
|
31
|
-
function getHeader(headers, headerName) {
|
32
|
-
const header = headers[headerName];
|
33
|
-
if (Array.isArray(header)) {
|
34
|
-
return header[0];
|
35
|
-
}
|
36
|
-
return header ?? null;
|
37
|
-
}
|
38
|
-
function extractAuthorizationData(authInput) {
|
39
|
-
let requestUrl;
|
40
|
-
try {
|
41
|
-
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
42
|
-
} catch (error) {
|
43
|
-
console.log("** Node URL Error **", error);
|
44
|
-
throw error;
|
45
|
-
}
|
46
|
-
const headers = authInput.req.headers;
|
47
|
-
const secretKey = getHeader(headers, "x-secret-key");
|
48
|
-
// prefer clientId that is explicitly passed in
|
49
|
-
let clientId = authInput.clientId ?? null;
|
50
|
-
if (!clientId) {
|
51
|
-
// next preference is clientId from header
|
52
|
-
clientId = getHeader(headers, "x-client-id");
|
53
|
-
}
|
54
|
-
|
55
|
-
// next preference is search param
|
56
|
-
if (!clientId) {
|
57
|
-
clientId = requestUrl.searchParams.get("clientId");
|
58
|
-
}
|
59
|
-
// bundle id from header is first preference
|
60
|
-
let bundleId = getHeader(headers, "x-bundle-id");
|
61
|
-
|
62
|
-
// next preference is search param
|
63
|
-
if (!bundleId) {
|
64
|
-
bundleId = requestUrl.searchParams.get("bundleId");
|
65
|
-
}
|
66
|
-
let origin = getHeader(headers, "origin");
|
67
|
-
// if origin header is not available we'll fall back to referrer;
|
68
|
-
if (!origin) {
|
69
|
-
origin = getHeader(headers, "referer");
|
70
|
-
}
|
71
|
-
// if we have an origin at this point, normalize it
|
72
|
-
if (origin) {
|
73
|
-
try {
|
74
|
-
origin = new URL(origin).host;
|
75
|
-
} catch (e) {
|
76
|
-
console.warn("failed to parse origin", origin, e);
|
77
|
-
}
|
78
|
-
}
|
79
|
-
|
80
|
-
// handle if we a secret key is passed in the headers
|
81
|
-
let secretKeyHash = null;
|
82
|
-
if (secretKey) {
|
83
|
-
// hash the secret key
|
84
|
-
secretKeyHash = hashSecretKey(secretKey);
|
85
|
-
// derive the client id from the secret key hash
|
86
|
-
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
87
|
-
// if we already have a client id passed in we need to make sure they match
|
88
|
-
if (clientId && clientId !== derivedClientId) {
|
89
|
-
throw new Error("KEY_CONFLICT");
|
90
|
-
}
|
91
|
-
// otherwise set the client id to the derived client id (client id based off of secret key)
|
92
|
-
clientId = derivedClientId;
|
93
|
-
}
|
94
|
-
let jwt = null;
|
95
|
-
let useWalletAuth = null;
|
96
|
-
// check for authorization header on the request
|
97
|
-
const authorizationHeader = getHeader(headers, "authorization");
|
98
|
-
if (authorizationHeader) {
|
99
|
-
const [type, token] = authorizationHeader.split(" ");
|
100
|
-
if (type.toLowerCase() === "bearer" && !!token) {
|
101
|
-
jwt = token;
|
102
|
-
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
103
|
-
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
104
|
-
if (walletAuthHeader?.toLowerCase() === "true") {
|
105
|
-
useWalletAuth = walletAuthHeader;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
return {
|
110
|
-
jwt,
|
111
|
-
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
112
|
-
secretKeyHash,
|
113
|
-
secretKey,
|
114
|
-
clientId,
|
115
|
-
origin,
|
116
|
-
bundleId,
|
117
|
-
targetAddress: authInput.targetAddress,
|
118
|
-
useWalletAuth
|
119
|
-
};
|
120
|
-
}
|
121
|
-
function hashSecretKey(secretKey) {
|
122
|
-
return node_crypto.createHash("sha256").update(secretKey).digest("hex");
|
123
|
-
}
|
124
|
-
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
125
|
-
return secretKeyHash.slice(0, 32);
|
126
|
-
}
|
127
|
-
function logHttpRequest(_ref) {
|
128
|
-
let {
|
129
|
-
source,
|
130
|
-
clientId,
|
131
|
-
req,
|
132
|
-
res,
|
133
|
-
isAuthed,
|
134
|
-
statusMessage
|
135
|
-
} = _ref;
|
136
|
-
const authorizationData = extractAuthorizationData({
|
137
|
-
req,
|
138
|
-
clientId
|
139
|
-
});
|
140
|
-
const headers = req.headers;
|
141
|
-
const _statusMessage = statusMessage ?? res.statusMessage;
|
142
|
-
console.log(JSON.stringify({
|
143
|
-
source,
|
144
|
-
pathname: req.url,
|
145
|
-
hasSecretKey: !!authorizationData.secretKey,
|
146
|
-
hasClientId: !!authorizationData.clientId,
|
147
|
-
hasJwt: !!authorizationData.jwt,
|
148
|
-
clientId: authorizationData.clientId,
|
149
|
-
isAuthed: !!isAuthed ?? null,
|
150
|
-
status: res.statusCode,
|
151
|
-
statusMessage: _statusMessage,
|
152
|
-
sdkName: headers["x-sdk-name"] ?? "unknown",
|
153
|
-
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
154
|
-
platform: headers["x-sdk-platform"] ?? "unknown"
|
155
|
-
}));
|
156
|
-
console.log(`statusMessage=${_statusMessage}`);
|
157
|
-
}
|
158
|
-
|
159
|
-
exports.SERVICES = services.SERVICES;
|
160
|
-
exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
|
161
|
-
exports.SERVICE_NAMES = services.SERVICE_NAMES;
|
162
|
-
exports.getServiceByName = services.getServiceByName;
|
163
|
-
exports.authorizeNode = authorizeNode;
|
164
|
-
exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
|
165
|
-
exports.extractAuthorizationData = extractAuthorizationData;
|
166
|
-
exports.hashSecretKey = hashSecretKey;
|
167
|
-
exports.logHttpRequest = logHttpRequest;
|
@@ -1,155 +0,0 @@
|
|
1
|
-
import { createHash } from 'node:crypto';
|
2
|
-
import { a as authorize } from '../../dist/index-ffddf746.esm.js';
|
3
|
-
export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
|
4
|
-
|
5
|
-
async function authorizeNode(authInput, serviceConfig) {
|
6
|
-
let authData;
|
7
|
-
try {
|
8
|
-
authData = extractAuthorizationData(authInput);
|
9
|
-
} catch (e) {
|
10
|
-
if (e instanceof Error && e.message === "KEY_CONFLICT") {
|
11
|
-
return {
|
12
|
-
authorized: false,
|
13
|
-
status: 400,
|
14
|
-
errorMessage: "Please pass either a client id or a secret key.",
|
15
|
-
errorCode: "KEY_CONFLICT"
|
16
|
-
};
|
17
|
-
}
|
18
|
-
return {
|
19
|
-
authorized: false,
|
20
|
-
status: 500,
|
21
|
-
errorMessage: "Internal Server Error",
|
22
|
-
errorCode: "INTERNAL_SERVER_ERROR"
|
23
|
-
};
|
24
|
-
}
|
25
|
-
return await authorize(authData, serviceConfig);
|
26
|
-
}
|
27
|
-
function getHeader(headers, headerName) {
|
28
|
-
const header = headers[headerName];
|
29
|
-
if (Array.isArray(header)) {
|
30
|
-
return header[0];
|
31
|
-
}
|
32
|
-
return header ?? null;
|
33
|
-
}
|
34
|
-
function extractAuthorizationData(authInput) {
|
35
|
-
let requestUrl;
|
36
|
-
try {
|
37
|
-
requestUrl = new URL(authInput.req.url || "", `http://${authInput.req.headers.host}`);
|
38
|
-
} catch (error) {
|
39
|
-
console.log("** Node URL Error **", error);
|
40
|
-
throw error;
|
41
|
-
}
|
42
|
-
const headers = authInput.req.headers;
|
43
|
-
const secretKey = getHeader(headers, "x-secret-key");
|
44
|
-
// prefer clientId that is explicitly passed in
|
45
|
-
let clientId = authInput.clientId ?? null;
|
46
|
-
if (!clientId) {
|
47
|
-
// next preference is clientId from header
|
48
|
-
clientId = getHeader(headers, "x-client-id");
|
49
|
-
}
|
50
|
-
|
51
|
-
// next preference is search param
|
52
|
-
if (!clientId) {
|
53
|
-
clientId = requestUrl.searchParams.get("clientId");
|
54
|
-
}
|
55
|
-
// bundle id from header is first preference
|
56
|
-
let bundleId = getHeader(headers, "x-bundle-id");
|
57
|
-
|
58
|
-
// next preference is search param
|
59
|
-
if (!bundleId) {
|
60
|
-
bundleId = requestUrl.searchParams.get("bundleId");
|
61
|
-
}
|
62
|
-
let origin = getHeader(headers, "origin");
|
63
|
-
// if origin header is not available we'll fall back to referrer;
|
64
|
-
if (!origin) {
|
65
|
-
origin = getHeader(headers, "referer");
|
66
|
-
}
|
67
|
-
// if we have an origin at this point, normalize it
|
68
|
-
if (origin) {
|
69
|
-
try {
|
70
|
-
origin = new URL(origin).host;
|
71
|
-
} catch (e) {
|
72
|
-
console.warn("failed to parse origin", origin, e);
|
73
|
-
}
|
74
|
-
}
|
75
|
-
|
76
|
-
// handle if we a secret key is passed in the headers
|
77
|
-
let secretKeyHash = null;
|
78
|
-
if (secretKey) {
|
79
|
-
// hash the secret key
|
80
|
-
secretKeyHash = hashSecretKey(secretKey);
|
81
|
-
// derive the client id from the secret key hash
|
82
|
-
const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
|
83
|
-
// if we already have a client id passed in we need to make sure they match
|
84
|
-
if (clientId && clientId !== derivedClientId) {
|
85
|
-
throw new Error("KEY_CONFLICT");
|
86
|
-
}
|
87
|
-
// otherwise set the client id to the derived client id (client id based off of secret key)
|
88
|
-
clientId = derivedClientId;
|
89
|
-
}
|
90
|
-
let jwt = null;
|
91
|
-
let useWalletAuth = null;
|
92
|
-
// check for authorization header on the request
|
93
|
-
const authorizationHeader = getHeader(headers, "authorization");
|
94
|
-
if (authorizationHeader) {
|
95
|
-
const [type, token] = authorizationHeader.split(" ");
|
96
|
-
if (type.toLowerCase() === "bearer" && !!token) {
|
97
|
-
jwt = token;
|
98
|
-
const walletAuthHeader = getHeader(headers, "x-authorize-wallet");
|
99
|
-
// IK a stringified boolean is not ideal, but it's required to pass it in the headers.
|
100
|
-
if (walletAuthHeader?.toLowerCase() === "true") {
|
101
|
-
useWalletAuth = walletAuthHeader;
|
102
|
-
}
|
103
|
-
}
|
104
|
-
}
|
105
|
-
return {
|
106
|
-
jwt,
|
107
|
-
hashedJWT: jwt ? hashSecretKey(jwt) : null,
|
108
|
-
secretKeyHash,
|
109
|
-
secretKey,
|
110
|
-
clientId,
|
111
|
-
origin,
|
112
|
-
bundleId,
|
113
|
-
targetAddress: authInput.targetAddress,
|
114
|
-
useWalletAuth
|
115
|
-
};
|
116
|
-
}
|
117
|
-
function hashSecretKey(secretKey) {
|
118
|
-
return createHash("sha256").update(secretKey).digest("hex");
|
119
|
-
}
|
120
|
-
function deriveClientIdFromSecretKeyHash(secretKeyHash) {
|
121
|
-
return secretKeyHash.slice(0, 32);
|
122
|
-
}
|
123
|
-
function logHttpRequest(_ref) {
|
124
|
-
let {
|
125
|
-
source,
|
126
|
-
clientId,
|
127
|
-
req,
|
128
|
-
res,
|
129
|
-
isAuthed,
|
130
|
-
statusMessage
|
131
|
-
} = _ref;
|
132
|
-
const authorizationData = extractAuthorizationData({
|
133
|
-
req,
|
134
|
-
clientId
|
135
|
-
});
|
136
|
-
const headers = req.headers;
|
137
|
-
const _statusMessage = statusMessage ?? res.statusMessage;
|
138
|
-
console.log(JSON.stringify({
|
139
|
-
source,
|
140
|
-
pathname: req.url,
|
141
|
-
hasSecretKey: !!authorizationData.secretKey,
|
142
|
-
hasClientId: !!authorizationData.clientId,
|
143
|
-
hasJwt: !!authorizationData.jwt,
|
144
|
-
clientId: authorizationData.clientId,
|
145
|
-
isAuthed: !!isAuthed ?? null,
|
146
|
-
status: res.statusCode,
|
147
|
-
statusMessage: _statusMessage,
|
148
|
-
sdkName: headers["x-sdk-name"] ?? "unknown",
|
149
|
-
sdkVersion: headers["x-sdk-version"] ?? "unknown",
|
150
|
-
platform: headers["x-sdk-platform"] ?? "unknown"
|
151
|
-
}));
|
152
|
-
console.log(`statusMessage=${_statusMessage}`);
|
153
|
-
}
|
154
|
-
|
155
|
-
export { authorizeNode, deriveClientIdFromSecretKeyHash, extractAuthorizationData, hashSecretKey, logHttpRequest };
|
package/node/package.json
DELETED
File without changes
|