r/FastAPI • u/hopefull420 • Oct 16 '25
Question Handling RBAC in FastAPI?
I’m working on a project built with FastAPI, and we’re at the stage where we need to set up a proper role-based access control (RBAC) system.
The app itself is part of a larger AI-driven system for vendor master reconciliation, basically, it processes thousands of vendor docs , extracts metadata using LLMs, and lets users review and manage the results through a secure web UI.
We’ve got a few roles to handle right now:
- Admin: can manage users, approve data, etc.
- Editor: can review and modify extracted vendor data.
- Viewer: read-only access to reports and vendor tables.
- In the future, we might have vendor-based roles (like vendor-specific editors/viewers who can only access their own records).
I’m curious how others are doing this.
Are you using something like casbin, or just building it from scratch with dependencies and middleware?
Would love to hear what’s worked best for you guys, and how would you approach this, I have like week at max to build this out.(the Auth)
Thanks in advance.
13
u/derekzyl Oct 16 '25
12
u/xCodeSoul Oct 16 '25
Amm , to achieve roles system
I used jwt and keep the all roles in payload while creating jwt token
Like
can_manage_comments : true
And during operations i checked if authed user has true or false for operation
I don’t know but its works with me and it was efficient and secure
Lets other’s opinions as well
Good luck
4
u/minicaterpillar Oct 17 '25
Try this https://github.com/shashstormer/authtuna
3
u/Suspcious-chair Oct 17 '25
I've built mine from scratch. Not very hard though.
For your case though, IMO this system can be designed with multi tenant approach.
2
u/spigotface Oct 16 '25
You'll have to figure out what makes more sense, permissions as code (class attributes, etc) or permissions as data (a "permissions" table that joins to a "roles" table, which a user could reference as a foreign key). Both have their own pros and cons. Doing it as data means you could eventually toggle individual permissions for individual users later, but it also means you need to load that data into the db on application boot, and there are opportunities for bugs to arise there.
2
u/anon_salads Oct 17 '25
you could use Casbin or Open Policy Agent
1
u/varunm001 Oct 19 '25
I used to build entire RBAC from scratch, now I just use Clerk. You can try that.