How to use JWT with REST API

JSON Web Tokens (JWT) have become a cornerstone in the realm of web development, particularly in securing and authorizing RESTful APIs. As a stateless and compact mechanism, JWT offers an efficient way to transmit information between parties, and its popularity continues to grow. In this article, we’ll delve into the fundamentals of JWT, exploring its definition, purpose, security aspects, and common challenges. Furthermore, we’ll provide hands-on examples in Node.js to illustrate how to harness the power of JWT in your REST API.

What is JWT?

JSON Web Token, or JWT, is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted, making JWT an ideal choice for authentication and authorization in web development.

A JWT consists of three parts:

  1. Header: Contains the type of token (JWT) and the signing algorithm being used.
  2. Payload: Contains the claims. Claims are statements about an entity (typically, the user) and additional data.
  3. Signature: Used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn’t changed along the way.

Why Do We Need JWT?

Stateless Authentication

One of the key advantages of JWT is its statelessness. Traditional session-based authentication requires the server to store user sessions, resulting in scalability challenges. JWTs, being self-contained, eliminate the need for server-side storage, making them highly scalable and suitable for microservices architectures.

Cross-Domain Communication

JWTs can be easily transmitted between parties, making them an excellent choice for communication across different domains. This enables single sign-on (SSO) and allows for seamless integration between services.

Payload Flexibility

The payload of a JWT can include custom claims, providing flexibility in the type and amount of information carried within the token. This makes JWT suitable for a wide range of use cases beyond authentication, such as authorization and custom application-specific data exchange.

Security in JWT

Token Signature

JWTs can be signed to ensure the integrity of the data they carry. The signature is generated using a secret key, and it allows the recipient to verify that the sender of the JWT is legitimate and that the token hasn’t been tampered with.

Token Encryption

While JWTs are commonly signed, they can also be encrypted to add an additional layer of security. Encryption ensures that the information within the JWT remains confidential and can only be decrypted by the intended recipient.

Token Expiration

JWTs often have an expiration time (exp claim) to mitigate the risks associated with long-lived tokens. By setting a reasonable expiration time, the system can automatically invalidate tokens, reducing the window of opportunity for unauthorized access.

Authorization and Authentication with JWT

Authentication Flow

JWTs are commonly used for authentication. When a user successfully logs in, a JWT is issued and sent to the client. The client includes the JWT in the headers of subsequent requests to prove its identity. The server then validates the JWT and, if successful, processes the request.

Let’s take a look at a basic example of using JWT for authentication in a Node.js environment:

const jwt = require('jsonwebtoken');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const secretKey = 'your-secret-key';

app.use(bodyParser.json());

// Endpoint for user authentication
app.post('/login', (req, res) => {
  const { username, password } = req.body;

  // Verify user credentials (this is a simplified example)
  if (username === 'example_user' && password === 'secret_password') {
    // Create a JWT
    const token = jwt.sign({ username }, secretKey, { expiresIn: '1h' });

    // Send the JWT to the client
    res.json({ token });
  } else {
    res.status(401).json({ error: 'Invalid credentials' });
  }
});

// Protected endpoint that requires a valid JWT
app.get('/protected', (req, res) => {
  const token = req.headers.authorization;

  // Verify the JWT
  jwt.verify(token, secretKey, (err, decoded) => {
    if (err) {
      return res.status(401).json({ error: 'Invalid token' });
    }

    // Token is valid, proceed with the request
    res.json({ message: 'Protected resource accessed', user: decoded.username });
  });
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

In this example, the /login endpoint generates a JWT upon successful authentication and the /protected endpoint requires a valid JWT for access.

Authorization Flow

JWTs are not only useful for authentication but also for authorization. Claims in the JWT payload can represent user roles or permissions, allowing the server to make informed decisions about whether a user has the necessary rights to perform a particular action.

Let’s extend our previous example to include user roles:

// ...

// Modified login endpoint to include user roles
app.post('/login', (req, res) => {
  const { username, password } = req.body;

  // Verify user credentials (this is a simplified example)
  if (username === 'admin' && password === 'admin_pass') {
    // Create a JWT with user role information
    const token = jwt.sign({ username, role: 'admin' }, secretKey, { expiresIn: '1h' });

    // Send the JWT to the client
    res.json({ token });
  } else {
    res.status(401).json({ error: 'Invalid credentials' });
  }
});

// Modified protected endpoint to check user roles
app.get('/admin', (req, res) => {
  const token = req.headers.authorization;

  // Verify the JWT
  jwt.verify(token, secretKey, (err, decoded) => {
    if (err) {
      return res.status(401).json({ error: 'Invalid token' });
    }

    // Check if the user has the 'admin' role
    if (decoded.role === 'admin') {
      res.json({ message: 'Admin resource accessed', user: decoded.username });
    } else {
      res.status(403).json({ error: 'Insufficient permissions' });
    }
  });
});

// ...

In this example, the /login endpoint now includes a user role in the JWT, and the /admin endpoint checks if the user has the ‘admin’ role before granting access.

Common Problems and Best Practices

Token Expiration and Refresh Tokens

JWTs have a finite lifespan, which can lead to challenges in managing sessions. One common solution is to use refresh tokens. Instead of issuing a new JWT every time the user logs in, a refresh token can be used to obtain a new JWT without requiring the user to re-enter their credentials.

// ...

// Endpoint for refreshing the JWT using a refresh token
app.post('/refresh', (req, res) => {
  const refreshToken = req.body.refreshToken;

  // Verify the refresh token (this is a simplified example)
  if (isValidRefreshToken(refreshToken)) {
    // Generate a new JWT
    const newToken = jwt.sign({ /* new claims */ }, secretKey, { expiresIn: '1h' });

    // Send the new JWT to the client
    res.json({ token: newToken });
  } else {
    res.status(401).json({ error: 'Invalid refresh token' });
  }
});

// ...

Token Revocation

In certain scenarios, it may be necessary to revoke a JWT before it expires. This can be achieved by maintaining a blacklist of revoked tokens on the server side.

// ...

// Modified token verification to check against the blacklist
app.get('/protected', (req, res) => {
  const token = req.headers.authorization;

  // Check if the token is in the blacklist
  if (isTokenRevoked(token)) {
    return res.status(401).json({ error: 'Revoked token' });
  }

  // Verify the JWT
  jwt.verify(token, secretKey, (err, decoded) => {
    if (err) {
      return res.status(401).json({ error: 'Invalid token' });
    }

    // Token is valid, proceed with the request
    res.json({ message: 'Protected resource accessed', user: decoded.username });
  });
});

// ...

Token Storage and Transmission

JWTs should be treated with care during storage and transmission. Storing sensitive information within the JWT payload can expose it to potential security risks. Additionally, using HTTPS is crucial when transmitting JWTs over the network to prevent interception and tampering.

Conclusion

JSON Web Tokens have become an integral part of modern web development, providing a versatile and secure solution for authentication and authorization in RESTful APIs. By understanding the fundamental concepts of JWT, addressing security considerations, and implementing best practices, developers can harness the power of JWT to build scalable, secure, and efficient web applications. In the Node.js examples provided, we’ve demonstrated how to implement JWT-based authentication and authorization, paving the way for you to integrate this powerful technology into your own projects. As you embark on your journey with JWT, always stay mindful of evolving best practices and security measures to ensure the robustness of your applications.