Integrate Casbin and FastAPI to support access control #7831
Replies: 5 comments
-
|
I think it should be implemented in independent library, not in fastapi core. |
Beta Was this translation helpful? Give feedback.
-
|
This could be easily achived with a custom middleware. Dummy Code Example: In your main file: |
Beta Was this translation helpful? Give feedback.
-
|
@NJannasch Thanks for your reply. Although I have implemented some function I imaged some days ago. I check the permissions with def get_current_active_authorized_user(request: Request, current_active_user: User = Security(get_current_active_user)) -> Optional[User]:
username = current_active_user.Username
path = request.state.path
sub = username
obj = path
act = request.method.lower()
print(sub, obj, act)
if enforcer.enforce(sub, obj, act):
return current_active_user
else:
raise HTTPException(status_code = HTTP_403_FORBIDDEN, detail="No permission")I just make all my code public here: https://github.com/shizidushu/simple-report-data-table-vuetify |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here everyone! π π Thanks @shizidushu for reporting back and closing the issue π |
Beta Was this translation helpful? Give feedback.
-
|
Guys, I made a small library for the integration of fastapi and casbin, which makes it possible to add a rights check to the router levels. Could you evaluate it? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is your feature request related to a problem? Please describe.
It is a useful addition to have access control over FastAPI. Some framework have builtin support and some can get this feature with a plugin.
Describe the solution you'd like
According to its docs, Casbin provides support for enforcing authorization based on various access control models.
Here is the function I image:
Maybe a function to execute on application startup; it gets all path operation's path, method and some other optional information like tags, user_id and user_role, then write it to Casbin policy file.
It cleans redundant row in Casbin policy file and keep it update with FastAPI routes.
Beta Was this translation helpful? Give feedback.
All reactions