Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Session vs Token Based Authentication
Next article icon

Session vs Token Based Authentication

Last Updated : 04 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Session and Token-based Authentication methods are used to make a server trust any request sent by an authenticated user over the internet. In this way, a user can interact with their account without continually specifying their credentials. These methods are usually used for different purposes. 

For example, sessions are commonly used in websites applications while tokens are preferred in server-to-server connections. 

Authentication

Session Authentication

A session is a small file, most likely in JSON format, that stores information about the user, such as a unique ID, time of login and expirations, and so on. It is generated and stored on the server so that the server can keep track of the user requests. The user receives some of these details, especially the ID, as cookies that will be sent with every new request, so that the server can recognize the ID and authorize the user's requests.

Working

  1. The user sends a login request to the server.
  2. The server authenticates the login request, sends a session to the database, and returns a cookie containing the session ID to the user.
  3. Now, the user sends new requests (with a cookie).
  4. The server checks in the database for the ID found in the cookie, if the ID is found it sends the requested pages to the user.
Session Authentication
Session Authentication

Pros/Cons

Since sessions are stored on the server, its administrators are in power over them. For example,  if a security team suspects an account is compromised, they can immediately invalidate the session ID, so that the user is immediately logged out.  On the other hand, since a session is stored on the server, the server is in charge of looking up the session ID that the user sends. This can cause scalability problems.

Cookies may be exposed to cross-site request forgery attacks. The attacker may mislead the user to a hostile website, where some JS scripts may exploit cookies to send malicious requests to the server. Another vulnerability regards the chances of a man-in-the-middle attack, where an attacker can intercept the session ID and perform harmful requests to the server.

Token-Based Authentication

A token is an authorization file that cannot be tampered with. It is generated by the server using a secret key, sent to and stored by the user in their local storage. Like in the case of cookies, the user sends this token to the server with every new request, so that the server can verify its signature and authorize the requests. 

Working

  1. The user sends a login request to the server.
  2. The server authorizes the login and sends a token to the user.
  3. Now, the user sends a new request(with a token).
  4. The server checks the token is valid or not, if the token is valid it sends the requested pages to the user.
 Token Authentication
 Token Authentication

Note- Those are not authentication files, they are authorization ones. While receiving a token, the server does not look up who the user is, it simply authorizes the user's requests relying on the validity of the token.

Pros/Cons

Tokens can be useful when the user wants to reduce the number of times they must send their credential. In the case of server-to-server connections, using credentials becomes difficult, and tokens overcome this problem. Moreover, servers that use tokens can improve their performances, because they do not need to continuously look through all the session details to authorize the user's requests. 

However, the authentication details are stored on the client, so the server cannot perform certain security operations as in the session method. As written above, the server does not authenticate the user, so linking a token to its user can be more difficult. If a hypothetical attacker manages to get a valid token, they may have unlimited access to the server databases. If the server generates keys using older algorithms, these keys can be breached.

Differences Between Session and Token-Based Authentication Methods

 Criteria

Session authentication method

Token-based authentication method
1.Which side of the connection stores the authentication detailsServerUser
2.What the user sends to the server to have their requests authorizedA cookieThe token itself
3.What the server does to authorize users' requestsLooking up in its databases to find the right session thanks to the ID the user sends with a cookieDecrypting the user's token and verifying its signature
4.Can the server admins perform securities operations like logging users out, changing their details, etcYes, because the session is stored on the serverNo, because the token is stored on the user's machine
5.From what kind of attacks the method may sufferMan-in-the-middle, Cross-site request forgeryMan-in-the-middle, Token steal, breaches of the secret key
6.Preferred method applicationUser-to-server connectionsServer-to-server connections

Conclusion

  1. Session and token-based are two authentication methods that allow a server to trust all the requests it receives from a user. The main difference is session-based authentication of the connection stores the authentication details. The session method makes the server store most of the details, while in the case of the token-based one the client stores them.
  2. The session authentication method is based on the concept of the ID being shared with the client through a cookie file, while the rest of the details are on the session file, stored on the server.
  3. The token-based authentication method is based on the concept that possessing a token is the only thing that a user needs to have their requests authorized by the server, which must only verify a signature. The token is secure to use because it cannot be tampered with.
  4. Both methods have inherent vulnerabilities that can be most easily resolved with different workarounds. In the end, developers must decide which method suits better to their needs and applications.

Next Article
Session vs Token Based Authentication

A

antoniopolara
Improve
Article Tags :
  • Computer Subject
  • Computer Networks

Similar Reads

    How does the Token-Based Authentication work ?
    Digital transformation brings security concerns for users to protect their identity from bogus eyes. According to US Norton, on average 8 lakh accounts are being hacked every year. There is a demand for high-security systems and cybersecurity regulations for authentication. Traditional methods rely
    6 min read
    Session-Based Authentication vs. JSON Web Tokens (JWTs) in System Design
    Authentication is essential for websites and apps to verify users' identities. It's similar to showing your ID before entering a secured place or using a service online. There are two main ways to do this: Session-Based Authentication and JSON Web Tokens (JWTs). Session-based authentication creates
    8 min read
    Flask API Authentication with JSON Web Tokens
    Authentication is the process of verifying the identity of the user. It checks whether the user is real or not. It is used to provide access to resources only to valid users. There are two types of Authentication: Single Factor Authentication: In this only one piece of information is needed to verif
    7 min read
    Authentication in Distributed System
    Authentication in distributed systems is crucial for verifying the identity of users, devices, and services to ensure secure access to resources. As systems span multiple servers and locations, robust authentication mechanisms prevent unauthorized access and data breaches. This article explores vari
    11 min read
    What is Authentication Tokens In Network Security?
    Password is the most common way of authentication. However, they are not as cheap and simple as we think, especially from the organization's point of view because they have to provide an id and password to each resource they use as well as they've to manage the passwords of so many people using thei
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences