What to Consider Before Using JWT | Serengeti (2024)

After readingthe previousarticle, we know that 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.

Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.

When should we use JWT?

  • 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. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
  • 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. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

Are JWTs Secure Enough?

No matter how much we know about JWTs, a good question arises. If I get a JWT and I can decode the payload, how is that secure? I could just grab the token out of the header, decode and change the user information in the payload, and then send it back with the same correctly encoded secret, couldn’t I?

Well, JWTs aren’t concerned with encryption. They care about validation. That is to say, they can always find out whether the contents of a particular token have been manipulated. This means that user manipulation of the JWT is futile because the server will know and disregard the token. When issuing a token to the client, the server adds a signature based on the payload. Later, it verifies the payload and its matching signature.

The real question is, what is its motivation for not being concerned with encrypted contents?

  1. The simplest reason is because it assumes this is a solved problem for the most part. If dealing with a client – like the web browser, for example – you can store the JWTs in a cookie that is secure (meaning it is not transmitted via HTTP, only via HTTPS), HttpOnly (meaning it can't be read by JavaScript) and that it talks to the server over an encrypted channel (HTTPS). Once you know you have a secure channel between the server and the client, you can securely exchange JWT or whatever else you want.
  2. This keeps things simple. A simple implementation makes adoption easier, but it also lets each layer do what it does best (lets HTTPS handle encryption).
  3. JWT isn't meant to store sensitive data. Once the server receives the JWT and validates it, it is free to look up the user ID in its own database for additional information about that user (like permissions, postal address, etc.) This keeps the JWT small in size and avoids inadvertent information leakage because everyone knows not to keep sensitive data in JWT.

JWTs can either be signed, encrypted, or both. If a token is signed but not encrypted, everyone can read its contents, but can’t change it without knowing the private key. Otherwise, the receiver will notice that the signature isn’t a match anymore.

Generally speaking, this is nice, but what happens if your entire JWT is stolen?

Because JWTs are used to identify the client, if one is stolen or compromised, the attacker has full access to the user’s account in the same way they would if the attacker had compromised the user’s username and password instead.

For instance, if an attacker gets hold of your JWT, they could start sending requests to the server identifying themselves as you and perform actions like making service changes, user account updates, etc. Once an attacker has your JWT, it’s game over.

However, there is one thing that makes a stolen JWT slightly better than a stolen username and password: timing. Because JWTs can be configured to automatically expire after a set amount of time – a minute, an hour, a day, etc. – attackers can only use your JWT to access the service until it expires.

In theory, that sounds great, right? One of the ways token authentication is said to make authentication more “secure” is via short-lived tokens. That’s one of the core reasons token-based authentication has really taken off in recent years: you can automatically expire tokens and mitigate the risk of relying on forever-cached “stateless” tokens.

In the security world, relying on cached data to make sensitive decisions like who can log into a service and what they can do is considered a bad thing. Because tokens are stateless and allow for some speed improvements over traditional session authentication, the only way in which they can remain somewhat “secure” is by limiting their lifespan, so they don’t cause too much harm when compromised.

The only problem here is that if an attacker was able to steal your token in the first place, they’re likely able to do it once you get a new token. The most common ways this happens is by man-in-the-middle (MITM) attacks on your connection or getting access to the client or server directly. And unfortunately, in these scenarios, even the shortest-lived JWTs won’t help you at all.

According tookta, tokens should be treated like passwords and protected as such. They should never be publicly shared and should be kept in secure data stores. For browser-based applications, this means never storing your tokens in HTML5 Local Storage and instead storing them in server-side cookies that are not accessible to JavaScript.

In general, token-based authentication does not provide any additional security over typical session-based authentication relying on opaque session identifiers. While there certainly is a good number of use cases for token-based authentication, knowing how the technology works and where your weak spots are is essential.

Another interesting thing to consider is that in some cases, a stolen JWT can actually be worse than a stolen username and password.

Let’s pretend, for a moment, that your username and password have been compromised. In this scenario, if the app you’re logging into is protected with multi-factor authentication, the attacker needs to bypass additional identity-proofing mechanisms in order to gain access to your account.

While guessing or brute-forcing a username and password is a very realistic scenario, being able to compromise a user’s multi-factor authentication setup can be quite difficult. Bypassing factors like app-based authorization, SMS verification, face ID or touch ID is significantly more challenging than guessing a user’s password.

Because of this, a compromised JWT can actually be a greater security risk than a compromised username and password. Imagine a scenario like the one above – where the app a user logs into is protected by multi-factor authentication. Once the user logs in and verifies themselves via multi-factor, they are assigned a JWT to prove who they are. If that JWT is stolen, the attacker no longer needs to bypass MFA directly like they would if they only had the user’s username and password – they can now directly make requests as the user without additional identity proofing. Quite a big risk.

JWT – Pros and Cons

Based on what we stated above, JWT stands as the authentication and user verification standard of today in web-based applications, but just like many other “new and cool things”, it has its ownpros and cons:

Pros

  • No Database Table: This implies fewer DB queries, which implies faster response time. In case you are using paid services like DynamoDB that charge per query, JWT might reduce the costs marginally. But these can be resolved using tools like Redis in case of sessions
  • Simpler to use if careful: If your architecture doesn’t use client Sessions and your security basics are clear, the development time in case of JWT is faster using existing libraries.
  • Used across services: You can have one authorization server that deals with Login/Registration and generates the token, all the subsequent requests will not have to go to the authorization server as the only the Auth-server will have the private key, and rest of the severs will have the public-key to verify the signature.This is really useful for corporate systems where the authorization server is in a secure environment, e.g. a user needs to be connected to the intranet to login. But once they are logged in, the public servers can verify and proceed on. A similar setup can be used for OAuth implementation. The best part is that there is no connection between the auth-server and the rest of the servers other than the pre-defined public key.

Cons

  • Compromised Secret Key: The best and the worst thing about JWT is that it relies on just one Key. Consider that this Key could be leaked by a careless or rogue developer/administrator, and then the whole system is compromised! The attacker (who has access to the Key) can easily access all user data if he has the user-id which can easily be acquired. The only way to recover from this point is to generate a new Key (Key-pair) that will be used across systems from here on. This would mean that all the existing client tokens are invalidated, and every user would have to login again. Imagine if one day all Facebook users are suddenly logged out.
  • Cannot manage client from the server: Let’s say that we want all users to logout by cleaning up the cookies, but we cannot ask them to do so every time. We also need to consider the case that if a user’s mobile is stolen, they want to logout from all existing sessions (e.g. Gmail’s logout other sessions feature). Well, this is not possible with JWT.
  • Cannot push Messages to clients (Identifying clients from server): In cases when we have no record about the logged-in clients on the DB end, we cannot push messages to all clients. In the case of JWT, this is not possible because identifying each client per user is not possible.
  • Crypto algorithms can be deprecated: JWT completely relies on the signing algorithm. Although it isn’t as frequent anymore, in the past, many Encryption/Signing algorithms have been deprecated. So, in the case of JWT, if such a thing happens, again, every user on the platform will have to login again. Yet again, one will have to wait till all the JWT libraries are updated with the latest crypto algorithm.
  • Data Overhead: The size of the JWT is more than that of a normal Session token. The more data you add to the JWT, the longer it gets linearly. Remember, each request needs the token in it for request verification. So, say a 1 KB JWT implies each request will have 1KB overhead upload, which is really bad when there is a low-speed internet connection.
  • Complicated to understand: JWT uses cryptographic Signature algorithms to verify the data and get the user-id from the token. Understanding the signing algorithm requires knowing the basics of cryptography. So, in cases where the developer does not possess this knowledge, they might introduce security loopholes in the system.

Conclusion

Considering everything mentioned above, the real question is not whether you would use JWT, but when and how you should use it. As we said, for authorization and information exchange JWT is a solid standard, and it can be very reliable with additional security methods.

So, it is not a big surprise that JWT presently stands as a very popular standard you can use to trust requests by using signatures and exchange information between parties. Make sure you know when it’s best used when it’s best to use something else, and how to prevent the most basic security issues.

What to Consider Before Using JWT | Serengeti (2024)

FAQs

When should you use JWT? ›

JWTs are well-suited for server-to-server or microservice-to-microservice communication scenarios within a backend architecture. In this context, JWTs serve as a means of securely transmitting information between services for authorization and authentication purposes.

What are the important claims in JWT? ›

Examples of the most important registered claims include:
  • iss (issuer): Issuer of the JWT.
  • sub (subject): Subject of the JWT (the user).
  • aud (audience): The JWT intended recipient or audience.
  • exp (expiration time): The time the JWT expires.

What should be included in a JWT? ›

Anatomy of a JWT

The payload contains the claims. There is a set of registered claims, for example: iss (issuer), exp (expiration time), sub (subject), and aud (audience). These claims are not mandatory but recommended to provide a set of useful, interoperable claims.

How to safely use JWT? ›

Secure JWT storage: Avoid storing JWTs in local storage or cookies, and opt for more secure alternatives like HTTP-only cookies or server-side session management. Validate and verify JWTs: Always validate JWTs upon receipt and verify the signature to ensure they haven't been tampered with.

Why use JWT instead of basic auth? ›

JWT Advantages

This eliminates the need to query the database or authentication server for that information on every request. JWTs can be verified efficiently and quickly, because they do not require a database lookup. JWTs are only stored on the client side—the server generates a JWT and sends it to the client.

Why avoid JWT? ›

JWTs which just store a simple session token are inefficient and less flexible than a regular session cookie, and don't gain you any advantage. The JWT specification itself is not trusted by security experts.

What are the three types of claims uses in JWT? ›

The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.

Should JWT be sent with every request? ›

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 JWT secret key? ›

Storing JSON Web Token (JWT) secret key in the source code (hardcoded) increases significantly the risk that it could be used by an attacker to forge arbitrary valid-looking tokens that would allow to bypass authentication or authorization checks.

Is JWT best for authentication? ›

JWT (JSON Web Token) is a very popular way to authenticate users. It's a way to securely exchange data between client and server through a token. Here is how it works: User sends their credentials (i.e. username and password) to the server.

What is the best algorithm for JWT? ›

JWTs are most commonly signed using one of two algorithms: HS256 (HMAC using SHA256), and RS256 (RSA using SHA256).

What are the downsides of using JWT for authentication? ›

Disadvantages of JWT Authentication:

Token Size: JWTs can become large if they carry extensive user data, leading to increased network traffic. You should strike a balance between token size and necessary information. Limited Token Expiry Control: Once issued, JWTs remain valid until they expire.

What are the disadvantages of JWT authentication? ›

Once a JWT is issued, there is no straightforward way to invalidate it before its expiration time. This can pose a problem if a user logs out or if their privileges need to be revoked due to a security concern. To address this weakness, developers must implement additional mechanisms for token revocation.

What are the benefits of using a JWT? ›

JWT Token Advantages in Superset
  • Enhanced Security with JWT.
  • Simplified User Experience.
  • Scalability and Performance Gains.
  • Cross-Domain Authentication Support.
  • Ease of Mobile Authentication.
  • Decentralized Authorization Information.
  • Improved Debugging and Development.
  • Standardization and Ecosystem.

What is the advantage of using JWT? ›

Benefits of Using JWT Tokens

Stateless Authentication: JWTs are self-contained and carry all the necessary information, which eliminates the need for a server-side session store. Scalability: Being stateless, JWTs are easily scalable across multiple servers as there's no need to share session data.

Should you use a JWT as an API key? ›

Both JWT authentication and API Key authentication are good options when building a secure API. Each has benefits and drawbacks. JWT authentication is standardized and there are libraries you can use to implement API key authentication quickly. However it is typically more complex for your API consumers.

Why is JWT better than API key? ›

The credentials can either be a cryptographically secure JSON Web Token (JWT) signed with the client's private key or a secret value generated from your authorization server. A private key JWT is more secure, as you won't risk exposing the secret value that accidentally creates similar access concerns as an API key.

Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6283

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.