-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (75 loc) · 2.75 KB
/
Copy pathindex.js
File metadata and controls
100 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require("dotenv/config");
const express = require("express");
const { PORT, SHARED_SECRET } = require("./config");
const { approver } = require("./approver");
const app = express();
app.use(express.json());
// Health check endpoint
app.get("/", function (_, res) {
res.json({ online: true });
});
// Secure endpoints with shared secret
app.use("/:sharedSecret", (req, res, next) => {
const { sharedSecret } = req.params;
console.log(`INFO: ${req.method} ${req.originalUrl.replace(/\/[^/]+/, "/[REDACTED]")}`);
if (sharedSecret !== SHARED_SECRET) {
return res.status(401).json({ error: "Unauthorized" });
}
next();
});
// Authorize endpoint
app.post("/:sharedSecret/authorize", async (req, res) => {
const { account, signedBy } = req.body;
if (!account || !signedBy) {
return res.status(400).json({ error: "Missing 'account' or 'signedBy' field" });
}
console.log(`INFO: Wallet ${signedBy} is requesting to authenticate with Account: ${account}.`);
// Example validation logic (replace with your own)
const isAllowed = true; // Set to false if the user is not allowed to login
if (!isAllowed) {
return res.json({ allowed: false, reason: "User not allowed to login" });
}
// Example sponsored logic (replace with your own)
const isSponsored = false; // Set to true if the user is sponsored
// in vercel `protocol` is `http`, but `x-forwarded-proto` is `https`
const protocol = req.headers["x-forwarded-proto"] || req.protocol;
const host = req.get("host");
const fullDomain = `${protocol}://${host}`;
res.json({
allowed: true,
sponsored: isSponsored,
appVerificationEndpoint: `${fullDomain}/${SHARED_SECRET}/verify-operation`,
});
});
// Operation verification endpoint
app.post("/:sharedSecret/verify-operation", async (req, res) => {
const requiredFields = ["deadline", "nonce", "operation", "account", "validator"];
const missingFields = requiredFields.filter((field) => !req.body[field]);
if (missingFields.length > 0) {
return res.status(400).json({
allowed: false,
reason: `The following field(s) are missing: ${missingFields.join(", ")}`,
});
}
console.log(
`INFO: Account ${req.body.account} is requesting to verify ${req.body.operation} operation.`
);
// Validate if the account is authorized to perform the requested operation
const isAllowed = true; // Replace with your validation logic
if (!isAllowed) {
return res.json({
allowed: false,
reason: "Operation not allowed for this account",
});
}
// Sign the operation approval
const signature = await approver.signOperationApproval(req.body);
res.json({
allowed: true,
signature,
});
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});