What is Json Web Token? JWT Token Explained

Code with Julian
7 Nov 202107:32

Summary

TLDRThe video explains authentication and authorization processes to control access to web resources. It then introduces JSON Web Tokens (JWTs) - an industry standard for securely representing user identity and access claims. JWTs have three components - a header, payload, and signature. The header and payload contain metadata, while the signature verifies integrity. When a user logs in, the server returns a signed JWT containing the user's claims to the client. The client stores the JWT and includes it in future requests. The server validates the JWT signature and claims on each request before allowing access.

Takeaways

  • 📝 JWTs allow secure representation of claims between parties
  • 🔑 They contain 3 components: header, payload & signature
  • 👤 Header & payload are encoded & signature verifies integrity
  • 🔐 JWTs used for authentication & authorization
  • 🛂 Server issues JWT when user logs in, client stores it
  • ⚖️ JWT sent with subsequent requests to access resources
  • ✅ Server validates JWT to allow/deny access to resources
  • 😄 Custom claims can share info between agreed parties
  • ⏱ JWT expiration times prevent reuse if compromised
  • 👍 JWT becoming industry standard for authentication

Q & A

  • What is the difference between authentication and authorization?

    -Authentication is the process of verifying the identity of a user. Authorization is the process of granting access to specific resources based on certain rules and policies.

  • What are the three main components of a JSON Web Token (JWT)?

    -The three main components of a JWT are: the header, the payload, and the signature.

  • What kind of information is stored in the header and payload of a JWT?

    -The header holds the token type and signing algorithm information. The payload holds claims about the user such as ID, role, or other custom claims.

  • What are the three types of claims that can be included in a JWT payload?

    -The three types of claims are: registered claims, public claims, and private claims.

  • What is the purpose of the signature component in a JWT?

    -The signature is used to verify that the JWT message was not tampered with along the way. It encodes the header, payload, and a secret key.

  • Where is the JWT token stored on the client side?

    -The JWT token is most often stored in a session cookie inside the web browser on the client side.

  • How does the server validate incoming requests that contain a JWT?

    -The server validates JWTs from incoming requests by decoding the token and checking factors like expiry, signature validity, and claims.

  • What does the server respond with if JWT validation fails?

    -If JWT validation fails, the server responds with an unauthorized or unauthenticated error status code to deny access.

  • Why are JWTs useful for authentication?

    -JWTs allow stateless authentication by encoding user data and credentials into a compact token that can be easily passed in requests for authorization.

  • When is a new JWT issued to the client?

    -A new JWT is issued to the client upon successful login or authentication with the server.

Outlines

00:00

📝 What is JSON Web Token and How Does It Work for Authentication

This paragraph provides an introduction to authentication, authorization, and JSON web tokens. It explains that authentication verifies user identity while authorization controls access to resources. JSON web tokens are an industry standard for securely representing claims between parties, consisting of a header, payload, and signature.

05:01

❗️ How JSON Web Tokens Work in Practice for Login and Data Access

This paragraph illustrates how JSON web tokens work in practice using a web client-server example. It shows the flow of the user logging in, the server issuing a JWT, the client storing and sending the JWT with requests, and the server validating the JWT to allow or deny access.

Mindmap

Keywords

💡authentication

Authentication is the process of verifying the identity of a user or process. It is an important concept related to accessing protected resources. As explained in the video, an ecommerce site uses authentication to prevent unregistered users from accessing seller dashboards. Only sellers who log in with valid credentials are authenticated and allowed access.

💡authorization

Authorization is the process of granting access to specific resources based on rules and policies. It determines what an authenticated user is allowed to access. As an example from the video, on Amazon a buyer is only authorized to access buyer related pages while a seller is authorized to access seller dashboards, based on their user roles.

💡JSON Web Token (JWT)

A JSON Web Token is an industry standard method for securely representing claims between parties. As explained in the video, it has become popular for implementing authentication and authorization in web and mobile applications. Some key properties are that it is self-contained, can be easily passed in requests, and integrity can be verified.

💡header

The header is one of the three components of a JWT. As stated in the video, it contains metadata like the type of token and the signing algorithm used. For example - {"typ": "JWT", "alg": "HS256"}.

💡payload

The payload is the second component of a JWT. As explained in the video, it contains the claims which are statements about the entity like user ID, role etc. There are three types - registered, public and private claims.

