Skip to content

Commit 621f74f

Browse files
authored
Merge pull request #3893 from sivaprasath2004/sivaprasath-closes-issue-3889
[Feature]: Add JWT in `/doc` section
2 parents 950e881 + bb21f96 commit 621f74f

7 files changed

+946
-0
lines changed

docs/JWT/Advanced-topic.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
id: advanced-jwt-topics
3+
title: Advanced Topics
4+
sidebar_label: Advanced Technique
5+
sidebar_position: 6
6+
tags: [JWT, web development, Security]
7+
description: Advanced JWT Technique.
8+
---
9+
10+
#### JWT in OAuth 2.0 and OpenID Connect
11+
12+
**How JWT is Used in These Protocols**:
13+
:::note
14+
- **OAuth 2.0**:
15+
- **Access Tokens**: In OAuth 2.0, JWTs are often used as access tokens to grant access to resources. The token includes information about the scopes and permissions granted.
16+
- **Bearer Tokens**: OAuth 2.0 can use JWTs as bearer tokens that are sent in HTTP headers to authenticate API requests.
17+
:::
18+
19+
Example (OAuth 2.0 Access Token in HTTP Header):
20+
```http
21+
Authorization: Bearer <your.jwt.token>
22+
```
23+
24+
- **OpenID Connect (OIDC)**:
25+
- **ID Tokens**: OpenID Connect, an identity layer on top of OAuth 2.0, uses JWTs as ID tokens. These tokens provide information about the user and their authentication.
26+
- **Claims**: The ID token contains claims about the user, such as their identity, and is used to establish a session in the client application.
27+
28+
Example (ID Token Claims):
29+
```json
30+
{
31+
"iss": "https://issuer.com",
32+
"sub": "user123",
33+
"aud": "client_id",
34+
"exp": 1618694400,
35+
"iat": 1618690800,
36+
"name": "John Doe",
37+
"email": "[email protected]"
38+
}
39+
```
40+
41+
42+
**Benefits of Using JWT in OAuth 2.0**:
43+
- **Statelessness**: JWTs are self-contained, meaning they carry all necessary information in the token itself, which eliminates the need for server-side session storage.
44+
- **Scalability**: Since JWTs are self-contained, they enable scalable distributed systems without the need for centralized session management.
45+
- **Flexibility**: JWTs support various signing algorithms, allowing flexibility in terms of security and performance based on the use case.
46+
47+
#### Handling Large Payloads
48+
49+
**Compressing the Payload**:
50+
- **Purpose**: Compressing the payload reduces the size of the JWT, which can be important for performance, especially when transmitting large amounts of data.
51+
- **Techniques**:
52+
- **Deflate**: Use compression algorithms such as gzip or deflate before encoding the payload.
53+
54+
Example (Compression in JavaScript):
55+
```javascript
56+
const pako = require('pako');
57+
58+
// Compress the payload
59+
const compressedPayload = pako.deflate(JSON.stringify(payload), { to: 'string' });
60+
61+
// Encode and sign the compressed payload
62+
const token = jwt.sign({ data: compressedPayload }, secret);
63+
```
64+
65+
- **Considerations**: Ensure that both the issuer and the consumer of the token can handle the compression and decompression of the payload.
66+
67+
**Handling Large Claims Sets**:
68+
- **Splitting Claims**: If the claims set is large, consider splitting the data into multiple tokens or using a combination of tokens and other storage mechanisms.
69+
- **Using External References**: Store large data externally and include a reference or URL in the JWT. This approach reduces the token size and helps manage large claims effectively.
70+
71+
#### Custom Claims and Namespaces
72+
73+
**Defining and Using Custom Claims**:
74+
- **Purpose**: Custom claims allow you to include additional data specific to your application’s needs.
75+
- **How to Define**: Add custom claims to the payload when creating the JWT.
76+
77+
Example (Adding Custom Claims):
78+
```javascript
79+
const payload = {
80+
sub: "user123",
81+
role: "admin",
82+
permissions: ["read", "write"]
83+
};
84+
85+
const token = jwt.sign(payload, secret);
86+
```
87+
88+
- **Best Practices**: Ensure that custom claims are used consistently and are well-documented.
89+
90+
**Namespacing to Avoid Conflicts**:
91+
- **Purpose**: Namespacing helps avoid conflicts between custom claims and standard claims or between different applications.
92+
- **How to Namespace**: Use a consistent naming convention for custom claims, such as prefixing with your application's domain.
93+
94+
Example (Namespaced Custom Claims):
95+
```json
96+
{
97+
"sub": "user123",
98+
"http://example.com/roles": ["admin"],
99+
"http://example.com/permissions": ["read", "write"]
100+
}
101+
```
102+
103+
104+
:::tip
105+
1. **JWT in OAuth 2.0 and OpenID Connect**: JWTs are integral to these protocols, used for access and ID tokens, offering benefits like statelessness and scalability.
106+
2. **Handling Large Payloads**: Compress payloads to reduce size and handle large claims by splitting data or using external references.
107+
3. **Custom Claims and Namespaces**: Define custom claims to include specific application data and use namespacing to avoid conflicts with standard claims or between different applications.
108+
:::

