Compare commits
4 Commits
c548abc9ed
...
v0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9311cd3c96 | |||
| 89e6832e73 | |||
| 109f22c231 | |||
| 355338e397 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kittyBE",
|
"name": "kittyBE",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"description": "Your go-to place for short and memorable URLs.",
|
"description": "Your go-to place for short and memorable URLs.",
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -15,5 +15,5 @@ export const AppDataSource = new DataSource({
|
|||||||
entities: [__dirname + '/entities/*.ts'],
|
entities: [__dirname + '/entities/*.ts'],
|
||||||
migrations: [__dirname + '/migrations/*.ts'],
|
migrations: [__dirname + '/migrations/*.ts'],
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
parseInt8: true
|
parseInt8: true // https://github.com/typeorm/typeorm/issues/9341#issuecomment-1268986627
|
||||||
})
|
})
|
||||||
|
|||||||
38
src/middleware/requireAdmin.ts
Normal file
38
src/middleware/requireAdmin.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { ErrorDTO } from '../schemas/miscSchema';
|
||||||
|
import * as jwt from '../tools/jwt';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if user has administrative privileges.
|
||||||
|
*
|
||||||
|
* This needs to happen AFTER ensuring this is not a guest session.
|
||||||
|
* So: use requireUser first, and after that requireAdmin to enforce
|
||||||
|
* admin privilege requirement.
|
||||||
|
*
|
||||||
|
* @param {Request} req The request
|
||||||
|
* @param {Response} res The resource
|
||||||
|
* @param {(Function|NextFunction)} next The next
|
||||||
|
* @return {any} Next function on success, unauthorized error otherwise
|
||||||
|
*/
|
||||||
|
const requireAdmin = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const user: jwt.JwtStatus = res.locals.user;
|
||||||
|
let error: ErrorDTO | null = null;
|
||||||
|
|
||||||
|
// Check if role is set to 1 (1 = admin, 0 = standard user).
|
||||||
|
if (user.decoded?.role !== 1)
|
||||||
|
error = {
|
||||||
|
status: 'error',
|
||||||
|
error: 'Unauthorized, admin access required',
|
||||||
|
code: 'unauthorized_non_admin'
|
||||||
|
};
|
||||||
|
|
||||||
|
// It is? Send 401 unauthorized.
|
||||||
|
if (error !== null)
|
||||||
|
return res.status(401)
|
||||||
|
.send(error);
|
||||||
|
|
||||||
|
// Otherwise jump to next endpoint.
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
|
||||||
|
export default requireAdmin;
|
||||||
@@ -1,7 +1,20 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from 'express';
|
||||||
import { ErrorDTO } from "../schemas/miscSchema";
|
import { ErrorDTO } from '../schemas/miscSchema';
|
||||||
import * as jwt from "../tools/jwt";
|
import * as jwt from '../tools/jwt';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if user is singed in.
|
||||||
|
* Returns 401 when user is unauthorized.
|
||||||
|
*
|
||||||
|
* To check if user is an admin, chain requireUser and requireAdmin together.
|
||||||
|
* So: use requireUser first, and after that requireAdmin to enforce
|
||||||
|
* admin privilege requirement.
|
||||||
|
*
|
||||||
|
* @param {Request} req The request
|
||||||
|
* @param {Response} res The resource
|
||||||
|
* @param {(Function|NextFunction)} next The next
|
||||||
|
* @return {any} Next function on success, unauthorized error otherwise
|
||||||
|
*/
|
||||||
const requireUser = (req: Request, res: Response, next: NextFunction) => {
|
const requireUser = (req: Request, res: Response, next: NextFunction) => {
|
||||||
const user: jwt.JwtStatus = res.locals.user;
|
const user: jwt.JwtStatus = res.locals.user;
|
||||||
let error: ErrorDTO | null = null;
|
let error: ErrorDTO | null = null;
|
||||||
@@ -38,4 +51,4 @@ const requireUser = (req: Request, res: Response, next: NextFunction) => {
|
|||||||
return next();
|
return next();
|
||||||
};
|
};
|
||||||
|
|
||||||
export default requireUser;
|
export default requireUser;
|
||||||
|
|||||||
@@ -104,9 +104,11 @@ linkRouter.get('/api/v1/link/fromWordlist', validateSchema(ls.sentenceLinkReques
|
|||||||
* post:
|
* post:
|
||||||
* description:
|
* description:
|
||||||
* Register a new shortened URL. <br/>
|
* Register a new shortened URL. <br/>
|
||||||
* See linkSchema.ts for constraints.
|
* See linkSchema.ts for constraints. <br/>
|
||||||
|
* <b>Note:</b> This endpoint's functionality differs depending on the user info,
|
||||||
|
* which means guests will be treated differently from authenticated users.
|
||||||
* tags: [Link]
|
* tags: [Link]
|
||||||
* summary: Shorten a link
|
* summary: "[AUTHED?] Shorten a link"
|
||||||
* requestBody:
|
* requestBody:
|
||||||
* required: true
|
* required: true
|
||||||
* content:
|
* content:
|
||||||
|
|||||||
Reference in New Issue
Block a user