Why PASETO is better than JWT for token-based authentication? (2024)

Nowadays, token-based authentication has become more and more popular in the development of web and mobile applications.

There are many different types of tokens, but among them, JSON web token (or JWT) is one of the most widely used.

However, in the past few years, we’ve also discovered several security issues regarding JSON web token, mainly because of its poorly designed standard.

So recently people have started migrating to other types of tokens such as PASETO, which promises to bring better security to the application.

In this lecture, we will learn everything about the security issues of JWT and how PASETO is designed to solve all of those problems.

Here's:

Token-based authentication

First, let’s talk a bit about token-based authentication.

Basically, in this authentication mechanism, the client will make the first request to log in user, where it provides the username and password to the server.

Why PASETO is better than JWT for token-based authentication? (1)

The server will check if the username and password are correct or not. If they are, the server will create and sign a token with its secret or private key, then sends back a 200 OK response to the client together with the signed access token.

The reason it’s called access token is that later the client will use this token to get access to other resources on the server.

For example, let’s say the client wants to get the list of bank accounts that belong to the logged-in user. Then it will make a GET /accounts request to the server, where it embeds the user’s access token in the header of the request.

Why PASETO is better than JWT for token-based authentication? (2)

Upon receiving this request, the server will verify if the provided token is valid or not. If it is valid, the request will be authorized, and a 200 OK response will be sent back to the client with the list of user’s bank accounts.

Note that an access token normally has a lifetime duration before it gets expired. And during this time, the client can use the same token to send multiple requests to the server.

So that’s how the token-based authentication works.

JSON Web Token

Now let’s talk about JSON Web Token! Here’s an example of a JSON Web Token:

Why PASETO is better than JWT for token-based authentication? (3)

It is a base64 encoded string, composed of 3 main parts, separated by a dot.

The first part (with the color red) is the header of the token. When we decode this part, we will get a JSON object that contains the token type JWT, and the algorithm used to sign the token: HS256 in this case.

The second part (in purple) of the token is the payload data. This part is where we store information about the logged-in user, such as username, and also the timestamp at which this token will be expired.

Why PASETO is better than JWT for token-based authentication? (4)

You can customize this JSON payload to store any other information you want. In this case, we also have an ID field to uniquely identify the token. It will be useful in case we want to revoke access of the token in case it is leaked.

Keep in mind that all data stored in the JWT is only base64-encoded, not encrypted. So you don’t need the secret/private key of the server in order to decode its content.

It also means that we can easily encode the header and payload data without the key. So how can the server verify the authenticity of the access token?

Well, that’s the purpose of the third part of the token: the digital signature (in blue color). If you don’t know how the digital signature algorithm works, then I recommend you to read my post about SSL/TLS before continuing.

Why PASETO is better than JWT for token-based authentication? (5)

The idea is simple, only the server has the secret/private key to sign the token. So if a hacker attempts to create a fake token without the correct key, it will be easily detected by the server in the verification process.

The JWT standards provide many different types of digital signature algorithms, but they can be classified into 2 main categories.

Symmetric-key algorithm

The first one is symmetric-key algorithm, where the same secret key is used to both sign and verify the tokens.

Why PASETO is better than JWT for token-based authentication? (6)

And since there’s only 1 key, it should be kept secret. So this algorithm is suitable for local use only, or in other words, for internal services, where the secret key can be shared.

Some specific algorithms which belong to this symmetric-key category are: HS256, HS384, and HS512.

Here HS256 is the combination of HMAC and SHA-256. HMAC stands for Hash-based Message Authentication Code, and SHA is the Secure Hashing Algorithm. While 256/384/512 is the number of output bits.

Why PASETO is better than JWT for token-based authentication? (7)

Symmetric-key algorithm is very efficient and suitable for most applications.

However, we cannot use it in case there’s an external third-party service that wants to verify the token, because it would mean we must give them our secret key.

In that case, we must use the second category: asymmetric-key algorithm.

Asymmetric-key algorithm

In this type of algorithm, there’s a pair of keys instead of just 1 single secret key.

Why PASETO is better than JWT for token-based authentication? (8)

The private key is used to sign the token, while the public key is used only to verify it.

Therefore, we can easily share our public key with any external third-party services without worrying about leaking our private key.

