Easy way to put user role in JWT

Published by Fernando Doglio on October 29, 2024
Easy way to put user role in JWT

Embedding user roles in JWTs (JSON Web Tokens) can be a game-changer for managing access control in your applications. Think of JWTs as a backstage pass to your app's resources, where each pass has different access levels based on the user's role.

Including user roles directly in the JWT makes it easier to implement role-based access control (RBAC) without having to constantly query the database. This guide will walk you through the easy way to include user roles in JWTs, helping you streamline authentication and authorization processes.

If you're new to JWTs, check out this beginner’s guide to JWTs to get up to speed.

Warning: Use the technique described in this article with care. If you end up abusing this method, you'll fall into the bloated JWT anti-pattern.

Understanding JWTs and user roles

JWTs are compact, URL-safe tokens that are commonly used for securely transmitting information between parties. They consist of three parts: Header, Payload, and Signature. The payload is where the actual data, known as "claims," is stored. These claims can include user information, roles, and other metadata that is necessary for your application's authorization logic.

Claims - Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private. In this context, the user role is a private claim because it's specific to your application's requirements.

User roles as claims - Embedding user roles as claims in a JWT allows you to quickly determine the user's permissions without needing to query the database on every request. This makes your application more efficient and scalable.

Embedding user roles directly into JWTs simplifies the process of implementing RBAC, ensuring that users only have access to resources and actions appropriate for their role. For a more in-depth look at JWTs and how they work, you can refer to this detailed explanation.

Why include user roles in JWTs?

Including user roles in JWTs has several advantages.

  1. Simplifies authorization logic - By including user roles directly in the JWT, you can easily check the user's role during the authorization process. This simplifies your codebase and reduces the number of database lookups needed to determine user permissions.
  2. Performance benefits - Since the JWT is self-contained and includes all necessary user information, it reduces the need to query the database for user roles on every request. This leads to better performance, especially in high-traffic applications.
  3. Stateless authentication - JWTs enable stateless authentication, meaning the server doesn't need to maintain a session for each user. All the information needed to verify the user and their roles is contained within the token itself.

Use cases where embedding roles in JWTs is essential include applications with complex RBAC requirements, APIs that need to handle a large number of requests, and microservices architectures where centralized authentication is beneficial. To understand more about role-based access control and its importance, you might want to read this guide on RBAC.

Steps to embed user role in JWT

Generate a JWT with user role

Structuring the JWT payload: When creating a JWT, you'll include user information and their role in the payload section. Here's an example structure for the payload:

{
  "sub": "1234567890",
  "sub": "1234567890",
  "name": "John Doe",
  "role": "admin",
  "iat": 1516239022
}

In this example, "role": "admin" is the claim that specifies the user's role.

Generating the JWT: Let's create a JWT with user roles using the jsonwebtoken library in Node.js. Here’s how you can do it:

const jwt = require('jsonwebtoken');

const user = {
  id: '1234567890',
  name: 'John Doe',
  role: 'admin'
};

const token = jwt.sign(user, 'your-secret-key', { expiresIn: '1h' });

console.log(token);

In this code, we're embedding the user's role directly into the JWT. The jsonwebtoken library takes care of signing the token with a secret key to ensure its integrity.

Further reading: For other programming languages, you can refer to their respective JWT library documentation. For example, PyJWT for Python, or JWT for Java.

Verify JWT and extract user role

Verifying the JWT: On the server side, you need to verify the JWT to ensure it's valid and has not been tampered with. This process also allows you to extract the user role from the token. Here’s an example of how to do this in Node.js:

const token = 'your-jwt-token-here';

jwt.verify(token, 'your-secret-key', (err, decoded) => {
  if (err) {
    return console.error('Token is invalid:', err);
  }

  console.log('Decoded token:', decoded);
  const userRole = decoded.role;
  console.log('User role:', userRole);
});

This code snippet verifies the token and, upon successful verification, extracts the role claim, which you can then use for authorization decisions.

Secure verification: It’s crucial to use secure algorithms and a secret key that’s difficult to guess. Make sure the secret key is stored securely and not exposed in your source code.

Use the user role in your application

Implementing Role-Based Access Control (RBAC): Once you have extracted the user role from the JWT, you can use it to control access to various parts of your application. For example:

if (userRole === 'admin') {
  // Allow access to admin routes
} else if (userRole === 'user') {
  // Allow access to user routes
} else {
  // Deny access
}

This basic example shows how you can check the user's role and conditionally grant or deny access to certain routes or resources.

Real-world scenarios: In a real-world application, you might use middleware to handle role-based authorization. For instance, a middleware function can verify the JWT and extract the role before proceeding to the actual route handler.

Security considerations

While embedding user roles in JWTs offers many benefits, it's important to handle JWTs securely to prevent vulnerabilities:

Signing and verification - Always sign your JWTs with a strong secret key or a private key if using asymmetric encryption (e.g., RS256). Ensure that the signature is verified on the server side to confirm that the token hasn't been tampered with.

Token expiration - Include an expiration time (exp claim) in your JWTs to limit their validity. This minimizes the risk of token reuse if it gets intercepted. For example:

const token = jwt.sign(user, 'your-secret-key', { expiresIn: '1h' });

Secure storage - Store JWTs securely on the client side. For web applications, use secure cookies with the HttpOnly and Secure flags set to protect against XSS attacks. Avoid storing JWTs in local storage if possible, as they can be more easily accessed by malicious scripts.

Token refresh strategies - Implement a token refresh strategy to keep users authenticated while maintaining security. A refresh token allows you to issue new access tokens without requiring the user to log in again.

Avoid adding information that can easily get stale - While the purpose of this article is to show you how to add the user's role into the JWT token, it's also important to remember that information that can change (or that should be allowed to change quickly) should usually be pulled from the database to avoid security problems with stale tokens (mainly to avoid users with outdated sessions to still be active on your system). You can read more about that in this article. So try to only store details inside your tokens that you know have a very small chance of changing (like the user's ID or their name).

Conclusion

Embedding user roles in JWTs is an effective way to simplify and optimize access control in your application. By including user roles as claims in the JWT, you streamline the authorization process, enhance performance, and reduce the load on your database. Just remember, with great power comes great responsibility. Ensure that you handle JWTs securely to prevent common vulnerabilities and that the information you're storing there cannot get stale.

JWT is a practical and basic alternative for implementing authorization, and once you peer through its cracks, you'll find that there are more powerful, and easier-to-implement alternatives that you should consider, much like Cerbos, a solution that can externalize the handling of your authorization logic with minimum impact to your project.

So next time you're considering implementing your own JWT-based authorization system, consider checking out Cerbos instead and save yourself multiple headaches.

Book a free Policy Workshop to discuss your requirements and get your first policy written by the Cerbos team