Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

200-Added-authorization.js-and-authentication.js-for-access-management #402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
env:
TOKEN_SECRET: ${{ secrets.TOKEN_SECRET }}
DB_URL: ${{ secrets.DB_URL }}
ENVIRONMENT: ${{ secrets.ENVIRONMENT }}

permissions:
contents: read
Expand Down
2 changes: 1 addition & 1 deletion _apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1593,4 +1593,4 @@
* @apiSuccess {String[]} faculty.designation Faculty member's designation.
* @apiSuccess {String} faculty.natureOfAssociation Nature of association with the institution.
* @apiSuccess {String} faculty.additionalResponsibilities Additional responsibilities of the faculty.
**/
*/
45 changes: 28 additions & 17 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@ import jwt from "jsonwebtoken";
import util from "#util";

async function authenticateToken(req, res, next) {
const authHeader = req.headers.authorization;
const token = authHeader && authHeader.split(" ")[1];
if (token == null) return res.sendStatus(401);
try {
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
const decryptedIP = util.decrypt(payload.ip);
if (decryptedIP !== req.ip) {
if (process.env.ENVIRONMENT === "local") {
return next();
}
const authHeader = req.headers.authorization || req.headers.Authorization;
// Inside header when we are going to provide the value for key authentication we have
// to start it with 'Bearer acesstoken'
if (authHeader && authHeader.startsWith("Bearer")) {
const token = authHeader.split(" ")[1];
if (token == null) return res.sendStatus(401);
try {
const payload = jwt.verify(token, process.env.TOKEN_SECRET);
const decryptedIP = util.decrypt(payload.ip);
if (decryptedIP !== req.ip) {
res.status(403);
res.send({ err: "Unauthorized" });
}

req.user = payload.data;
next();
return true;
} catch (error) {
res.status(403);
res.send({ err: "Unauthorized" });
return false;
}

req.user = payload.data;
next();
return true;
} catch (error) {
res.status(403);
res.send({ err: "Unauthorized" });
return false;
} else {
res.json({
msg: "Kindly login",
});
}
return null;
}

export default { authenticateToken };
export default authenticateToken;
14 changes: 14 additions & 0 deletions middleware/authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function authorization(access = []) {
return (req, res, next) => {
// remove this in production
if (process.env.ENVIRONMENT === "local") {
return next();
}
if (!req.user) return res.json({ msg: "kindly login first" });
if (!access.includes(req.user.type))
return res.json({ msg: "Unauthorized request" });
return next();
};
}

export default authorization;
23 changes: 17 additions & 6 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ const userSchema = {
emailId: { type: String, unique: true, required: true },
password: { type: String, required: true },
uid: { type: String, unique: true, required: true },
userType: { type: String, required: true },
userType: {
type: String,
required: true,
enum: ["ADMIN", "FACULTY", "EMPLOYEE", "STUDENT"],
default: "ADMIN",
// for now we are keeping the default usertype as ADMIN
},
};

const User = connector.model("User", userSchema);
Expand All @@ -18,9 +24,7 @@ async function remove(filter) {
}

async function create(userData) {
const {
name, password, emailId, uid, userType,
} = userData;
const { name, password, emailId, uid, userType } = userData;
const hashedPassword = await hashPassword(password);
const user = new User({
name,
Expand All @@ -39,10 +43,17 @@ async function read(filter, limit = 1) {
}

async function update(filter, updateObject, options = { multi: true }) {
const updateResult = await User.updateMany(filter, { $set: updateObject }, options);
const updateResult = await User.updateMany(
filter,
{ $set: updateObject },
options,
);
return updateResult.acknowledged;
}

export default {
create, read, update, remove,
create,
read,
update,
remove,
};
16 changes: 14 additions & 2 deletions routes/accreditation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import accreditationController from "#controller/accreditation";

const router = express.Router();
router.get("/list", accreditationController.showAccreditation);
router.post("/add", accreditationController.addAccreditation);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN"]),
accreditationController.showAccreditation,
);
router.post(
"/add",
authenticateToken,
authorization(["ADMIN"]),
accreditationController.addAccreditation,
);
router.delete("/delete/:id", accreditationController.deleteAccreditation);
router.post("/update/:id", accreditationController.updateAccreditation);

Expand Down
34 changes: 28 additions & 6 deletions routes/activity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import activityController from "#controller/activity";

const router=express.Router();
router.post("/add",activityController.addActivity);
router.get("/list",activityController.getActivity);
router.post("/update/:id",activityController.updateActivity);
router.delete("/delete/:id",activityController.deleteActivity);
const router = express.Router();
router.post(
"/add",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
activityController.addActivity,
);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
activityController.getActivity,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
activityController.updateActivity,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
activityController.deleteActivity,
);

export default router;
export default router;
30 changes: 26 additions & 4 deletions routes/assignment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import express from "express";
import assingmentController from "#controller/assignment";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";

const router = express.Router();
router.post("/add", assingmentController.addAssignment);
router.get("/list", assingmentController.getAssignment);
router.post("/update/:id", assingmentController.updateAssignment);
router.delete("/delete/:id", assingmentController.deleteAssignment);
router.post(
"/add",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
assingmentController.addAssignment,
);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
assingmentController.getAssignment,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
assingmentController.updateAssignment,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
assingmentController.deleteAssignment,
);

export default router;
30 changes: 26 additions & 4 deletions routes/coursework.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import courseworkController from "#controller/coursework";

const router = express.Router();
router.post("/add", courseworkController.addCoursework);
router.get("/list", courseworkController.getCoursework);
router.post("/update/:id", courseworkController.updateCoursework);
router.delete("/delete/:id", courseworkController.deleteCoursework);
router.post(
"/add",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
courseworkController.addCoursework,
);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
courseworkController.getCoursework,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
courseworkController.updateCoursework,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
courseworkController.deleteCoursework,
);

export default router;
30 changes: 26 additions & 4 deletions routes/department.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import departmentContoller from "#controller/department";

const router = express.Router();

router.get("/list", departmentContoller.showdepartments);
router.post("/create", departmentContoller.addDepartment);
router.delete("/delete/:id", departmentContoller.removedepartmentbyid);
router.post("/update/:id", departmentContoller.updatedDepartment);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN"]),
departmentContoller.showdepartments,
);
router.post(
"/create",
authenticateToken,
authorization(["ADMIN"]),
departmentContoller.addDepartment,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN"]),
departmentContoller.removedepartmentbyid,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN"]),
departmentContoller.updatedDepartment,
);

