Kinnu

Authentication

Authentication and User Management

What is authentication?

Imagine a passport control station: your identity is verified before you’re granted access to a country. Sometimes you'll need to provide special proof, like a passport or a visa, that you are who you say you are.

Passport control. Image: Connor Danylenko via Pexels (https://www.pexels.com/photo/photo-of-people-in-airport-2612113/)

Similarly, in backend development, authentication involves verifying the identity of users, typically using a username and password, before granting them access to a system or application.

Without authentication, systems would be vulnerable to unauthorized access, leading to potential data breaches and other security issues. Can you imagine the horror of someone getting access to your Facebook message history? You probably have first-hand experience with many authentication methods already.

User registration

User registration is a crucial process in backend development. This typically involves a username and password, which are used to authenticate the user during the login process.

Login form. Image: MZMcBride, Public domain, via Wikimedia Commons

How does a login actually work? To your web browser, the string of characters making up your username and password are just random text. But behind the scenes, backend logic compares the entered password with the stored password. If the two match, you’re granted access to the system. A Backend Developer can force a user to create a strong password by rejecting any passwords that do not contain a mix of letters and numbers.

To make this process more secure, developers can implement multi-factor authentication (MFA). This system requires users to provide two or more forms of identity verification, such as a password and a one-time code sent to their mobile device, which will all be confirmed by the backend. With MFA, even if a hacker got hold of your password, they wouldn't be able to log in.

Hashing

If a user's login details are stored in a database, this database needs to be secure.

Enter hashing: instead of storing a plaintext file of your password, your password is transformed into a unique value by a special hashing algorithm. Even if the hashed password is stolen, the original password can’t just be read from the file. This is something a Backend Developer can set up using a hashing function like bcrypt.

Hash function. Image: helix84, Public domain, via Wikimedia Commons

Sometimes a 'salt', or a random string of characters, is added to the user's password before it is hashed. This makes it significantly more difficult for attackers to decipher the password again, thereby providing an additional layer of security.

Tokens

As you've probably noticed, some websites let you stay logged in, even after you've walked away or completely turned off your computer.

This is achieved using authentication tokens. In basic terms, when you first log in, the website sends you a token, which is typically stored in your browser. When you open the website up again, it detects that token, and does not ask for your log in details again.

JSON Web Token (JWT) is a popular standard for creating authentication tokens. JWTs are compact, self-contained tokens that contain all the necessary information to authenticate the user. They are typically used in stateless authentication mechanisms, where the server does not need to keep a record of the user's session. In stateless authentication, because all the user info is contained within the JWT itself, the server can validate the user without needing any extra storage for sessions.

Cookies

Cookies are small pieces of data stored on a user's browser. You’re probably most familiar with marketing and advertising cookies, but they can be used for authentication too.

A good example of this is Single Sign-On (SSO). This system is useful when you have a suite of applications like Gmail, Google Docs, Google Sheets, Google Slides and Google Classroom. You have the same login details for all of them, and don't want to enter those details over and over again – and with SSO, you won't have to. A cookie tells your browser that you've already logged into Gmail, so you're given instant access to all those other places too.

To enhance the security of cookies, developers can use Secure flags. The Secure flag ensures that the cookie is only sent over secure (HTTPS) connections, and can't be snatched up by hackers.

Security Measures and Testing

Endpoint security

User logins are not the only security concern for developers. API endpoints are like doors into the sides of their applications – and for obvious reasons, these doors should be secure as well.

A great approach is something called Transport Layer Security (TLS). Basically, when two sides begin to exchange data using an API, they will also exchange a key. This key is used to encrypt and decrypt all the data shared between them.

TLS protocol stack. Image: Gorivero, CC BY 3.0 <https://creativecommons.org/licenses/by/3.0>, via Wikimedia Commons

TLS makes sure that the data flying between API endpoints is secure. Even if a hacker managed to eavesdrop on the exchange of data, they would not be able to decrypt it.

Testing

As a Backend Developer, it's not enough to create authentication and security mechanisms – they also need to check that those measures work.

Many Backend Developers use a technique called penetration testing. This involves simulating attacks on the system in order to identify potential weaknesses. For example, a developer might deliberately attempt an SQL injection, which is when you intentionally input malicious code into a database. If this injection works, then the Backend Developer can fix it. If it fails, then it's obviously nothing they need to worry about.

In 2012, Yahoo! suffered a SQL injection that resulted in a breach of 450,000 Yahoo! user credentials. The hackers entered malicious data into the database, and the application “read” that data as a command to spit out sensitive user information.

Automated security testing tools, such as OWASP ZAP, can also be used to identify common security vulnerabilities.