happydecoder Profile picture
Sep 21 โ€ข 26 tweets โ€ข 6 min read Twitter logo Read on Twitter
๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—๐—ช๐—ง ๐—น๐—ถ๐—ธ๐—ฒ ๐˜†๐—ผ๐˜‚'๐—น๐—น ๐—ป๐—ฒ๐˜ƒ๐—ฒ๐—ฟ ๐—ณ๐—ผ๐—ฟ๐—ด๐—ฒ๐˜.

๐Ÿงต Image
Before I tell you all the fun part.

First, it's important to understand two key concepts.
- ๐—”๐˜‚๐˜๐—ต๐—ผ๐—ฟ๐—ถ๐˜€๐—ฎ๐˜๐—ถ๐—ผ๐—ป
- ๐—”๐˜‚๐˜๐—ต๐—ฒ๐—ป๐˜๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป

They are similar but not the same.

Let's see how ?
Say you're a developer in a tech company.

Your company ID card is your ๐—”๐—จ๐—ง๐—›๐—˜๐—ก๐—ง๐—œ๐—–๐—”๐—ง๐—œ๐—ข๐—ก - it's your identity.

As a developer you're authorized to code push and deploy.
But you're not authorized to access mergers and acquisition data.
These rules about who has access to what is known as ๐—”๐—จ๐—ง๐—›๐—ข๐—ฅ๐—œ๐—ฆ๐—”๐—ง๐—œ๐—ข๐—ก (used for access control, permissions etc.)

Here's is a sweet nugget to remember it forever: Image
Since you've understood what AUTHORISATION is, you must also understand that

- Authentication and Authorisation go hand in hand.

- They are two independent concepts, yet you need them to work together for designing an effective ๐—จ๐—ฆ๐—˜๐—ฅ ๐—ฆ๐—˜๐—ฆ๐—ฆ๐—œ๐—ข๐—ก ๐— ๐—”๐—ก๐—”๐—š๐—˜๐— ๐—˜๐—ก๐—ง. Image
Now, let's see how to implement authentication and authorisation, and how tokens (JWTs) are useful.

A simple user session management service needs 2 things:
- who the user is : authentication
- do they have permissions to access the requested resource : authorisation Image
Now, to design a system where all its resources are accessible to everyone is pretty simple.

No permission control, no authorisation required.
Everybody has access to every resource in the system, think of public CDNs.
The difficulties arise when the response from the server is dynamic.
Meaning userA logged in should not have access to userB's data.

Please understand, since http is a stateless protocol the server won't remember who is the request is coming from.
To fix that, we need some sort of information ( unique token) with each request.

Using this token the server could easily identify the user. Image
There are two methods in which web applications manage and remember users (sessions),

๐Ÿญ - ๐—ฆ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป ๐—ฏ๐—ฎ๐˜€๐—ฒ๐—ฑ
๐Ÿฎ - ๐—ง๐—ผ๐—ธ๐—ฒ๐—ป ๐—ฏ๐—ฎ๐˜€๐—ฒ๐—ฑ
๐—ฆ๐—ฒ๐˜€๐˜€๐—ถ๐—ผ๐—ป ๐—ฏ๐—ฎ๐˜€๐—ฒ๐—ฑ
- when you authenticate, the server creates a session and keeps track of it itself

- it creates a session_id to associate with that session and gives it back to client

- think of it as a reference token, holds a reference to user state on the server Image
- for the subsequent requests the client passes this session_id to the server as a part of every requests.

- the most common approach is to save the session_id in a http cookie

Next, let's see Token Based Approach
๐—ง๐—ผ๐—ธ๐—ฒ๐—ป ๐—ฏ๐—ฎ๐˜€๐—ฒ๐—ฑ

- server gives the user the details itself

- server encrypts entire user details in the form of a single JSON token

- server doesn't have to remember anything

- it's the client's responsibility to get these details every time it makes the requests
- think of it as value token, it contains the entire user info Image
Both the methods have its own advantages and disadvantages, and we'll discuss that in a separate post.

For the purpose of this post, your focus should be on the TOKEN.

A token simply put is a string of random characters.

But what's so special about a JWT token ?
The internet experts got together and drafted a neat way of token generation for data sharing on the internet.

That open standard is called RFC7519.

The cool name is JWT a.k.a JSON Web Token.

rfc-editor.org/rfc/rfc7519
Image
Although, JWT is commonly used for managing authorisation.

The idea behind JWT is to define a standard way between two parties to communicate information securely.

RFC7519 standard simply dictates:
- how the JSON data should be structured
- ways to encrypt it
- ways to sign it
In our token based method, JWT is the token, it follows the RFC7519 standard.

Now we've everything covered to learn about JWT.

Let's understand JWT and how JWT is the magic pill for almost all AUTHORISATION problems.
First, a JWT has a strictly defined structure to represent your data.

A JWT token structure contains three parts, each part is separated by a comma.

๐—›๐—˜๐—”๐——๐—˜๐—ฅ. ๐—ฃ๐—”๐—ฌ๐—Ÿ๐—ข๐—”๐——.๐—ฆ๐—œ๐—š๐—ก๐—”๐—ง๐—จ๐—ฅ๐—˜ Image
By now you would've understood that, a token is an imp. piece of information.

