JSON Web Token (JWT)

In recent years, role-based access control has emerged particularly for distributed and microservices-based applications. JSON Web Tokens (JWT) have proven themselves above all in the popular and convenient SSO (single sign-on) process. Beyond that, JWTs can also be used much more flexibly in client-server processes in which signed and encrypted information has to be transmitted.

Security plays a particularly large role here and also poses a particular challenge in the context of microservices and a matching design. Compared with sessions and cookies, JSON Web Tokens are “stateless.” For this reason, all client-server requests have to carry all the information needed for processing. The JWT standard, which is based on OpenID Connect (OIDC), is nevertheless particularly well suited to this.

The Advantages of JSON Web Tokens

Compared with conventional authorization methods, such as storing cookies on the systems or working with a PHP session, the use of JSON Web Tokens offers considerable advantages. Here are the most important advantages at a glance.

  • Using JSON Web Tokens is stateless and therefore more secure than working with session variables and cookies. The information required for authentication is supplied and verified again with every request.
  • JSON Web Tokens can be integrated and used very easily in many frameworks and programming languages, such as .NET, Python, Java, JavaScript, Perl, or Ruby, without major integration effort. You can find a complete list in the jwt.io project.
  • A major advantage of JWT is its reliable authentication. In addition, the required network overhead is kept low, which is especially noticeable in the performance of mobile applications.
  • Compared with SAML applications and RESTful APIs, for example, the use of JSON Web Tokens results in a considerably smaller footprint, which has a positive effect on overall performance.

Structure and Function of the JWT

A JSON Web Token is a comparatively simple construct that is designed purely as a text string. This string is then transmitted either as a request parameter or in the header of a client/server request. The structure of the token always follows the three-part scheme below, with the sequences separated by dots.

  • The first sequence is the header of the token. Its content describes the signature and/or encryption algorithm used as well as the token type used. All information is stored in JSON format and additionally encoded with a Base64 algorithm. The details are generated as value pairs following the scheme {"alg":"HS256", "typ":"JWT"}. The Base64 code for this header would be [e+KAnmFsZ+KAnDri gJxIUzI1NuKAnCwg4 oCedHlw4oCcOu KAnEpXVOKAnH0=].
  • The second sequence contains the payload, that is, any list of value pairs. These value pairs are also called “claims” and can be divided into registered, public, and private types. A corresponding example would be {"IdNr":“140075“, "name":"Test", "vorname":"Manfred"}. Encoded in Base64, this payload corresponds to the sequence [e+KAnklkTnLig Jw64oCcMTQw MDc14oCcLCDigJ5uY ` `W1l4oCcOuKAn FRlc3TigJwsIO KAnnZvcm5hbWXi gJw64oCcTWFuZ nJlZOKAnH0=].
  • The last sequence contains a signature that is calculated separately from the two parts with header and payload and a secret parameter. The calculation can be represented symbolically with the pseudocode content=bas64UrlEncode(header) . base64UrlEncode(payload) . signature=HMACSHA256(content,secret).

Standardized Claims

As a rule, a claim (value pair) is used in the header of a token to represent a specific purpose. The function of a registered claim is defined in the RFC 7519 standard. Examples are “sub” for a user identifier or “exp” for a time span and the validity of a token. Public claims allow additional value pairs to be used. To avoid collisions with registered claims, they are publicly registered. You can find an overview at www.iana.org. Private claims are custom keys that may, however, have differing semantics. They are used whenever two parties want to model an explicit use case and agree on a special key for it.

The JWT Flow in Brief

Using JSON Web Tokens is simple, and the authorization process is quickly explained. SSO or a classic user login can serve as application examples. The sequence after login is as follows.

  • After the login data has been submitted, an authorization service checks the credentials and creates a new JSON Web Token. In addition to the header, the payload and the private secret are worked into this token and signed accordingly. Ideally, an expiration date is also used for the token validity, which also increases security.
  • An application can now send the token created this way in the Authorization header, for example to authorize itself with a website. As mentioned above, the individual sequences of the JWT are separated from one another by dots.
  • The website, or a microservice, can now check the token for validity. If the result is positive, the user is thereby legitimized, classified as trustworthy, and may now use the services the provider offers.

Conclusion

JSON Web Tokens provide an easily integrated, versatile, and above all standardized solution for SSO and classic login implementations. JWTs are therefore particularly well suited to authorization and the exchange of information in secured environments between two parties. Optional and multiple encryption safeguards the integrity and authenticity of the content. As a rule, integrating JSON Web Tokens into your own projects no longer requires any programming of your own. A large number of ready-made implementations exist for different programming languages and can simply be used. Because JWTs also have some security gaps, however, particularly sensitive data, above all in the payload, should be encrypted carefully and strongly.

Share LinkedIn X Email