Within this asymmetric-key category, there are several groups of algorithms, such as RS group, PS group, or ES group.

Why PASETO is better than JWT for token-based authentication? (9)

Here, RS256 is basically RSA algorithm with PKCSv1.5 and SHA256.

PS256 is also RSA algorithm but with Probabilistic Signature Scheme and SHA256. It was designed to be more secured than PKCSv1.5

And the last one ES256 is simply Elliptic Curve Digital Signature Algorithm with SHA256.

Problems of JWT

OK, so far it sounds like JWT is a good standard, and it gives us a lot of flexibility to choose whatever signing algorithms we want. So what exactly are its problems?

Weak algorithms

Well, the first problem is weak signing algorithms. JWT gives developers too many algorithms to choose from, including the algorithms that are already known to be vulnerable, such as:

  • RSA with PKCSv1.5 is susceptible to a padding oracle attack.
  • Or ECDSA can face an invalid-curve attack.

Why PASETO is better than JWT for token-based authentication? (10)

For developers without deep experience in security, it would be hard for them to know which algorithm is the best to use.

So the fact that JWT gives developers too much flexibility to choose the algorithm is like giving them a gun to shoot themselves in the foot.

Trivial Forgery

But it’s not the worst. JSON web token makes token forgery so trivial, that if you are not careful in your implementation or if you choose a poorly implemented library for your project, your system will easily become a vulnerable target.

One bad thing about JWT is that it includes the signing algorithm in the token header.

Because of this, we have seen in the past, an attacker can just set the alg header to none to bypass the signature verification process.

Of course, this issue has been identified and fixed in many libraries, but it’s something you should carefully check when choosing the community-developed library for your project.

Why PASETO is better than JWT for token-based authentication? (11)

Another more dangerous potential attack is to purposely set the algorithm header to a symmetric-key one, such as HS256 while knowing that the server actually uses an asymmetric-key algorithm, such as RSA to sign and verify the token.

Let me explain how!

Basically, the server’s RSA public key is clearly known to the public because it’s a public key.

So the hacker can just create a fake token of the admin user, where he purposely set the algorithm header to HS256, which is a symmetric-key algorithm.

Then, he just signs this token with the server’s public key and uses it to access resources on the server.

Why PASETO is better than JWT for token-based authentication? (12)

Now, keep in mind that, the server normally uses an RSA algorithm, such as RS256 to sign & verify the token, so it will use the RSA public key as the key to verify the token signature.

However, since the token’s algorithm header is saying HS256, the server will verify the signature with this symmetric algorithm HS256 instead of RSA.

And because the same key is used by the hacker to sign the token payload, this signature verification process will be successful, and the request of the hacker will be authorized.

This kind of attack is very simple, but still so powerful and dangerous, and it has actually happened in the past because the developers didn’t check the algorithm header before verify the token signature.

So, in order to prevent this attack, it’s crucial that in your server code, you must check the token’s algorithm header to make sure that it matches with the one your server uses to sign and verify tokens.

Why PASETO is better than JWT for token-based authentication? (13)

OK, so now you know why JSON web token is not a very well-designed standard. It opens the door to many potential threats.

Therefore, many people are trying to stay away from it, and migrate to something more robust.

PASETO - Platform Agnostic Security Token

PASETO, or Platform Agnostic Security Token is one of the most successful designs that is being widely accepted by the community as the best-secured alternative to JWT.

Strong algorithms

It solves all issues of JSON web token by first, provide strong signing algorithms out of the box.

Developers don’t have to choose the algorithm anymore. Instead, they only need to select the version of PASETO they want to use.

Why PASETO is better than JWT for token-based authentication? (14)

Each PASETO version has already been implemented with 1 strong cipher suite. And at any time, there will be only at most 2 latest versions of PASETO are active.

Right now, 2 active PASETO versions are version 1 and version 2.

PASETO version 1

Version 1 is older, and should only be used for legacy systems that cannot use modern cryptography.

Similar to JWT, PASETO also has 2 algorithm categories for 2 main use cases.

For local or internal services, we use a symmetric-key algorithm.

Why PASETO is better than JWT for token-based authentication? (15)

