djoser: Problem using social auth in stateless webapp
I have the same problem as here, meaning, the last step of Google OAuth2 authentication is not working. After some searching, I saw that the problem comes from the validation of state : the value in the request is checked against the value from the previous request that was saved in session. My problem, and I suppose it is the same one as @Emnalyeriar’s, is that my app is stateless, I don’t use session nor cookies so getting previous value of state is impossible, nor is it restful. djoser main target are stateless apps, not being able to use the OAuth2 protocol (which is the standard for most providers) make social auth unusable. Any use of session should therefore be removed. What do you think ?
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 10
- Comments: 19 (2 by maintainers)
If anyone’s interested I created my own social login views with requests_oauthlib:
I also have one for FB but its very similar
Damn, that’s a shame. I was hoping to switch from django-rest-auth to djoser for a complete restful local account and social solution. I’m currently trying to get django-rest-social-auth working for the social side of things but it sucks having to use multiple different libraries.
Any update on this?
I think the code above by @Emnalyeriar is not really secure: It basically does not check if the
statein the POST matches thestategenerated in the GET:statein GET is stored as variable, but never used again, the OAuth session is discarded after the request - which is not the case in the requests_oauthlib examplesstatepassed in the request - ignoring thestatevalue that was generated in the GETAs I understand this allows CSRF attacks, the implementation should check if both states are the same. See for example the Github OAuth docs on state:
I’m not sure what a correct solution here is. Obviously sessions work which is what the Django social auth lib uses. Maybe it is sufficient to set the state as a cookie in the GET response which can be used to initialize state in the OAuth session in the POST - but I’m far from an expert in this matter.
Edit: After looking some more into the matter, the cookie approach will only work if you use same-site cookies. That in turn will only work if your frontend is hosted on the same domain as your REST API.
If your frontend is hosted on another domain, it should be a valid approach to:
statevalueI would add session auth as own auth method. And, try to separate further, ie not use sessions at all in other auth methods, if possible? a test suite that disables sessions for some cases would ensure this stays that way.
In fact, django-rest-social-auth uses social-django as well, they just basically negate all of the session stuff in a custom strategy