OAuth2 Password Flow

noun

say it: “OH-awth-TOO PASS-wurd FLOH” /ˌoʊˌɔθˈtu ˈpæsˌwɝd floʊ/

A sign-in where the app sends a username and password to a token endpoint and gets back a token to send on later requests; FastAPI has helpers for it.

What is OAuth2 Password Flow?

The OAuth2 password flow is a login where the client sends a username and password to a token endpoint and gets back a token to send on later requests. FastAPI has helpers for it.

oauth2_scheme = OAuth2PasswordBearer(tokenUrl='token')

@app.post('/token')
def login(form: Annotated[OAuth2PasswordRequestForm, Depends()]):
    user = check_password(form.username, form.password)
    return {'access_token': make_token(user), 'token_type': 'bearer'}

In a sentence

  • “The /token route follows the OAuth2 password flow, so the docs' Authorize button works.”
  • “With the OAuth2 password flow, the password is sent once and the token after that.”

Level: Freelancer · More FastAPI terms at this level

Updated