This commit is contained in:
Madeline
2023-01-05 17:16:55 +11:00
parent a665a39523
commit df449169bd
124 changed files with 3639 additions and 1697 deletions
+42 -23
View File
@@ -1,13 +1,23 @@
import { Request, Response, Router } from "express";
import { route } from "@fosscord/api";
import { Application, generateToken, User, BotModifySchema, handleFile, DiscordApiErrors } from "@fosscord/util";
import {
Application,
generateToken,
User,
BotModifySchema,
handleFile,
DiscordApiErrors,
} from "@fosscord/util";
import { HTTPError } from "lambert-server";
import { verifyToken } from "node-2fa";
const router: Router = Router();
router.post("/", route({}), async (req: Request, res: Response) => {
const app = await Application.findOneOrFail({ where: { id: req.params.id }, relations: ["owner"] });
const app = await Application.findOneOrFail({
where: { id: req.params.id },
relations: ["owner"],
});
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
@@ -31,7 +41,7 @@ router.post("/", route({}), async (req: Request, res: Response) => {
await app.save();
res.send({
token: await generateToken(user.id)
token: await generateToken(user.id),
}).status(204);
});
@@ -42,7 +52,10 @@ router.post("/reset", route({}), async (req: Request, res: Response) => {
if (owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (owner.totp_secret && (!req.body.code || verifyToken(owner.totp_secret, req.body.code)))
if (
owner.totp_secret &&
(!req.body.code || verifyToken(owner.totp_secret, req.body.code))
)
throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
bot.data = { hash: undefined, valid_tokens_since: new Date() };
@@ -54,30 +67,36 @@ router.post("/reset", route({}), async (req: Request, res: Response) => {
res.json({ token }).status(200);
});
router.patch("/", route({ body: "BotModifySchema" }), async (req: Request, res: Response) => {
const body = req.body as BotModifySchema;
if (!body.avatar?.trim()) delete body.avatar;
router.patch(
"/",
route({ body: "BotModifySchema" }),
async (req: Request, res: Response) => {
const body = req.body as BotModifySchema;
if (!body.avatar?.trim()) delete body.avatar;
const app = await Application.findOneOrFail({ where: { id: req.params.id }, relations: ["bot", "owner"] });
const app = await Application.findOneOrFail({
where: { id: req.params.id },
relations: ["bot", "owner"],
});
if (!app.bot)
throw DiscordApiErrors.BOT_ONLY_ENDPOINT;
if (!app.bot) throw DiscordApiErrors.BOT_ONLY_ENDPOINT;
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (body.avatar)
body.avatar = await handleFile(
`/avatars/${app.id}`,
body.avatar as string,
);
if (body.avatar)
body.avatar = await handleFile(
`/avatars/${app.id}`,
body.avatar as string,
);
app.bot.assign(body);
app.bot.assign(body);
app.bot.save();
app.bot.save();
await app.save();
res.json(app).status(200);
});
await app.save();
res.json(app).status(200);
},
);
export default router;
export default router;
+47 -23
View File
@@ -1,57 +1,81 @@
import { Request, Response, Router } from "express";
import { route } from "@fosscord/api";
import { Application, OrmUtils, DiscordApiErrors, ApplicationModifySchema, User } from "@fosscord/util";
import {
Application,
OrmUtils,
DiscordApiErrors,
ApplicationModifySchema,
User,
} from "@fosscord/util";
import { verifyToken } from "node-2fa";
import { HTTPError } from "lambert-server";
const router: Router = Router();
router.get("/", route({}), async (req: Request, res: Response) => {
const app = await Application.findOneOrFail({ where: { id: req.params.id }, relations: ["owner", "bot"] });
const app = await Application.findOneOrFail({
where: { id: req.params.id },
relations: ["owner", "bot"],
});
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
return res.json(app);
});
router.patch("/", route({ body: "ApplicationModifySchema" }), async (req: Request, res: Response) => {
const body = req.body as ApplicationModifySchema;
router.patch(
"/",
route({ body: "ApplicationModifySchema" }),
async (req: Request, res: Response) => {
const body = req.body as ApplicationModifySchema;
const app = await Application.findOneOrFail({ where: { id: req.params.id }, relations: ["owner", "bot"] });
const app = await Application.findOneOrFail({
where: { id: req.params.id },
relations: ["owner", "bot"],
});
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (app.owner.totp_secret && (!req.body.code || verifyToken(app.owner.totp_secret, req.body.code)))
throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
if (
app.owner.totp_secret &&
(!req.body.code ||
verifyToken(app.owner.totp_secret, req.body.code))
)
throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
if (app.bot) {
app.bot.assign({ bio: body.description });
await app.bot.save();
}
if (app.bot) {
app.bot.assign({ bio: body.description });
await app.bot.save();
}
app.assign(body);
app.assign(body);
await app.save();
await app.save();
return res.json(app);
});
return res.json(app);
},
);
router.post("/delete", route({}), async (req: Request, res: Response) => {
const app = await Application.findOneOrFail({ where: { id: req.params.id }, relations: ["bot", "owner"] });
const app = await Application.findOneOrFail({
where: { id: req.params.id },
relations: ["bot", "owner"],
});
if (app.owner.id != req.user_id)
throw DiscordApiErrors.ACTION_NOT_AUTHORIZED_ON_APPLICATION;
if (app.owner.totp_secret && (!req.body.code || verifyToken(app.owner.totp_secret, req.body.code)))
if (
app.owner.totp_secret &&
(!req.body.code || verifyToken(app.owner.totp_secret, req.body.code))
)
throw new HTTPError(req.t("auth:login.INVALID_TOTP_CODE"), 60008);
if (app.bot)
await User.delete({ id: app.bot.id });
if (app.bot) await User.delete({ id: app.bot.id });
await Application.delete({ id: app.id });
res.send().status(200);
});
export default router;
export default router;
+1 -1
View File
@@ -8,4 +8,4 @@ router.get("/", route({}), async (req: Request, res: Response) => {
res.json([]).status(200);
});
export default router;
export default router;
+29 -17
View File
@@ -1,30 +1,42 @@
import { Request, Response, Router } from "express";
import { route } from "@fosscord/api";
import { Application, ApplicationCreateSchema, trimSpecial, User } from "@fosscord/util";
import {
Application,
ApplicationCreateSchema,
trimSpecial,
User,
} from "@fosscord/util";
const router: Router = Router();
router.get("/", route({}), async (req: Request, res: Response) => {
let results = await Application.find({ where: { owner: { id: req.user_id } }, relations: ["owner", "bot"] });
let results = await Application.find({
where: { owner: { id: req.user_id } },
relations: ["owner", "bot"],
});
res.json(results).status(200);
});
router.post("/", route({ body: "ApplicationCreateSchema" }), async (req: Request, res: Response) => {
const body = req.body as ApplicationCreateSchema;
const user = await User.findOneOrFail({ where: { id: req.user_id } });
router.post(
"/",
route({ body: "ApplicationCreateSchema" }),
async (req: Request, res: Response) => {
const body = req.body as ApplicationCreateSchema;
const user = await User.findOneOrFail({ where: { id: req.user_id } });
const app = Application.create({
name: trimSpecial(body.name),
description: "",
bot_public: true,
owner: user,
verify_key: "IMPLEMENTME",
flags: 0,
});
const app = Application.create({
name: trimSpecial(body.name),
description: "",
bot_public: true,
owner: user,
verify_key: "IMPLEMENTME",
flags: 0,
});
await app.save();
await app.save();
res.json(app);
});
res.json(app);
},
);
export default router;
export default router;