Skip to content

Instantly share code, notes, and snippets.

@nabily4e-dev
Created January 18, 2024 16:49
Show Gist options
  • Save nabily4e-dev/46a55a503099b9fca125bcf76914a060 to your computer and use it in GitHub Desktop.
Save nabily4e-dev/46a55a503099b9fca125bcf76914a060 to your computer and use it in GitHub Desktop.

Difference between cookies, session and tokens

In this video, Valentin Despa, a software developer and instructor, explains the concepts and uses of cookies, sessions, and tokens in web development. He uses examples and diagrams to illustrate the differences and similarities between these three terms.

What are cookies?

Cookies are small pieces of data that are stored by the browser and sent to the server with every request. They can be used to store information such as user preferences, shopping cart items, or authentication status. Cookies have a name, a value, an expiration date, and a domain attribute that specifies which websites can access them.

What are sessions?

Sessions are server-side mechanisms that store information about the user's state across multiple requests. They are usually implemented by generating a unique session ID for each user and sending it as a cookie to the browser. The server then uses the session ID to retrieve the session data from a database or a file. Sessions can store more information than cookies, but they also consume more server resources and can be vulnerable to session hijacking attacks.

What are tokens?

Tokens are self-contained pieces of data that contain information about the user's identity, permissions, and other claims. They are usually encoded and signed using a secret key or a public-private key pair. They can be used to implement stateless authentication, where the server does not need to store any session data, but only verifies the validity and integrity of the token. Tokens can be more secure, scalable, and performant than cookies and sessions, but they also require more logic and infrastructure to handle them.

Comparison of cookies and sessions versus tokens

The video compares and contrasts the advantages and disadvantages of using cookies and sessions versus tokens for authentication. Some of the factors discussed are:

  • Security: Cookies and sessions can be vulnerable to CSRF (cross-site request forgery) and XSS (cross-site scripting) attacks, while tokens can prevent these attacks by using HTTPS and the same-origin policy. However, tokens can also be vulnerable to replay attacks if they are not encrypted or have a short expiration time.
  • Scalability: Cookies and sessions can be difficult to scale across multiple servers or domains, as they require synchronization and coordination. Tokens can be easily scaled, as they do not depend on the server state and can be validated by any server that has the secret key or the public key.
  • Performance: Cookies and sessions can affect the performance of the application, as they increase the size of the request and the response, and require database queries or file operations. Tokens can improve the performance, as they reduce the overhead and the latency of the communication, and eliminate the need for database or file access.
  • Cross-domain access: Cookies and sessions can be limited by the domain attribute, which restricts the access to the same domain or subdomain. Tokens can enable cross-domain access, as they can be sent as a header or a query parameter, and can be validated by any server that has the key.

Conclusion

The video concludes by highlighting the benefits of using tokens for authentication, such as flexibility, security, and performance. However, it also acknowledges that there is no one-size-fits-all solution, and that the choice of the authentication method depends on the requirements and the context of the application.

Source: Conversation with Bing, 1/18/2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment