tiangolo/fastapi · error · HTTPException
Inactive user
Error message
Inactive user
What it means
Disabled-account gate in the JWT tutorial (tutorial004). After get_current_user validates the JWT signature, decodes the 'sub' claim, and loads the user, get_current_active_user checks current_user.disabled and raises HTTP 400. Reaching this line means the token was fully valid and the user exists — only the disabled flag fails. The tutorial ships only johndoe (disabled=False), so the as-shipped code never reaches this line unless a disabled user is added.
Solutions
- Set disabled=False for the user in fake_users_db and re-login to mint a fresh JWT.
- Return 403 Forbidden for disabled-but-authenticated users instead of HTTP 400.
- Use a distinct 'suspended' status rather than overloading 'disabled' for soft-deletion.
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
# JWT version: decode locally to inspect disabled state 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
- Short JWT TTLs so disabled state is enforced quickly after suspension.
- Maintain a revocation list (jti) for disabled accounts.
- Prefer 403 Forbidden over 400 for disabled-but-authenticated users.
When it happens
Trigger: Add a user with disabled=True to fake_users_db, obtain a valid JWT for them via POST /token, then call GET /users/me/ — the token decodes and loads the user, then line 116 raises.
Common situations: Suspended/banned accounts in production; a second fixture user left disabled; soft-deleted accounts modelled via the disabled flag.
Related errors
AI-assisted analysis of tiangolo/fastapi@3e8d1526d8 (2026-08-11).
Data as JSON: /api/errors/3baadc72a93d71de.
Report an issue: GitHub.
Appendix: source
Thrown at docs_src/security/tutorial004_an_py310.py:117
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: Annotated[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: Annotated[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)