Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple API auth method #298

Merged
merged 2 commits into from
Jun 23, 2022
Merged

Decouple API auth method #298

merged 2 commits into from
Jun 23, 2022

Conversation

florimondmanca
Copy link
Collaborator

@florimondmanca florimondmanca commented Jun 21, 2022

Actuellement, la vérification des permissions et la méthode d'authentification sont couplés. En effet, current_user() authentifie l'utilisateur à partir du Bearer token dans la requête, mais on l'utilise aussi pour les permissions : IsAuthenticated, HasRole.

Il faut découpler ces deux aspects. Je m'en suis rendu compte en regardant à #294 : si on implémentait une authentification "DataPass" (via auth.api.gouv), on emploierait une autre méthode (passage par le serveur OpenID Connect) mais il faut que la logique des permissions ne change pas.

Cette PR refactor le code pour utiliser le mécanisme de AuthenticationBackend de Starlette. Ainsi, la logique de current_user() migre vers un TokenAuthBackend, et celui-ci est branché à un AuthMiddleware. On a alors accès à request.user dans les routes avec une interface unique indépendante du backend, ce qui n'était pas le cas avant, et c'est là-dessus que se basent les utilitaires de permissions.

@florimondmanca florimondmanca added refactor Modifications du code sans incidence utilisateur ou fonctionnelle back labels Jun 21, 2022
@florimondmanca florimondmanca force-pushed the fm/refactor-auth-backend branch 4 times, most recently from 4694d64 to 9444431 Compare June 21, 2022 17:33
@florimondmanca florimondmanca force-pushed the fm/refactor-auth-backend branch 7 times, most recently from 4b19b8b to df7d964 Compare June 23, 2022 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
back refactor Modifications du code sans incidence utilisateur ou fonctionnelle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant