JSON web token | JWT - GeeksforGeeks (2024)

A JSON web token(JWT) is JSON Object which is used to securely transfer information over the web(between two parties). It can be used for an authentication system and can also be used for information exchange.The token is mainly composed of header, payload, signature. These three parts are separated by dots(.). JWT defines the structure of information we are sending from one party to the another, and it comes in two forms – Serialized, Deserialized. The Serialized approach is mainly used to transfer the data through the network with each request and response. While the deserialized approach is used to read and write data to the web token.

Deserialized

JWT in the deserialized form contains only the header and the payload.Both of them are plain JSON objects.

Header

A header in a JWT is mostly used to describe the cryptographic operations applied to the JWT like signing/decryption technique used on it. It can also contain the data about the media/content type of the information we are sending.This information is present as a JSON object then this JSON object is encoded to BASE64URL. The cryptographic operations in the header define whether the JWT is signed/unsigned or encrypted and are so then what algorithm techniques to use. A simple header of a JWT looks like the code below:

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

The ‘alg’ and ‘typ’ are object key’s having different values and different functions like the ‘typ’ gives us the type of the header this information packet is, whereas the ‘alg’ tells us about the encryption algorithm used.
Note: HS256 and RS256 are the two main algorithms we make use of in the header section of a JWT.
Some JWT’s can also be created without a signature or encryption. Such a token is referred to as unsecured and its header should have the value of the alg object key assigned to as ‘none’.

 { "alg":"none" }

Payload

The payload is the part of the JWT where all the user data is actually added. This data is also referred to as the ‘claims’ of the JWT.This information is readable by anyone so it is always advised to not put any confidential information in here. This part generally contains user information. This information is present as a JSON object then this JSON object is encoded to BASE64URL. We can put as many claims as we want inside a payload, though unlike header, no claims are mandatory in a payload. The JWT with the payload will look something like this:

 { "userId":"b07f85be-45da", "iss": "https://provider.domain.com/", "sub": "auth/some-hash-here", "exp": 153452683 }

The above JWT contains userId,iss,sub,and exp. All these play a different role as userId is the ID of the user we are storing, ‘iss’ tells us about the issuer, ‘sub’ stands for subject, and ‘exp’ stands for expiration date.

Serialized

JWT in the serialized form represents a string of the following format:

[header].[payload].[signature]

all these three components make up the serialized JWT. We already know what header and payload are and what they are used for.Let’s talk about signature.

Signature

This is the third part of JWT and used to verify the authenticity of token. BASE64URL encoded header and payload are joined together with dot(.) and it is then hashed using the hashing algorithm defined in a header with a secret key. This signature is then appended to header and payload using dot(.) which forms our actual token header.payload.signature

Syntax :

HASHINGALGO( base64UrlEncode(header) + “.” + base64UrlEncode(payload),secret)

So all these above components together are what makes up a JWT. Now let’s see how our actual token will look like:

JWT Example :

header:{ "alg" : "HS256", "typ" : "JWT"}Payload:{ "id" : 123456789, "name" : "Joseph"}Secret: GeeksForGeeks

JSON Web Token

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTIzNDU2Nzg5LCJuYW1lIjoiSm9zZXBoIn0.OpOSSw7e485LOP5PrzScxHb7SR6sAOMRckfFwi4rp7o


Last Updated : 21 Dec, 2021

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

JSON web token | JWT - GeeksforGeeks (2024)

FAQs

JSON web token | JWT - GeeksforGeeks? ›

A JSON web token(JWT) is JSON Object which is used to securely transfer information over the web(between two parties). It can be used for an authentication system and can also be used for information exchange. The token is mainly composed of header, payload, signature. These three parts are separated by dots(.).

What is JSON web token used for? ›

JSON Web Tokens (JWTs) are a standardized way to securely send data between two parties. They contain information (claims) encoded in the JSON format. These claims help share specific details between the parties involved. At its core, a JWT is a mechanism for verifying the authenticity of some JSON data.

What is the difference between JWT and JSON web token? ›

JSON web token (JWT), pronounced "jot", is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. Again, JWT is a standard, meaning that all JWTs are tokens, but not all tokens are JWTs.

What is the role of JSON web token? ›

The tokens are designed to be compact, URL-safe, and usable, especially in a web-browser single-sign-on (SSO) context. JWT claims can typically be used to pass identity of authenticated users between an identity provider and a service provider, or any other type of claims as required by business processes.

