{"record":{"id":"80a392c0326bc71a","repo":"tiangolo/fastapi","slug":"not-enough-permissions-80a392","errorCode":null,"errorMessage":"Not enough permissions","messagePattern":"Not enough permissions","errorType":"http","errorClass":"HTTPException","httpStatus":401,"severity":"error","filePath":"docs_src/security/tutorial005_py310.py","lineNumber":134,"sourceCode":"        detail=\"Could not validate credentials\",\n        headers={\"WWW-Authenticate\": authenticate_value},\n    )\n    try:\n        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])\n        username: str = payload.get(\"sub\")\n        if username is None:\n            raise credentials_exception\n        scope: str = payload.get(\"scope\", \"\")\n        token_scopes = scope.split(\" \")\n        token_data = TokenData(scopes=token_scopes, username=username)\n    except (InvalidTokenError, ValidationError):\n        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: 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: OAuth2PasswordRequestForm = Depends(),","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/security/tutorial005_py310.py#L116-L152","documentation":"Legacy-DI variant of error 51. get_current_user iterates the route's required SecurityScopes against the token's space-delimited 'scope' claim and raises HTTP 401 'Not enough permissions' with WWW-Authenticate: Bearer scope=\"<required>\" when a required scope is missing. Semantics identical to the Annotated version; only the DI syntax differs.","triggerScenarios":"Call a route guarded by Security(get_current_user, scopes=['items']) (e.g. GET /users/me/items/) with a token whose scope claim lacks 'items' (e.g. scope=me only).","commonSituations":"Wrong scopes requested at /token; scope naming mismatch; token minted before scopes existed.","solutions":["Request the required scope at POST /token via the form field scope (e.g. scope='me items').","Re-login to obtain a token containing the needed scope.","Confirm scope names match those declared on OAuth2PasswordBearer."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import jwt\nREQUIRED = {\"items\"}\ndef has_required_scopes(token: str, required: set[str]) -> bool:\n    payload = jwt.decode(token, SECRET_KEY, algorithms=[\"HS256\"])\n    token_scopes = set(payload.get(\"scope\", \"\").split())\n    return required.issubset(token_scopes)","typeGuard":"from typing import TypeGuard\ndef token_has_scopes(token: str, need: set[str]) -> TypeGuard[str]:\n    import jwt\n    p = jwt.decode(token, SECRET_KEY, algorithms=[\"HS256\"])\n    return need.issubset(set(p.get(\"scope\", \"\").split()))","tryCatchPattern":"import httpx\ntry:\n    r = httpx.get(\"/users/me/items/\", headers={\"Authorization\": f\"Bearer {token}\"})\n    r.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 401:\n        need = parse_required_scopes(e.response.headers.get(\"www-authenticate\", \"\"))\n        token = login_with_scopes(need)","preventionTips":["Request all required scopes in the 'scope' form field at /token.","Match scope names to those declared on OAuth2PasswordBearer.","Decode the JWT locally to fail fast on missing scopes."],"tags":["fastapi","authentication","oauth2","scopes","authorization","legacy-di"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}