"AuthStateMissing: Session value state missing" on logins with set session cookies
I'm working to integrate Facebook and Google OAuth2 into a site that already has working login using contrib.auth. I'm now stuck with a situation where I will receive a AuthStateMissing for both Google-Oauth2 and Facebook. The only way to successfully log in with those services is to wipe out my site cookie prior to clicking login. If I do that I can login once. Then if I try to login again I'll receive another AuthStateMissing. I've checked past issues and the site is on a subdomain of a TLD, the callback urls are correct.
The traceback:
Traceback (most recent call last):
File "/home/env/tcm/lib/python3.4/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/newrelic-2.42.0.35/newrelic/hooks/framework_django.py", line 497, in wrapper
return wrapped(*args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/social/apps/django_app/utils.py", line 52, in wrapper
return func(request, backend, *args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/social/apps/django_app/views.py", line 28, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/social/actions.py", line 40, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/social/backends/base.py", line 40, in complete
return self.auth_complete(*args, **kwargs)
File "/home/env/tcm/lib/python3.4/site-packages/social/backends/oauth.py", line 361, in auth_complete
state = self.validate_state()
File "/home/env/tcm/lib/python3.4/site-packages/social/backends/oauth.py", line 87, in validate_state
raise AuthStateMissing(self, 'state')
social.exceptions.AuthStateMissing: Session value state missing.
My relevant settings:
INSTALLED_APPS = {
'social.apps.django_app.default',
}
...
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend'
)
...
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
The errors only trigger once I am sent back to my site. So it doesn't appear to be related to either Google or Facebook.
How to repeat:
- Try to login with some auth provider
- Remove session cookie (and you can to restart uwsgi, if you want)
- Try to login again
- Get 502 error
Same here with Pyramid framework.
This is also a problem if you are using a partial pipeline, for example during email validation. If the user tries to complete the sign up from a different device so the session is simply not available. Also if you have a partial pipeline and the user uses a second backend to login while the first is partially complete the partial for the first is removed. This mean if they come back later to to complete the validation through the email link it will no longer be valid.
As far as I can tell every version post 0.2.0 has this problem. I tried upgrading to the latest and noticed this issue.
Having the same issue here when logging through mobile
Any update on this? I have encountered this similar issue, and it raises AuthStateMissing: Session value state missing. both on Facebook and Twitter, it happens across different desktop/mobile devices (Windows/Mac/Android/iOS).
I am on version 0.2.11.
It happened to me that trying to access with: http://localhost/complete/oauth2test?state=vkTqtm0 works, but: http://localhost/complete/oauth2test/?state=vkTqtm0EsOj8E5oTDg5bGMabIgl172A0 did not.
Once the trailing '/' (oauth2test/) was removed, the error disappeared.
@ericson-cepeda, interesting and thanks for the tip :+1: I am using Django with it and have set to append the url with trailing slash to get other modules playing nice together, so it could well be the culprit like you suggested. Will give it a go on Monday and provide updates.
While debugging this adding traces to the server log, I noticed the following sequence of events WRT session state, during Google OAuth2 interactions. (Function names refer to oauth.py.) First, there is state:
get_or_create_state has state OiVSTFTN1O4CsnYNA72RZDP65QaTDAFm for google-oauth2_state
(The value is returned by self.strategy.session_get). Then there is a 302 /login/google-oauth2/, and after that, the session state value seems to have gone:
get_session_state returning None for google-oauth2_state
It is validate_state that calls get_session_state and then raises AuthStateMissing, since state is now gone, indeed. (Could it be that something is not flused to the session/db?)
FTR, earlier in the server log lines there are Python strings for SQL; they seem to lack quotes around the values to be compared (but I'm no expert in these ORM matters!),
... WHERE ("django_session"."session_key" = %s AND "django_session"."expire_date" > %s)
@ericson-cepeda, by removing the trailing slash on provider/ like you suggested, I can confirm the same error hasn't come back since :+1:
FTR, there are TRAILING_SLASH (urls.py), and APPEND_SLASH (Django, in case). Maybe a stackoverflow discussion on redirects applies.
@TeckFA I hit the same problem, same sympton as you observed. Digging deeper, I found that cookies are NOT port specific , so if you run both OAuth client and provider on the same host, when being redirected to the provider to authenticate, it will overwrite the sessionid cookie set by the client, making client unable to retrieve the correct session object after being redirected back by the provider.
@pellaeon OMG, thanks a lot!!! You saved my day!
I am seeing the same issue on my side as well:
AuthStateMissing: Session value state missing.
File "django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "social/apps/django_app/utils.py", line 51, in wrapper
return func(request, backend, *args, **kwargs)
File "social/apps/django_app/views.py", line 28, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "social/actions.py", line 44, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "social/backends/base.py", line 41, in complete
return self.auth_complete(*args, **kwargs)
File "social/utils.py", line 246, in wrapper
return func(*args, **kwargs)
File "social/backends/oauth.py", line 385, in auth_complete
state = self.validate_state()
File "social/backends/oauth.py", line 88, in validate_state
raise AuthStateMissing(self, 'state')
I have tried disabling the trailing slash with the TRAILING_SLASH parameter but the result is the same.
I am hitting this as well, but I don't know how to act on the information that @pellaeon provides: This is a single Django instance on a single machine, on the same port, so I have a "client" page on http://localhost:8000/pages that presents the user with a login link:
<a href="http://localhost:8000/auth/login/google-oauth2?next=%2Fauth%2Fcomplete%2Fgoogle-oauth2">click here</a>
I click this, and then run through the google auth process, where google redirects to:
http://localhost:8000 /auth/complete/google-oauth2/?state=...&code=...&authuser=0&hd=...&session_state=...&prompt=none HTTP/1.1
While the initial redirect clearly has a state value, that disappears somewhere:
Internal Server Error: /auth/complete/google-oauth2/
Traceback (most recent call last):
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 51, in wrapper
return func(request, backend, *args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/apps/django_app/views.py", line 28, in complete
redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/actions.py", line 44, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/backends/base.py", line 41, in complete
return self.auth_complete(*args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/utils.py", line 246, in wrapper
return func(*args, **kwargs)
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/backends/oauth.py", line 385, in auth_complete
state = self.validate_state()
File "/Users/Pomax/Documents/git/temp/venv/lib/python2.7/site-packages/social/backends/oauth.py", line 86, in validate_state
raise AuthMissingParameter(self, 'state')
AuthMissingParameter: Missing needed parameter state
[23/Nov/2016 21:37:44] "GET /auth/complete/google-oauth2/ HTTP/1.1" 500 98565
No amount of adding or removing trailing slashes seems to have any effect on this, and this happens every time, not intermittently
There was an error that made state getting validated on error responses from the provider that masked the real error as an state-missing problem, this got fixed by https://github.com/python-social-auth/social-core/commit/4d2903c1a26cc433731dcc103fd211a7a4716b96
I just discovered that adding "SESSION_COOKIE_SECURE = True" can start this error happening. Obviously that's pilot error - me telling the site not to accept session cookies unless over HTTPS but trying to use HTTP anyway. But I imagine it would be hard to diagnose if I hadn't just changed just this one setting (in fact I came looking for a more complete answer anyway).
Thanks @lisad, this was my issue!
@lisad is that a thing you might be able to file PR-fix for? Sounds like a good thing to generate a logged warning for (since user error isn't a true error, but a warning message to help user figure out they were the problem is super important)
@lisad Applied your solution still getting "AuthStateMissing: Session value state missing". How did you solve it in production server?
Struggled with the exact problem for days, turned out that reason for missing state was caused by the caching. We had Django's LocMemCache on use (django.core.cache.backends.locmem.LocMemCache), changed it to MemcachedCache (django.core.cache.backends.memcached.MemcachedCache) and the problem disappeared.