APIs with FastAPI · Chapter 13 of 26

Passwords and tokens

By the end of this chapter you can store a password so that nobody, not even DakiyaGo, can read it back; let a person log in once with the OAuth2 password flow; hand them a signed JWT that proves who they are on every later request; and say exactly what that token protects and what it does not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Security and real-world behaviour
  • 4 tasks
  • FastAPI
  • In the all labs plan

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.

Why you need this

Merchants' software already calls DakiyaGo with an API key. Now Imran is building a dashboard for the merchants' people: Neha at Neelkanth Blue Pottery wants to log in, see her parcels and download labels. People do not paste API keys; they type an email and a password.

That brings two problems you have not met yet. A table of passwords is the most dangerous thing a company can leak, so DakiyaGo must keep something it can check a password against but can never read. And Neha must not send her password on every click: she logs in once, gets a token, and shows the token instead, until it expires.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    signup.py: sign up without keeping the password

    Merchants' staff need dashboard accounts, and DakiyaGo must never be able to read their passwords. Make an app with USERS = {} at the top of the file (email to account) and POST /signup taking the JSON body {"email", "name", "password"}, the password at least 10 characters (422 otherwise). Save USERS[email] = {"name": ..., "password_hash": ...} with the hash from pwdlib's recommended hasher (Argon2), and answer 201 with only the email and the name. An email that already has an account answers 409 and changes nothing.

    The check signs people up, reads USERS, verifies the saved hash against the password, and looks for the password and the hash in everything you saved and answered.

  2. Task 2

    login.py: log in and get a token

    Make an app with POST /token for the OAuth2 password flow: a form (not JSON) with username (the email) and password. The right password answers 200 with {"access_token": <JWT>, "token_type": "bearer"}; the JWT is signed with SECRET and HS256, its sub is the email, and its exp is 30 minutes from now. A wrong password and an unknown email both answer 401, word for word the same, with the header WWW-Authenticate: Bearer.

    USERS in the starter holds two test accounts: neha@neelkanth.in (password blue-pottery-2026) and farhan@gulabi.in (password block-print-2026). The check logs in as both, decodes your tokens with SECRET, and tries a wrong password and an email with no account.

  3. Task 3

    me.py: who is asking?

    Make an app with GET /me: take the token with OAuth2PasswordBearer (tokenUrl "token"), decode it with SECRET and HS256, and answer {"email": <the token's sub>, "name": ..., "merchant_id": ...} from USERS in the starter.

    Answer 401 with WWW-Authenticate: Bearer for no token, an expired token, a token signed with another secret, a token that is not a JWT, an unsigned token (alg none), and a token for an email that is not in USERS. None of them may be a 500. The check makes the tokens itself with PyJWT.

  4. Task 4 · interview question

    Interview: claims.py, what a token protects

    Imran found a merchant's developer reading our tokens on a JWT website, without the secret. Make an app with POST /token as in task 2 (same form, same 401), but the token's claims are sub (the email), merchant_id, plan and exp (30 minutes), and nothing else: never the API key or the password hash. Add GET /plan, which takes the token as in task 3 and answers {"merchant_id": ..., "plan": ...} from the token's claims alone, with 401 for a bad token.

    The check reads your token without the secret, then edits its plan to enterprise (keeping the old signature) and sends it to GET /plan. The interviewer will ask what a JWT protects, and what it does not.

What you will learn

  • Hashing passwords with Argon2 (pwdlib), and why a fast hash is wrong.
  • The OAuth2 password flow: a form to POST /token, a token back.
  • JWTs: what is inside, how the signature works, and expiry.
  • OAuth2PasswordBearer: reading Authorization: Bearer ... in a route.
  • What a token protects (who it is) and what it does not (secrecy).

More in Security and real-world behaviour

Build "Passwords and tokens" yourself

DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.