forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpair.js
More file actions
65 lines (58 loc) 路 2.14 KB
/
Copy pathpair.js
File metadata and controls
65 lines (58 loc) 路 2.14 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
export async function onRequestPost(context) {
const { env, request } = context;
if (!env.MISAKANET_KV) {
return new Response(JSON.stringify({ error: "KV not configured" }), {
status: 503,
headers: { "Content-Type": "application/json" },
});
}
let pairBody;
try {
pairBody = await request.json();
} catch {
return new Response(JSON.stringify({ error: "Invalid JSON" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
const code = String(pairBody.code || "").replace(/[^A-Z0-9]/g, "").slice(0, 10);
if (!code || code.length !== 6) {
return new Response(JSON.stringify({ error: "Invalid code format" }), {
status: 400,
headers: { "Content-Type": "application/json" },
});
}
const pairKey = `pair:${code}`;
const pairData = await env.MISAKANET_KV.get(pairKey, "json");
if (!pairData) {
return new Response(JSON.stringify({ error: "Invalid or expired code" }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
if (pairData.status !== "pending") {
return new Response(JSON.stringify({ error: "Code already used" }), {
status: 409,
headers: { "Content-Type": "application/json" },
});
}
// Mark code as used
pairData.status = "used";
pairData.used_at = new Date().toISOString();
pairData.used_ip = request.headers.get("CF-Connecting-IP") || "unknown";
await env.MISAKANET_KV.put(pairKey, JSON.stringify(pairData), { expirationTtl: 86400 });
// Generate short-lived token (24h)
const tokenChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
let token = "mcp_";
for (let i = 0; i < 32; i++) token += tokenChars[Math.floor(Math.random() * tokenChars.length)];
// Store token in KV for validation
await env.MISAKANET_KV.put(`mcp_token:${token}`, JSON.stringify({
created: new Date().toISOString(),
expires: new Date(Date.now() + 86400000).toISOString(),
ip: pairData.ip,
}), { expirationTtl: 86400 });
return new Response(JSON.stringify({ token, expires_in: 86400 }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}