skip to content

Your FastAPI service issues its own JWTs from an OAuth2PasswordRequestForm login endpoint; when should a lead keep that design, and when hand sign-in to an identity provider?

level: principalimportance: nice to knowfreq 25%

answer

  1. what the token endpoint commits you to
  2. one first-party client, or many
  3. the current-user dependency is the seam
  4. who answers a credential incident

basics

~20 s

Keep in-app login while one first-party client needs plain username and password and the team can own hashing, signing keys, revocation and abuse controls. Delegate once several apps, single sign-on, MFA or audit duties arrive; the FastAPI app then only verifies tokens.

solid answer

~40 s

The FastAPI docs' design, `OAuth2PasswordRequestForm` at `/token`, pwdlib hashes, PyJWT tokens and a `get_current_user` chain, is small and fully under your control. What it costs is ownership: the team stores password hashes, keeps the dummy-hash and same-response rules, rotates the signing secret, decides how a stolen token is revoked, and builds lockout, reset and MFA itself. That is a fair trade for one first-party client and a team that can carry it. When several applications share accounts, or single sign-on, MFA or audit duties appear, an identity provider is cheaper: the FastAPI app deletes the login route and keeps only verification. If endpoints already depend on one `CurrentUser` alias, that move changes one dependency's internals and no path operation.

go deeper

for a junior

Recall that a FastAPI app can either issue its own tokens at a login route or accept tokens issued by an external identity provider.

for a middle

Explain which FastAPI pieces each design uses: the form class and token route on one side, and a verifying get_current_user on both.

for a senior

Size the ownership list of the in-app design, from hashing and the dummy verify to key rotation and revocation, against what the team can actually operate.

for a principal

Weigh client count, sign-in requirements and staffing, choose a design, and keep identity behind one dependency so the choice can be reversed cheaply.

## The decision Every FastAPI service that needs identity ends up in one of two shapes. In the first, the one FastAPI's tutorial builds, the app **issues** tokens: a `POST /token` route takes `OAuth2PasswordRequestForm`, checks a pwdlib hash, signs a JWT with PyJWT, and a `get_current_user` dependency verifies it on later calls. In the second, an external **identity provider** signs users in and issues tokens, and the FastAPI app only **verifies** them. Both are legitimate; the question is which costs the team less over the service's life. Which OAuth2 grant the provider should use, and how tokens should be validated, are protocol questions settled in the OAuth2 and JWT material; this decision is about where the FastAPI app's responsibilities end. ## What the in-app design makes you own Keeping the token endpoint means the team owns, permanently: - **Password storage**: the hasher choice, its cost parameters, and rehashing old hashes as the cost rises. - **The login route's hygiene**: identical failure responses, the dummy-hash verify for unknown usernames, and the CPU load of Argon2 checks on the service's own workers. - **Signing keys**: generating the secret, keeping it out of source, rotating it without logging everyone out. - **Token lifetime and revocation**: the tutorial's 30-minute expiry, and what happens when a token is stolen or an account is disabled mid-session. - **Everything around login**: registration, password reset, lockout and rate limits, MFA, and the audit trail of sign-ins. Little of that is FastAPI-specific work: FastAPI supplies the request plumbing and a documented recipe, not the operations behind it. A team that adopts the tutorial wholesale should see that list before it ships. ## Signals for each side | Signal | Points to | |---|---| | One first-party client (the team's own mobile or web app) | keeping login in the app | | Plain username and password is all the product needs | keeping login in the app | | A team that can carry the ownership list above | keeping login in the app | | Several applications sharing one account base | an identity provider | | Single sign-on, or social or enterprise sign-in | an identity provider | | MFA, compliance or sign-in audit requirements | an identity provider | | Security staffing too thin to handle a credential incident | an identity provider | ## What changes in the FastAPI code if you delegate The FastAPI side shrinks rather than grows: 1. **Delete the token endpoint** and `OAuth2PasswordRequestForm`, along with the password hashes and the dummy-hash logic. 2. **Replace the security declaration.** `OAuth2PasswordBearer` would still extract a bearer token, but it advertises a password flow at a `tokenUrl` the app no longer serves, so the docs' Authorize dialog would post passwords nowhere. Declare the provider's flow instead; FastAPI has declarations for that, and like the password one they read the `Authorization` header without validating anything. 3. **Change the inside of `get_current_user`**: verify the provider's tokens with the provider's published keys and expected issuer and audience, then map the subject to the local user record. 4. **Keep its return type.** If every path operation depends on one alias such as `CurrentUser = Annotated[User, Depends(get_current_active_user)]`, no endpoint changes, and tests that replace the current-user dependency keep working. ## A staged path A lead does not have to choose once and forever: 1. Start with the in-app design for a single first-party client, but route all identity through one current-user dependency from day one. 2. Keep token-issuing code isolated in the login route and one token helper, so it can be deleted cleanly. 3. When a delegation signal appears, introduce the provider, accept both token kinds in `get_current_user` for one release, and migrate clients. 4. Remove the login route and the old signing secret once no client uses them. ## Where leads go wrong - **Copying the tutorial into production without the ownership list**, then discovering revocation and reset only after the first incident. - **Delegating but keeping a half-used `/token` route** for scripts, which keeps every in-app duty alive for one convenience. - **Spreading token decoding across endpoints**, which turns a later move into a rewrite of every path operation. - **Treating the choice as a library choice**, PyJWT or another package, when the cost is in operations and staffing, not in code.

  • After delegating sign-in, can a FastAPI app keep OAuth2PasswordBearer just to read the bearer header?
    It would still extract the token, since it only reads `Authorization: Bearer`. But it tells the generated schema that clients get tokens by posting a password to `tokenUrl`, so the docs' Authorize dialog would send passwords to a route that no longer exists. Declare the provider's flow so the schema matches reality.
  • What makes the move from in-app login to an identity provider cheap or expensive in a FastAPI codebase?
    Whether identity flows through one dependency. If every endpoint takes a `CurrentUser` alias over `get_current_active_user`, the move rewrites that dependency's internals and deletes the login route. If endpoints decode tokens themselves, each one changes, and so do the tests that fake identity.

  • Delegating sign-in means the FastAPI app no longer checks tokens
  • Shipping the tutorial's login design without owning revocation and key rotation
  • Rewriting every path operation to switch where tokens come from
  • Treating the decision as a choice between JWT libraries