From Zero to Hero (▀̿Ĺ̯▀̿ ̿) in Authentication | Part 1
Tags: beginners, webdev, security, computerscience
Published: 2021-04-22 12:14:03

# Authentication  
First focus on **"WHAT"**.
> {% wikipedia https://en.wikipedia.org/wiki/Authentication %}

OKAY.🤨! But it is kinda hard for me. Let's understand with a simple example. For example, you're Sheldon Cooper you go to the bank to withdraw money. You say to the cashier give me $5000, they were like _"Who are you?? I don't know you so I can't"_. This happens because they don't know you, to identify you as a real holder of your bank account. They provided you a bank account number and pin/signature to identify you. When you give these details to the cashier, he will like _"Thank you! Mr. Cooper, here is your $5000"_.  So this shows that by giving the details it identifies you as the real holder of the bank account. _So the process or action of verifying the identity of a user or process is **Authentication**_.
![Security](https://media.giphy.com/media/3o6MbmTd7iUDK5AiHu/giphy.gif)

# Types of Authentication

1. Stateful
   - Cookie/Session Based Auth.
2. Stateless
   - Basic Auth
   - JWT
   - OpenID Connect 

# Stateful Authentication
![Cookie](https://media.giphy.com/media/7GYHmjk6vlqY8/giphy.gif)
Stateful Authentication is a way to verify users by having the server or backend store much of the session information, such as user properties. 
In this method whenever a client requests to the server, the request carries the unique-id provided by the server at the authentication time and this ID is matched against its identity provider (IdP).
Stateful authentication is also called session-based authentication or cookie-based authentication for the session information the server must store on the user.
## Advantages
- Revoke the session anytime.
- Easy to implement and manage.

## Disadvantages
- Increasing server overhead: As the number of logged-in users increases, the more server resources are occupied.
- Server has to store the session id which limits scalability.

# Stateless Authentication
![Token-based](https://media.giphy.com/media/eSgUxM1lzE4YYL4A9O/giphy.gif)
Stateless authentication is a way to verify a user by storing its data on the client-side and signing it with some cryptographic signature. This signed data is known as a token. Whenever a client tries to request the server, the server verifies the signed token, check if the token is valid or not.

## Advantages
- Easy to scale 
- Lower server overhead: Server does not have to allocate resources to the client.

## Disadvantages
- Cannot revoke the session anytime: As token is stored at client side, server does not have any rights to delete the session.
- Relatively complex to implement: It increases the technical complexity while implementing.
- If a token gets steal before expiring and a hacker uses it, the server has no way to identify it. That's why token's expiry time should be less(<=15min).
![Man-In-Middle](https://media.giphy.com/media/wY6uwmZZAchWM/giphy.gif)


|                                                         | Stateful                                                                                                                                  | Stateless                                                                                                                             |
| ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Session information could be stolen                     | ✅ It is impossible to steal session information from the session identifier because it is just an identifier associated with the session | ❌ Session identifier contains all authentication information and it is possible to steal sensitive information, it is not encrypted. |
| Resource consuming                                      | ✅ When retrieving session information, service always gets access to session storage which causes additional resource consumption.       | ❌ The session identifier contains all session information.                                                                           |
| Easy to implement                                       | ✅When session information stored in an external database, there is a need to implement session database persistence                      | ❌Session identifier contains all session information, there is no need to implement additional functionality                         |
| Easy to scale                                           | ❌While adding new instances, there is a need to implement additional scale to session storage as well                                    | ✅Adding new service instances does not require additional effort                                                                     |
| Authentication token size                               | 🤏It is just an identifier, So size is small                                                                                              | 🥵 It contains a large amount of data, the authentication token also becomes large                                                    |
| Restrict access among different parts of an application | ✅It is possible to configure the system so different parts of the system will only have access to the data necessary for their work      | ❌All parts of the system have access to all session data                                                                             |
| Possibility to revoke session                           | ✅It is possible to revoke a session at any time                                                                                          | ❌Since the session token contains an expiration date, it is impossible to revoke the authentication session                          |

# Conclusion
Both approaches make sense, both have their advantages and disadvantages. Stateless authentication easier to implement and scale, but stateful authentication is more secure and easier to manage. In the next part, we'll understand each way of authentication like JWT, Cookie, OAuth, Etc. 👋🏻
![Bye](https://media.giphy.com/media/YnGDmAudv7F2UASBqK/giphy.gif)

### Bibliography
- [Stateful vs Stateless Authentication](https://www.openidentityplatform.org/blog/stateless-vs-stateful-authentication)
- [Difference between cookies, session and tokens](https://www.youtube.com/watch?v=44c1t_cKylo)