|
| 1 | +// Requirements for this to work. |
| 2 | +const express = require("express") |
| 3 | +const cors = require("cors") |
| 4 | +const fetch = require("node-fetch") |
| 5 | + |
| 6 | +// Creates the app. |
| 7 | +const app = express() |
| 8 | + |
| 9 | +// Adds CORS to the app. |
| 10 | +app.use(cors()) |
| 11 | + |
| 12 | +// A list of tokens which are allowed. |
| 13 | +const tokens = {} |
| 14 | + |
| 15 | +// Handles the auth in the mock server. |
| 16 | +app.get("/uploaders_api/v1/auth/swap/:uploader", async(req, res) => { |
| 17 | + const uploader = req.params.uploader |
| 18 | + |
| 19 | + const fetchRes = await fetch(`https://api.magiccap.me/swap_tokens/create/${encodeURIComponent(uploader)}`) |
| 20 | + const json = await fetchRes.json() |
| 21 | + if (!fetchRes.ok) { |
| 22 | + res.status(fetchRes.status) |
| 23 | + console.log(`FAIL: ${JSON.stringify(json)}`) |
| 24 | + res.json(json) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + tokens[json.client_token] = json.expires |
| 29 | + console.log(`OK: ${JSON.stringify(json)}`) |
| 30 | + delete json.client_token |
| 31 | + res.json(json) |
| 32 | +}) |
| 33 | + |
| 34 | +// Middleware to handle swap auth. |
| 35 | +const authMiddleware = (req, res, next) => { |
| 36 | + const forbidden = () => { |
| 37 | + res.status(403) |
| 38 | + res.json({ |
| 39 | + success: false, |
| 40 | + message: "Forbidden.", |
| 41 | + }) |
| 42 | + console.log("Auth middleware fail.") |
| 43 | + } |
| 44 | + |
| 45 | + const authorization = req.headers.authorization |
| 46 | + if (!authorization) return forbidden() |
| 47 | + |
| 48 | + const authSplit = authorization.split(/ /) |
| 49 | + if (authSplit.length !== 2) return forbidden() |
| 50 | + |
| 51 | + const bearer = authSplit[0].toLowerCase() |
| 52 | + if (bearer !== "bearer") return forbidden() |
| 53 | + |
| 54 | + const token = authSplit[1] |
| 55 | + const row = tokens[token] |
| 56 | + if (!row || Math.floor(Date.now() / 1000) > row) return forbidden() |
| 57 | + |
| 58 | + req.token = token |
| 59 | + req.uploaderSlug = row.uploader |
| 60 | + |
| 61 | + next() |
| 62 | + console.log("Auth middleware complete.") |
| 63 | +} |
| 64 | + |
| 65 | +// Handles token revokes. |
| 66 | +app.get("/uploaders_api/v1/auth/revoke", [authMiddleware], (req, res) => { |
| 67 | + const token = req.token |
| 68 | + delete tokens[token] |
| 69 | + res.json({ |
| 70 | + success: true, |
| 71 | + }) |
| 72 | + console.log("Token revoked.") |
| 73 | +}) |
| 74 | + |
| 75 | +// Defines if the uploader is default. |
| 76 | +let defaultUploader = true |
| 77 | + |
| 78 | +// Allows for a uploader to check if it is default. |
| 79 | +app.get("/uploaders_api/v1/uploaders/default_check", [authMiddleware], (_, res) => { |
| 80 | + res.json({ |
| 81 | + success: true, |
| 82 | + default: defaultUploader, |
| 83 | + }) |
| 84 | + console.log(`Default check ran. Returned ${defaultUploader}.`) |
| 85 | +}) |
| 86 | + |
| 87 | +// Toggles the default uploader boolean. |
| 88 | +const uploaderPrompt = () => defaultUploader = !defaultUploader |
| 89 | + |
| 90 | +// Allows for a uploader to prompt to be default. |
| 91 | +app.get("/uploaders_api/v1/uploaders/default_prompt", [authMiddleware], (_, res) => { |
| 92 | + res.json({ |
| 93 | + success: true, |
| 94 | + }) |
| 95 | + |
| 96 | + uploaderPrompt() |
| 97 | + console.log("Prompt ran. Uploader toggled as default.") |
| 98 | +}) |
| 99 | + |
| 100 | +// Allows for the write-only editing of uploaders. |
| 101 | +app.get("/uploaders_api/v1/uploaders/set", [authMiddleware], (req, res) => { |
| 102 | + const config = {} |
| 103 | + |
| 104 | + const query = req.query |
| 105 | + for (const queryPart of Object.keys(query)) { |
| 106 | + let jsonParse |
| 107 | + try { |
| 108 | + jsonParse = JSON.parse(query[queryPart]) |
| 109 | + } catch (_) { |
| 110 | + res.status(400) |
| 111 | + res.json({ |
| 112 | + success: false, |
| 113 | + message: "Failed to JSON parse a part of your configuration.", |
| 114 | + }) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + config[queryPart] = jsonParse |
| 119 | + } |
| 120 | + |
| 121 | + res.json({ |
| 122 | + success: true, |
| 123 | + }) |
| 124 | + console.log(`This would add ${JSON.stringify(config)} to your configuration.`) |
| 125 | +}) |
| 126 | + |
| 127 | +// Starts the app. |
| 128 | +app.listen(61222, () => console.log("Listening on port 61222.")) |
0 commit comments