Authentication

Controlling who can do what

Concepts

Why authentication?

HTTP Basic Auth

Session-based login

Protecting routes

What this does not cover

Check for Understanding

What is the difference between authentication and authorization?

Authentication answers "who are you?" by verifying a claimed identity (username + password, certificate, token). Authorization answers "are you allowed to do this?" by checking the authenticated identity against a set of permissions. A logged-in user is authenticated; whether they can delete a record depends on authorization. You can have authentication without authorization (everyone who logs in can do everything) or, less commonly, coarse authorization without strong authentication.

Why is it unsafe to store passwords in a config file as plaintext, even for an internal research tool?

Config files are often committed to version control, shared with collaborators, backed up to cloud storage, or read by anyone who has shell access to the server. A plaintext password in a file is readable by anyone who can read the file. Even if the risk seems low today, credentials tend to outlive the context in which they were created, and people reuse passwords across systems. Hashing protects against these scenarios.

What does "hashing" a password mean, and why does using sha256 alone not provide adequate protection?

Hashing transforms a password into a fixed-length string that cannot be reversed to recover the original. sha256 is fast—designed for checksums, not passwords—which means an attacker with the hash database can test billions of guesses per second. Password hashing algorithms like bcrypt or Argon2 are deliberately slow and can be tuned to make each guess take milliseconds, reducing an attacker's throughput from billions per second to thousands per second.

If a Litestar guard returns a 401, what does the browser typically do next?

For HTTP Basic Auth, the browser displays a built-in login dialog prompting for a username and password. If the server also sends a WWW-Authenticate header specifying the realm and scheme. For session-based auth, a 401 typically triggers a JavaScript redirect to the login page (or the server itself redirects with a 302). The exact behavior depends on how the guard and the client are implemented.

Exercises

Add a logout route

Add a POST /logout route that clears the session cookie and redirects to the home page. Write a pytest test that: (1) logs in, (2) makes a successful request to a protected route, (3) calls the logout route, (4) makes the same request to the protected route again, and (5) asserts the response is 401.

Test the guard

Write two pytest tests for a protected route: one that sends a request with no credentials and asserts the response is 401, and one that sends correct credentials and asserts the response is 200. Use create_test_client and pass credentials in the request headers; do not rely on a running server.

Audit the threat model

List the three most realistic threats to a research tool deployed on a university server (for example: a disgruntled collaborator, accidental exposure of the URL, a compromised user account). For each threat, describe whether the authentication scheme implemented in this lesson addresses it, partially addresses it, or does not address it. Suggest one additional measure that would reduce each remaining risk.

Compare authentication approaches

Ask the LLM to explain the differences between HTTP Basic Auth, session-based login, and API token authentication. Evaluate the explanation: is it accurate? Does it explain when each approach is appropriate? Add a sentence for anything the LLM omitted or got wrong.