Mastering in JWT token

A JWT, or JSON Web Token, is a way to represent information between two parties securely. It is often used for authentication and information exchange in web applications.

Click here for Business logic test cases
Click here for Web application penetration testing
Click here for HTTP Security Header

What is JWT token?

A JWT (JSON Web Token) is like a digital passport. It’s a small piece of data that proves who you are and what you’re allowed to do. When you log in to a website, the server gives you this token. Every time you make a request (like viewing your profile or accessing your dashboard), you show this token to prove your identity.

How Does JWT Work?
  • Logging In: When you log into a website, the server checks your username and password. If they’re correct, it creates a JWT token just for you.
  • Storing the Token: You receive this token and store it in your browser (usually in local storage or cookies).
  • Using the Token: For every action you take on the website (like viewing your profile), you send this token along with your request. It’s like showing your passport every time you want to enter a new country.
  • Verification: The server checks the token to make sure it’s valid. If everything checks out, you get access to the requested information or service.
How JWT Token works
Structure of a JWT Token:

A JWT is made up of three parts, separated by dots (.). These parts are:

  • Header
  • Payload
  • Signature
Structure of JWT Token in detail

Header: The header tells us what type of token it is and what algorithm was used to create it. For example, it might say that it’s a JWT and that it was signed using a method called such as HMAC SHA256 or RSA.

{
  "alg": "HS256",
  "typ": "JWT"
}

Payload: The payload contains the actual information, or claims, about the user. This can include things like the user’s ID, name, and any other relevant data.

{
  "user_id": 1,
  "role": "admin"
}

Signature: The signature is what makes the JWT secure. It’s created by taking the encoded header, the encoded payload, and a secret key. This ensures that the token hasn’t been changed by anyone else. If someone tries to alter the token, the signature will not match, and the server will know it’s not valid.

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)

Once you have the header, payload, and signature, you can combine them to form the complete JWT. It looks something like this:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJyb2xlIjoiYWRtaW4ifQ.abc123xyz
Why should Use JWT Token?
  • Stateless: Unlike traditional sessions, JWTs don’t require the server to store session information. This makes them lightweight and easy to scale.
  • Cross-Domain: JWTs can be used across different domains, making them great for single sign-on (SSO) applications.
  • Secure: As long as you keep your secret key safe, JWTs are a secure way to transmit information.
Where should use JWT Token ?
  • Authentication: JWTs are primarily used for user authentication. When a user logs in, the server generates a JWT token that contains user information and permissions. This token is then sent to the client, which stores it (usually in local storage or cookies). For subsequent requests, the client sends the JWT to the server, allowing the user to access protected resources without needing to log in again.
  • Authorization: In addition to authentication, JWTs can be used for authorization. The payload of a JWT can include user roles and permissions, allowing the server to determine what resources a user can access. This is particularly useful in applications with different user roles, such as admin, editor, and viewer.
  • Single Sign-On (SSO): JWTs are often used in Single Sign-On systems, where a user can log in once and gain access to multiple applications without needing to log in again for each one. The JWT can be shared across different services, allowing seamless access.
  • API Security: Many APIs use JWTs to secure endpoints. When a client makes a request to an API, it includes the JWT in the request header. The API server verifies the token before processing the request, ensuring that only authorized users can access the API.
Best practice for JWT Token:
  • Use Strong Signing Algorithms
  • Keep Your Secret Key Secure
  • Implement Short Expiration Times
  • Validate Tokens on the Server Side
  • Avoid Storing Sensitive Information in JWTs
  • Use HTTPS
  • Implement Token Revocation
  • Monitor and Log Usage

JWT is a powerful tool for modern web applications. It makes authentication and authorization fast, secure, and scalable. However, like any technology, it must be used responsibly to ensure the safety of users and systems.

By understanding JWT and following best practices, you can build secure and efficient applications that provide a seamless experience for your users.