Introduction to JWT (Also JWS, JWE, JWA, JWK) (2024)

Cybersecurity · · 11 min read

The security and privacy of users' data have been a growing concern for the past few years. At the same time, JWT, as one technology to combat it, has been used more and more.

  • Brilian Firdaus

The security and privacy of users' data have been a growing concern for the past few years. At the same time, JWT, as one technology to combat it, has been used more and more.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (2)

The security and privacy of users' data have been a growing concern for the past few years. At the same time, JWT, as one technology to combat it, has been used more and more. Understanding JWT will give you an edge over the other software engineers. JWT might seem simple at first, but it is pretty hard to understand.

In this article, we will explore mainly JWT and JWS. In addition, we'll also go through JWE, JWA, and JWK quickly. This article aims to make the reader understand the concept of JWT without diving too deep into the topic.

Note: If you're interested in learning how to implement JWT using Java Spring Boot, you can visit my other article:

How to Implement JSON Web Token (JWT) in Java Spring BootJSON Web Token or JWT has been famous as a way to communicate securely between services. There are two form of JWT, JWS and JWE. This article will explore the implementation of the JWT in Java Spring Boot.CodeCuratedBrilian Firdaus

What Are They?

Before taking a deeper look, it‘s better if we know the connection between JWT, JWS, JWE, JWA, and JWK.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (5)

From the diagram above, we can see that:

  • JSON Web Token (JWT) is an abstract that is represented in the form of JSON Web Signature (JWS) and JSON Web Encryption (JWE).
  • JSON Web Signature (JWS) and JSON Web Encryption (JWE) use signature and encryption algorithms defined in JSON Web Algorithm (JWA) as a way of securing themselves.
  • The public key of the signature algorithm defined in the JSON Web Algorithm (JWA) can be hosted as JSON Web Key (JWK).

Now that we know their relationship, let’s take a deeper look at the JWT, JWS, JWE, JWA, and JWK.

JSON Web Token (JWT)

First, let's see the definition of JWT defined in the RFC 7519.

JSON Web Token (JWT) is a compact claims representation format
intended for space constrained environments such as HTTP
Authorization headers and URI query parameters. JWTs encode claims
to be transmitted as a JSON object that is used as the
payload of a JSON Web Signature (JWS) structure or as the
plaintext of a JSON Web Encryption (JWE) structure, enabling
the claims to be digitally signed or integrity protected with a
Message Authentication Code (MAC) and/or encrypted. JWTs are always
represented using the JWS Compact Serialization or the JWE Compact
Serialization.

From the text, we can understand that JWT is not a structure but a set of claims in the shape of either JWS or JWE as its way of securing itself. In the most basic form, the difference between JWS and JWE is that everyone can see the payload of JWS while the JWE one is encrypted.


In this article, we will explore more about JWS than JWE.

JSON Web Algorithm (JWA)

JWA (RFC 7518), which stands for JSON Web Algorithm, is a specification defining which hashing and encryption algorithm to make a JWT.

For example, the following are the hashing algorithms we can use to create a JWT with a JWS structure.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (6)

JSON Web Key (JWK)

JWK (RFC 7517) stands for JSON Web Key. JWK is a JSON data structure that contains information about hashing function's cryptographic key. It's a way to store your hashing key in JSON format.

{ "kty":"EC", "crv":"P-256", "x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU", "y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0", "kid":"Public key used in JWS spec Appendix A.3 example"}

The JWK is usually used to host a public key for a hashing function with an asymmetric key (private key and public key), so the consumer can get the key by themself.

JSON Web Signature (JWS)

JWS (RFC 7515), which stands for JSON Web Signature, is one of the structures used by JWT. It's the most common implementation of the JWT. JWS consists of 3 parts: the JOSE header, payload, and signature.

The following is an example of JWS in compact serialization.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

If we decode the JWS, we will get JOSE (JavaScript Object Signing and Encryption) header:

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

The payload:

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

The next part is the signature. We won't decode it because it's a bytes value.

SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

The most common question related to JWT (in JWS structure) is what makes it secure since everyone can decode the JWT and see its data. The JWT is secure because not everyone can create it, but only the one with the secret key.

JSON Web Encryption (JWE)

JWE (RFC 7516), unlike JWS, encrypts its content using an encryption algorithm. The only one that can see what is inside the JWT is the one with the key.

The structure of JWE compact serialization is as follows:

