Huge news!Announcing our $20M Series A led by Andreessen Horowitz.Learn more
Log inDemoInstall

← Back to Glossary


JSON Web Token (JWT)

Introduction to JSON Web Token (JWT)#

JSON Web Tokens, often referred to as JWTs, are a popular method for securely transmitting information between parties as a JSON object. This compact and self-contained mechanism is designed to be easy to send via URL, POST parameter, or inside an HTTP header. The information contained within a JWT can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

JWTs are frequently used to implement authentication and authorization mechanisms in web applications. When a user logs in, the server creates a unique JWT that represents the user's session and sends it back to the user. The user's browser then sends this JWT back to the server with each subsequent request, allowing the server to verify that the request is authorized.

A JWT contains claims, which are statements about an entity (typically, the user) and additional metadata. There are three types of claims: registered, public, and private claims. Registered claims are predefined by the JWT standard and are not mandatory but recommended, to provide a set of useful, interoperable claims.

The Anatomy of a JWT#

JWTs consist of three parts separated by dots (.), which are: Header, Payload, and Signature. Let's break down each of these parts:

  • Header: The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA. This information is Base64Url encoded to form the first part of the JWT.
  • Payload: The second part of the token is the payload, which contains the claims. These claims can be statements about the user or other data. This section is also Base64Url encoded to form the second part of the JWT.
  • Signature: To create the signature, you have to take the encoded header, the encoded payload, a secret, and the algorithm specified in the header, and sign it. This signature is 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.

How JWTs Work in Authentication & Authorization#

In a typical authentication flow, JWTs work as follows:

  • A client sends a request to authenticate with its credentials (like a username and password).
  • If the credentials are valid, the server generates a JWT and returns it in the response.
  • The client saves this JWT and sends it along with every subsequent request to the server.
  • The server verifies the JWT and if it's valid, allows the request.

For authorization, JWTs can contain scopes and roles in the claims which denote what resources the bearer can access. This makes JWTs very efficient for decentralized or microservice-based systems.

Pros and Cons of JWTs#

Like any technology, JWTs have their strengths and weaknesses.


  • Stateless and Scalable: Because JWTs carry all the necessary information within themselves, they are stateless. This means the server doesn't need to keep a session store, making it easier to scale.
  • Decoupling: The issuer of a token and the consumer do not have to be in the same system or even share the same programming language.
  • Performance: Only one database trip is needed upon login to generate the token.


  • Size: Because they include a full payload, JWTs are larger than session cookies. This means, especially in large applications, the size of the requests can increase significantly.
  • Storage: Storing JWTs securely on the client side can be tricky.
  • Expiration: There's no perfect way to expire or invalidate individual JWTs prematurely.

Securing JWTs: Best Practices and Common Vulnerabilities#

Securing JWTs is crucial to the security of an application. Here are some best practices:

  • Always use HTTPS: To prevent tokens from being intercepted during transmission.
  • Store JWTs securely: Storing tokens in HTML5 Web Storage (local storage or session storage) is prone to XSS attacks. A better approach is to use HTTP Only cookies.
  • Short expiration times: Limiting the lifetime of a JWT reduces the likelihood of it being used maliciously.
  • Handle token expiration: Your application should be able to handle token expiration appropriately and issue new tokens when needed.

While JWTs offer many advantages, they are also vulnerable to certain types of attacks, including:

  • Brute Force attacks: If the JWT is not encrypted, a hacker could guess the secret key by brute force.
  • Man in the Middle (MitM) attacks: A token can be intercepted and used by a hacker if it's not sent over an encrypted channel.
  • Algorithm alteration attacks: A token can be manipulated by changing the algorithm from RS256 (asymmetric) to HS256 (symmetric).

Role of Socket in Securing JWTs and Open Source Software#

Given the security concerns surrounding JWTs, it is important to have robust security tools and practices in place. This is where Socket comes into play. Socket's proactive security platform can help manage and monitor security concerns related to JWT usage as part of its comprehensive protection for open source code.

Socket helps developers find, audit, and manage open source dependencies, detecting and blocking various signals of supply chain risk. It is not a traditional vulnerability scanner; it provides comprehensive visibility, defense-in-depth, and proactive supply chain protection.

In the context of JWTs, Socket can provide visibility into how JWTs are being used in your code, help identify vulnerabilities like weak keys or algorithm misconfigurations, and assist in applying best practices in JWT security.

Remember, the goal is not just to implement security features, but to integrate security into your development processes. Socket's emphasis on minimizing security busywork helps developers focus more on their core tasks while maintaining high standards of application security.

Table of Contents

Introduction to JSON Web Token (JWT)The Anatomy of a JWTHow JWTs Work in Authentication & AuthorizationPros and Cons of JWTsSecuring JWTs: Best Practices and Common VulnerabilitiesRole of Socket in Securing JWTs and Open Source Software
SocketSocket SOC 2 Logo


Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc