tiangolo/fastapi · error · HTTPException

Inactive user

Error message

Inactive user

What it means

Legacy-DI variant of error 47 in tutorial004. After a valid JWT resolves to a real user, get_current_active_user raises HTTP 400 'Inactive user' when current_user.disabled is truthy. Only the DI syntax differs from the Annotated version; the semantics are identical.

Solutions

  1. Use a user with disabled=False (johndoe in the shipped fixture).
  2. Set disabled=False and re-login.
  3. Prefer 403 Forbidden over 400 for the disabled-but-authenticated case.
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

When it happens

Trigger: Mint a valid JWT for a disabled user, then call GET /users/me/ — JWT decodes, user loads, then line 113 raises.

Common situations: Suspended accounts in production; a disabled fixture user used by mistake.

Related errors


AI-assisted analysis of tiangolo/fastapi@3e8d1526d8 (2026-08-11). Data as JSON: /api/errors/8c93c3ab373f4bff. Report an issue: GitHub.

Appendix: source

Thrown at docs_src/security/tutorial004_py310.py:114

        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username = payload.get("sub")
        if username is None:
            raise credentials_exception
        token_data = TokenData(username=username)
    except InvalidTokenError:
        raise credentials_exception
    user = get_user(fake_users_db, username=token_data.username)
    if user is None:
        raise credentials_exception
    return user


async def get_current_active_user(current_user: User = Depends(get_current_user)):
    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: OAuth2PasswordRequestForm = Depends(),
) -> Token:
    user = authenticate_user(fake_users_db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={"sub": user.username}, expires_delta=access_token_expires
    )

View on GitHub (pinned to 3e8d1526d8)