Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is Role-based Access Control (RBAC)?
RBAC is a way to control who can do what in an app by assigning roles to users. Each role has permissions that allow certain actions.
Click to reveal answer
beginner
How do you define roles in FastAPI for RBAC?
You define roles as simple strings or enums and check them in your route dependencies to allow or deny access.
Click to reveal answer
intermediate
What FastAPI feature helps enforce RBAC in routes?
Dependencies let you run code before a route runs. You can use them to check user roles and block access if needed.
Click to reveal answer
intermediate
Why use OAuth2 or JWT with RBAC in FastAPI?
OAuth2 or JWT help identify users securely. RBAC uses this identity to check roles and permissions safely.
Click to reveal answer
beginner
What happens if a user tries to access a route without the required role in FastAPI RBAC?
FastAPI returns a 403 Forbidden error, meaning the user is not allowed to access that resource.
Click to reveal answer
In FastAPI RBAC, where do you usually check user roles?
AIn database migrations
BIn route dependencies
CIn CSS files
DIn HTML templates
✗ Incorrect
Route dependencies run before the route and are the right place to check user roles for access control.
What HTTP status code does FastAPI return when access is denied due to RBAC?
A403 Forbidden
B401 Unauthorized
C200 OK
D404 Not Found
✗ Incorrect
403 Forbidden means the user is authenticated but does not have permission to access the resource.
Which of these is a common way to represent roles in FastAPI?
AAs HTML tags
BAs SQL queries
CAs CSS classes
DAs strings or enums
✗ Incorrect
Roles are usually simple strings or enums that describe user permissions.
Why combine JWT with RBAC in FastAPI?
ATo style the app
BTo speed up database queries
CTo securely identify users and their roles
DTo create HTML forms
✗ Incorrect
JWT tokens carry user identity and role info securely, helping RBAC enforce permissions.
What is the main benefit of using RBAC in an app?
AIt controls user access based on roles
BIt makes the app load faster
CIt changes the app's colors
DIt creates user accounts automatically
✗ Incorrect
RBAC helps keep the app safe by allowing only users with the right roles to do certain actions.
Explain how you would implement role-based access control in a FastAPI app.
Think about how FastAPI runs code before routes and how you can check user info there.
You got /4 concepts.
Describe why RBAC is important for app security and how FastAPI supports it.
Consider how roles protect resources and how FastAPI checks them.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of role-based access control (RBAC) in FastAPI?
easy
A. To speed up API response times
B. To limit user actions based on their assigned roles
C. To automatically generate API documentation
D. To handle database migrations
Solution
Step 1: Understand RBAC concept
RBAC restricts what users can do depending on their roles, like admin or user.
Step 2: Identify RBAC purpose in FastAPI
FastAPI uses RBAC to check user roles before allowing access to certain endpoints.
Final Answer:
To limit user actions based on their assigned roles -> Option B
Quick Check:
RBAC = limit actions by roles [OK]
Hint: RBAC controls user permissions by roles, not speed or docs [OK]
Common Mistakes:
Confusing RBAC with performance optimization
Thinking RBAC auto-generates docs
Assuming RBAC manages database tasks
2. Which of the following is the correct way to declare a dependency that checks for an admin role in FastAPI?
easy
A. def admin_required(user: User = Depends(get_current_user)):
if user.role == 'guest':
raise HTTPException(status_code=401)
B. def admin_required(user: User):
if user.role == 'admin':
return True
C. def admin_required():
return 'admin' in user.roles
D. def admin_required(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403)
Solution
Step 1: Check dependency signature
def admin_required(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403) uses Depends to get current user, which is required for role checking.
Step 2: Verify role check logic
def admin_required(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403) raises HTTP 403 if user is not admin, correctly enforcing access control.
Final Answer:
def admin_required(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403) -> Option D
Quick Check:
Depends + role check + HTTPException = def admin_required(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403) [OK]
Hint: Use Depends to get user, then check role and raise HTTPException [OK]
Common Mistakes:
Not using Depends to get current user
Checking wrong role or missing exception
Returning True instead of raising exception
3. Given this FastAPI endpoint with role check dependency:
What happens if a user with role 'user' calls this endpoint?
medium
A. The endpoint raises HTTP 403 Forbidden error
B. The endpoint returns {"data": "secret"}
C. The endpoint raises HTTP 401 Unauthorized error
D. The endpoint returns an empty response
Solution
Step 1: Understand admin_required behavior
admin_required raises HTTP 403 if user role is not 'admin'.
Step 2: Apply to user role 'user'
User role 'user' is not 'admin', so HTTP 403 is raised before endpoint runs.
Final Answer:
The endpoint raises HTTP 403 Forbidden error -> Option A
Quick Check:
Non-admin user triggers 403 error [OK]
Hint: Non-admin roles cause 403 error before endpoint runs [OK]
Common Mistakes:
Confusing 401 Unauthorized with 403 Forbidden
Expecting endpoint to return data for non-admin
Thinking empty response is returned
4. Identify the error in this FastAPI role check dependency:
def check_admin(user: User = Depends(get_current_user)):
if user.role == 'admin':
return True
else:
return False
@app.get('/admin')
async def admin_panel(is_admin: bool = Depends(check_admin)):
if not is_admin:
raise HTTPException(status_code=403)
return {"msg": "Welcome admin"}
medium
A. Dependency should raise HTTPException directly, not return bool
B. Depends should not be used inside dependency functions
C. The endpoint should not check is_admin, dependency handles it
D. The function should return user object, not bool
Solution
Step 1: Analyze dependency behavior
check_admin returns True/False instead of raising HTTPException on failure.
Step 2: Understand best practice for RBAC in FastAPI
Dependencies should raise HTTPException to stop execution early, not return bool flags.
Final Answer:
Dependency should raise HTTPException directly, not return bool -> Option A
Quick Check:
Raise exception in dependency, don't return bool [OK]
Hint: Raise HTTPException in dependency to block access immediately [OK]
Common Mistakes:
Returning bool instead of raising exception
Not stopping request early in dependency
Misusing Depends inside dependencies
5. You want to create a reusable role checker in FastAPI that allows multiple roles (e.g., 'admin' or 'moderator') to access an endpoint. Which approach correctly implements this?
hard
A. def role_checker(user: User = Depends(get_current_user)):
if user.role == 'admin' and user.role == 'moderator':
return True
raise HTTPException(status_code=403)
B. def role_checker(user: User = Depends(get_current_user)):
if user.role != 'admin' or user.role != 'moderator':
raise HTTPException(status_code=403)
C. def role_checker(allowed_roles: list[str]):
def checker(user: User = Depends(get_current_user)):
if user.role not in allowed_roles:
raise HTTPException(status_code=403)
return checker
D. def role_checker(allowed_roles: list[str]):
for role in allowed_roles:
if role == user.role:
return True
return False
Solution
Step 1: Understand reusable dependency pattern
def role_checker(allowed_roles: list[str]):
def checker(user: User = Depends(get_current_user)):
if user.role not in allowed_roles:
raise HTTPException(status_code=403)
return checker returns a function that checks if user role is in allowed_roles, raising HTTPException if not.
Step 2: Verify logic for multiple roles
def role_checker(allowed_roles: list[str]):
def checker(user: User = Depends(get_current_user)):
if user.role not in allowed_roles:
raise HTTPException(status_code=403)
return checker correctly uses 'not in' to allow any role in the list, making it reusable.
Final Answer:
def role_checker(allowed_roles: list[str]):
def checker(user: User = Depends(get_current_user)):
if user.role not in allowed_roles:
raise HTTPException(status_code=403)
return checker -> Option C
Quick Check:
Reusable role check with allowed_roles list = def role_checker(allowed_roles: list[str]):
def checker(user: User = Depends(get_current_user)):
if user.role not in allowed_roles:
raise HTTPException(status_code=403)
return checker [OK]
Hint: Return inner function checking role in allowed_roles, raise HTTPException [OK]
Common Mistakes:
Using incorrect logic with 'or' instead of 'in'
Returning bool instead of raising exception
Checking impossible conditions like role == 'admin' and 'moderator'