{"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/42a41db11f6882807ac3c057b942178d53b97438/docs_src/security/tutorial005_py310.py#L116-L152","documentation":"Default-parameter twin of error 54: `get_current_user` raises HTTP 401 'Not enough permissions' (header `Bearer scope=\"...\"`) when the token's scopes do not cover the route's required scopes (loop at line 132). The JWT and user are valid; only the authorization scope is missing.","triggerScenarios":"`GET /users/me/items/` (requires `items`) with a token whose `scope` claim lacks `items` (e.g. only `me`).","commonSituations":"Wrong scopes requested at /token; client assuming a scope it never obtained; required-scopes changed server-side; case/whitespace mismatch in scope strings.","solutions":["Re-request a token with the required scope(s): `scope=items` or `scope=me items`.","Align the client's requested scopes with every route's `Security(..., scopes=...)`.","Recheck scope strings for exact case/whitespace."],"exampleFix":"# before\n# token scope=me; calling an 'items' route => 401\n# after\n# re-login with scope='me items', then retry","handlingStrategy":"validation","validationCode":"def has_scopes(token_scopes, required) -> bool:\n    return all(s in token_scopes for s in required)\n# ensure 'items' (or whichever the route needs) is in token_scopes before calling","typeGuard":"def has_required_scope(token_scopes: list[str], required: list[str]) -> bool:\n    return set(required).issubset(set(token_scopes))","tryCatchPattern":"try:\n    r = client.get('/users/me/items/', headers=auth_header(token))\n    r.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 401 and e.response.json().get('detail') == 'Not enough permissions':\n        token = request_token(scopes=['me','items'])\n    raise","preventionTips":["Request the union of scopes all target endpoints need.","Match scope strings exactly with server config.","On insufficient scopes, re-login for broader scopes once."],"tags":["authorization","oauth2","scopes","security","fastapi"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}