Vincent
Created: October 29, 2023
Updated: May 15, 2024
A JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. These claims can be user data or other relevant information encrypted for security purposes. JWTs are used in authentication and authorization protocols, including OAuth 2.0 and OpenID Connect, but can also be used in any context where claims about a subject need to be conveyed and integrity protected possibly also ensuring confidentiality.
JWTs consist of three parts: a header, a payload, and a signature.
The combination of these three parts provides a robust framework for the transmission of data with validation.
JWTs offer several advantages over traditional cookies and sessions. They are:
However, like all technologies, JWTs have their challenges. One needs to ensure the token's security and manage token expiration appropriately to prevent unauthorized access.
Traditionally, sessions and cookies were used for user authentication. When a user logs in, the server creates a session for the user, and the session ID is stored in a cookie on the user's browser. For subsequent requests, this session ID is used to fetch the session data and validate the user. JWTs, on the other hand, eliminate the need for sessions and cookies since the token itself contains all the necessary information about the user. This makes JWT a preferred choice for single-page applications and API-based architectures, where stateless authentication mechanisms are more suitable.
JWT claims are pieces of information asserted about a subject within a JSON Web Token. Claims are presented as name/value pairs within the JWT payload. These claims include both standard claims, which are predefined in the JWT specification, and custom claims, which are additional user-defined attributes relevant to the specific application.
Standard claims are predefined in the JWT specification and are intended to facilitate interoperability between different systems. Common standard claims include:
Custom claims are additional claims that are not registered or predefined by the JWT standard. These can be used to convey information specific to your application, such as user roles or other attributes. Custom claims should be namespaced to avoid collisions with standard claims and other custom claims.
JWT is a token format, while a cookie is a storage mechanism. JWT can be stored in a cookie, but unlike session cookies, JWT can contain more user-specific data and doesn't rely on server-side session management.
JWTs are primarily used for authentication and secure data exchange. They are commonly used in single-page applications, mobile applications, and API-based architectures for stateless, server-side authentication.
JWT itself is just a data format, so its security depends on its usage. If implemented correctly, with proper signature validation and using HTTPS, JWT can be as secure as sessions. However, JWTs can be vulnerable if not properly handled, especially if the token is leaked or not encrypted when storing sensitive information.
Standard tokens in JWT refer to predefined claims specified in the JWT standard, which are intended to ensure interoperability between different systems. Examples include iss (issuer), exp (expiration time), and sub (subject), which provide essential data about the token's context and usage.
Custom tokens in JWT are user-defined claims that allow you to include additional information specific to your application needs, such as user roles or operational permissions. These are not registered or predefined and should be carefully namespaced to avoid collisions with standard claims.
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free