And, if a malicious user gets access to your JWT, they can impersonate you.

But token hijaking is not the problem what JWT solves.

I'm not gonna tell you why, follow me if you're curious. ๐Ÿ‘‡
A JWT token simply ensures that, your data in not tempered.

To temper the data, you'll need the secret_key.

All this is achieved using the signature part of the token.

๐˜€๐—ถ๐—ด๐—ป๐—ฎ๐˜๐˜‚๐—ฟ๐—ฒ = ๐—›๐—˜๐—”๐——๐—˜๐—ฅ + ๐—ฃ๐—”๐—ฌ๐—Ÿ๐—ข๐—”๐—— + ๐—ฎ_๐˜€๐—ฒ๐—ฐ๐—ฟ๐—ฒ๐˜_๐—ธ๐—ฒ๐˜†
Anyone who is in possession of this key can generate a new tokes with a valid signature.

Most commonly used crypto algorithms used for generating signatures are
- HS256
- RS256

Play around here , tweak the secret in the signature.

It'll all make sense.jwt.io
Few other characteristics of a JWT token is that,
- it's compact,
- self contained
- and fast.

Compact because it's just a simple string.
It can be easily send/receive via URL, post, http headers etc

This also helps in faster transfer.
Self contained because, this encoded string contains all the required info about the user.

Fast because since you've all the info available in the token.
You can avoid making user details query to the database more than once.
There is a lot more to cover about JWT, like, encryption, disadvantages, advantages etc..

For the curious ones, you go on to read about encryption, digital signature etc

I wanna keep going on and on, but I also want to hear your feedbacks, before I shove it all down.
For now, thanks for staying with me for so long.

Do visit my profile, @happydecoder see if you like my other tweets.

Don't hesitate to give a follow and Retweet.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with happydecoder

happydecoder Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @happydecoder

Aug 28
Requests on the internet consumes resources(cpu, time, memory etc).

If you're aiming for a high performance, high throughput system.

You should understand and internalise the concept of batching.

Follow, and Retweet. So my tweets can haunt you. Image
The idea of batching is pretty simple. Instead of sending each request or message separately, you group them together in a single request.

Usually, it is better to make fewer requests with more data as opposed to making more requests with less data.

why ? Image
With batching :
- you no need to pass http headers with every request

- no need to establish multiple connections and allocate memory resources for each requests.

- no need to create multiple threads to process requests (if the server is using the thread per request model).
Read 15 tweets
Aug 24
PROCESS VS THREAD

Reading this will help you understand how computer programs work and how they use your computerโ€™s resources. Image
Let's say you've got google chrome installed on your system.
Itโ€™s simply a file sitting on your disk.

When you click open chrome, you execute the file.
Your program comes to life. Memory space and other computing resources get allocated.

In other words, you initiated a process. Image
A process is a program in execution.

program = code file
process = code file + program counter + process stack + registers + heap etc

A program can have a single process or multiple processes.

for example - every time open a new tab in chrome, chrome spawns a new process. Image
Read 13 tweets
Aug 21
In a distributed world the micro services need to be able to talk to each other.

For instance,
When you hit the โค๏ธ button, the reaction service(service A) talks to the notification service(service B), and I'm notified of your reaction.

How does micro services communicateโ“ Image
There are two ways a service instance A can communicate with another service instance B.

1. Synchronous
2. Asynchronous
Synchronous -
In synchronous messaging the client sends its request directly to the server, and waits for the response to arrive.

The Client is dependent on the response from the server to proceed with its other requests.

HTTP REST is a good example. Image
Read 16 tweets
Oct 18, 2022
In the next 10 mins you'll learn/relearn Javascript prototypes.

-Thread-
Well, It's understandable to get confused about Javascript prototypes.

The clarity lies in understanding, the difference between

- [[Prototype]]
- __proto__
- prototype
- F.prototype
- Native.prototype
In programming, we often want to take something and extend it.

How do we extent something in the javascript world ?

In Javascript, Objects have a special HIDDEN property [[Prototype]], think of it as a HIDDEN reference to another object. Image
Read 27 tweets
Oct 6, 2022
Learn JWT like you'll never forget.

-Thread-
Before I tell you all the fun parts.
First, it's important to understand two key concepts.

Authorisation and Authentication.
They are similar but not the same.

Let's see how ?
Say you're a developer in a tech company.
Your company ID card is your AUTHENTICATION (it's your identity).

As a developer you're authorized to code push and deploy. But you're not authorized to access mergers and acquisition data.
Read 26 tweets
Oct 5, 2022
Are you struggling with "what should you do next ?"

-Thread-
Often times we get lost with no clear thoughts on what to do next.
Here is something that should help you breakout of that loop.

1 - Research your options:
- spend timed 30 minutes exploring options,
- see what you find exciting
- look into different programs and decide which one is the best fit for you

2 - Take the next steps:
- once you have decided on a program, take the necessary steps to apply
- devise a plan, a todo list, write it down
- writing it down helps you get back to it sooner
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(