export default router;
30 changes: 26 additions & 4 deletions routes/faculty.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import facultyController from "#controller/faculty";

const router = express.Router();
router.post("/create", facultyController.addFaculty);
router.get("/list", facultyController.getFaculty);
router.post("/update/:id", facultyController.updateFaculty);
router.delete("/delete/:id", facultyController.deleteFaculty);
router.post(
"/create",
authenticateToken,
authorization(["ADMIN"]),
facultyController.addFaculty,
);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN"]),
facultyController.getFaculty,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN"]),
facultyController.updateFaculty,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN"]),
facultyController.deleteFaculty,
);

export default router;
30 changes: 26 additions & 4 deletions routes/group.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import express from "express";
import authenticateToken from "#middleware/auth";
import authorization from "#middleware/authorization";
import groupController from "#controller/group";

const router = express.Router();
router.post("/add", groupController.addGroup);
router.get("/list", groupController.getGroup);
router.post("/update/:id", groupController.updateGroup);
router.delete("/delete/:id", groupController.deleteGroup);
router.post(
"/add",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
groupController.addGroup,
);
router.get(
"/list",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
groupController.getGroup,
);
router.post(
"/update/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
groupController.updateGroup,
);
router.delete(
"/delete/:id",
authenticateToken,
authorization(["ADMIN", "FACULTY"]),
groupController.deleteGroup,
);

export default router;
Loading