r/FastAPI • u/CemDoruk • Feb 17 '24
Question How to get rid of authentication boilerplate
I am a beginner so there is probably something important that I am missing. I have this boilerplate a lot in my code:
@router.post("/{branch_name}", response_model= schemas.BranchResponseSchema, status_code=status.HTTP_201_CREATED)
def create_branch(user_name: str, repository_name : str, branch_name: str,
db: Session = Depends(database.get_db),
current_user = Depends(oauth2.get_current_user)):
if current_user.name != user_name:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
detail="User does not have permission to create a branch for another user")
And I was wondering what the correct way to handle the cases where a user tries to change something he does not own.
4
u/Trinkes Feb 17 '24
You can create a function with the authentication code and use it as dependency of the endpoint.
1
u/postmath_ Feb 17 '24
I don't really understand the use case here, why would you have a user_name parameter for your endpoint if it has to be the same as the current user?
Use Annotated to shorten the dependency call, or a middleware to add the user information to the request if you really want to get rid of it, but its gonna introduce a different dependency you have to use.
2
u/wjziv Feb 17 '24 edited Feb 17 '24
It may depend on your usecase.
As /u/postmath_ mentioned, it seems strange that your API permits the developer-user to define the
user_namein the POST body when it's dictated by the session. Consider removing this and trust the token.I think any knowledgable developer might suggest this route without knowing the rest of your application's usecase.
If you absolutely must hold a
user_namein the body, you can use a dependency in the router. This way, anything with this dependency will perform this check. You can even place the dependency in therouter, if you wanted. I do not like this pattern, as it places payload params into resources which don't appear to necessarily expect/need them. If I were a new developer on this project, I'd be confused by this structure.If you do choose to follow this route, but you need access to this
user_name, you can still include it in the payload, and trust that it will still run through thatDependscheck. It certainly feels redundant if you're still requiring the token in your endpoint...