{"record":{"id":"6d7b7787a50a5021","repo":"tiangolo/fastapi","slug":"inactive-user-6d7b77","errorCode":null,"errorMessage":"Inactive user","messagePattern":"Inactive user","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/security/tutorial005_py310.py","lineNumber":146,"sourceCode":"        raise credentials_exception\n    user = get_user(fake_users_db, username=token_data.username)\n    if user is None:\n        raise credentials_exception\n    for scope in security_scopes.scopes:\n        if scope not in token_data.scopes:\n            raise HTTPException(\n                status_code=status.HTTP_401_UNAUTHORIZED,\n                detail=\"Not enough permissions\",\n                headers={\"WWW-Authenticate\": authenticate_value},\n            )\n    return user\n\n\nasync def get_current_active_user(\n    current_user: User = Security(get_current_user, scopes=[\"me\"]),\n):\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_for_access_token(\n    form_data: OAuth2PasswordRequestForm = Depends(),\n) -> Token:\n    user = authenticate_user(fake_users_db, form_data.username, form_data.password)\n    if not user:\n        raise HTTPException(status_code=400, detail=\"Incorrect username or password\")\n    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)\n    access_token = create_access_token(\n        data={\"sub\": user.username, \"scope\": \" \".join(form_data.scopes)},\n        expires_delta=access_token_expires,\n    )\n    return Token(access_token=access_token, token_type=\"bearer\")\n\n","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/security/tutorial005_py310.py#L128-L164","documentation":"Legacy-DI variant of error 52. get_current_active_user raises HTTP 400 'Inactive user' when the JWT-authenticated, scope-authorized user has disabled=True. alice is seeded with disabled=True, so she fails this gate after passing auth and scopes. Only DI syntax differs from the Annotated version.","triggerScenarios":"Authenticate as alice with the correct scope, then call GET /users/me/ — passes JWT, user lookup, and scope checks, then line 145 raises.","commonSituations":"Testing the disabled path with alice; suspended production accounts.","solutions":["Use johndoe (disabled=False) for normal calls.","Set alice.disabled = False if alice needs access.","Return 403 Forbidden for disabled-but-authenticated, correctly-scoped users."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import jwt\ndef user_disabled_for_token(token: str, db: dict) -> bool:\n    payload = jwt.decode(token, SECRET_KEY, algorithms=[\"HS256\"])\n    u = db.get(payload.get(\"sub\"))\n    return bool(u and u.get(\"disabled\"))","typeGuard":"from typing import TypeGuard\ndef is_active_user(u: dict) -> TypeGuard[dict]:\n    return not u.get(\"disabled\", False)","tryCatchPattern":"import httpx\ntry:\n    r = httpx.get(\"/users/me/\", headers={\"Authorization\": f\"Bearer {token}\"})\n    r.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code in (400, 403) and \"Inactive\" in e.response.text:\n        prompt_reactivation()","preventionTips":["Avoid issuing tokens for the disabled 'alice' fixture except when testing that path.","Revoke outstanding tokens on disable.","Prefer 403 Forbidden over 400 here."],"tags":["fastapi","authentication","oauth2","scopes","authorization","python"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}