JWT vs PASETO: New Era of Token-Based Authentication

Ege Aytin
Permify Tech Blog
Published in
12 min readJun 6, 2024

This article delves into a comprehensive comparison of Paseto and JWT, dissecting their core functionalities, security features, and potential drawbacks.

APIs and modern web applications have brought token-based authentication to the forefront of secure authorization methods.

Offering advantages like scalability, statelessness, and enhanced security compared to traditional session-based authentication, tokens have become the preferred choice for developers worldwide.

Among various token-based approaches, JSON Web Token (JWT) has gained widespread popularity because of its simplicity and ease of implementation.

However, concerns surrounding potential vulnerabilities and the need for meticulous implementation have led to the exploration of more robust alternatives.

Paseto (Platform-Agnostic Security Tokens) has emerged as a better solution, directly addressing the shortcomings of JWT.

Designed with a focus on security, Paseto provides a more secure foundation for token-based authentication by mitigating vulnerabilities and enforcing secure defaults.

This article delves into a comprehensive comparison of Paseto and JWT, dissecting their core functionalities, security features, and potential drawbacks.

By analyzing their respective strengths and weaknesses, aiming to equip you with the knowledge to make informed decisions regarding token-based authentication in their projects.

A small request 🤗 We’re aiming to get our open-source project Permify to 3k stars. Could you help us out by starring the repository?

https://github.com/Permify/permify

Your support means a lot!

How Does Token-Based Authentication Work?

Token-based authentication provides a secure and efficient way to manage user access in modern applications. Unlike traditional session-based methods that rely on server-side storage, token-based systems issue tokens to clients upon successful authentication.

Let’s break down the typical flow:

  1. User Login: The user initiates the process by providing their credentials (username and password) to the application.
  2. Authentication: The application validates these credentials against a database or other authentication mechanism, verifying the user’s identity.
  3. Token Generation: Upon successful authentication, the application generates a unique, digitally signed token containing relevant user information and permissions. This token acts as a secure representation of the user’s identity and access rights.
  4. Token Delivery: The application sends the generated token to the client, usually included in the HTTP response header or body.
  5. Client-Side Storage: Here the client securely stores the received token, often in local storage, session storage, or cookies, for use in subsequent requests.
  6. Resource Requests: When the client needs to access a protected resource, it includes the token in the authorization header of the HTTP request. This signals to the server that the client is attempting to access a restricted area.
  7. Token Verification: When a request with a token is received, the server confirms its validity and integrity by utilizing the corresponding secret key or public key for the token’s signing algorithm.
  8. Access Control: Based on the validated token and its embedded permissions, the server determines whether the client has the necessary authorization to access the requested resource. If authorized, the server grants access and fulfills the request. Else, access is denied.

Let’s visualize the flow:

A flowchart illustrating the token-based authentication process

What is JWT?

JWT stands for JSON Web Token. It’s an open standard (RFC 7519) defining a compact and self-contained method for securely transmitting information between parties as JSON objects.

JWTs are commonly used to verify user identities and granting access to private resources. Also with JWT you can securely share information between applications.

A JWT comprises three parts:

Header: This defines the token type (JWT) and the signing algorithm used. Example,

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

Payload: It contains statements about an entity (typically, the user), and additional data. Example,

{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}

Signature: This verifies the token’s authenticity and integrity. Its created by combining the encoded header, encoded payload, a secret, and the specified signing algorithm. Example (using HMAC SHA256):

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

How JWT Works?

The process involves:

  1. Token Generation: Upon successful user authentication, the server generates a JWT containing user information and permissions. This token is signed using a secret key.
  2. Token Sent to Client: The server sends the JWT to the client, usually within the HTTP response header.
  3. Client Stores Token: The client securely stores the JWT, often in local storage or cookies.
  4. Client Requests Resource: The client includes the JWT in the authorization header for subsequent requests to private resources.
  5. Server Validates Token: The server validates the JWT’s signature and expiration time by using the secret key.
  6. Access Granted/Denied: Based on the token validation, the server grants or denies access to the requested resource.

Pitfalls Of JWT

While JWT offers many advantages, it’s essential to be aware of potential pitfalls and security concerns that can arise if not implemented properly.

Here are some key issues associated with JWT tokens:

Security Risks from Improper Algorithm Choice

There are different ways to encrypt JWTs. If the encryption method isn’t properly chosen or implemented, it can be vulnerable to attacks.

Key Management Is Issues

If the secret key used to create and verify JWTs is compromised, an attacker could forge tokens and gain unauthorized access.

Revocation Challenges

It can be tricky to revoke a JWT once it’s been issued. This means that if a user’s credentials are compromised, their token might still be valid.

Bypassing Signature Verification

Vulnerabilities in certain JWT libraries and implementations allow for signature verification to be bypassed. These vulnerabilities could enable attackers to create forged tokens that appear legitimate to the application.

What is PASETO (Platform Agnostic Security Token)?

