JSON Web Token (JWT)

A JSON Web Token (JWT) is a compact format for transferring information to verify the identity of an entity and ensure data integrity.

Feb 3, 20269 min read

What is a JSON Web Token (JWT)?

A JSON Web Token, or JWT for short, is a standardized access token that enables the secure exchange of data in the form of JSON objects between two parties. Because it contains all the essential information about an entity, no database query is required, and the session does not need to be stored on the server. Verified by a digital signature, JWTs are frequently used for authentication and authorization, such as enabling Single Sign-On (SSO) processes or securing APIs. Their compact structure, combined with cryptographic validation, provides a robust option for securing communication between parties.

JSON Web Tokens are defined and standardized in the RFC 7519 specification. RFC stands for “Request for Comments,” which describes a series of numbered documents that define internet standards, protocols, and concepts. These serve as the basis for communication between different devices on the internet.

JSON Web Tokens consist of the JSON text format and digital credentials in the form of tokens.

JSON

JSON stands for “JavaScript Object Notation”, an open and language-independent text format used to store and transmit data. Its ease of use makes it ideal for web applications and APIs, as it is easy for humans to understand and easy for machines to generate or parse.

Token

Tokens are digital credentials that have become indispensable in modern authentication systems. They store a user’s identity and authorization in a compact and secure form, ensuring that only authorized users can access protected resources.

Structure of JSON Web Token

A signed JSON Web Token is a simple string consisting of three parts separated by periods and encoded in Base64. Users don’t typically see the JSON Web Token string itself. However, they interact with it when using a website or application that uses JWTs in the background. The three parts of a JSON Web Token are the header, payload, and signature.

The header, which usually consists of two parts, provides important information about the token. It describes the token type and the signature and/or encryption method used.

Payload

The payload contains the actual user information to be transmitted to the application. This could include, for example, user ID, name, or expiration time. Provided as key/value pairs, this information is also referred to as “claims”, with a distinction made between private, public, and registered claims. Payloads can contain any number of claims. However, the larger the JSON Web Token (JWT) becomes, the more resources are required for encoding and decoding. A JWT is typically transmitted in the headers of HTTP requests. Therefore, the larger the token, the larger the actual request. It is thus recommended to limit the information contained in the JWT to the bare minimum.

Signature

The final step is the signature. This is created by combining the encoded header, the encoded payload, a secret key, and the signature or encryption method specified in the header. Thes signing ensures that the message is not altered in transit and that the sender of the JSON Web Token is indeed who they claim to be.

There are different methods, depending on the sensitivity of the data.

  1. No Security: With a low security level, a signature can be omitted. In this case, the JWT consists only of a header and payload and is readable in plaintext after Base64 decryption. Whether the message originates from the correct sender or has been altered in transit cannot be verified. This method is almost never used. Without a signature, a JWT offers no added security and can be functionally replaced by other, simpler data formats.

  2. Signature (JWS): The JSON Web Signature (JWS) scheme is used to ensure that a message originates from the correct sender and has not been altered in transit. This verification is sufficient for most scenarios. Even with this method, the payload can be read in plaintext after Base64 decryption. Signature (JWS) is considered a standard procedure and is used by almost all systems.

  3. Signature (JWS) and Encryption (JWE): In addition to JSON Web Signature (JWS), JSON Web Encryption (JWE) can be used. In this case, JWE encrypts the payload contents, which are then signed with JWS. The contents are decrypted by providing a shared password or a private key. After Base64 decryption, the payload is not readable in plaintext, the sender is verified, and it is secured that the message is authentic and confidential. This method is a niche approach and is rarely used because application support is very limited. In practice, it is usually sufficient to use the signature method (JWS, see section 2) in combination with secure and protected connections and to limit the information contained in the payload to the necessary minimum.

Each of the methods mentioned should additionally use SSL (Secure Sockets Layer) to establish an encrypted connection between client (e.g. a web browser) and server to protect the data during communication.

How a JSON Web Token Works

The functionality of a JSON Web Token can be easily explained using a user login as an example:

When a user logs in with their e-mail address and password, the server creates a JSON Web Token (JWT). This token is encoded with the relevant information and signed with a secret key (secret) or a private key (in the case of an asymmetric signature) to prevent misuse. After the JWT is created, it is sent to the client and stored locally.

With each subsequent request to the server, the client sends the JWT, for example, as a parameter in the GET request. The server decrypts the JWT, verifies the authenticity of the token, and checks information such as the issuer, target audience, and validity period. After successful verification, the client gains access to the requested resource.

Additional security is provided by an expiration time that JWTs typically have. During validation, the server checks the token’s validity period and rejects processing if it has expired. In this case, the client is prompted to update the token.

Advantages of Using JSON Web Tokens

JSON Web Tokens are very useful for applications that exchange information between different systems. They enable the secure exchange of sensitive data, are extremely flexible, scalable, and lightweight. This makes them easy to use with mobile applications.

Further advantages include:

Disadvantages of Using JSON Web Tokens

Where there is light, there is also shadow. And while JWTs offer many advantages, we also want to mention some drawbacks of using JSON Web Tokens.

Use Cases for JSON Web Tokens

Due to their enhanced security and flexibility, JSON Web Tokens (JWTs) are the ideal choice for various authentication methods. They are primarily used for stateless authentication, authorization, and session management in modern applications. JWTs enable both server-side and client-side applications to securely authenticate users and exchange critical information without requiring a database query for every request. Below are some of the most common use cases for JSON Web Tokens:

Conclusion

A JSON Web Token (JWT) is a powerful tool and therefore ideal for authentication and data exchange. Servers don’t need to store session state, as JWTs contain all the necessary information. They are flexible, scalable, compact, and easily transferable.

However, JWTs are not encrypted by default. Sensitive data must therefore be additionally encrypted using JSON Web Encryption (JWE). The validity period should also be carefully chosen, with shorter periods being recommended.

Thus, and despite some limitations, JSON Web Tokens, together with OpenID Connect (OIDC) and OAuth 2.0, form a solid, modern foundation for secure authentication and authorization.