docs/JWT/Components.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
id: jwt-components
3+
title: JWT Components
4+
sidebar_label: Components
5+
sidebar_position: 2
6+
tags: [JWT, web development, Security]
7+
description: JWT Components.
8+
---
9+
10+
#### Header
11+
12+
**Structure and Format**:
13+
The header of a JWT is a JSON object that contains two main pieces of information: the type of the token and the signing algorithm being used.
14+
15+
**Common Fields**:
16+
- **alg**: Specifies the algorithm used to sign the token, such as HMAC SHA256 (HS256) or RSA (RS256).
17+
- **typ**: Specifies the type of the token, which is usually set to "JWT".
18+
19+
Example Header:
20+
```json
21+
{
22+
"alg": "HS256",
23+
"typ": "JWT"
24+
}
25+
```
26+
This JSON object is then base64Url encoded to form the first part of the JWT.
27+
28+
#### Payload
29+
30+
The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims:
31+
32+
**Registered Claims**:
33+
These are a set of predefined claims which are not mandatory but recommended to provide a set of useful, interoperable claims:
34+
:::important
35+
- **iss (issuer)**: Identifies the principal that issued the JWT.
36+
- **sub (subject)**: Identifies the principal that is the subject of the JWT.
37+
- **aud (audience)**: Identifies the recipients that the JWT is intended for.
38+
- **exp (expiration time)**: Identifies the expiration time on or after which the JWT must not be accepted for processing.
39+
- **nbf (not before)**: Identifies the time before which the JWT must not be accepted for processing.
40+
- **iat (issued at)**: Identifies the time at which the JWT was issued.
41+
- **jti (JWT ID)**: Provides a unique identifier for the JWT.
42+
:::
43+
44+
Example Payload with Registered Claims:
45+
```json
46+
{
47+
"iss": "example.com",
48+
"sub": "user123",
49+
"aud": "example.com",
50+
"exp": 1618704000,
51+
"nbf": 1618700400,
52+
"iat": 1618700400,
53+
"jti": "unique-id-123"
54+
}
55+
```
56+
57+
**Public Claims**:
58+
These claims can be defined at will by those using JWTs but should be defined in the IANA JSON Web Token Registry to avoid collisions.
59+
60+
Example Public Claims:
61+
```json
62+
{
63+
"role": "admin",
64+
"username": "johndoe"
65+
}
66+
```
67+
68+
**Private Claims**:
69+
These are custom claims created to share information between parties that agree on using them. These claims are not registered or public, thus avoiding collisions in usage.
70+
71+
Example Private Claims:
72+
```json
73+
{
74+
"customClaim": "customValue"
75+
}
76+
```
77+
78+
This JSON object is then base64Url encoded to form the second part of the JWT.
79+
80+
#### Signature
81+
82+
**How It's Created**:
83+
To create the signature part, you have to take the encoded header, the encoded payload, a secret key, and the algorithm specified in the header, and sign that.
84+
85+
For example, if you use the HMAC SHA256 algorithm, the signature will be created in the following way:
86+
```
87+
HMACSHA256(
88+
base64UrlEncode(header) + "." +
89+
base64UrlEncode(payload),
90+
secret
91+
)
92+
```
93+
94+
**Purpose and Importance**:
95+
The signature is used to verify that the sender of the JWT is who it says it is (authentication) and to ensure that the message wasn't changed along the way (integrity). When the recipient receives the token, they can decode the header and payload, but they must verify the signature using the secret key to ensure that the token is valid and has not been tampered with.
96+
97+
Example JWT Signature:
98+
```json
99+
HMACSHA256(
100+
base64UrlEncode(header) + "." +
101+
base64UrlEncode(payload),
102+
secret
103+
)
104+
```
105+

