Managing Sessions with JWT:
Registering and Validating User Sessions

Published on: August 26, 2024

Managing user sessions is a critical aspect of building secure web applications. In this guide, we will explore how to register and validate user sessions using JSON Web Tokens (JWT).

Registering a New Session

Diagram Loading...

  1. Client sends a login request to the authentication server.
  2. Authentication Server retrieves user credentials from the database and validates them.
  3. Authentication Server generates a JWT token and sends it back to the client after saving it in the registered_tokens table.
  4. Client stores the JWT token in local storage and includes it in the Authorization header for subsequent requests to resource servers.

Registered Tokens Table

uuid
access_token
refresh_token
d3b07384-d9a3-45b8-b3cd-4a99e834f194
access_token
refresh_token

The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.

Why Store Tokens?

Storing tokens in a database allows you to keep track of active user sessions, revoke access, and manage token expiration. We will explore how these are used in the refreshing sessions section.

Verifying a Session

Diagram Loading...

  1. Client sends a request to the resource server with the JWT token in the Authorization header.
  2. Resource Server sends the JWT token to the Authentication Server to validate the user session.
  3. Authentication Server verifies the JWT token is valid, checks if it is blacklisted, retrieves user data from the user table, and sends it back to the Resource Server.
  4. Resource Server processes the request or denies it based on the verification outcome.

Blacklisted Tokens Table

tokens
access_token
refresh_token

This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.

This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.

Refreshing a Session

Diagram Loading...

  1. Client sends a request to the authentication server with the refresh_token.
  2. Authentication Server verifies the refresh_token is valid.
  3. Authentication Server ensures that the refresh_token used matches the one registered for the user.
  4. Authentication Server retrieves user data from the user table.
  5. Authentication Server generates a new access_token and refresh_token.
  6. Authentication Server updates the generated tokens in the registered_tokens table for the user with the specified uuid.
  7. Authentication Server blacklists the old tokens and sends the new tokens back to the client.
  8. Client stores the new tokens in local storage and includes the access_token in the Authorization header for subsequent requests to resource servers.

Unregistering a Session

Diagram Loading...

  1. Client sends a request to the authentication server to log out.
  2. Authentication Server verifies the access_token is valid.
  3. Authentication Server blacklists the access_token and refresh_token.
  4. Authentication Server deletes the access_token and refresh_token present in the registered_tokens table.
  5. Authentication Server sends a response to the client.
  6. Client removes the tokens from local storage.

Conclusion

  • Managing user sessions effectively is crucial for building secure web applications, particularly in distributed systems like microservices.
  • Implementing JWT allows for secure communication between clients and servers while maintaining a scalable and maintainable authentication flow.
  • Key steps in session management include registering, verifying, refreshing, and unregistering sessions.
  • Regularly review and update session management strategies to adapt to evolving security threats.
  • Handle tokens securely throughout their lifecycle, including storage, verification, and blacklisting, to reduce vulnerabilities in your authentication system.
  • Following these practices helps build a strong session management system that protects both your users and your application.