knowledge/technology/internet/JWT.md

76 lines
3.9 KiB
Markdown
Raw Normal View History

2023-12-04 10:02:23 +00:00
---
website: https://jwt.io/
obj: concept
2024-03-08 21:23:31 +00:00
aliases: ["Json Web Token"]
2023-12-04 10:02:23 +00:00
rfc: https://datatracker.ietf.org/doc/html/rfc7519
2024-03-08 21:53:45 +00:00
rev: 2024-03-08
2023-12-04 10:02:23 +00:00
---
# [Json](../files/JSON.md) Web Token (JWT)
2024-01-17 08:44:04 +00:00
[JSON](../files/JSON.md) Web Token (JWT) is an open standard ([RFC 7519](https://tools.ietf.org/html/rfc7519)) that defines a compact and self-contained way for securely transmitting information between parties as a [JSON](../files/JSON.md) object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the **HMAC** algorithm) or a public/private key pair using **RSA** or **ECDSA**.
2023-12-04 10:02:23 +00:00
2024-01-17 08:44:04 +00:00
Signed tokens can verify the _integrity_ of the claims contained within it, while encrypted tokens _hide_ those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.
2023-12-04 10:02:23 +00:00
[JSON](../files/JSON.md) Web Token can be stored in a [Cookie](Cookie.md)
## Structure
In its compact form, [JSON](../files/JSON.md) Web Tokens consist of three parts separated by dots (`.`), which are:
- Header
- Payload
- Signature
Therefore, a JWT typically looks like the following.
`xxxxx.yyyyy.zzzzz`
### Header
2024-01-17 08:44:04 +00:00
The header _typically_ consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.
2023-12-04 10:02:23 +00:00
For example:
```
{
"alg": "HS256",
"typ": "JWT"
}
```
2024-01-17 08:44:04 +00:00
Then, this [JSON](../files/JSON.md) is **Base64Url** encoded to form the first part of the JWT.
2023-12-04 10:02:23 +00:00
### Payload
2024-01-17 08:44:04 +00:00
The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: _registered_, _public_, and _private_ claims.
2023-12-04 10:02:23 +00:00
2024-01-17 08:44:04 +00:00
- [**Registered claims**](https://tools.ietf.org/html/rfc7519#section-4.1): These are a set of predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims. Some of them are: **iss** (issuer), **exp** (expiration time), **sub** (subject), **aud** (audience), and [others](https://tools.ietf.org/html/rfc7519#section-4.1).
2023-12-04 10:02:23 +00:00
> Notice that the claim names are only three characters long as JWT is meant to be compact.
2024-01-17 08:44:04 +00:00
- [**Public claims**](https://tools.ietf.org/html/rfc7519#section-4.2): These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the [IANA JSON Web Token Registry](https://www.iana.org/assignments/jwt/jwt.xhtml) or be defined as a URI that contains a collision resistant namespace.
- [**Private claims**](https://tools.ietf.org/html/rfc7519#section-4.3): These are the custom claims created to share information between parties that agree on using them and are neither _registered_ or _public_ claims.
2023-12-04 10:02:23 +00:00
An example payload could be:
```
{
"sub": "1234567890",
"name": "John Doe",
"admin": true
}
```
2024-01-17 08:44:04 +00:00
The payload is then **Base64Url** encoded to form the second part of the [JSON](../files/JSON.md) Web Token.
2023-12-04 10:02:23 +00:00
### Signature
To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.
For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
```
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
```
The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.
### Putting all together
The output is three Base64-URL strings separated by dots that can be easily passed in [HTML](HTML.md) and [HTTP](HTTP.md) environments, while being more compact when compared to [XML](../files/XML.md)-based standards such as SAML.
The following shows a JWT that has the previous header and payload encoded, and it is signed with a secret.
2024-09-04 15:55:16 +00:00
![JWT](./jwt.png)