{"record":{"id":"d3af55f814ec16a8","repo":"tiangolo/fastapi","slug":"inactive-user-d3af55","errorCode":null,"errorMessage":"Inactive user","messagePattern":"Inactive user","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"docs_src/security/tutorial005_an_py310.py","lineNumber":147,"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: Annotated[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: Annotated[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":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/security/tutorial005_an_py310.py#L129-L165","documentation":"Disabled-account gate in the scopes tutorial, running AFTER scope validation inside get_current_active_user. The alice fixture is seeded with disabled=True, so even a valid JWT carrying the correct scopes for alice is rejected with HTTP 400. Reaching here means authentication AND authorization both passed; only the disabled flag fails.","triggerScenarios":"Authenticate as alice (POST /token with username=alice, correct password and the needed scope), then call GET /users/me/ — JWT, user lookup, and scope checks all pass, then line 146 raises because alice.disabled is True.","commonSituations":"Using alice to exercise the disabled path; suspended accounts in production; forgetting to flip disabled after reactivating a fixture.","solutions":["Use johndoe (disabled=False) for normal requests.","Set alice.disabled = False in fake_users_db if alice must be usable.","Return 403 Forbidden instead of 400 for disabled-but-authenticated, correctly-scoped users."],"exampleFix":"// before\nif current_user.disabled:\n    raise HTTPException(status_code=400, detail=\"Inactive user\")\n\n// after\nif current_user.disabled:\n    raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=\"Inactive user\")","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":["Do not hand out tokens for the seeded disabled 'alice' except to test that path.","Revoke tokens when an account is disabled.","Prefer 403 Forbidden for disabled-but-authenticated, correctly-scoped users."],"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"}