Paseto, which stands for Platform-Agnostic Security Tokens, is a specification for secure stateless tokens.

It provides a modern and better alternative to JWT, addressing some of its inherent vulnerabilities and emphasizing secure defaults and ease of implementation.

Paseto Structure

Unlike JWT’s single, generic structure, Paseto employs a versioned approach with two distinct token purposes:

  • Local tokens: This is designed for stateful, server-side sessions where the tokens are securely stored on the server-side and associated with a user’s session.
  • Public tokens: This is intended for stateless applications and use cases involving public-key cryptography. These tokens can be securely transmitted and verified without needing server-side storage.

Both local and public tokens share a similar structure, consisting of three parts:

  • Header: This section identifies the Paseto version and purpose (local or public) along with the specific cryptographic algorithm used.
  • Payload: Similar to JWT, the payload contains claims representing information about an entity (usually the user) and any additional data relevant to the application.
  • Footer (optional): The footer can include additional authenticated data, providing extra security and context to the token.

How PASETO Works?

Paseto’s core strength lies in its focus on secure defaults and well-defined implementations.

It eliminates the risk of algorithm confusion, a known vulnerability in JWT, by explicitly specifying which cryptographic algorithms should be used for each version and purpose. This approach ensures that developers don’t inadvertently choose insecure options.

  • Local tokens: Typically use symmetric-key cryptography, where the same secret key is used for both encryption and decryption. This makes them suitable for server-side session management, where the server maintains control over the key.
  • Public tokens: Employ public-key cryptography, involving a public key for encryption and a private key for decryption. This enables secure communication and verification without sharing the secret key.

How to Implement JWT or Paseto in Your Project?

Here’s a basic guideline for implementing either JWT or Paseto in your project:

  1. Choose a Library: Select a library for your project. There are several options available for popular languages like Python, Node.js, Java, and Ruby.
  2. Generate a Secret Key: Create a strong and secure secret key to sign and verify tokens. Store it securely. For local tokens, generate a secure secret key. For public tokens, generate a public-private key pair.
  3. Implement Token Generation: Develop a mechanism to generate the token upon successful user authentication, including relevant claims in the payload.
  4. Implement Token Validation: Implement logic on the server-side to validate the token received in requests, verifying the signature and expiration time.
  5. Secure Token Storage: On the client-side, securely store the JWT (e.g., HttpOnly cookies with the Secure flag).
  6. Handle Token Expiration: Implement a strategy to handle expired tokens, such as refresh tokens or requiring re-authentication.
  7. Payload extraction and verification: This steps works only for Paseto, where the payload and verified the to ensure the authenticity and integrity of the information.

Key Differences Between Paseto vs JWT

Structure

Security Features

Use Case Scenarios

token structures

An image showing the structural difference of Paseto and JWT

Understanding how tokens are revoked is an important aspect of building secure systems. Let’s take a closer look at token revocation in the next section.

Token Revocation: A Deeper Dive

Token revocation is an essential part of secure authentication. It ensures that compromised tokens are invalidated, preventing unauthorized access. Here’s a closer look at how it works with JWT and Paseto:

JWT

  • Because JWT is typically stateless, a server doesn’t keep track of issued tokens unless a specific revocation mechanism is used.
  • Common approaches for JWT revocation:
  • Blacklists: Maintaining a list of revoked tokens, which the server checks before granting access.
  • Token Binding: This approach links the token to a specific user agent (like a browser), making it invalid if the user agent changes.
  • Dedicated Revocation Service: A separate service can manage token revocation and communicate with the server.

Paseto

  • Paseto’s local tokens: As discussed earlier, the server does maintain a list of issued local tokens, making revocation simpler. When a user logs out, the server can invalidate the token, preventing further access.
  • Paseto’s public tokens: These are stateless, so the server doesn’t have a list of active tokens. You would need to use a mechanism like token binding, a separate revocation service, or another solution to handle revocation.

Best Practices for Token Revocation:

  • Implement a better revocation mechanism: Choose an approach that aligns with your application’s security requirements and the type of tokens you’re using.
  • Ensure proper communication: If you’re using a dedicated revocation service, make sure it is securely integrated with your application.
  • Test your revocation process: Regularly test your revocation system to confirm that it works as expected.

Beyond token revocation, there are other architectural patterns that can improve token security and management. One such pattern is the Backend for Frontend (BFF).

The Backend for Frontend (BFF) Pattern

The Backend for Frontend (BFF) pattern is a powerful approach to handling authentication and token management. It often works with Oauth. The BFF involves a server-side frontend (like Next.js) that acts as a bridge between the client and the backend.

The BFF manages tokens server-side, only sending cookies to the client. This removes the need for client-side token storage, enhancing security.

Benefits of the BFF pattern:

  • Enhanced Security: Keeping tokens server-side reduces the risk of client-side vulnerabilities and token theft.
  • Simplified Client Development: Clients only need to manage cookies, making development easier.
  • Improved Performance: The BFF can optimize API requests for the client.

