When it comes to safeguarding API resources, implementing Role-Based Access Control (RBAC) in FastAPI is a game-changer. Essentially, it ensures users can only interact with data they’re authorized to, depending on their assigned roles. Sounds cool, right? Let’s dive into how to pull this off effectively using FastAPI.
Think of Role-Based Access Control (RBAC) as a bouncer at an exclusive club, letting you in only if your name’s on the list. Each role—like a VIP pass—grants access to certain areas of the club (or in our case, API resources). It’s an efficient way to handle permissions since you don’t have to keep track of individual permissions for every single user.
First, let’s talk about setting up FastAPI. You need a solid foundation before building the mansion, after all. Here’s how you lay down those bricks:
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel
from passlib.context import CryptContext
from datetime import datetime, timedelta
import jwt
app = FastAPI()
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Password context for hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# User model
class User(BaseModel):
id: int
username: str
password: str
role: str
# Fake user database for demo purposes
fake_users = [
{'id': 1, 'username': 'admin', 'password': '$2b$12$N.i74Kle18n5Toxhas.rVOjZreVC2WM34fCidNDyhSNgxVlbKwX7i', 'role': 'admin'},
{'id': 2, 'username': 'client', 'password': '$2b$12$KUgpw1m0LF/s9NS1ZB5rRO2cA5D13MqRm56ab7ik2ixftXW/aqEyq', 'role': 'client'}
]
# Authenticate users
def authenticate_user(username: str, password: str) -> User:
for user in fake_users:
if user['username'] == username:
if pwd_context.verify(password, user['password']):
return User(**user)
raise HTTPException(status_code=401, detail="Invalid credentials")
# Get current user from token
def get_current_user(token: str = Depends(oauth2_scheme)) -> User:
try:
payload = jwt.decode(token, 'secret', algorithms=['HS256'])
username = payload['sub']
for user in fake_users:
if user['username'] == username:
return User(**user)
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token has expired")
except jwt.InvalidTokenError:
raise HTTPException(status_code=401, detail="Invalid token")
raise HTTPException(status_code=401, detail="User not found")
# Generate a JWT token
def generate_token(user: User) -> str:
payload = {
'sub': user.username,
'exp': datetime.utcnow() + timedelta(minutes=30)
}
return jwt.encode(payload, 'secret', algorithm='HS256')
# Login endpoint to get token
@app.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(form_data.username, form_data.password)
if not user:
raise HTTPException(status_code=401, detail="Invalid credentials")
token = generate_token(user)
return {"access_token": token, "token_type": "bearer"}
Once up and running, it’s time to implement RBAC. To do so, define roles and the permissions tied to them. Here’s an example:
# Define roles and their permissions
roles = {
'admin': ['items:read', 'items:write', 'users:read', 'users:write'],
'client': ['items:read']
}
You also need a role checker. Think of it as a filter that validates whether a user has the right role to access a certain part of the API or not. Here’s a snippet to show how that’s done:
from fastapi import Depends
from typing import Annotated
class RoleChecker:
def __init__(self, allowed_roles: list):
self.allowed_roles = allowed_roles
def __call__(self, user: User = Depends(get_current_user)):
if user.role not in self.allowed_roles:
raise HTTPException(status_code=403, detail="Operation not permitted")
return user
# Role checkers for specific roles
admin_role_checker = RoleChecker(allowed_roles=['admin'])
client_role_checker = RoleChecker(allowed_roles=['client'])
# Securing endpoints with role checkers
@app.get("/admin/data")
def get_admin_data(_: Annotated[bool, Depends(admin_role_checker)]):
return {"data": "This is admin data"}
@app.get("/client/data")
def get_client_data(_: Annotated[bool, Depends(client_role_checker)]):
return {"data": "This is client data"}
Now, if you prefer using an external service like Auth0, you can still integrate it seamlessly with FastAPI. Auth0 does half the heavy lifting for you, making the entire process smooth and secure. Here’s the gist of how to make that integration happen:
- Set up Auth0 by registering your API, enabling RBAC, and creating roles and permissions.
- Validate access tokens in your FastAPI application to make sure the tokens include necessary permissions.
- Enforce RBAC using role checkers or similar mechanisms to confirm users have the permissions they need to access specific endpoints.
Here’s how you might integrate Auth0 with FastAPI:
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi import Depends, HTTPException
import jwt
# Auth0 settings
auth0_domain = 'your-auth0-domain.com'
auth0_api_audience = 'your-api-audience'
auth0_algorithms = ['RS256']
# Define the authentication scheme
auth_scheme = HTTPBearer()
# Get current user from token
def get_current_user(token: HTTPAuthorizationCredentials = Depends(auth_scheme)) -> User:
try:
payload = jwt.decode(token.credentials, key=None, algorithms=auth0_algorithms, audience=auth0_api_audience)
username = payload['sub']
# Fetch user details from your database or Auth0
user = User(username=username, role=payload.get('https://your-namespace/role'))
return user
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token has expired")
except jwt.InvalidTokenError:
raise HTTPException(status_code=401, detail="Invalid token")
raise HTTPException(status_code=401, detail="User not found")
# Example protected route
@app.get("/protected")
def protected_route(user: User = Depends(get_current_user)):
if user.role != 'admin':
raise HTTPException(status_code=403, detail="Operation not permitted")
return {"message": "Hello, Admin!"}
RBAC in FastAPI isn’t just about segmenting access, it’s about creating a streamlined, maintainable, and scalable security setup. Whether it’s through a simple in-memory method or an integration with Auth0, locking down your API to only the right users is crucial. This not only amps up security but keeps the access rights management straightforward, letting you focus on scaling and enhancing your application without the headache of user permissions bogging you down.
So let’s sum it up. RBAC in FastAPI involves drawing clear boundaries around your API resources. Define roles, assign permissions, and let FastAPI’s dependency injection work its magic to enforce those permissions. Whether you’re rolling with a basic setup or going all in with Auth0, RBAC is essential for a robust security strategy, keeping your APIs safe and sound.