DEV Community

Cover image for JSON Web Tokens (JWTs): A Comprehensive Guide
Igor Venturelli
Igor Venturelli

Posted on • Originally published at igventurelli.io

JSON Web Tokens (JWTs): A Comprehensive Guide

In the realm of web development and security, JSON Web Tokens (JWTs) have emerged as a popular method for securely transmitting information between parties. JWTs are compact, self-contained, and can carry authentication and authorization data. In this article, we will delve into the intricacies of JWTs, exploring their formation, the importance of signing, methods for verifying authenticity, and common attributes within the payload.

What is a JSON Web Token (JWT)?

A JSON Web Token (JWT) is a compact and URL-safe means of representing claims securely between two parties. These claims, typically referred to as the payload, can include various information such as user identity, permissions, and additional metadata. JWTs are commonly used in authentication and authorization mechanisms within web applications, acting as a form of digitally signed and encrypted information exchange.

Formation of a JWT

A JWT consists of three sections separated by periods (.): the header, the payload, and the signature. Each section is Base64URL encoded, making JWTs compact and suitable for transmission across networks. Let's break down each part:

  1. Header: The header typically consists of two parts: the type of token (JWT) and the signing algorithm being used. For example:

    {
      "alg": "HS256",
      "typ": "JWT"
    }
    
    
  2. Payload: The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims. An example payload might look like this:

    {
      "sub": "1234567890",
      "name": "John Doe",
      "admin": true
    }
    
    
  3. Signature: The 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. The signature is created by combining the encoded header, encoded payload, a secret, and the chosen algorithm.

Importance of Signing JWTs

JWTs can be signed to ensure their integrity and authenticity. Without proper signing, JWTs can be tampered with and manipulated. Signing involves using a secret key known only to the server to generate a signature, which is then included in the JWT. When the recipient receives the JWT, they can verify the signature using the same secret key to ensure that the JWT has not been altered in transit.

Verifying JWT Authenticity

To verify the authenticity of a JWT, the recipient needs access to the same secret key used by the issuer to sign the token. The recipient recalculates the signature using the received header and payload along with the secret key. If the recalculated signature matches the signature included in the JWT, the token is considered authentic.
The issue server may offer a verification endpoint also, but before using this strategy, the team shall keep in mind that it will cost +1 network request for every request you will receive on your system.

Common Attributes within the Payload

While the payload of a JWT can vary depending on the application, there are some common attributes often found within JWTs:

  • iss (Issuer): Identifies the principal that issued the JWT.
  • sub (Subject): Identifies the subject of the JWT.
  • aud (Audience): Identifies the recipients for which the JWT is intended.
  • exp (Expiration Time): Specifies the expiration time after which the JWT must not be accepted for processing.
  • nbf (Not Before): Specifies the time before which the JWT must not be accepted for processing.
  • iat (Issued At): Specifies the time at which the JWT was issued.
  • jti (JWT ID): Provides a unique identifier for the JWT.

Conclusion

JSON Web Tokens (JWTs) offer a versatile and secure method for transmitting information between parties. By understanding their formation, the importance of signing, methods for verifying authenticity, and common attributes within the payload, developers can effectively implement JWT-based authentication and authorization mechanisms in their applications. As with any security mechanism, careful implementation and adherence to best practices are essential to ensure the integrity and confidentiality of data exchanged via JWTs.


Let’s connect!

πŸ“§ Don't Miss a Post! Subscribe to my Newsletter!
➑️ LinkedIn
🚩 Original Post
β˜•Β Buy me a Coffee

Top comments (0)