-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (104 loc) · 3.39 KB
/
index.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Requirements for this to work.
const express = require("express")
const cors = require("cors")
const fetch = require("node-fetch")
// Creates the app.
const app = express()
// Adds CORS to the app.
app.use(cors())
// A list of tokens which are allowed.
const tokens = {}
// Handles the auth in the mock server.
app.get("/uploaders_api/v1/auth/swap/:uploader", async(req, res) => {
const uploader = req.params.uploader
const fetchRes = await fetch(`https://api.magiccap.me/swap_tokens/create/${encodeURIComponent(uploader)}`)
const json = await fetchRes.json()
if (!fetchRes.ok) {
res.status(fetchRes.status)
console.log(`FAIL: ${JSON.stringify(json)}`)
res.json(json)
return
}
tokens[json.client_token] = json.expires
console.log(`OK: ${JSON.stringify(json)}`)
delete json.client_token
res.json(json)
})
// Middleware to handle swap auth.
const authMiddleware = (req, res, next) => {
const forbidden = () => {
res.status(403)
res.json({
success: false,
message: "Forbidden.",
})
console.log("Auth middleware fail.")
}
const authorization = req.headers.authorization
if (!authorization) return forbidden()
const authSplit = authorization.split(/ /)
if (authSplit.length !== 2) return forbidden()
const bearer = authSplit[0].toLowerCase()
if (bearer !== "bearer") return forbidden()
const token = authSplit[1]
const row = tokens[token]
if (!row || Math.floor(Date.now() / 1000) > row) return forbidden()
req.token = token
req.uploaderSlug = row.uploader
next()
console.log("Auth middleware complete.")
}
// Handles token revokes.
app.get("/uploaders_api/v1/auth/revoke", [authMiddleware], (req, res) => {
const token = req.token
delete tokens[token]
res.json({
success: true,
})
console.log("Token revoked.")
})
// Defines if the uploader is default.
let defaultUploader = true
// Allows for a uploader to check if it is default.
app.get("/uploaders_api/v1/uploaders/default_check", [authMiddleware], (_, res) => {
res.json({
success: true,
default: defaultUploader,
})
console.log(`Default check ran. Returned ${defaultUploader}.`)
})
// Toggles the default uploader boolean.
const uploaderPrompt = () => defaultUploader = !defaultUploader
// Allows for a uploader to prompt to be default.
app.get("/uploaders_api/v1/uploaders/default_prompt", [authMiddleware], (_, res) => {
res.json({
success: true,
})
uploaderPrompt()
console.log("Prompt ran. Uploader toggled as default.")
})
// Allows for the write-only editing of uploaders.
app.get("/uploaders_api/v1/uploaders/set", [authMiddleware], (req, res) => {
const config = {}
const query = req.query
for (const queryPart of Object.keys(query)) {
let jsonParse
try {
jsonParse = JSON.parse(query[queryPart])
} catch (_) {
res.status(400)
res.json({
success: false,
message: "Failed to JSON parse a part of your configuration.",
})
return
}
config[queryPart] = jsonParse
}
res.json({
success: true,
})
console.log(`This would add ${JSON.stringify(config)} to your configuration.`)
})
// Starts the app.
app.listen(61222, () => console.log("Listening on port 61222."))