What is Authentication?
In the context of web development, authentication is simply a process of verifying if a user is who they say they are, essentially confirming the identity of the user.
You can think of authentication as a contract or agreement that binds a user to a website or application, allowing them access based on their verified identity.
There are different forms of authentication, such as;
Password-based authentication
One-time password authentication
Biometric authentication (use of human features like a thumbprint, retina scan, etc.)
A combination of two or more types of authentications, e.g. Password and OTP
The most common form of authentication method is providing a username and password. When a user submits these credentials, they are compared with the credentials stored on the application server, which the user provided during the sign-up phase. If the credentials are valid, the user is granted access.
Authentication and its Importance in Web Development
There are various reasons why authentication is crucial in web development, including;
Protects confidential data
Protects web applications from unauthorized access
Builds trust and reputation between users and systems/applications
Keep track of users' behavior.
There are various methods involved in authenticating web applications; examples include;
Session-based authentication
Token-based authentication (JWT-)
Third-party authentication (OAuth, API token)
SAML (Security Assertion Markup Language)
OpenID (Standard and decentralized authentication protocol)
The two main methods of user authentication in web development are session authentication and JWT (JSON Web Token Authentication).
Overview of Session Authentication
When a user provides the necessary credentials required to log into a website, session authentication can be used to manage the authentication state.
The server creates a persistent record that represents the session, and this record contains a unique identifier created for the user. The record is stored in the database or file system and returned to the browser in the form of a cookie. The browser then stores the record as a cookie, allowing the server to recognize the user on subsequent requests.
Each following HTTP request from the browser sent to the server contains the session cookie. The server then uses this cookie to look up the session record and verify that it is valid and then ensures the user remains authenticated during every request.
Let’s break this down;
The server receives an initial login request sent by the user via login credentials.
The server checks that login credentials are accurate against stored information in the database.
The server creates a new session and stores it (typically in a database or memory cache), the data might include data ID, expiration time and other metadata.
The server sends a response with a unique session ID in the form of a cookie.
The user receives the server's response along with the session ID and saves it in the cookie.
On subsequent requests, the client sends a session ID cookie with each request
The server receives the request with the cookie, identifies and verifies that the corresponding session data is in the session store, and uses the data to authenticate and process the request.
This process occurs for subsequent HTTP requests sent from the client to the server. As long as the user is logged in, the server knows which user is making a request instead of making the user re-authenticate their credentials.
The session ID is destroyed on both the client and the server side when the client logs out. This means that once the session is terminated, the user is no longer authenticated, preventing unauthorized access to the account after the user has logged out.
Benefits of Session Authentication
Revoking sessions can be done easily since the session is stored on the server. The server can invalidate the session at any time, effectively ending the users’ authentication.
Session authentication is easy to use if you're developing a web-based application because cookies are supported by browsers on the client side.
Session cookies do not require large storage, making it efficient for storage on the client side.
The server controls sessions, therefore, it can implement security measures such as session timeout or IP address validation.
Limitations of Session Authentication
In large-scale projects, session-based authentication can introduce latency issues because it requires interaction with a centralized store such as the database. Also, if there's a heavy load, it affects the performance of the application.
In a distributed system where an application runs on multiple servers, the servers need access to the same session data. Using a centralized session store that all servers can access may add latency and complexity to each request because the server needs to make a separate trip to the session store.
If session IDs are not secured properly, they are vulnerable to XSS attacks allowing attackers the ability to impersonate users.