💡signature

The third component of a JWT. It is constructed using the encoded header, encoded payload, and the secret key. As stated in the video, it allows verifying that the message has not been altered in transit.

💡registered claims

These are standardized recommended claims as part of the JWT specification. Some examples mentioned in video are iss (issuer), exp (expiration time) and sub (subject).

💡public claims

Custom claims defined for a specific implementation, as stated in video. Care should be taken to avoid collision with registered and private claims.

💡private claims

Custom claims shared between two parties, according to the video. For example, security roles for a user authorization.

💡sign-in algorithm

The algorithm specified in JWT header to encrypt/decrypt the signature component. As per video this provides integrity that token has not been changed in transit. Example HS256.

Highlights

Authentication verifies user identity, authorization gives access to resources based on rules

JSON Web Token has header with token type and algorithm, payload with claims, and signature to verify integrity

Registered claims like ISS and EXP are standard, public claims are custom, private claims are for parties to share info

Signature encodes header and payload with secret key to detect tampering

When user logs in, server issues JWT with claims, browser stores it, then sends it with subsequent requests

On future requests, server validates JWT token before allowing access to resources

If JWT valid, server responds with data, if not valid responds with unauthorized error

JWT allows stateless authentication since token contains all the user info needed

Registered claims like ISS and EXP follow standard, avoid collisions with public/private claims

Private claims shared between agreeing parties like user role or ID

JSON Web Tokens became an industry standard for secure claims between parties

JWT payload holds claims, which are pieces of info like ISS, EXP, and SUB

Authorization gives access to resources based on user role assigned during registration

Amazon checks user role to determine if they can access order data or seller dashboard

JWT allows securely transmitting user identity and privileges for access control

Transcripts

play00:00

today we'll talk about what a json web

play00:02

token is and how it works

play00:05

so let's get coding if you arrived at

play00:07

this video you're probably no stranger

play00:09

to the concept of authentication and

play00:11

authorization but in summary

play00:13

authentication is the process of

play00:15

verifying the identity of a user or a

play00:17

process what this means is that there

play00:19

are certain rules and policies in place

play00:21

to prevent unauthenticated users from

play00:23

accessing certain pages or even data

play00:26

let's take an e-commerce website for

play00:27

example such as amazon so you're able to

play00:29

uh access that website and to see a list

play00:32

of products or at least the product that

play00:34

you're looking for

play00:36

in a list you're able to sort through it

play00:39

and you're able to see prices and the

play00:40

more details about the product and

play00:42

reviews about it however what you're not

play00:45

able to see

play00:46

is a seller's dashboard with the

play00:48

revenues and profits that the seller has

play00:51

made and that is due to authentication

play00:53

if you were to be a seller and you were

play00:56

to be authenticated then you would have

play00:58

had access to the seller's dashboard

play01:00

failing that you don't have access to it

play01:03

but you're authenticated on amazon say

play01:05

and you don't have access to any

play01:07

seller's dashboard and you might ask

play01:09

yourself well i'm logged in why can't i

play01:12

see any sellers dashboard well that's

play01:14

because of authorization authorization

play01:17

is the process of giving access to

play01:19

specific resources based on certain

play01:22

rules or policies if we take our example

play01:25

when you go on amazon and you register

play01:27

you register as a buyer therefore the

play01:30

application assigns you a security role

play01:32

of a buyer if you register as a seller

play01:35

you guessed it you get assigned a role

play01:37

of seller so this is how the application

play01:39

knows the two different types of users

play01:42

now that we understand these two very

play01:43

important concepts authentication and

play01:45

authorization we are now ready to talk

play01:47

about json web token and according to

play01:50

jwt.io a json web token is an industry

play01:53

standard rfc 7519 method for

play01:57

representing claims between two

play01:58

different parties securely and i'll

play02:02

explain what that means in a moment a

play02:04

token as such is made of three main

play02:07

components

play02:09

a header a payload and a signature

play02:12

and let's talk about each one of them in

play02:15

particular so the header holds two

play02:17

things the type of token identified with

play02:20

typ notation and the sign-in algorithm

play02:24

used under the alg notation in this case

play02:27

the type of algorithm is jwt and the alg

play02:31

is the

play02:32

sha-256

play02:34

or

play02:35

as you see here hs256

play02:39

the payload

play02:41

holds claims and claims are just pieces

play02:43

of information describing the subject

play02:45

right there are three three types of

play02:47

claims registered public and private

play02:51

let's talk about them individually very

play02:53

briefly registered claims are three

play02:55

characters long and are not mandatory

play02:57

but recommended some examples are iss

play03:00

issuer exp

play03:03

obviously the token expiration time and

play03:05

sub subject or aud you'll see more often

play03:10

meaning audience

play03:12

so that's registered claims public

play03:14

claims

play03:15

these are custom claims that we can

play03:17

define ourselves

play03:19

be careful to avoid collisions however

play03:21

with the private or registered ones and

play03:23

i'll link you up with the full list of

play03:26

registered claims in the description and

play03:28

last but not least the private claims

play03:30

created to share information between

play03:31

parties that agree on using them some

play03:34

examples are the user security role or

play03:36

the user id

play03:38

so the third component of a json web

play03:40

token is the signature which is used to

play03:42

verify the message

play03:44

that the message wasn't tampered with

play03:46

along the way and it holds three pieces

play03:48

of data the encoded header

play03:51

and the payload and the secret key if

play03:53

this video is helpful to you so far why

play03:55

not hit that like button so that this

play03:57

video can spread to as many people as

play03:58

possible i would really appreciate it

play04:00

and i do weekly tutorials and

play04:02

discussions such as this one so if

play04:03

you're into this kind of content

play04:05

consider subscribing now that we

play04:07

understand what authentication means

play04:09

what um authorization is and what a json

play04:12

web token is and the the three main

play04:14

components of it

play04:16

let's switch over to a diagram that i've

play04:17

got for you and let's explain how jwt

play04:21

actually works okay so here we are and

play04:24

on the left hand side we've got the

play04:26

client and this is the user that goes on

play04:30

say amazon and um we have here what i

play04:34

wanted it to be a web page and this

play04:37

represents the browser and then on the

play04:39

on the right hand side we have the

play04:41

server which is a place that users can

play04:45

clients can access data through so that

play04:48

could be

play04:49

an api say for our example okay so the

play04:52

very first thing for a user for the

play04:54

client is to log in so the client

play04:58

attempts the user attempts to log in

play05:01

so what the server does is the point of

play05:03

logging in obviously it logs the user in

play05:06

if the user is

play05:08

registered in the system and it issues a

play05:11

jwt at that specific point so when the

play05:14

user logs in so here we've got the jwt

play05:17

the json web token this uh token

play05:19

contains all the information that we've

play05:21

talked about

play05:23

previously um and uh this is issued on

play05:26

the server side and is sent to the

play05:28

client um with the response back from

play05:32

the from the login request yeah and then

play05:35

the client stores this

play05:37

in the browser so this jwt is stored in

play05:41

more often than not in a in a cookie in

play05:44

a session cookie inside the browser b

play05:46

chrome firefox you name it and then the

play05:50

next thing that the client wants to do

play05:53

say is

play05:54

search for a product search for hair

play05:58

products on amazon so that means that

play06:01

the client sends a request to the server

play06:04

and the server when the very next

play06:08

request that comes to the server from

play06:10

the client um

play06:12

the server validates that jwt because

play06:16

the jwt token is also sent along with

play06:20

the request so say the user searches for

play06:24

uh hair products in the search bar this

play06:27

jwt goes along with the data that the

play06:30

user

play06:32

requested and at that point the server

play06:34

can take this jwt and validate it and if

play06:37

it's valid then the server responds back

play06:40

with the 200 if it's not valid

play06:43

and then the server responds back with

play06:46

uh um

play06:47

unauthenticated with a not authenticated

play06:50

bet request

play06:52

and

play06:53

the server does not allow the client to

play06:56

access

play06:57

any resources because the jwt

play07:00

is not valid but in our case everything

play07:03

has gone smoothly the token has been

play07:05

validated because it's just been issued

play07:07

and it's not expired yet so everything

play07:09

is okay

play07:11

okay so this is in short what json web

play07:13

token is why it's important and how it

play07:16

works keep an eye out for a video that i

play07:18

will be publishing very shortly to show

play07:20

you how you can add authentication and

play07:21

authorization to your asp.net core web

play07:24

api with json web token matter of fact

play07:26

check it out on screen right now if it

play07:28

is available already until next time

play07:31

stay safe