docs/JWT/Introduction.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
id: jwt-turorial
3+
title: What is JWT?
4+
sidebar_label: Introduction
5+
sidebar_position: 1
6+
tags: [JWT, web development, Security]
7+
description: JSON Web Token (JWT) is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object.
8+
---
9+
10+
**Definition and Purpose**:
11+
JSON Web Token (JWT) is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with HMAC algorithm) or a public/private key pair using RSA or ECDSA.
12+
13+
**Typical Use Cases**:
14+
- **Authentication**: After a user logs in, the server generates a JWT and sends it to the client. The client then sends this token in subsequent requests, allowing the server to verify the user's identity.
15+
- **Information Exchange**: JWTs can securely transmit information between parties because they can be signed, ensuring the data's integrity and authenticity.
16+
17+
#### JWT Structure
18+
19+
A JWT is composed of three parts, separated by dots (`.`):
20+
21+
1. **Header**
22+
2. **Payload**
23+
3. **Signature**
24+
25+
Each part is base64Url encoded and concatenated with dots.
26+
27+
**Header**:
28+
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.
29+
30+
:::important
31+
Example:
32+
```json
33+
{
34+
"alg": "HS256",
35+
"typ": "JWT"
36+
}
37+
```
38+
:::
39+
40+
This JSON is then Base64Url encoded to form the first part of the JWT.
41+
42+
:::important
43+
**Payload**:
44+
The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims:
45+
- **Registered Claims**: These are predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims. Some examples are `iss` (issuer), `exp` (expiration time), `sub` (subject), and `aud` (audience).
46+
- **Public Claims**: These can be defined at will by those using JWTs but should be defined in the IANA JSON Web Token Registry to avoid collisions.
47+
- **Private Claims**: These are custom claims created to share information between parties that agree on using them.
48+
:::
49+
Example:
50+
```json
51+
{
52+
"sub": "1234567890",
53+
"name": "John Doe",
54+
"admin": true
55+
}
56+
```
57+
58+
This JSON is then Base64Url encoded to form the second part of the JWT.
59+
60+
**Signature**:
61+
To create the signature part, you have to take the encoded header, the encoded payload, a secret, and the algorithm specified in the header and sign that.
62+
63+
For example, if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
64+
```
65+
HMACSHA256(
66+
base64UrlEncode(header) + "." +
67+
base64UrlEncode(payload),
68+
secret
69+
)
70+
```
71+
72+
The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.
73+
74+
**Putting It All Together**:
75+
The output is three Base64Url strings separated by dots that can be easily passed in HTML and HTTP environments:
76+
```
77+
xxxxx.yyyyy.zzzzz
78+
```
79+
80+
### Example JWT:
81+
A JWT might look like this:
82+
```
83+
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
84+
```
85+
:::note
86+
- The first part (before the first dot) is the encoded header.
87+
- The second part is the encoded payload.
88+
- The third part is the signature.
89+
:::
90+

docs/JWT/_category_.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "JWT",
3+
"position": 25,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "JSON Web Token (JWT) is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. It is commonly used for authentication and information exchange. "
7+
}
8+
}

0 commit comments

Comments
 (0)