r/FastAPI • u/[deleted] • May 23 '24
Question Fine grained access control?
I am designing a huge-ass API for a client. And one of the things we are scratching our heads over is how to give people different access to different nodes.
Eg. (Examples, not actual)
/api/v1/employess # only internal people/api/v1/projects # customers GET, internal POST/api/v1/projects/{projectid}/timeline #customers GET/api/v1/projects/{projectid}/updates # customers GET/POSTetc...
We have also the usual login/jwt authentication stuff
I was thinking of grouping users and writing a custom decorator that matches the path to the access.
Am I on the right track or are you all going "WTF am I reading?"
Or is this something OAuth scopes should handle? (I have never used that)
Edit: It seems that OAuth scopes is designed exactly for this kind of situation. I guess I have some learning to do.
Edit2: Thanks, I definitely have something to go on now.
2
u/bsenftner May 23 '24
This can be very easy to implement. Associate a list/array of strings with every user and every resource you want to control access. Any resources you want access control, define a unique string that stands for each permission the resource offers, things like "ReadProjects", and "CreateProject"; and when creating a project that project creation includes creating the permission strings "Read[ProjectName]", "Modify[ProjectName]", "Delete[ProjectName]" and then the user accounts that have this access receive these strings on their user's listing of permissions.
I've purposely described this in the most obvious manner, but most seem to try to optimize this logic somewhat; I suggest keeping it butt simple. Simplicity enables complexity where it matters. K.I.S.S.