First version of OpenAuth remake
This commit is contained in:
88
src/api/admin/client.ts
Normal file
88
src/api/admin/client.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import { Router, Request } from "express";
|
||||
import { GetUserMiddleware } from "../middlewares/user";
|
||||
import RequestError, { HttpStatusCode } from "../../helper/request_error";
|
||||
import promiseMiddleware from "../../helper/promiseMiddleware";
|
||||
import Client from "../../models/client";
|
||||
import User from "../../models/user";
|
||||
import verify, { Types } from "../middlewares/verify";
|
||||
import { randomBytes } from "crypto";
|
||||
|
||||
|
||||
const ClientRouter: Router = Router();
|
||||
ClientRouter.use(GetUserMiddleware(true, true), (req: Request, res, next) => {
|
||||
if (!req.isAdmin) res.sendStatus(HttpStatusCode.FORBIDDEN)
|
||||
else next()
|
||||
});
|
||||
ClientRouter.route("/")
|
||||
.get(promiseMiddleware(async (req, res) => {
|
||||
let clients = await Client.find({});
|
||||
//ToDo check if user is required!
|
||||
res.json(clients);
|
||||
}))
|
||||
.delete(promiseMiddleware(async (req, res) => {
|
||||
let { id } = req.query;
|
||||
await Client.delete(id);
|
||||
res.json({ success: true });
|
||||
}))
|
||||
.post(verify({
|
||||
internal: {
|
||||
type: Types.BOOLEAN,
|
||||
optional: true
|
||||
},
|
||||
name: {
|
||||
type: Types.STRING
|
||||
},
|
||||
redirect_url: {
|
||||
type: Types.STRING
|
||||
},
|
||||
website: {
|
||||
type: Types.STRING
|
||||
},
|
||||
logo: {
|
||||
type: Types.STRING,
|
||||
optional: true
|
||||
}
|
||||
}, true), promiseMiddleware(async (req, res) => {
|
||||
req.body.client_secret = randomBytes(32).toString("hex");
|
||||
let client = Client.new(req.body);
|
||||
client.maintainer = req.user._id;
|
||||
await Client.save(client)
|
||||
res.json(client);
|
||||
}))
|
||||
.put(verify({
|
||||
id: {
|
||||
type: Types.STRING,
|
||||
query: true
|
||||
},
|
||||
internal: {
|
||||
type: Types.BOOLEAN,
|
||||
optional: true
|
||||
},
|
||||
name: {
|
||||
type: Types.STRING,
|
||||
optional: true
|
||||
},
|
||||
redirect_url: {
|
||||
type: Types.STRING,
|
||||
optional: true
|
||||
},
|
||||
website: {
|
||||
type: Types.STRING,
|
||||
optional: true
|
||||
},
|
||||
logo: {
|
||||
type: Types.STRING,
|
||||
optional: true
|
||||
}
|
||||
}, true), promiseMiddleware(async (req, res) => {
|
||||
let { id } = req.query;
|
||||
let client = await Client.findById(id);
|
||||
if (!client) throw new RequestError(req.__("Client not found"), HttpStatusCode.BAD_REQUEST);
|
||||
for (let key in req.body) {
|
||||
client[key] = req.body[key];
|
||||
}
|
||||
await Client.save(client);
|
||||
res.json(client);
|
||||
}))
|
||||
|
||||
export default ClientRouter;
|
Reference in New Issue
Block a user