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

OAuth1Session.parse_authorization_response breaks with unquoted url #429

Open
qria opened this issue Oct 18, 2020 · 1 comment
Open

OAuth1Session.parse_authorization_response breaks with unquoted url #429

qria opened this issue Oct 18, 2020 · 1 comment

Comments

@qria
Copy link

qria commented Oct 18, 2020

With callback url there can be additional parameters such as next as well as the oauth_token and oauth_verificer, and there can be unquoted strings in it.

For example, my callback url was:
"/login/twitter-callback?next=ㅋㅋ&oauth_token=<redacted>&oauth_verifier=<redacted>"

Calling OAuth1Session.parse_authorization_response on above url yields an error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

But this is not the culprit because of how urldecode is coded to ignore failed urldecode attempts:

def urldecode(body):
    """Parse query or json to python dictionary"""
    try:
        return _urldecode(body)
    except Exception:   # <-
        import json

        return json.loads(body)

The real error is:

ValueError: Error trying to decode a non urlencoded string. Found invalid characters: {'ㅋ'} in the string: 'next=ㅋ&oauth_token=<redacted>&oauth_verifier=<redacted>'. Please ensure the request/response body is x-www-form-urlencoded.

You can reproduce this error with this code:

import json
from oauthlib.common import urldecode

from urllib.parse import urlparse
url = 'https://www.example.com/login/twitter-callback?next=ㅋㅋ&oauth_token=aaa&oauth_verifier=bbb'
urldecode(urlparse(url).query)
@qria
Copy link
Author

qria commented Oct 18, 2020

As a workaround I am doing this for now:

import urllib.parse

# oauth_session.parse_authorization_response(request.url)  # this is bugged
qs_dict = urllib.parse.parse_qs(urllib.parse.urlparse(request.url).query)
token = {
    'oauth_token': qs_dict['oauth_token'][0],
    'oauth_verifier': qs_dict['oauth_verifier'][0],
}
oauth_session.token = token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant