Skip to content

Commit c71785d

Browse files
authored
Merge pull request jsonwebtoken#332 from jsonwebtoken/intro-fix
Conceptual fixes in the introduction.
2 parents 1d5ce27 + aba6487 commit c71785d

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jwt.io",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/jsonwebtoken/jsonwebtoken.github.io"

views/website/md/introduction.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
**NEW:** get the [JWT Handbook for free](https://auth0.com/e-books/jwt-handbook) and learn JWTs in depth!
22

33
## What is JSON Web Token?
4-
JSON 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 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**.
4+
JSON 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 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**.
55

66
Although JWTs can be encrypted to also provide secrecy between parties, we will focus on *signed* tokens. 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.
77

8-
Let's explain some concepts further.
9-
10-
- **Compact**: Because of their smaller size, JWTs can be sent through a URL, POST parameter, or inside an HTTP header. Additionally, the smaller size means transmission is fast.
11-
12-
- **Self-contained**: The payload contains all the required information about the user, avoiding the need to query the database more than once.
13-
148
## When should you use JSON Web Tokens?
159
Here are some scenarios where JSON Web Tokens are useful:
1610

17-
- **Authentication**: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
11+
- **Authorization**: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
1812

1913
- **Information Exchange**: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.
2014

@@ -48,7 +42,7 @@ Then, this JSON is **Base64Url** encoded to form the first part of the JWT.
4842

4943
### Payload
5044

51-
The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional metadata.
45+
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.
5246
There are three types of claims: *registered*, *public*, and *private* claims.
5347

5448
- [**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).
@@ -99,24 +93,25 @@ If you want to play with JWT and put these concepts into practice, you can use [
9993
![JWT.io Debugger](https://cdn.auth0.com/blog/legacy-app-auth/legacy-app-auth-5.png)
10094

10195
## How do JSON Web Tokens work?
102-
In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned and must be saved locally (typically in local storage, but cookies can be also used), instead of the traditional approach of creating a session in the server and returning a cookie.
103-
104-
> There are security considerations that must be taken into account with regards to the way tokens are stored. These are enumerated in [Where to Store Tokens](https://auth0.com/docs/security/store-tokens).
96+
In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned. Since tokens are credentials, great care must be taken to prevent security issues. In general, you should not keep tokens longer than required.
10597

10698
Whenever the user wants to access a protected route or resource, the user agent should send the JWT, typically in the **Authorization** header using the **Bearer** schema. The content of the header should look like the following:
10799

108100
```
109101
Authorization: Bearer <token>
110102
```
111103

112-
This is a stateless authentication mechanism as the user state is never saved in server memory.
113-
The server's protected routes will check for a valid JWT in the Authorization header, and if it's present, the user will be allowed to access protected resources. As JWTs are self-contained, all the necessary information is there, reducing the need to query the database multiple times.
104+
This can be, in certain cases, a stateless authorization mechanism. The server's protected routes will check for a valid JWT in the `Authorization` header, and if it's present, the user will be allowed to access protected resources. If the JWT contains the necessary data, the need to query the database for certain operations may be reduced, though this may not always be the case.
105+
106+
If the token is sent in the `Authorization` header, Cross-Origin Resource Sharing (CORS) won't be an issue as it doesn't use cookies.
114107

115-
This allows you to fully rely on data APIs that are stateless and even make requests to downstream services. It doesn't matter which domains are serving your APIs, so Cross-Origin Resource Sharing (CORS) won't be an issue as it doesn't use cookies.
108+
The following diagram shows how a JWT is obtained and used to access APIs or resources:
116109

117-
The following diagram shows this process:
110+
![How does a JSON Web Token work](https://cdn2.auth0.com/docs/media/articles/api-auth/client-credentials-grant.png)
118111

119-
![How does a JSON Web Token works](https://cdn.auth0.com/content/jwt/jwt-diagram.png)
112+
1. The application or client requests authorization to the authorization server. This is performed through one of the different authorization flows. For example, a typical [OpenID Connect](http://openid.net/connect/) compliant web application will go through the `/oauth/authorize` endpoint using the [authorization code flow](http://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth).
113+
2. When the authorization is granted, the authorization server returns an access token to the application.
114+
3. The application uses the access token to access a protected resource (like an API).
120115

121116
Do note that with signed tokens, all the information contained within the token is exposed to users or other parties, even though they are unable to change it. This means you should not put secret information within the token.
122117

0 commit comments

Comments
 (0)