But unlike JWT, which only does base64-encode the payload, and sign the token, PASETO actually encrypts and authenticates all data in the token with a secret key, using a strong Authenticated Encryption with Associated Data (or AEAD) algorithm. If you don’t know what AEAD is, you can watch my video about SSL/TLS.

The AEAD algorithm used in PASETO version 1 is AES256 CTR with HMAC SHA384.

For public cases, where there are external services that need to verify the token, we have to use an asymmetric-key algorithm.

In that case, PASETO uses the same approach as JWT, which means, it doesn’t encrypt the token data, but only base64-encode it, and uses the private key to sign the content with a digital signature.

Why PASETO is better than JWT for token-based authentication? (16)

The chosen asymmetric-key algorithm in PASETO version 1 is RSA PSS with SHA384.

PASETO version 2

In the latest version of PASETO (version 2), 2 more secured and modern algorithms are being used.

For local symmetric-key scenario, it uses XChacha20 with Poly1305 algorithm.

Why PASETO is better than JWT for token-based authentication? (17)

And for a public asymmetric-key scenario, Edward-curve digital signature algorithm with curve 25519 is used.

This choice reminds me of how TLS 1.3 was designed to improve the security of its older version TLS 1.2, and also simplify & reduce the number of TLS cipher suites at the same time.

Why PASETO is better than JWT for token-based authentication? (18)

Non-trivial forgery

Now with the design of PASETO, token forgery is no longer trivial.

Because the algorithm header doesn’t exist anymore, so the attacker cannot set it to none, or force the server to use the algorithm it chose in this header.

Why PASETO is better than JWT for token-based authentication? (19)

Everything in the token is also authenticated with AEAD, so it’s not possible to tamper with it.

Moreover, if you use a local symmetric-key algorithm, the payload is now encrypted, not just encoded, so it’s impossible for hackers to read or change the data stored in the token without knowing the server’s secret key.

Sounds amazing, right?

In the next lecture, I’m gonna show you how to implement both JWT and PASETO using Golang.

You will see PASETO not only makes it safer but also easier and so much simpler to implement, compared to JWT.

For now, let’s take a look at the structure of a PASETO token.

Why PASETO is better than JWT for token-based authentication? (20)

This is a PASETO version 2 token for local usage purposes. There are 4 main parts of the token, separated by a dot.

The first part is PASETO version (with red color), which is version 2.

The second part is the purpose of the token, is it used for local or public scenarios? In this case, it is local, which means using a symmetric-key authenticated encryption algorithm.

The third part (with green color) is the main content or the payload data of the token. Note that it is encrypted, so if we decrypt it using the secret key, we will get 3 smaller parts:

Why PASETO is better than JWT for token-based authentication? (21)

  • First, the payload body. In this case, we just store a simple message and the expiration time of the token.
  • Second, the nonce value that is used in both encryption and message authentication process.
  • And finally the message authentication tag to authenticate the encrypted message and its associated unencrypted data.

Why PASETO is better than JWT for token-based authentication? (22)

In this case, the unencrypted data is the version, the purpose, and the footer of the token (with purple color).

You can store any public information in the footer because it won’t be encrypted like the payload body, but only base64 encoded. So anyone who has the token can decode it to read the footer data.

Why PASETO is better than JWT for token-based authentication? (23)

In this case, it is Paragon Initiative Enterprises, the one who invented PASETO.

Note that the footer is optional, so you can have a PASETO token without a footer. For example, this is another PASETO token for the public usage scenario:

Why PASETO is better than JWT for token-based authentication? (24)

It only has 3 parts, with no footer. The first part is PASETO version, which is version 2.

The second part is its purpose: public in this case, which means an asymmetric-key digital signature algorithm is used to sign the token, and its payload data will not be encrypted, but only base64 encoded.

As you can see here, the green part of the payload is actually the encoded body, which we can easily decode to get this JSON object.

Why PASETO is better than JWT for token-based authentication? (25)

While the blue part of the payload is the signature of the token, created by the digital signature algorithm using the private key.

The server will use its paired public key to verify the authenticity of this signature.

Why PASETO is better than JWT for token-based authentication? (26)

And that’s it for today’s lecture about token-based authentication.

We have learned about the design flaws of JSON Web Token that causes many security issues in the past, and how PASETO was invented to fix all of those problems, and thus, make our developers’ life much easier.

