JWT tokens and security - working principles and use cases (2024)

The usual way to manage users’ sessions in a PHP application is to use session cookies, named “PHPSESSID” by default. When a user connects to the application, it generates a unique session identifier, that is stored on the server and then returned to the client with the “Set-Cookie” header. With that, the session cookie is stored on the web browser.
Cookies are designed to be systematically sent back to the server, on every request. That solution is a good way to handle usual user login and logout scenarios.

However, this type of mechanics does not allow multiple platforms or applications to easily authenticate a user with a single session. Also, the server needs to keep the session’s state and data in its memory.

JWT tokens are “stateless”, meaning that session information is not stored server-side. On top of the great gain in terms of saved memory on the server, JWT tokens can be used to authenticate users on multiple applications. To do that, the different applications will need to share the same private key to sign and verify tokens. Users will therefore be able to authenticate one single time on the application that manages user accounts, and to seamlessly use other applications that use the same private key, to verify the tokens’ validity.

Description

JSON Web Tokens (JWT) are tokens generated by the server upon user authentication on a web application, and then sent to the client (usually a browser).

These tokens are then sent on every HTTP request, which allows the server to authenticate the user.

To ensure integrity, information contained in the token is signed by a private key, owned by the server. When the server gets the token back from the client, it just has to compare the signature sent by the client with the one it will generate with its private key. If the signatures are identical, the token is then valid.

JWT tokens and security - working principles and use cases (1)

Anatomy of a JWT token

According to RFC 7519, a JWT token is made of the following elements:

A “Header” section, containing the algorithm used for the signature, as well as the type of token (“JWT” in our case). The whole thing is encoded in Base64.
A “Payload” section, containing the token data, like the user name, date of token generation or expiry date. All of that is written in JSON and also encoded in Base64
A “Signature” section, that is the result of Header and Payload, concatenated and then encrypted with the private key.

Here is an example of a JWT token :

“Header” section :
{
“alg”: “HS256”,
“typ”: “JWT”
}

“Payload” section :
{
“iat”: 1480929282,
“exp”: 1480932868,
“name”: “Username”
}

A lot of information can be put into that second section (Payload), like the username, or the user’s rights on the application. The JWT specifications however clearly mention some keywords to be used, like “iat” (issued at = date and time of token generation, with a timestamp) or exp (expiry date).

As explained earlier, the Signature is generated by concatenating the Header and Payload, and by encrypting the whole thing. Which gives us, with a private key “L2VE5VpgChrVPmgh1hgL” :

Signature = HMACSHA256(
base64UrlEncode({“alg”: “HS256″,”typ”: “JWT”}) + “.” +
base64UrlEncode({“iat”: 1480929282,”exp”: 1480932868,”name”: “Username”}),
L2VE5VpgChrVPmgh1hgL
)

The resulting JWT token is :

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODA5MjkyODIsImV4cCI6MTQ4MDkzMjg2OCwibmFtZSI6IlVzZXJuYW1lIn0.gZeuWNbjO8kyEX92AjgX5oLy5qhu6YWTPr6vtYELZQ4

The different colors specify:
Header
Payload
Signature

Which signature should I use?

The JWT standard is quite flexible and allows developers to use no signature algorithm at all for the token. Of course, this kind of configuration is highly discouraged.
It is however possible to choose which algorithm to use. It is therefore possible to use HMAC SHA256 like in the examples exposed in this article, which is the recommended approach. It is also possible to use an asymmetric encryption configuration (with a private key owned by the server, and a public key used to sign the token). The problem with that second method is that anyone can sign a token (with the public key). To know more about that vulnerability, please refer to the following article :
https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/

JWT tokens and security - working principles and use cases (2)

Use case

On the client-side, tokens can be stored in two different ways : stored in a cookie, or stored in the sessionStorage (or localStorage) of the browser. Each of these two methods have their own pros and cons, in terms of security and functionality.

Token stored in the sessionStorage or localStorage of the browser

If the token is stored that way, then it will have to be included in every request sent to the server, for instance with a header “Authorization: Bearer <token>”. That solution is really great for applications with a front-end written in JavaScript that communicates with the back-end through Ajax requests. Also, that solution natively protects the application against CSRF attacks considering that the token is unpredictable and cannot be retrieved by an attacker. Of course this last point is only valid if all sensitive requests use that JWT token. However, since the token must be made available to the JavaScript application, il will be exposed in case of XSS vulnerabilities and might be stolen.

Token stored in a cookie

When stored in the browser’s cookies, it is possible to set the “HttpOnly” flag (and “Secure”), to get protected against token theft in case of XSS attacks. The drawback of this implementation is that no CSRF protection can be expected from the token. Indeed, the token is automatically sent with the cookies (and therefore with any CSRF attack request).
Although that implementation remains “stateless”, it does not offer a seamless integration across different applications on different domains. Cookies are attached to a domain (or sub-domain) and cannot be used by another application hosted on a different domain.

Drawbacks of JWT tokens

