Yes, it is possible to define a string that represents the permissions of a user on an endpoint function in FastAPI and then check it. You can achieve this by using dependency injection to pass the user's permissions to your endpoint functions and then validating those permissions within the function.
Here's a step-by-step guide on how to do this:
- Define a Permission Model: Create a model to represent the permissions.
- Create a Dependency to Get User Permissions: Use a dependency to fetch the user's permissions.
- Check Permissions in Endpoint: Use the dependency in your endpoint to check the user's permissions.
Step 1: Define a Permission Model
First, define a model to represent the permissions. This can be a simple string or a more complex structure depending on your needs.
from pydantic import BaseModel
class Permission(BaseModel):
permissions: str
Step 2: Create a Dependency to Get User Permissions
Create a dependency that fetches the user's permissions. This could be from a database, a token, or any other source.
from fastapi import Depends, HTTPException, status
async def get_user_permissions(token: str = Depends(oauth2_scheme)):
# Simulate fetching user permissions from a database or token
user_permissions = "read:data write:data" # Example permissions
return Permission(permissions=user_permissions)
Step 3: Check Permissions in Endpoint
Use the dependency in your endpoint to check the user's permissions.
from fastapi import FastAPI, Depends, HTTPException, status
app = FastAPI()
@app.get("/data")
async def read_data(permissions: Permission = Depends(get_user_permissions)):
required_permission = "read:data"
if required_permission not in permissions.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this data."
)
return {"data": "This is some data"}
@app.post("/data")
async def write_data(permissions: Permission = Depends(get_user_permissions)):
required_permission = "write:data"
if required_permission not in permissions.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to write data."
)
return {"message": "Data has been written"}
Full Example
Here is the complete example put together:
from fastapi import FastAPI, Depends, HTTPException, status
from pydantic import BaseModel
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
class Permission(BaseModel):
permissions: str
async def get_user_permissions(token: str = Depends(oauth2_scheme)):
# Simulate fetching user permissions from a database or token
user_permissions = "read:data write:data" # Example permissions
return Permission(permissions=user_permissions)
app = FastAPI()
@app.get("/data")
async def read_data(permissions: Permission = Depends(get_user_permissions)):
required_permission = "read:data"
if required_permission not in permissions.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to access this data."
)
return {"data": "This is some data"}
@app.post("/data")
async def write_data(permissions: Permission = Depends(get_user_permissions)):
required_permission = "write:data"
if required_permission not in permissions.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="You do not have permission to write data."
)
return {"message": "Data has been written"}
In this example, the get_user_permissions dependency fetches the user's permissions and passes them to the endpoint functions. The endpoint functions then check if the required permissions are present in the user's permissions string. If not, they raise a 403 Forbidden error.