tiangolo/fastapi · error · HTTPException
Inactive user
Error message
Inactive user
What it means
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.
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.
Example fix
// before
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
// after
if current_user.disabled:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Inactive user") Defensive patterns
Strategy: try-catch
Validate before calling
import jwt
def user_disabled_for_token(token: str, db: dict) -> bool:
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
u = db.get(payload.get("sub"))
return bool(u and u.get("disabled")) Type guard
from typing import TypeGuard
def is_active_user(u: dict) -> TypeGuard[dict]:
return not u.get("disabled", False) Try / catch
import httpx
try:
r = httpx.get("/users/me/", headers={"Authorization": f"Bearer {token}"})
r.raise_for_status()
except httpx.HTTPStatusError as e:
if e.response.status_code in (400, 403) and "Inactive" in e.response.text:
prompt_reactivation() Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Using alice to exercise the disabled path; suspended accounts in production; forgetting to flip disabled after reactivating a fixture.
Related errors
- Inactive user
- Inactive user
- Incorrect username or password
- Incorrect username or password
- Not enough permissions
AI-assisted analysis of tiangolo/fastapi@3e8d1526d8 (2026-08-11).
Data as JSON: /api/errors/d3af55f814ec16a8.
Report an issue: GitHub.
Appendix: source
Thrown at docs_src/security/tutorial005_an_py310.py:147
raise credentials_exception
user = get_user(fake_users_db, username=token_data.username)
if user is None:
raise credentials_exception
for scope in security_scopes.scopes:
if scope not in token_data.scopes:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not enough permissions",
headers={"WWW-Authenticate": authenticate_value},
)
return user
async def get_current_active_user(
current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
@app.post("/token")
async def login_for_access_token(
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
) -> Token:
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user:
raise HTTPException(status_code=400, detail="Incorrect username or password")
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.username, "scope": " ".join(form_data.scopes)},
expires_delta=access_token_expires,
)
return Token(access_token=access_token, token_type="bearer")
View on GitHub (pinned to 3e8d1526d8)