BASE64URL(UTF8(JWE Protected Header)) || ’.’ ||BASE64URL(JWE Encrypted Key) || ’.’ ||BASE64URL(JWE Initialization Vector) || ’.’ ||BASE64URL(JWE Ciphertext) || ’.’ ||BASE64URL(JWE Authentication Tag)

Let's take a look at one example:

eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0.RD09fEltrYPVNoGt2KY1Odv_5eDxkU4VX1f__P8b9zl9uzh5bmvvJy35dL-hYlUib1g63qnWBEfeSyDk5cAIQiMt6PZCBQzuWQJQlQtuo2UPLZznmLPqah37uHKB4a57q_lWf_W9soyZbO7Zj7QRNz4ZR4s5ozRHArSZcc1pAL-pYuHKyeh6Ey8t4bk66wkthjjfOjXvIfOlgbemhibegmE4GpQL6F-m0teqcAE-OxkaBRTmmb4AD5HdrCJWCIIuC52fzuWrhcoNmHM74ggtWUUjlHaKpwcVE-IWINTFaz5Pi9u4U3vnVNOZwDwB0TLSQvqnPwTZ-bYWNj8vH4TS_w.Pjo5QK1u1otxgcuBR7e8ew._OElhHugS2L6Kp04HhbFt6dLij_KXhO654RmT4JKyswYBX0wqRWt7ZzAE6eCHfJSJdMQYxqVSNloGb4OSIzYcTEo174lBZBINkHW-w2K6E0.QBDgBFizm80HLVkZvfBPCg

The example uses the RSA_OAEP_256 key management algorithm and AES_128_CBC_HMAC_SHA_256 as its encryption algorithm. If we decrypted the JWE, we will get:

{ "iss": "https://codecurated.com", "exp": 1651417524, "iat": 1651417224}

Decoding the JWT

Now let's take a deeper look at the JWT by using the following example:

eyJhbGciOiJIUzI1NiIsImtpZCI6IjIwMjItMDUtMDEifQ.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkJyaWxpYW4gRmlyZGF1cyIsImlhdCI6MTY1MTQyMjM2NX0.qsg3HKPxM96PeeXl-sMrao00yOh1T0yQfZa-BsrtjHI

The JWT in the example is a JWT with JWS compact serialization structure. We need to split the JWT by . (dot), and Base64 decode them to look at what's inside it.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (7)

After splitting and decoding the JWT, we will get three parts:

  • JOSE (JavaScript Object Signature and Encryption) Header
  • Payload
  • Signature

JOSE Header

eyJhbGciOiJIUzI1NiIsImtpZCI6IjIwMjItMDUtMDEifQ

After doing a Base64 decode on the string, we will get:

{ "alg": "HS256", "kid": "2022-05-01"}

This part of JWT is called the JOSE header. With the JOSE header, the JWT can inform the client how to handle the JWT.


Let's break down the two fields we have in our JWT:

  • alg: contains the information regarding the signing algorithm of the JWT.
  • kid: contains the information of the id of the key used for verifying the JWT. We will explore more about this in the JWK section.

alg is the only mandatory header, and it's the only one needed for most cases, but there are many more headers you can check here.

Payload

With the JOSE header out of the way, let's take a look at the second part, the payload.

eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkJyaWxpYW4gRmlyZGF1cyIsImlhdCI6MTY1MTQyMjM2NX0

After we decode it:

{ "sub": "1234567890", "name": "Brilian Firdaus", "iat": 1651422365}

This part of the JWT is called the payload. There are fields with the term JWT claims ( sub, name, iat). So, now seems like a good time to talk about JWT claims.

There are three types of JWT claims:

  • Registered Claims
  • Public Claims
  • Private Claims

Let's break it down one by one, starting from Registered Claims. Registered claims are claims that have been documented initially in the RFC 7519.

  • iss (Issuer): indicates who is the issuer of the JWT.
  • sub (Subject): indicates the user's id requesting the JWT.
  • aud (Audience): shows who's the intended consumer of the JWT.
  • exp (Expiration): the expiration time of the JWT.

You can check the complete list here. None of these claims are mandatory, as dictated in the RFC 7519, but they are essential in securing your JWT.

The next type of claim we'll explore is private claims. These claims can be anything. It's up to the JWT creator or consumer to determine the claims name and function.

When specifying private claims, you need to be careful not to cause any collision in the name.

The last one is public claims, which is a type of claim that is publicly registered through IETF. You can check the list of public claims here.

Most people don't have any use case needing them to register their claims. For best practice, you can search the list of Public Claims and use the one suitable for your use case.

Signature