I hope you like this article, and see you in the next one, where we will write codes to create and verify JWT and PASETO tokens in Golang.

If you like the article, please subscribe to our Youtube channel and follow us on Twitter or Facebook for more tutorials in the future.

If you want to join me on my current amazing team at Voodoo, check out our job openings here. Remote or onsite in Paris/Amsterdam/London/Berlin/Barcelona with visa sponsorship.

Why PASETO is better than JWT for token-based authentication? (2024)

FAQs

What is the difference between JWT and Paseto? ›

Key Differences between Paseto and JWT

Unlike JSON Web Tokens (JWT), which gives developers more than enough rope with which to hang themselves, Paseto only allows secure operations. JWT gives you "algorithm agility", Paseto gives you "versioned protocols".

Which is better alternative for JWT? ›

OAuth2, Passport, Spring Security, Auth0, and Keycloak are the most popular alternatives and competitors to JSON Web Token.

What is the purpose of Paseto? ›

What is PASETO? PASETO is a new specification (currently a draft RFC) that allows you to create secure and stateless tokens that can be safely shared over the web. Essentially, PASETO allows you to take JSON data and condense it into a single token you can easily share over the internet in a safe, tamperproof way.

What are the downsides of using JWT for authentication? ›

One of the major cons of relying on tokens is that it relies on just one key. Yes, JWT uses only one key, which if handled poorly by a developer/administrator, would lead to severe consequences that can compromise sensitive information.

Which is better JWT or Passport? ›

JSON Web Token has a broader approval, being mentioned in 29 company stacks & 15 developers stacks; compared to Passport, which is listed in 11 company stacks and 11 developer stacks.

Why JWT is not good for sessions? ›

Although JWT does eliminate the database lookup, it introduces security issues and other complexities while doing so. Security is binary—either it's secure or it's not. Thus making it dangerous to use JWT for user sessions.

What is the difference between JWT and token? ›

Once the token is generated, it is used across the ecosystem to determine what the token holder can and cannot do. Additionally, API keys authenticate the application not the user; whereas, JWT authenticates both the user and the application.

When should you not use JWT? ›

The reason to avoid JWTs comes down to a couple different points:
  1. The JWT specification is specifically designed only for very short-live tokens (~5 minute or less). ...
  2. 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.

What is Paseto token format? ›

The PASETO Format

The PASETO token format has two different versions: v1 is a compatibility mode, which is ideal for legacy systems and uses cryptographic primitives that are wildly available today. v2 is the recommended option, which uses the latest cryptographic primitives.

What is the difference between local and public Paseto? ›

The PASETO specification defines two types of tokens: local and public. Local tokens are always symmetrically encrypted with a shared secret key, which means no one can view the contents of a local PASETO unless they have the correct secret key. Public tokens are readable by anyone and are validated with a public key.

What is the point of a JSON Web Token Why would we want to use it? ›

Information Exchange: JWTs are a good way of securely transmitting information between parties because they can be signed, which means you can be sure 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.

Which is better JWT or 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.

What is the difference between password based and token based authentication? ›

Token-based authentication is different from traditional password-based or server-based authentication techniques. Tokens offer a second layer of security, and administrators have detailed control over each action and transaction. But using tokens requires a bit of coding know-how.

Why use token instead of password? ›

At its core, authentication is a method for verifying that a user is who they claim to be, and used to keep bad actors out of your network. Unlike passwords, which can be easily compromised and used by hackers for data breaches, tokens are more secure. 61% of data breaches involve the use of unauthorized credentials.

Is Passport good for authentication? ›

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

Is Passport using JWT? ›

A Passport strategy for authenticating with a JSON Web Token. This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.

Is JWT enough for authentication? ›

JWTs can be used as an authentication mechanism that does not require a database. The server can avoid using a database because the data store in the JWT sent to the client is safe.

Why JWT token is more secure? ›

The general opinion is that they're good for being used as ID tokens or access tokens and that they're secure — as the tokens are usually signed or even encrypted. You have to remember though, that JWT is not a protocol but merely a message format.

Is JWT best security? ›

It is designed for allowing parties to transmit information securely. JWT is a good choice when implementing custom security mechanisms in applications because, in addition to the security, almost every popular technology provides support for JWTs.

How secure is token authentication? ›

