Skip to content

Commit 47d505a

Browse files
Authentication authorization (#42)
* Update server.ts * Clean up the code to be more modular * Authorization and Authentication * Login Page * Encryption and decryption schema Will update the code later to have individaul private keys for each researcher, but for now, we are the only researcher so one private key should satisfy. * Encryption * Update views/login.pug Co-authored-by: Mark Whiting <[email protected]> * Token exists middleware && made encryption not depend on query * CSURF protection added * Csurf retry. * Needed tp add csrf module to every router * Remove verify admin token on encrypt Co-authored-by: Mark Whiting <[email protected]>
1 parent 03b6ad2 commit 47d505a

File tree

15 files changed

+4846
-4278
lines changed

15 files changed

+4846
-4278
lines changed

@types/express/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as express from "express"
2+
declare global {
3+
namespace Express {
4+
interface Request {
5+
user? : Record<any, any>
6+
}
7+
}
8+
}

config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Database_Wrapper } from "./interfaces";
2+
import Nedb from "./databases/test_db";
3+
import Mongo from "./databases/prod_db";
4+
import exp = require("constants");
5+
6+
7+
8+
const env_config = {
9+
PORT: parseInt(process.env.PORT),
10+
URI: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_URI : process.env.TEST_URI,
11+
DB: process.env.PROD.toLowerCase() == "true" ? process.env.PROD_DB : process.env.TEST_DB,
12+
RANDOM: process.env.RANDOM,
13+
TOKEN_KEY: process.env.TOKEN_KEY,
14+
SECRET_KEY: process.env.SECRET_KEY,
15+
ENCRYPT_KEY: process.env.ENCRYPT_KEY,
16+
IV_KEY: process.env.IV_KEY,
17+
DOMAIN: process.env.DOMAIN
18+
}
19+
20+
if (env_config.URI === undefined || env_config.DB === undefined) {
21+
throw new Error("Please set mongo db uri or db")
22+
}
23+
24+
const Db_Wrapper: Database_Wrapper = new Mongo(env_config);
25+
Db_Wrapper.set_db(env_config.DB);
26+
27+
export {Db_Wrapper, env_config}

interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ export interface Database_Wrapper {
66
update(filter: any, updateDoc: any, options: any, collection: string);
77
find(query: any, collection: string);
88
export(query: any, collection: string);
9-
}
9+
}

middlewares/auth.middleware.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {Db_Wrapper, env_config} from "../config"
2+
const jwt = require('jsonwebtoken');
3+
4+
export const verifyToken = (req, res, next) => {
5+
const token = req.session.token
6+
7+
if (token === undefined) {
8+
return res.status(403).redirect("/login/researcher")
9+
}
10+
11+
jwt.verify(token, env_config.TOKEN_KEY, (err, user) => {
12+
if (err) {
13+
return res.status(404).send("token is not verified")
14+
} else {
15+
req.user = user;
16+
next()
17+
}
18+
})
19+
20+
}
21+
22+
export const existsToken = (req, res, next) => {
23+
// if token exists, it passes non sensitive user info
24+
const token = req.session.token
25+
26+
if (token === undefined) {
27+
next()
28+
return
29+
}
30+
31+
jwt.verify(token, env_config.TOKEN_KEY, (err, user) => {
32+
if (err) {
33+
next()
34+
return
35+
} else {
36+
req.user = user;
37+
next()
38+
}
39+
})
40+
41+
}
42+
43+
export const verifyAdminToken = (req, res, next) => {
44+
const token = req.session.token
45+
46+
if (token === undefined) {
47+
return res.status(403).redirect("/login/admin")
48+
}
49+
50+
jwt.verify(token, env_config.TOKEN_KEY, (err, user) => {
51+
if (err) {
52+
return res.status(404).send("token is not verified")
53+
} else if (!user.admin) {
54+
return res.status(403).send("USER IS NOT ADMIN")
55+
} else {
56+
req.user = user;
57+
next()
58+
}
59+
})
60+
61+
62+
}

0 commit comments

Comments
 (0)