Bird
0
0

How can you combine role-based access control with OAuth2 scopes in FastAPI to restrict access to an endpoint requiring both 'admin' role and 'write' scope?

hard🚀 Application Q9 of 15
FastAPI - Authentication and Security
How can you combine role-based access control with OAuth2 scopes in FastAPI to restrict access to an endpoint requiring both 'admin' role and 'write' scope?
AUse a dependency that checks user roles and OAuth2 scopes together, raising HTTPException if either check fails
BUse separate endpoints for role and scope checks without combining them
CCheck only OAuth2 scopes since roles are redundant
DUse middleware to check roles and ignore scopes
Step-by-Step Solution
Solution:
  1. Step 1: Understand combined access control

    Both role and scope must be checked together to enforce strict access.
  2. Step 2: Identify correct approach

    Use a dependency that checks user roles and OAuth2 scopes together, raising HTTPException if either check fails combines checks in one dependency and raises exception if either fails, which is correct.
  3. Final Answer:

    Combine role and scope checks in one dependency raising exceptions if unauthorized -> Option A
  4. Quick Check:

    Combine role and scope checks in one dependency [OK]
Quick Trick: Combine role and scope checks in one dependency [OK]
Common Mistakes:
MISTAKES
  • Splitting checks into separate endpoints
  • Ignoring role or scope checks
  • Using middleware incorrectly for RBAC

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes