@tanakayuto/intmax402-express 0.2.2 → 0.2.4
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/middleware.js +51 -62
- package/package.json +20 -3
package/dist/middleware.js
CHANGED
|
@@ -4,78 +4,67 @@ exports.intmax402 = intmax402;
|
|
|
4
4
|
const intmax402_core_1 = require("@tanakayuto/intmax402-core");
|
|
5
5
|
const crypto_1 = require("./crypto");
|
|
6
6
|
const verify_payment_1 = require("./verify-payment");
|
|
7
|
-
function buildWWWAuthenticate(nonce, config) {
|
|
8
|
-
let header = `INTMAX402 realm="intmax402", nonce="${nonce}", mode="${config.mode}"`;
|
|
9
|
-
if (config.serverAddress) {
|
|
10
|
-
header += `, serverAddress="${config.serverAddress}"`;
|
|
11
|
-
}
|
|
12
|
-
if (config.amount) {
|
|
13
|
-
header += `, amount="${config.amount}"`;
|
|
14
|
-
}
|
|
15
|
-
if (config.tokenAddress) {
|
|
16
|
-
header += `, tokenAddress="${config.tokenAddress}"`;
|
|
17
|
-
}
|
|
18
|
-
if (config.chainId) {
|
|
19
|
-
header += `, chainId="${config.chainId}"`;
|
|
20
|
-
}
|
|
21
|
-
return header;
|
|
22
|
-
}
|
|
23
7
|
function intmax402(config) {
|
|
24
8
|
return async (req, res, next) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
const credential = (0, intmax402_core_1.parseAuthorization)(authHeader);
|
|
39
|
-
if (!credential) {
|
|
40
|
-
res.status(401).json({ error: "Invalid authorization header" });
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const ip = req.ip || req.socket.remoteAddress || "unknown";
|
|
44
|
-
if (!(0, intmax402_core_1.verifyNonce)(credential.nonce, config.secret, ip, req.path, config.bindIp ?? false)) {
|
|
45
|
-
res.status(401).json({ error: "Invalid or expired nonce" });
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (config.allowList && config.allowList.length > 0) {
|
|
49
|
-
if (!config.allowList.includes(credential.address.toLowerCase())) {
|
|
50
|
-
res.status(403).json({ error: "Address not in allow list" });
|
|
9
|
+
try {
|
|
10
|
+
const authHeader = req.headers.authorization;
|
|
11
|
+
if (!authHeader) {
|
|
12
|
+
const ip = req.ip || req.socket.remoteAddress || "unknown";
|
|
13
|
+
const nonce = (0, intmax402_core_1.generateNonce)(config.secret, ip, req.path, config.bindIp ?? false);
|
|
14
|
+
const statusCode = config.mode === "payment" ? 402 : 401;
|
|
15
|
+
res.setHeader("WWW-Authenticate", (0, intmax402_core_1.buildWWWAuthenticate)(nonce, config));
|
|
16
|
+
res.status(statusCode).json({
|
|
17
|
+
error: config.mode === "payment" ? "Payment Required" : "Unauthorized",
|
|
18
|
+
protocol: "INTMAX402",
|
|
19
|
+
mode: config.mode,
|
|
20
|
+
});
|
|
51
21
|
return;
|
|
52
22
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
res.status(401).json({ error: "Invalid signature" });
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
if (config.mode === "payment") {
|
|
60
|
-
if (!credential.txHash) {
|
|
61
|
-
res.status(402).json({ error: "Payment transaction hash required" });
|
|
23
|
+
const credential = (0, intmax402_core_1.parseAuthorization)(authHeader);
|
|
24
|
+
if (!credential) {
|
|
25
|
+
res.status(401).json({ error: "Invalid authorization header" });
|
|
62
26
|
return;
|
|
63
27
|
}
|
|
64
|
-
|
|
65
|
-
|
|
28
|
+
const ip = req.ip || req.socket.remoteAddress || "unknown";
|
|
29
|
+
if (!(0, intmax402_core_1.verifyNonce)(credential.nonce, config.secret, ip, req.path, config.bindIp ?? false)) {
|
|
30
|
+
res.status(401).json({ error: "Invalid or expired nonce" });
|
|
66
31
|
return;
|
|
67
32
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
33
|
+
if (config.allowList && config.allowList.length > 0) {
|
|
34
|
+
if (!config.allowList.includes(credential.address.toLowerCase())) {
|
|
35
|
+
res.status(403).json({ error: "Address not in allow list" });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const isValidSig = (0, crypto_1.verifySignature)(credential.signature, credential.nonce, credential.address);
|
|
40
|
+
if (!isValidSig) {
|
|
41
|
+
res.status(401).json({ error: "Invalid signature" });
|
|
71
42
|
return;
|
|
72
43
|
}
|
|
44
|
+
if (config.mode === "payment") {
|
|
45
|
+
if (!credential.txHash) {
|
|
46
|
+
res.status(402).json({ error: "Payment transaction hash required" });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (!config.serverAddress || !config.amount) {
|
|
50
|
+
res.status(500).json({ error: "Server misconfigured: serverAddress and amount required for payment mode" });
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const paymentResult = await (0, verify_payment_1.verifyPayment)(credential.txHash, config.amount, config.serverAddress);
|
|
54
|
+
if (!paymentResult.valid) {
|
|
55
|
+
res.status(402).json({ error: paymentResult.error || "Payment verification failed" });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
req.intmax402 = {
|
|
60
|
+
address: credential.address,
|
|
61
|
+
verified: true,
|
|
62
|
+
txHash: credential.txHash,
|
|
63
|
+
};
|
|
64
|
+
next();
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
next(err);
|
|
73
68
|
}
|
|
74
|
-
req.intmax402 = {
|
|
75
|
-
address: credential.address,
|
|
76
|
-
verified: true,
|
|
77
|
-
txHash: credential.txHash,
|
|
78
|
-
};
|
|
79
|
-
next();
|
|
80
69
|
};
|
|
81
70
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanakayuto/intmax402-express",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./crypto": {
|
|
13
|
+
"import": "./dist/crypto.js",
|
|
14
|
+
"require": "./dist/crypto.js",
|
|
15
|
+
"types": "./dist/crypto.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./verify-payment": {
|
|
18
|
+
"import": "./dist/verify-payment.js",
|
|
19
|
+
"require": "./dist/verify-payment.js",
|
|
20
|
+
"types": "./dist/verify-payment.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
6
23
|
"dependencies": {
|
|
7
|
-
"@tanakayuto/intmax402-core": "0.2.2",
|
|
8
24
|
"ethers": "^6.16.0",
|
|
9
|
-
"intmax2-server-sdk": "^1.5.2"
|
|
25
|
+
"intmax2-server-sdk": "^1.5.2",
|
|
26
|
+
"@tanakayuto/intmax402-core": "0.2.4"
|
|
10
27
|
},
|
|
11
28
|
"peerDependencies": {
|
|
12
29
|
"express": "^4.18.0"
|