You can consider using the BFF pattern, by assessing whether the BFF pattern aligns with your architecture and security requirements.

Choosing Between Paseto and JWT

Both Paseto and JWT offer distinct advantages and disadvantages, making the choice dependent on your specific needs and priorities.

Here are some factors that can influence your decision:

Security Needs

  • Paseto: If your application demands robust security and protection against common vulnerabilities, then Paseto is probably the best fit for you. Because it is designed to mitigate issues like algorithm confusion and promote better key management practices, ensuring a higher level of security.
  • JWT: While JWT can be secure when implemented correctly, it necessitates meticulous attention to detail and a thorough understanding of potential pitfalls. Developers must be vigilant in avoiding common misconfiguration and vulnerabilities.

Application Architecture

  • Paseto: Paseto offers a clear distinction between local and public tokens, catering to different architectural requirements. Local tokens, designed for stateful server-side sessions, are ideal for traditional web applications with session management. Public tokens, geared towards stateless applications and public-key cryptography, align well with microservices and API-driven architectures.
  • JWT: It’s flexible structure accommodates both stateful and stateless applications. However, this flexibility can also lead to ambiguity and potential misuse, if not handled carefully.

Developer Familiarity

  • Paseto: While Paseto’s ecosystem is steadily growing, it might not yet match the breadth and depth of JWT’s support. Developers might need to invest additional effort in finding appropriate libraries and understanding Paseto’s specific implementations.
  • JWT: Due to its earlier adoption and widespread usage, JWT benefits from a larger community and readily available libraries, frameworks, and documentation across various programming languages. This makes it easier for developers to find resources and implement JWT within their projects.

Ecosystem Support

  • Paseto: It’s ecosystem is expanding, with libraries and tools becoming increasingly available for popular programming languages. However, it may not yet match the comprehensive support of JWT, particularly for less common languages or frameworks.
  • JWT: JWT enjoys extensive support across numerous programming languages, frameworks, and libraries. This widespread adoption ensures readily available resources and simplifies integration with existing tools and infrastructure.

The Future of Web Tokens

The web tokens landscape is constantly evolving, driven by advancements in cryptography, changing security threats, and the ever-growing need for secure and efficient authentication mechanisms.

Here are some emerging ideas that may shape the future of web tokens:

Enhanced Security and Cryptography

  • Quantum-resistant cryptography: With the looming possibility of quantum computers breaking existing cryptographic algorithms, the development and adoption of quantum-resistant algorithms will be crucial for future-proofing web tokens.
  • Post-quantum cryptography: Research and standardization efforts are underway to develop and integrate post-quantum cryptography algorithms into token-based systems, ensuring long-term security and resilience against quantum threats.

Decentralized Identity and Self-Sovereign Identity (SSI)

  • Verifiable credentials: With the rise of verifiable credentials, this allows individuals to control and manage their digital identities, which may influence how tokens are used for authentication and authorization.
  • Decentralized Identifiers (DIDs): This enables decentralized and self-owned identifiers, and could be integrated with token-based systems to enhance privacy and user control over personal data.

Improved Usability and Standardization

  • Simplified token management: Efforts to streamline token generation, revocation, and renewal processes will improve user experience and developer efficiency.
  • Standardization of token formats and protocols: Further standardization of token formats and communication protocols will promote interoperability and simplify integration across different platforms and services.

Emerging Token Mechanisms

  • Macaroons: This offer a more flexible and granular approach to authorization, allowing for delegation and attenuation of access rights.
  • Token binding: This mechanisms can mitigate token theft and replay attacks, enhancing the security of token-based systems.

Influence on Paseto and JWT

Paseto’s focus on security and well-defined use cases positions it well for adoption in environments with high-security requirements and where standardization is valued.

Its versioned approach allows for adaptation and integration of future cryptographic advancements.

JWT’s flexibility and widespread adoption may lead to its continued use in various applications. However, its security shortcomings might necessitate additional safeguards and careful implementation practices to mitigate risks.

The future of web tokens will probably involve a combination of existing and emerging mechanisms, each catering to specific needs and security considerations.

Summing Up

In this article, we’ve highlighted the strengths and weaknesses of each token mechanism, emphasizing the importance of understanding your specific needs and priorities.

While JWT provides simplicity and flexibility, Paseto prioritize security and well-defined use cases.

Evaluating factors such as security requirements, application architecture, and developer familiarity will guide you toward the most suitable option.

Additionally, exploring emerging solutions like Permify, which offers a comprehensive authorization platform with fine-grained access control, can further enhance your application’s security and flexibility.

The choice between JWT and Paseto is not a one-size-fits-all answer, but a decision based on your unique context.

Let’s keep the conversation going! Join our Discord community to share your thoughts and experiences with JWT, Paseto, and other token mechanisms.

References

JWT Resources:

Paseto Resources:

Further Reading:

--

--

Ege Aytin
Permify Tech Blog

Product person, passionate about Golang and open source software. Building Permify, an open-source authorization service: https://github.com/Permify/permify