Markdown

JWT

JWT is the Acronym for JSON Web Token

A secure, compact way to transmit information between parties as a JSON object. Think of it as a digital passport that proves who you are and what you're allowed to do. It comprises three parts separated by dots: a header, payload, and signature (e.g., xxxxx.yyyyy.zzzzz). JWTs are primarily used for authentication and authorization in web applications. When you log into a website, the server can give you a JWT that proves your identity for future requests, instead of making you log in again. It's like getting a wristband at a festival – once you have it, you can enter different areas without showing your ID again.

{
  "userId": "123",
  "name": "John",
  "role": "admin",
  "expiresIn": "1 hour"
}

Structure of a JWT

A JWT consists of three parts separated by dots: a header, a payload, and a signature.

  • Header: Contains metadata about the token, such as the type of token and the signing algorithm.
  • Payload: Holds the claims, which are statements about an entity (typically, the user) and additional data.
  • Signature: Ensures the token hasn't been altered. It is created by taking the encoded header, encoded payload, a secret, and the algorithm specified in the header.

How JWTs Work

JWTs are widely used in modern web applications, especially in single-page applications (SPAs) and microservices architectures, because they're stateless and can work across different domains.

  • Login: You log into a website with your username and password.
  • Token Creation: The server creates a JWT containing your user information.
  • Token Usage: For your next requests, you include this JWT (usually in the Authorization header).
  • Token Verification: The server verifies the JWT to know who you are and what you can access.

Security Considerations

JWTs are designed to be compact, URL-safe, and usable, especially in web contexts. However, they come with security considerations.

  • Signature: The tokens are signed either using a private secret or a public/private key, ensuring that the sender cannot deny having sent the information and that the message was not changed along the way.
  • Encryption: While JWTs can be encrypted, this is optional and not always used, which means the payload is often readable by anyone.
  • Expiration: JWTs can include an expiration time to limit their validity, enhancing security.

Articles Tagged JWT

View Additional Articles Tagged JWT