JWT Stands For...
Tags: javascript
Published: 2019-10-07 15:47:05

##Intro to JWT##

JWT stands for JSON Web Token*. JWTs are JSON objects that allow for secure transmission of information.

JWTs are made up of 3 components that are separated by periods. The 3 components are:
* Header
* Payload
* Signature

Each component in the JWT is encoded using Base64Url ([check out this article for more information on Base64Url encoding](https://brockallen.com/2014/10/17/base64url-encoding/)). An example of a Base64Url encoded JWT is shown below.

```javascript
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

//note the dots separating each string of characters
```

The **header** of a JWT contains the type of token (JWT) and the algorithm used for signing the token. Below is an example of the type of information contained in the header.

```javascript
{ "typ": "JWT", "alg": "HS256" }
//HS256 is short for HMAC-SHA256
```
There are 3 types of signing algorithms that you might see:

* HMAC (Hash-based Message Authentication Code)
  * Uses cryptographic hash function and a secret cryptographic key - [Wikipedia Article on HMAC](https://en.wikipedia.org/wiki/HMAC)
* RSA (Rivest–Shamir–Adleman)
  * Asymmetric (there are two different keys) cryptographic algorithm - [Wikipedia Article on RSA](https://simple.wikipedia.org/wiki/RSA_algorithm)
* ECDSA (Elliptic Curve Digital Signature Algorithm)
  * Variant of DSA (Digital Signature Algorithm) - [Wikipedia Article on ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)

The **payload** of a JWT is where you might find information about a user.

>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. - [JWT Docs](https://jwt.io/introduction/)

*Be careful not to include any unencrypted secret information in the payload! You can use a gem like BCrpyt ([Wikipedia Article on BCrypt](https://en.wikipedia.org/wiki/Bcrypt)) to encode password information.*

The **signature** of the JWT verifies that the token hasn't been changed and looks at any private claims in the payload to verify that the sender of the token is who it claims to be. The signature is calculated using the header and the payload.

>Check out [JWT's Debugger Tool](https://jwt.io/) to "decode, verify, and generate JWT"

##JWT in Action##

JWT is commonly used for authorization. After a successful login the server returns a JWT that can be used to verify the user when they want to access protected routes. You can store the token in browser storage or cookies and there are trade-offs with either.

* If you want to read more about why you might choose to store JWTs in one place versus the other, you can read this [article on where to store JWTs by Tom Abbott](https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage) and [this blurb about local storage from OWASP](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html#local-storage).

When a user wants to access a protected route the JWT can be sent for verification using an authorization header using the "bearer schema". 

```javascript
'Authorization': 'Bearer <token>'
```
>Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens. The name “Bearer authentication” can be understood as “give access to the bearer of this token.”[Swagger.io Docs - Bearer Authentication](https://swagger.io/docs/specification/authentication/bearer-authentication/)

Once the token is sent back to the server it can be validated - if the token is valid the user will be able to access the protected route.

##In Conclusion...##

JWT is a great option for implementing authorization. Using JWT you can authorize a user and be certain that their credentials haven't been tampered with (thanks to the header, payload, signature combination!). This was just a quick introduction to JWT and there is so much more to cover, but don't you worry, there's plenty more great places all over this internet. I'll be looking for you next time, on Desktops, Dev.to's, and Drives!

Resources:
* [JWT.io Docs](https://jwt.io/introduction/)
* [JWT's Debugger Tool](https://jwt.io/)
* [Ruby JWT Docs](https://github.com/jwt/ruby-jwt)
* [Base64Url Article by Brock Allen](https://brockallen.com/2014/10/17/base64url-encoding/)
* [Where to Store your JWTs – Cookies vs HTML5 Web Storage by Tom Abbott](https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage)
* [OWASP HTML5 Security Cheat Sheet - Local Storage](https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html#local-storage)
* [Swagger.io Docs - Bearer Authentication](https://swagger.io/docs/specification/authentication/bearer-authentication/)
* Wikipedia!

*If you made it this far, then I have to tell you something. I lied. JWT does not stand for JSON Web Token. JWT stands for Jonathan Webtaylor Thomas.

![Classic JTT (Jonathan Taylor Thomas) circa 1990](https://thepracticaldev.s3.amazonaws.com/i/2yviga9sohoa5g069686.jpg)