python-social-auth icon indicating copy to clipboard operation
python-social-auth copied to clipboard

Occassionally getting tthis error: AuthMissingParameter: Missing needed parameter state

Open pradeepjmc opened this issue 11 years ago • 7 comments

Traceback (most recent call last):

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response response = callback(request, _callback_args, *_callback_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view return view_func(_args, *_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 52, in wrapper return func(request, backend, _args, *_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/apps/django_app/views.py", line 20, in complete redirect_name=REDIRECT_FIELD_NAME, _args, *_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/actions.py", line 43, in do_complete user = backend.complete(user=user, _args, *_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/backends/base.py", line 40, in complete return self.auth_complete(_args, *_kwargs)

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/backends/oauth.py", line 357, in auth_complete state = self.validate_state()

File "/opt/../virtualenvs/pyp/lib/python2.7/site-packages/social/backends/oauth.py", line 79, in validate_state raise AuthMissingParameter(self, 'state')

AuthMissingParameter: Missing needed parameter state.

Please help..

pradeepjmc avatar Jan 20 '15 07:01 pradeepjmc

I'm also getting it from time to time since many months ago on both Facebook and Twitter. It's not related to mobile or slow connections. It seldom happened to me, but when it does, I go back, click the login button again and then it works. In production it happens like 5 times a day every day, always to different people.

I'll add the results of my research:

  • This is not about the URL. Don't look for a missing parameter "state" in the URL. There is a &state=… parameter in OAuth2, and also a &redirect_state=…, but they work all right when this bug happens. What PSA is complaining about is a state parameter inside session, which is called facebook_state or twitter_state
  • It happened with OAuth2 (Facebook) and very rarely with OAuth1 (Twitter). Always in the /complete/ step
  • It always happens when logging in (that means the user cannot log in and stays anonimous) except a very rare occasion when it happened on logout
  • You can reproduce it easily by going to a URL which has an expired token, although I'm not sure the conditions are the same as the bug in the wild. E.g. http://www.yourdomain.com/account/complete/twitter/?redirect_state=8edOO0H4AcNXohUB7IOOYRaiEhv6iK4b&oauth_token=E1DsnqYwM6ie0U5Zd2RqHsOixWqOZgQh&oauth_verifier=0VeL60uek54y1pWgAmQegysG1lwW0IFN
  • If I look at the session data at the time of the error (in a middleware), I confirm that the session doesn't have a twitter_state parameter. It has: [('currency', 'IDR'), ('django_language', 'id')], while in a normal (non-failing) login, there is a twitter_state parameter: [(u'twitterunauthorized_token_name', [u'oauth_token=rh19NS7EkL3Wovwn9Mi8KbM24ZLDfOt7&oauth_token_secret=3NON79A7UYYD5Ec73SB0p1c8f3M7Igfk&oauth_callback_confirmed=true']), (u'next', u'/'), (u'currency', u'IDR'), (u'twitter_state', u'75N2XDSQnyIOaKEBV6RsofkmcLTKyDDG'), (u'_language', u'id'), (u'django_language', u'id')]
  • I'm using memcached and nothing too strange.
  • I'm suspicious that this bug is because: the user starts logging in and uses the auth_token etc, then something goes wrong and the browser tries to load the URL again. Fb/Twitter doesn't allow to use those tokens a 2nd time, and therefore the /complete/ step doesn't have all the necessary information in session.

Any comments are appreciated! I want to add more debug information or some workaround for this.

clemente avatar Feb 02 '15 10:02 clemente

I had this issue for a long time with google-oauth2 as well and finally figured out the root cause of the problem last night. I'm not sure if you are experiencing exactly the same problem, but for me it turned out to be an issue of conflicting apps.

Apart from python-social-auth, we use django-session-security to automatically logout a user after a certain amount of time. The security app stores the expiration time in the context data of the request, but psa's context processor switches all of that context data to be stored in a "backend" key during the complete step. At this point, It looks like the base template that was rendered during this step would occasionally try to execute some javascript with context data that didn't exist, so the expiration time would be zero and the page would automatically reload.

This was a tricky problem to debug because none of the error information seems to point to another app and it rarely happens because of the variable time that it will take javascript to begin execution before the page is redirected. If the issue you're having is similar, look for any javascript in your code that executes a page reload and try to find if it depends on context data. If it does, you will just have to make sure that this code only runs if the context data exists.

darkaardvark2 avatar Feb 13 '15 22:02 darkaardvark2

That's an interesting story, I'm glad you found the cause.

I'm not sure that psa is „shifting“ your other context variables into a dict called „backends“. I think that psa is just adding to the context a new dict that you can use as needed, but it doesn't change any other context variable (all dicts returned by context processors will be joined together by Django). If one context processor destroys the others, that's the bug. Maybe what you saw can be solved by changing the order of things (of middlewares, of apps maybe, …).

But the problem I saw happens at login time (you get a 500 page before any AJAX or JS is loaded). But I was wondering: could it be a problem if a session ends but the user unknowingly continues doing AJAX requests? But no, it's not because of that, because all the errors I saw happen in the URL /account/complete/facebook/, and AJAX would never try to connect there. All the problems happen after seeing the Facebook website, not in background. Therefore I think that it can be a different error!

clemente avatar Mar 27 '15 19:03 clemente

I have also seen this issue caused where you are giving the user multiple oauth login options and using a email verification step. The user uses one oauth login and then another without verifying the first. Then when they come to validate the first link of the first oauth provider the session has been changed.

craig-hacklaunch avatar Apr 28 '15 03:04 craig-hacklaunch

In the case of linkedin, they've recently made changes to their API policy which will reject token requests for r_fullprofile. When this happens, python social auth does not check this condition, and looks for the state entry in the dictionary returned and it's not there.

There should probably be some better error checking around this.

catskul avatar May 13 '15 17:05 catskul

Some news of this issue ?

gustavi avatar Dec 16 '15 10:12 gustavi

There was an error on how the state validation was done previous to error checking which masked the original error as an state-missing one. Fixed by https://github.com/python-social-auth/social-core/commit/4d2903c1a26cc433731dcc103fd211a7a4716b96

omab avatar Dec 27 '16 13:12 omab