DEV Community

Vidyarathna Bhat
Vidyarathna Bhat

Posted on

Understanding JSON Web Tokens (JWT) and Their Use in Web Authentication

In today's web development landscape, secure authentication mechanisms are more important than ever. One of the most popular methods for handling authentication in modern web applications is the JSON Web Token (JWT). In this post, we'll explore what JWTs are, how they work, and why they're so widely adopted.

What is a JWT?

A JSON Web Token (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 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.

Structure of a JWT

A JWT is composed of three parts, separated by dots (.):

  1. Header
  2. Payload
  3. Signature

1. Header

The header typically consists of two parts: the type of token (JWT) and the signing algorithm being used, such as HMAC SHA256 or RSA.

{
  "alg": "HS256",
  "typ": "JWT"
}
Enter fullscreen mode Exit fullscreen mode

This JSON is then Base64Url encoded to form the first part of the JWT.

2. Payload

The payload contains the claims. Claims are statements about an entity (typically, the user) and additional metadata. 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
}
Enter fullscreen mode Exit fullscreen mode

This JSON is Base64Url encoded to form the second part of the JWT.

3. Signature

To create the signature part, you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example, if you want to use the HMAC SHA256 algorithm, the signature will be created in this way:

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)
Enter fullscreen mode Exit fullscreen mode

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.

How JWTs Work

When a user logs in, the server generates a JWT and sends it back to the client. The client stores this token (usually in local storage or a cookie) and sends it along with every subsequent request to the server.

Here's a basic flow:

  1. User logs in: The server authenticates the user and issues a JWT.
  2. Client stores the token: The JWT is stored client-side (in local storage, session storage, or cookies).
  3. Client sends the token: With each request, the client sends the JWT, typically in the Authorization header using the Bearer schema.
  4. Server verifies the token: The server verifies the token's signature to ensure it's valid and then processes the request.

Why Use JWT?

  1. Stateless: JWTs are stateless, meaning the server doesn't need to keep a session store. This can improve scalability and performance.
  2. Compact: JWTs are compact and can be sent via URL, POST parameter, or inside an HTTP header.
  3. Self-contained: JWTs can contain all the required information about the user, avoiding the need for multiple database queries.
  4. Security: With proper implementation, JWTs are secure. They can be signed to ensure data integrity and encrypted for confidentiality.

Best Practices

  1. Keep it secret, keep it safe: Your secret keys must be stored securely.
  2. Use HTTPS: Always use HTTPS to ensure your tokens are not intercepted.
  3. Set an expiration: Always set an expiration for your JWT to reduce the risk of token theft.
  4. Validate: Always validate the JWT on the server side to ensure it's not tampered with.

Conclusion

JWTs provide a robust solution for handling authentication in modern web applications. Their stateless, compact, and self-contained nature makes them an excellent choice for secure communication between parties. By understanding and implementing best practices, you can leverage JWTs to build secure and efficient authentication systems.

Happy coding!

Top comments (0)