Node.js Techniques - Role Based Authorization at the API level

Search for a command to run...

No comments yet. Be the first to comment.
Automating the boring stuff in job applications, while keeping the final decision in your hands.

Automating the boring stuffs ...

Managing S3 Case Sensitivity in Python Workflows

π©οΈ Cloud computing has revolutionized how applications are developed and deployed, providing a more flexible and scalable environment for running workloads. AWS offers several services for event-driven computing, including Amazon CloudWatch Events a...

Introductionπ Looking to streamline your complex workflows and keep everything on track? Look no further than state machines - a powerful tool for precision and ease in process management. πͺπΌ And when it comes to implementing state machines, AWS S...

Peterson Oaikhenah -- Senior Software Engineer | Community leader | Cloud Engineer | Open Source
38 posts
Motivated senior software engineer, open-source advocate & community leader π»β‘οΈ. Sharing valuable insights to help others scale π‘. Building strong communities π. Passionate about promoting tech
APIs are everywhere and have many different faces and capabilities.
Essentially APIs act as transports vehicles π that pull information from one source and feed it to another. For example, Google maps API used in navigation systems or Stripe API used for Online payments, etc.
APIs in a lot of applications serve as data points for client-based applications π₯οΈ or other API clients, with read-write permissions; and as such certain endpoints need to be protected to provide access to privileged users of the application.
In this tutorial, we'll go through a simple implementation of role-based authorization or access control in a Node.js API with the Express framework. In this example, I'll work with JWT, for token-based authentication in Node.js.
Assumptions : This tutorial assumes the audience has some knowledge of Nodejs and the Express framework
Intall jsonwebtoken - npm into your project directory
npm i jsonwebtoken
Base_dir/helpers/authhelpers
This directory contains helper functions written as independent and reusable bits of code invoked within a router or controller file when needed.
// function to decode token
exports.decoder = (token) => jwt.decode(token);
const mustBeLoggedIn = (req, res, next) => {
let token = req.headers["x-access-token"] || req.headers.authorization || req.body.token;
// Express headers are auto converted to lowercase
if (token && token.startsWith("Bearer ")) {
// Remove Bearer from string
token = token.slice(7, token.length).trimLeft();
}
try {
// we extract the JWTSECRET from the .env file
req.apiUser = jwt.verify(token, process.env.JWTSECRET);
res.locals.apiUser = req.apiUser;
// res.locals is guaranteed to hold state over the life of a request.
next();
} catch (error) {
res.status(401).json({
status: false,
message: "Sorry, you must provide a valid token."
});
}
};
N.B
The JWTSECRET property in the .env is used by the API to sign and verify the JWT token. do update it with your unique random string to restrict unauthorized access to your application.
Base_dir/routes/<routesname>.jsThis middleware is written to intercept a request for a route. the middleware here, checks if the bearer token is sent as header or body information if the token is still valid and if so, sends the token payload such as user_id and role to another method within the request life cycle.
The middleware as used in this snippet is called mustBeLoggedIn
const express = require("express");
const { retrieveAllUsers } = require("../controllers/Controller");
const { mustBeLoggedIn } = require("../helpers/authhelper");
const router = express.Router();
router.get("/", mustBeLoggedIn, retrieveAllUsers);
module.exports = router;
The above routes are protected with a middleware that checks for a bearer token and extracts the payload, coupled with an error handler.
If there are no errors in the payload extracted from the token, we send this payload containing user data to the controller.
Base_dir/controllers/<controllersname>.jsThis controller defines all routes for the API, the route definitions are grouped together at the top of the file and the route implementations are below.
Routes that use the mustBeLoggedIn middleware are restricted to authenticated users, if the role is included (e.g. authorize(Role.Admin or Role.Superuser)) then the route is restricted to users in the specified role/roles.
Otherwise, if the role is not included (e.g. mustBeLoggedIn()) then the route is restricted to all authenticated users regardless of their role.
Thus Routes that don't use the authorize middleware are publicly accessible.
const jwt = require("jsonwebtoken");
const User = require("../models/users");
const retrieveAllUsers = async (req, res) => {
// the payload passed as request state from the middleware
if (res.locals.apiUser.role) {
try {
await User.find({ role: "Admin" }, {
password: 0,
dateCreated: 0,
__v: 0
})
.then((response) => {
res.status(200).json({
status: true,
message: "Succesfully Fetched all User data",
data: response
});
})
.catch((error) => {
res.status(404).json({
status: false,
message: error.message,
data: null,
errors: error
});
});
} catch (error) {
res.status(500).json({
status: false,
message: "Server error, try again later."
});
}
}
};
module.exports = retrieveAllUsers
This is a simple implementation of a role-based Authorization or access control on an API.
I'd love to learn about the awesome projects you build with larger-scale implementations of access control for application APIs π.
Thanks for the audience and I hope you found this article helpful π€. feel free to reach out to Github, Twitter and LinkedIn. Do drop a like, comment, and share π.