Appart from pros and cons already detailed above, the JWT standard has its own issues :

  • If a user account needs to be blocked or deactivated, the application will have to wait for the token to expire in order for the lockout to be fully effective.
  • If a user needs to change their password (for instance in case of account hijacking) and if an authentication has been performed beforehand, then a token generated with the previous password will still be valid until expiry.
  • No “refresh” token is specified by the standard implementation. On expiry, the user will therefore have to re-authenticate.
  • It is not possible to destroy a token without breaching the “stateless” aspect of JWT tokens : Even if the token is deleted from the browser, it is still valid until expiry, so no real logout is possible.

To deal with these challenges, some JWT libraries add a layer above the standard specification and allow refresh token mechanisms as well as some features like forcing a user to re-authenticate, if need be.

Conclusion

JWT tokens are spreading over the web, mainly on applications relying on a (heavy) frontend and where users have to use a single account on different platforms. From a security point of view, JWT tokens have quite a good architecture but have some drawbacks that developers need to have in mind before integrating them into an application. It is very important to keep in mind that the security of the whole thing relies on the algorithm and private key used to sign tokens. Choosing a strong algorithm and key, and keeping the key in a safe place is therefore an important consideration.

Resources

To learn more on this topic, we encourage your to go through the following elements :
RFC 7519 (JWT tokens) : https://tools.ietf.org/html/rfc7519
Website with an overall JWT tokens presentation and references to libraries in several programming languages : https://jwt.io/

Author: Romain GARCIA

JWT tokens and security - working principles and use cases (2024)

FAQs

What is the use case of JWT token? ›

JWT can be used as an access token to prevent unwanted access to a protected resource. They're often used as Bearer tokens, which the API will decode and validate before sending a response.

How does JWT security 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 the principle of JWT? ›

Working Principle

For every subsequent request that requires authentication, the client sends the JWT in the request headers. The server validates the token by checking the signature and decoding the payload to ensure the user's authenticity and authorization.

What are JWT tokens and how do you use it? ›

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 main objective of JWT? ›

Information exchange: JWTs are a good way of securely transmitting information between parties because they can be signed, which means you can be certain that the senders are who they say they are. Additionally, the structure of a JWT allows you to verify that the content hasn't been tampered with.

What is the difference between JWT and OAuth? ›

JWT is mainly used for APIs while OAuth can be used for web, browser, API, and various apps or resources. JWT token vs oauth token: JWT defines a token format while OAuth deals in defining authorization protocols. JWT is simple and easy to learn from the initial stage while OAuth is complex.

Is JWT enough for security? ›

It's important to note that a JWT guarantees data ownership but not encryption. The reason is that the JWT can be seen by anyone who intercepts the token because it's serialized, not encrypted. It is strongly advised to use JWTs with HTTPS, a practice that extends to general web security.

What are the security vulnerabilities of JWT? ›

Token Leakage

JWTs are typically stored in client-side storage mechanisms, such as browser cookies or local storage. This design choice can expose tokens to various client-side vulnerabilities. Cross-site scripting (XSS) attacks, for example, can enable attackers to steal JWTs stored in a user's browser.

What is the difference between token authentication and JWT? ›

Additionally, JWTs can be utilized across numerous domains and can be easily verified by different services without relying on a central authority. In contrast, normal token-based authentication requires the server to keep up the session state or store tokens in a database to approve them.

What are the three parts of a JWT 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.

How should JWT be used? ›

JWTs can be used as access tokens or ID tokens, or sometimes for other purposes. It is thus important to differentiate the types of tokens. When validating JWTs, always make sure that they are used as intended. E.g., a resource server should not accept an ID token JWT as an access token.

How does the JWT token work in the rest API? ›

Client sends the stored JWT in an Authorization header for every request to the service provider. For each request, the service provider takes the JWT from the Authorization header and decrypts it, if needed, validates the signature, and if everything is OK, extracts the user data and permissions.

What is the use case of JWT? ›

Here are some scenarios where JSON Web Tokens are useful: Authorization: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token.

How to protect JWT tokens? ›

  1. 1 Choose a secure storage option. One of the most important decisions you have to make is where to store your JWT tokens in the browser. ...
  2. 2 Use short expiration times. ...
  3. 3 Use token revocation and blacklisting. ...
  4. 4 Use HTTPS and secure headers. ...
  5. 5 Use libraries and frameworks. ...
  6. 6 Here's what else to consider.
Mar 20, 2023

What is the advantage of using JWT? ›

Stateless Authentication: One of the primary advantages of JWT authentication is its statelessness. The server does not need to maintain session state for each user, which simplifies scaling and load balancing. Single Sign-On (SSO): JWTs can be used to enable Single Sign-On across multiple applications.

What is the correct use of JWT? ›

JWTs can be used as access tokens or ID tokens, or sometimes for other purposes. It is thus important to differentiate the types of tokens. When validating JWTs, always make sure that they are used as intended. E.g., a resource server should not accept an ID token JWT as an access token.

What is a token use case? ›

Tokenization use case for Stablecoins

By using stablecoins, businesses and individuals can avoid the currency fluctuations and high transaction fees associated with traditional methods of sending money abroad. Benefits: Internalize payments. New revenue opportunities through yield generation (interest on deposits)

What is the use of JWT token in Java? ›

JSON Web Token is used to carry information related to the identity and characteristics (claims) of a client. This information is signed by the server in order for it to detect whether it was tampered with after sending it to the client.

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6223

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.