If you've come to this part, you might wonder what makes the JWT secure since everyone can see its content. Well, the payload of the JWS is intended for everyone to read. What makes the JWT safe is the consumer can verify who is the one issued the JWT.

The signature part in JWT is created by using a hash function. If you're not familiar with a hash function, it's an algorithm that maps an object to another object. A hash function has two crucial traits which make it suitable to secure the JWT:

  • It only works one way
  • The results of hashing process are always the same

In this article, we'll explore 2 of the most common hash function used in JWT:

  • HMAC SHA-256
  • ECDSA256

Now, let's break apart the signature we use in the example:

qsg3HKPxM96PeeXl-sMrao00yOh1T0yQfZa-BsrtjHI

Which is generated using HS256 MAC algorithm with secret key (in base64):

7TgIAQCcYUA27bCI5+m7InRwp/mzQ+ArnFW/4c0Q51U=

The HS256 MAC algorithm receives bytes value as its secret key parameter and produces bytes value as its output. So, unlike the header and the payload, we will get bytes value if we try to decode the signature.

Let's explore the digital signature or MAC algorithm more thoroughly.

HMAC SHA-256

The first algorithm we'll explore is HMAC SHA-256 (HS256), a MAC algorithm using a hash function with a symmetric key. Hashing function with a symmetric key means only one key for the hashing function. So, the producer and consumer of the JWT will use the same key to sign and verify the JWT. The advantage of using this algorithm is it doesn't need many CPU resources to create the hash.

The minimum bytes length recommended for the hs256 secret key is 32 bytes. The secret key must be generated with a Cryptographically-secure pseudorandom number generator to ensure its randomity.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (8)

ECDSA-256

ECDSA-256 (ES256), unlike HMAC, is an algorithm that uses hashing function with an asymmetric key. Hashing function with an asymmetric key means we will need to generate two keys. One key is called a private key, which can be used to both sign and verify the JWT signature. The other key is called a public key, which can only be used to verify the JWT signature.

Introduction to JWT (Also JWS, JWE, JWA, JWK) (9)

As their name implies, a public key can be stored in a public space (usually as a JWK), so the client that needs to verify your JWT signature can get it quickly. In contrast, a private key must be secured and treated as a credential.

Which Hashing Function to Choose?

We have learned two algorithms, HS256 and ES256, but when to choose one instead of another? You can easily decide about it by thinking about whether the producer and the consumer of the JWT are not the same components.
If the consumer of the JWT is the same component, then you can use the HS256 algorithm. This hashing function's most common use case is when you're making an authentication system using JWT.

On the other hand, if the JWT is produced and consumed by a different component, you can use ES256. This way, you can secure your key and ensure that no one else (even the public key owner) can create a JWT on your behalf.

Some people consider HS256 a kind of anti-pattern because JWT is supposedly used to increase security, but most of the use case that uses this algorithm decreases security.

For example, suppose you plan to use JWT as an authentication session instead of a database. In that case, your system is more insecure because you can't expire the JWT, so you can't kick sessions.

Storing Public Key as JWKs (JSON Web Key Set)

If you plan to let the public consume and verify your JWT, it's recommended to host the public key as a JWKs on a URL. This way, if the consumer wants to verify your JWT, they can query a specific URL hosting the JWKs and get the public key.

{ "keys": [ { "kty": "EC", "kid": "2022-05-01", "x": "g_pYyqY7Htj8Aa989Ura0_mwRdqJPEnhknKzaUrztj8", "y": "MwOFYLE-VYre92hU0iDjNx36dk7cX6xdGgdgLIPt6Ts", "crv": "P-256" }, { "kty": "EC", "kid": "2020-01-01", "x": "6bw04ZlSMjxVzC7gXv75XAposOVTONh45ZPR0AeYaoU", "y": "vYyCSIt0m5k4Q5A_uW8h3nEYJvgA8PgREErLcaiAHgQ", "crv": "P-256" } ]}

You might've noticed more than one key in the JSON, which is why it's called JSON Web Key Set. If your product uses more than one key, you can host every key in the JWKs. To identify which key to use, you will need to add the kid or key id field in your JOSE Header.

{ "alg":"ES256", "kid":"2022-05-01"}

This way, the client will know which key to get by comparing the kid in the JOSE header JWT to the one in the JWK. The other fields, combined, will make the public key.

Takeaway

