{"record":{"id":"3fc278352c08ca17","repo":"tiangolo/fastapi","slug":"incorrect-username-or-password-3fc278","errorCode":null,"errorMessage":"Incorrect username or password","messagePattern":"Incorrect username or password","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/security/tutorial003_py310.py","lineNumber":77,"sourceCode":"        raise HTTPException(\n            status_code=status.HTTP_401_UNAUTHORIZED,\n            detail=\"Not authenticated\",\n            headers={\"WWW-Authenticate\": \"Bearer\"},\n        )\n    return user\n\n\nasync def get_current_active_user(current_user: User = Depends(get_current_user)):\n    if current_user.disabled:\n        raise HTTPException(status_code=400, detail=\"Inactive user\")\n    return current_user\n\n\n@app.post(\"/token\")\nasync def login(form_data: OAuth2PasswordRequestForm = Depends()):\n    user_dict = fake_users_db.get(form_data.username)\n    if not user_dict:\n        raise HTTPException(status_code=400, detail=\"Incorrect username or password\")\n    user = UserInDB(**user_dict)\n    hashed_password = fake_hash_password(form_data.password)\n    if not hashed_password == user.hashed_password:\n        raise HTTPException(status_code=400, detail=\"Incorrect username or password\")\n\n    return {\"access_token\": user.username, \"token_type\": \"bearer\"}\n\n\n@app.get(\"/users/me\")\nasync def read_users_me(current_user: User = Depends(get_current_active_user)):\n    return current_user\n","sourceCodeStart":59,"sourceCodeEnd":89,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/security/tutorial003_py310.py#L59-L89","documentation":"Legacy-DI variant of error 41. The /token handler raises HTTP 400 'Incorrect username or password' when fake_users_db.get(form_data.username) is None. Same anti-enumeration rationale and same message as the password-mismatch branch at line 81: an attacker cannot tell whether the username or the password was wrong.","triggerScenarios":"POST /token with username not equal to 'johndoe' or 'alice' (case/whitespace sensitive), or with the field misnamed (e.g. 'user' instead of 'username').","commonSituations":"Case mismatch, copy-paste whitespace, JSON body instead of form-encoded, stale test fixture.","solutions":["POST username exactly matching a fake_users_db key plus the matching password.","Verify the OAuth2PasswordRequestForm field name is 'username' and Content-Type is application/x-www-form-urlencoded.","Normalize input (strip/lower) before lookup if your domain requires case-insensitive logins."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"KNOWN_USERS = {\"johndoe\", \"alice\"}\ndef valid_login_form(username: str, password: str) -> bool:\n    return isinstance(username, str) and username.strip() in KNOWN_USERS and bool(password)","typeGuard":"from typing import TypeGuard\ndef is_non_empty_creds(pair: tuple) -> TypeGuard[tuple[str, str]]:\n    u, p = pair\n    return isinstance(u, str) and isinstance(p, str) and u.strip() != \"\" and p != \"\"","tryCatchPattern":"import httpx\ntry:\n    r = httpx.post(\"/token\", data={\"username\": u, \"password\": p})\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code in (400, 401):\n        show_generic_login_error()","preventionTips":["Send 'username' as a form field with the correct Content-Type.","Normalize whitespace/case if the domain requires it.","Handle 400 and 401 identically on the client."],"tags":["fastapi","authentication","oauth2","python","user-enumeration"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}