What is the difference between API key and JSON web token? ›

Typically, the API key provides only application-level security, giving every user the same access; whereas the JWT token provides user-level access. A JWT token can contain information like its expiration date and a user identifier to determine the rights of the user across the entire ecosystem.

Why should we use JWT? ›

Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are.

What are two scenarios where JSON Web tokens can be useful? ›

A JSON Web Token, or JWT, is a compact and self-contained way to represent information between two parties securely. It is encoded as a JSON object and digitally signed. JWTs are often used for authentication and authorization, both on the client and server sides of an application.

What are the three parts of a JSON Web Token? ›

Anatomy of a JWT

Figure 1 shows that a JWT consists of three parts: a header, payload, and signature. The header typically consists of two parts: the type of the token, which is JWT, and the algorithm that is used, such as HMAC SHA256 or RSA SHA256. It is Base64Url encoded to form the first part of the JWT.

Can a JWT token be stolen? ›

Remember, once a JWT (JSON Web Token) is stolen, it can be the worst thing for an individual and the enterprise as there's a huge chance of data breach and exploitation.

Are JSON Web tokens secure? ›

JSON Web Token, commonly referred to as JWT, is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. The token is digitally signed, ensuring its authenticity and integrity.

How to use a JWT token for authentication? ›

To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication.

Do JSON Web tokens expire? ›

That user basically has 5 to 10 minutes to use the JWT before it expires. Once it expires, they'll use their current refresh token to try and get a new JWT. Since the refresh token has been revoked, this operation will fail and they'll be forced to login again.

How to decode a JWT token? ›

JWT Decoder
  1. *First, remember that JWTs are tokens that are often used as the credentials for SSO applications. ...
  2. Grab a JWT (RFC 7519) you want to decode. ...
  3. Paste the JWT into the first text box.
  4. Press the Decode button.
  5. Read the decoded outputs for the header and payload!

How does JSON web token work? ›

The JWT is usually generated by the authentication server after the user logs in and contains the user's identity and access rights. The JWT is then sent with every API request as a bearer token in the authorization header. Identifies the client, limits API usage. Authenticates and authorizes the user.

What is alternative to JSON web token? ›

Json Web Token alternatives and similar libraries
  • PHP OAuth 2.0 Server. 9.2 8.9 Json Web Token VS PHP OAuth 2.0 Server. ...
  • HybridAuth. 8.7 4.5 L3 Json Web Token VS HybridAuth. ...
  • OAuth 2.0 Client. 8.5 4.4 L5 Json Web Token VS OAuth 2.0 Client. ...
  • Opauth. ...
  • PHP oAuthLib. ...
  • Sentinel. ...
  • TwoFactorAuth. ...
  • OAuth 1.0 Client.
Apr 16, 2024

Are OAuth and JWT the same? ›

Here are some differences between OAuth and JWT: Main function: OAuth is used for authorization, while JWT is used for authentication and exchanging information. Security: OAuth is a secure way to manage authorization flows, while JWT is a lightweight and self-contained token.

What is the use of JSON Web key? ›

The JSON Web Key Set (JWKS) is a set of keys containing the public keys used to verify any JSON Web Token (JWT) issued by the Authorization Server and signed using the RS256 signing algorithm. When creating applications and APIs in Auth0, two algorithms are supported for signing JWTs : RS256 and HS256.

What is the benefit of using JSON Web token instead of session cookie? ›

JWTs are ideal for stateless, distributed systems with a focus on scalability and single sign-on, while session-based approaches are more appropriate for applications that prioritise server-side control, robust session management, and sensitive data protection.

What is the purpose of JWT verification in authentication? ›

JSON Web Token (JWT) authentication is a stateless method of securely transmitting information between parties as a JavaScript Object Notation (JSON) object. It is often used to authenticate and authorize users in web applications and APIs.

What is the purpose of JSON in web development? ›

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

Top Articles
Latest Posts
Article information

Author: Maia Crooks Jr

Last Updated:

Views: 5805

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Maia Crooks Jr

Birthday: 1997-09-21

Address: 93119 Joseph Street, Peggyfurt, NC 11582

Phone: +2983088926881

Job: Principal Design Liaison

Hobby: Web surfing, Skiing, role-playing games, Sketching, Polo, Sewing, Genealogy

Introduction: My name is Maia Crooks Jr, I am a homely, joyous, shiny, successful, hilarious, thoughtful, joyous person who loves writing and wants to share my knowledge and understanding with you.