In this article, we've learned that:

  1. 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.
  2. 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.
  3. What makes JWS considered secure even though everyone can see its payload is that the creator of the JWS can be verified by its signature using MAC or Signature Verifying algorithm. This way, the consumer can be sure that the one created the JWT is the one intended.
  4. We've explored two types of algorithms, HS256 and ES256. HS256 is suitable when the producer and the consumer of the JWT are the same components, while ES256 is suitable when the producer and the consumer are different components.
  5. We can use JSON Web Key Set to host a public key for hashing function with an asymmetric key. You need to set the kid field in the JOSE header of your JWT so the consumer can compare it to the one in the JWKs to get a compatible key.

Alas, I want to thank you for reading until the end. The next step you can take is to learn how to implement it (How to Implement JWT in Java Spring Boot). There are many libraries for popular programming languages that you can check here.

Share Tweet Share Share Email

Read next

Elasticsearch ·

Getting Started With Elasticsearch in Java Spring Boot

Both Java and Elasticsearch is a popular technology stack companies use. Java is a programming language that was released back

  • Brilian Firdaus

Platform Engineering · Featured

Incident Management Platform in Blibli.com

An incident is an unplanned event affecting the system’s customer experience. This could be system failure, errors, slow performance,

  • Brilian Firdaus

Java ·

How to Implement JSON Web Token (JWT) in Java Spring Boot

JSON Web Token or JWT has been famous as a way to communicate securely between services. There are two form of JWT, JWS and JWE. This article will explore the implementation of the JWT in Java Spring Boot.

  • Brilian Firdaus
Introduction to JWT (Also JWS, JWE, JWA, JWK) (2024)

FAQs

What is JWE JWS and JWT? ›

JSON Web Token (JWT) is an abstract that is represented in the form of JSON Web Signature (JWS) and JSON Web Encryption (JWE). JSON Web Signature (JWS) and JSON Web Encryption (JWE) use signature and encryption algorithms defined in JSON Web Algorithm (JWA) as a way of securing themselves.

What is JWT and JWK? ›

The JSON Web Key Set (JWKS) is a set of keys containing the public keys used to verify any JSON Web Token (JWT) issued by the Authorization Server and signed using the RS256 signing algorithm. When creating applications and APIs in Auth0, two algorithms are supported for signing JWTs : RS256 and HS256.

What is the difference between JWA and JWK? ›

TL;DR: JWA: Defines a set of crypto algorithms and identifiers used in JWE, JWK and JWS. JWK: Defines a representation of crypto keys used as input for the algorithms defined in JWA using JSON-based data structures. JWE: Defines encrypted content using JSON-based data structures.

What is the JWT format of JWS? ›

JWT Structure. A JWS (the most common type of JWT) contains three parts separated by a dot ( . ). The first two parts (the "header" and "payload") are Base64-URL encoded JSON, and the third is a cryptographic signature. If you have a JWT with more than three sections, it's probably a JWE.

What is the main purpose of JWT? ›

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

What does JWE mean? ›

JSON Web Encryption (JWE) is an IETF standard providing a standardised syntax for the exchange of encrypted data, based on JSON and Base64. It is defined by RFC 7516.

What is the difference between JWE and JWKS? ›

JSON Web Encryption (JWE) specification standardizes the way to represent an encrypted content in a JSON-based data structure. JSON Web Key (JWK) defines a consistent way to represent a cryptographic key in a JSON structure which is used to sign (JWS) and/or encrypt (JWE) a specific content.

What is JWK in OAuth? ›

Each realm configured for OAuth 2.0 exposes a JSON web key (JWK) URI endpoint that contains public keys that clients can use to: Verify the signature of client-based access tokens and OpenID Connect ID tokens. Encrypt OpenID Connect ID requests to AM sent as a JWT.

How to verify JWT with JWK? ›

To validate a JWT using JWKS in node js:
  1. Create/have a JWKS endpoint.
  2. Create/have a token endpoint and sign the token.
  3. Retrieve the JWKS from the JWKs endpoint.
  4. Extract the JWT from the request's authorization header.
  5. Decode the JWT and grab the unique kid (Key ID) property of the token from the header.
Sep 20, 2021

What does JWK do? ›

A JWK is a JSON object representing a single cryptographic key. It holds vital identification data, such as the key type, key identifier, the cryptographic algorithm used to sign it, usage restrictions, and other additional details for verifying a JWT signature and decoding it to plaintext.

How does JWE work? ›

JWE — JSON Web Encryption

JSON Web Encryption enables encrypting a token so that only the intended recipient can read it. It standardizes the way to represent the encoded data in a JSON data structure. Representation of the encrypted payload may be by JWE compact serialization or JWE JSON serialization.

Why use JWKs? ›

