tiangolo/fastapi · error · HTTPException
Inactive user
Error message
Inactive user
What it means
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.
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.
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
- Avoid issuing tokens for the disabled 'alice' fixture except when testing that path.
- Revoke outstanding tokens on disable.
- Prefer 403 Forbidden over 400 here.
When it happens
Trigger: Authenticate as alice with the correct scope, then call GET /users/me/ — passes JWT, user lookup, and scope checks, then line 145 raises.
Common situations: Testing the disabled path with alice; suspended production accounts.
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/6d7b7787a50a5021.
Report an issue: GitHub.
Appendix: source
Thrown at docs_src/security/tutorial005_py310.py:146
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: 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: 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)