1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-23 10:52:30 +01:00

Merge branch 'fix/nicknames' into slowcord

This commit is contained in:
Madeline 2022-07-03 01:35:11 +10:00
commit e6ffc7c13c
4 changed files with 135 additions and 0 deletions

View File

@ -7213,6 +7213,9 @@
"items": {
"type": "string"
}
},
"nick": {
"type": "string"
}
},
"additionalProperties": false,

View File

@ -7,6 +7,7 @@ const router = Router();
export interface MemberChangeSchema {
roles?: string[];
nick?: string;
}
router.get("/", route({}), async (req: Request, res: Response) => {
@ -34,6 +35,8 @@ router.patch("/", route({ body: "MemberChangeSchema" }), async (req: Request, re
member.roles = body.roles.map((x) => new Role({ id: x })); // foreign key constraint will fail if role doesn't exist
}
if (body.nick) member.nick = body.nick;
await member.save();
member.roles = member.roles.filter((x) => x.id !== everyone.id);

128
slowcord/build/index.js Normal file
View File

@ -0,0 +1,128 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var _a;
import "dotenv/config";
import express from "express";
import cookieParser from "cookie-parser";
import { initDatabase, generateToken, User, Config } from "@fosscord/util";
import path from "path";
import fetch from "node-fetch";
// apparently dirname doesn't exist in modules, nice
/* https://stackoverflow.com/a/62892482 */
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cookieParser());
const port = process.env.PORT;
// ip -> unix epoch that requests will be accepted again
const rateLimits = {};
const allowRequestsEveryMs = 0.5 * 1000; // every half second
const allowedRequestsPerSecond = 50;
let requestsThisSecond = 0;
setInterval(() => {
requestsThisSecond = 0;
}, 1000);
class Discord {
}
_a = Discord;
Discord.getAccessToken = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { code } = req.query;
const body = new URLSearchParams(Object.entries({
client_id: process.env.DISCORD_CLIENT_ID,
client_secret: process.env.DISCORD_SECRET,
redirect_uri: process.env.DISCORD_REDIRECT,
code: code,
grant_type: "authorization_code",
})).toString();
const resp = yield fetch("https://discord.com/api/oauth2/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: body
});
const json = yield resp.json();
if (json.error)
return null;
return {
access_token: json.access_token,
token_type: json.token_type,
expires_in: json.expires_in,
refresh_token: json.refresh_token,
scope: json.scope,
};
});
Discord.getUserDetails = (token) => __awaiter(void 0, void 0, void 0, function* () {
const resp = yield fetch("https://discord.com/api/users/@me", {
headers: {
"Authorization": `Bearer ${token}`,
}
});
const json = yield resp.json();
if (!json.username || !json.email)
return null; // eh, deal with bad code later
return {
id: json.id,
email: json.email,
username: json.username,
};
});
const handlers = {
"discord": Discord,
};
app.get("/oauth/:type", (req, res) => __awaiter(void 0, void 0, void 0, function* () {
requestsThisSecond++;
if (requestsThisSecond > allowedRequestsPerSecond)
return res.sendStatus(429);
const ip = req.headers["x-forwarded-for"] || req.socket.remoteAddress;
console.log(`${ip}`);
if (!rateLimits[ip]) {
rateLimits[ip] = Date.now() + allowRequestsEveryMs;
}
else if (rateLimits[ip] > Date.now()) {
rateLimits[ip] += allowRequestsEveryMs;
console.log(`${new Date()} : user ${ip} was timed out for ${(rateLimits[ip] - Date.now()) / 1000}s`);
return res.sendStatus(429);
}
else {
delete rateLimits[ip];
}
const { type } = req.params;
const handler = handlers[type];
if (!type || !handler)
return res.sendStatus(400);
const data = yield handler.getAccessToken(req, res);
if (!data)
return res.sendStatus(500);
const details = yield handler.getUserDetails(data.access_token);
if (!details)
return res.sendStatus(500);
let user = yield User.findOne({ where: { email: details.email } });
if (!user) {
user = yield User.register({
email: details.email,
username: details.username,
req
});
}
const token = yield generateToken(user.id);
res.cookie("token", token);
res.sendFile(path.join(__dirname, "../public/login.html"));
}));
app.use(express.static("public", { extensions: ["html"] }));
(() => __awaiter(void 0, void 0, void 0, function* () {
yield initDatabase();
yield Config.init();
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
}))();
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,eAAe,CAAC;AACvB,OAAO,OAA8B,MAAM,SAAS,CAAC;AACrD,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC3E,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,YAAY,CAAC;AAE/B,oDAAoD;AACpD,0CAA0C;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;AAE3C,MAAM,GAAG,GAAG,OAAO,EAAE,CAAC;AACtB,GAAG,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC;AACxB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;AAE9B,wDAAwD;AACxD,MAAM,UAAU,GAA8B,EAAE,CAAC;AACjD,MAAM,oBAAoB,GAAG,GAAG,GAAG,IAAI,CAAC,CAAC,oBAAoB;AAE7D,MAAM,wBAAwB,GAAG,EAAE,CAAC;AACpC,IAAI,kBAAkB,GAAG,CAAC,CAAC;AAC3B,WAAW,CAAC,GAAG,EAAE;IAChB,kBAAkB,GAAG,CAAC,CAAC;AACxB,CAAC,EAAE,IAAI,CAAC,CAAC;AAET,MAAM,OAAO;;;A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