JWKS is an essential component in the modern authentication ecosystem, especially when JWTs are involved. Its dynamic and standardized nature ensures that the process of key management becomes less cumbersome and more secure. Find out more on how to check JWTs with JWKS in our backend guides.

What is the JWE format? ›

JSON Web Encryption (JWE) is an IETF standard for representing encrypted content using JSON. In Auth0, you can configure APIs to encrypt the details inside an access token using the JWE format.

What are the three parts of a JWT? ›

Anatomy of a JWT

Figure 1 shows that a JWT consists of three parts: a header, payload, and signature. The header typically consists of two parts: the type of the token, which is JWT, and the algorithm that is used, such as HMAC SHA256 or RSA SHA256. It is Base64Url encoded to form the first part of the JWT.

How to create a JWE token? ›

The JWE is built using the elements:
  1. header: Includes the kid and alg parameters.
  2. Content Encryption Key (CEK): The unique encryption key used to encrypt the token.
  3. ciphertext. The encrypted JSON payload.
  4. initialization vector: ...
  5. authentication tag:

What is the JWE authentication tag? ›

JWE Authentication Tag Authentication Tag value resulting from authenticated encryption of the plaintext with Additional Authenticated Data. JWE Protected Header JSON object that contains the Header Parameters that are integrity protected by the authenticated encryption operation.

What is difference between CSRF and JWT? ›

Conclusion. To conclude, JWT and CSRF tokens serve critical yet different roles in securing web applications. JWTs facilitate secure information exchange for authentication and authorization, while CSRF tokens protect against unwanted actions on behalf of authenticated users.

What is the difference between JWT and passport JWT? ›

JWT is a standardized token-based authentication mechanism that is widely supported and used for transmitting authentication information. Passport, on the other hand, is a flexible and extensible middleware that provides a strategy-based authentication framework for Node. js applications.

What is the difference between JWT and normal token? ›

The token is stored on the server-side and used to authenticate subsequent requests from the same user. In contrast, client-side authentication using JWT involves issuing a signed token to the client upon successful login, which is then stored on the client-side and sent back to the server with each subsequent request.

Top Articles
Shopper shares little-known hack to cash in your old coins - but there’s a catch
How To Play Brawlhalla | Brawlhalla Guide | F2P Hub
Umbc Baseball Camp
Design215 Word Pattern Finder
El Paso Pet Craigslist
Manhattan Prep Lsat Forum
Identifont Upload
Guardians Of The Galaxy Showtimes Near Athol Cinemas 8
Chelsea player who left on a free is now worth more than Palmer & Caicedo
What's Wrong with the Chevrolet Tahoe?
Devourer Of Gods Resprite
Zoebaby222
Cvb Location Code Lookup
The Cure Average Setlist
Convert 2024.33 Usd
Craigslist West Valley
Gayla Glenn Harris County Texas Update
Transactions (zipForm Edition) | Lone Wolf | Real Estate Forms Software
Phoebus uses last-second touchdown to stun Salem for Class 4 football title
Mj Nails Derby Ct
Rochester Ny Missed Connections
Prey For The Devil Showtimes Near Ontario Luxe Reel Theatre
Amelia Chase Bank Murder
Sorrento Gourmet Pizza Goshen Photos
4 Times Rihanna Showed Solidarity for Social Movements Around the World
Yayo - RimWorld Wiki
Tracking every 2024 Trade Deadline deal
Select The Best Reagents For The Reaction Below.
R/Orangetheory
Armor Crushing Weapon Crossword Clue
The Rise of "t33n leaks": Understanding the Impact and Implications - The Digital Weekly
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
P3P Orthrus With Dodge Slash
Lowell Car Accident Lawyer Kiley Law Group
Most popular Indian web series of 2022 (so far) as per IMDb: Rocket Boys, Panchayat, Mai in top 10
Joe's Truck Accessories Summerville South Carolina
PA lawmakers push to restore Medicaid dental benefits for adults
Louisville Volleyball Team Leaks
Tirage Rapid Georgia
Wisconsin Women's Volleyball Team Leaked Pictures
Colorado Parks And Wildlife Reissue List
Best Restaurants Minocqua
The Angel Next Door Spoils Me Rotten Gogoanime
Natasha Tosini Bikini
[Teen Titans] Starfire In Heat - Chapter 1 - Umbrelloid - Teen Titans
The Bold and the Beautiful
Goosetown Communications Guilford Ct
Where and How to Watch Sound of Freedom | Angel Studios
Metra Union Pacific West Schedule
Craigslist Charlestown Indiana
7 National Titles Forum
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 6399

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.