Because tokens can only be gleaned from the device that produces them—whether that be a key fob or smartphone—token authorization systems are considered highly secure and effective. But despite the many advantages associated with an authentication token platform, there is always a slim chance of risk that remains.

What are the dangers of JWT? ›

As a result, using JWT for user sessions is dangerous. The biggest problem with JWTs is that the token will continue to work until it expires, and the server has no easy way to revoke it. This could be extremely dangerous in situations such as the following: Logout doesn't actually log you out of the system.

Is JWT more secure than session? ›

JWTs versus sessions cookies

JWTs enable faster authorization and more interoperability with external apps, but they demand more developer investment to address their security complexities, and might not be the best fit for applications that enable access to sensitive data or actions.

Which is better session or token? ›

These methods are usually used for different purposes. For example, sessions are commonly used in websites applications while tokens are preferred in server-to-server connections.

What are the 3 parts of JWT token? ›

Figure 1 shows that a JWT consists of three parts: a header, payload, and signature.

What are the types of token based authentication? ›

Types of tokens
  • Access tokens.
  • ID tokens.
  • Self-signed JWTs.
  • Refresh tokens.
  • Federated tokens.
  • Bearer tokens.

Is JWT outdated? ›

JWT Deprecation - The JWT app type will be completely deprecated as of June 2023. New and current users will have 12 months to migrate their JWT based solutions to the server-to-server OAuth app type.

Does Netflix use JWT? ›

Lately, Netflix has been partnering with device manufacturers to merchandise Netflix content to members as well as nonmembers, and sometimes from the partners' UI itself. For these integrations, we built specific APIs and we chose to use an open standard like JWT to better integrate with partner infrastructure.

What is the lifespan of JWT token? ›

The JWT access token is only valid for a finite period of time. Using an expired JWT will cause operations to fail. As you saw above, we are told how long a token is valid through expires_in . This value is normally 1200 seconds or 20 minutes.

What is difference between JWT and JWE? ›

JWT is an abstract concept about how to allow one or more parties to exchange information securely. The implementation of JWT comes in the form of JWS or JWE. The difference function between JWS and JWE is that JWS allows everyone to see its payload, while JWE doesn't allow it by using an encryption method.

Is JWT token base64? ›

The format of a JWT token is simple: <base64-encoded header>.

What is JSON Web Token based authentication? ›

JWT authentication is a token-based stateless authentication mechanism. It is popularly used as a client-side-based stateless session, this means the server doesn't have to completely rely on a data store (or) database to save session information. JWTs can be encrypted, but they are typically encoded & signed.

Is localStorage more secure than cookies? ›

Cookies are not accessible via JavaScript provided the proper secure attributes for the cookies are set. This makes cookie data less vulnerable than localStorage data to JavaScript-based attacks. Also, there are a variety of approaches to securing cookies, so you might have more security options than with localStorage.

Why should we use JWT instead of cookie based authentication? ›

This makes JWTs more suitable to use in an API, since the API server doesn't need to keep track of user sessions. Cross-platform capabilities: Because of their stateless nature, tokens can be seamlessly implemented on mobile platforms and internet of things (IoT) applications, especially in comparison to cookies..

Why we use JWT token for authentication? ›

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 are benefits of authentication tokens? ›

Tokens streamline the login process: Authentication tokens ensure that users do not have to re-enter their login credentials every time they visit a website. Tokens add a barrier to prevent hackers: A 2FA barrier to prevent hackers from accessing user data and corporate resources.

What is 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 the same as SAML? ›

SAML is independent of OAuth, relying on an exchange of messages to authenticate in XML SAML format, as opposed to JWT. It is more commonly used to help enterprise users sign in to multiple applications using a single login.

Is JWT front end or backend? ›

The restrictions could be anything ranging from the availability of a user to the role of the user. This is referred to as authorization. In this post, I am going to show you how to implement authorization with a frontend (React) and a backend (Node JS) using JSON Web Token (JWT).

Is JWT signed or encrypted? ›

JSON Web Tokens (JWT) are commonly used in many different applications which require some sort of cryptographic primitive. Most often, the JSON Web Signature (JWS) structure is chosen as its contents are signed and not encrypted; however, the JSON Web Encryption (JWE) structure may also be used to make a JWT.

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6480

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.