What is a "JSON Web Token" (JWT)? Please what are the three parts of a JWT, what is the "payload", what is the "signature" and what does it guarantee, and is a JWT encrypted by default.
Assistant
We are asked: "What is a 'JSON Web Token' (JWT)? Please what are the three parts of a JWT, what is the 'payload', what is the 'signature' and what does it guarantee, and is a JWT encrypted by default."
We need to provide a clear, concise explanation. Let's break down:
Definition: JWT is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. It is compact and self-contained, meaning it includes the necessary data and the signature to verify it.
Three parts: JWT consists of three parts, each separated by a dot (.). The format is: Header.Payload.Signature. The header and payload are Base64Url encoded JSON objects. The signature is computed using the header, payload, and a secret (or private key).
Payload: The payload contains claims (statements) about an entity (usually the user) and additional data. Claims are of three types: registered, public, and private. Registered claims include iss (issuer), exp (expiration time), sub (subject), etc. The payload is also Base64Url encoded.
Signature: The signature is created by taking the encoded header and payload, concatenating them with a dot, and then signing that string with a cryptographic algorithm (like HMAC SHA256 for symmetric, or RSA/ECDSA for asymmetric). The signature is used to verify that the token hasn't been tampered with and that the sender is who they say they are (if using a secret shared only between parties). It guarantees integrity and authenticity (and optionally non-repudiation if using asymmetric keys). However, note that by default the payload is not encrypted, so anyone can read the Base64Url decoded payload. So confidentiality is not guaranteed unless you use JWE (JSON Web Encryption) separately.
Is a JWT encrypted by default? No, JWT by default (JWS - JSON Web Signature) is only signed, not encrypted. The payload is just Base64Url encoded, which is easily reversible. To achieve confidentiality, you would need to use JWE (JSON Web Encryption) to encrypt the token. But note that JWT can refer to both JWS and JWE; however, in common usage "JWT" often means the signed version (JWS). The standard says that JWT can be either signed (JWS) or encrypted (JWE) or both (nested). But by default, when we say JWT we usually mean the compact serialization of a JWS, which is not encrypted.
We should also mention that the three parts are all Base64Url encoded (except the signature which is binary but represented in Base64Url in the compact form). The compact form is: xxxxx.yyyyy.zzzzz where each part is Base64Url.
Now, let's structure the answer.
Start with definition. Then list three parts. Then explain payload. Then explain signature and what it guarantees. Then answer the question about encryption.
We should be precise: The signature guarantees that the token was created by a trusted party (authenticity) and that the content has not been altered (integrity). It does not guarantee confidentiality (unless encrypted) and does not inherently guarantee non-repudiation if symmetric keys are used (because both parties share the secret, so the receiver could have forged it too).
Also note: JWT can be used for authorization (like bearer token) and information exchange.
Now, write the answer in a clear, explanatory manner.
What is a JSON Web Token (JWT)?
A JSON Web Token (JWT) is an open standard (RFC 7519) for securely representing claims between parties in a compact, self-contained format. It is often used for authentication and authorization, such as bearer tokens in OAuth 2.0, or for exchanging information between services. A JWT is digitally signed (or optionally encrypted) to ensure its integrity and authenticity.
Three Parts of a JWT
A JWT has three parts, separated by dots (.), in the following structure:
code
Header.Payload.Signature
All three parts are Base64Url-encoded strings (except the signature, which is a binary value encoded into Base64Url).
Header: A JSON object that declares the type of token ("typ":"JWT") and the signing algorithm used (e.g., "alg":"HS256" for HMAC SHA-256).
Example: {"alg":"HS256","typ":"JWT"}
Payload: Contains the claims – statements about an entity (usually the user) and additional metadata. Claims can be:
Registered claims: Predefined, standardized claims like iss (issuer), exp (expiration time), sub (subject), aud (audience), etc.
Public claims: Custom claims defined by the application, but to avoid collisions they should be registered or use a namespace.
Private claims: Custom claims agreed upon by the communicating parties.
The payload is the data you want to transmit.
Example: {"sub":"1234567890","name":"John Doe","admin":true}
Signature: Created by taking the encoded header and payload, concatenating them with a dot, and then signing that string with a cryptographic algorithm and a secret (or private key). The signature ensures that the token has not been tampered with.
The signature provides two main security properties:
Integrity: The signature ensures that the concatenated header and payload have not been altered after the token was signed. Any change to these parts will cause signature verification to fail.
Authenticity: When verified with a shared secret (symmetric) or a public key (asymmetric), the signature confirms that the token was issued by a trusted party (the holder of the secret/private key).
⚠️ Important Notes:
The signature does not guarantee confidentiality; the payload is only Base64Url-encoded, not encrypted. Anyone can decode it and read its contents.
If symmetric keys (e.g., HMAC) are used, the same secret is known by both the issuer and the verifier, so the verifier could also create valid tokens. Therefore, non-repudiation (proof that the issuer cannot later deny creating the token) is only achieved with asymmetric keys (e.g., RSA/ECDSA).
Is a JWT Encrypted by Default?
No. By default, a JWT is signed (using JWS – JSON Web Signature), not encrypted. This means the payload is easily readable after Base64Url decoding. If confidentiality is required (e.g., to hide sensitive data in the payload), you must explicitly encrypt the token using JWE (JSON Web Encryption) or employ other means like TLS for transmission.
In practice, the term "JWT" is often used interchangeably with the signed compact serialization (JWS). However, the JWT specification supports both signature and encryption, and even nested combinations (signed then encrypted). But unless encryption is applied, the payload remains exposed. Always consider whether sensitive data should be included in a JWT, and if needed, use JWE or store such data server-side and reference it via a token identifier.