|
47 | 47 | from .tokens import AccessToken, RequestToken |
48 | 48 |
|
49 | 49 |
|
50 | | -def force_unicode(val): |
| 50 | +def force_unicode(val, encoding="unicode-escape"): |
51 | 51 | if type(val) == text_type: |
52 | 52 | return val |
53 | 53 | else: |
54 | 54 | if PY3: |
55 | | - return str(val, "unicode-escape") |
| 55 | + return val.decode(encoding, errors="replace") |
56 | 56 | else: |
57 | | - return unicode(val, "unicode-escape") |
| 57 | + return unicode(val, encoding, errors="replace") |
58 | 58 |
|
59 | 59 |
|
60 | 60 | def initiate(mw_uri, consumer_token, callback='oob', |
@@ -89,37 +89,38 @@ def initiate(mw_uri, consumer_token, callback='oob', |
89 | 89 | auth=auth, |
90 | 90 | headers={'User-Agent': user_agent}) |
91 | 91 |
|
92 | | - credentials = parse_qs(r.content) |
| 92 | + request_token = process_request_token(r.content) |
| 93 | + |
| 94 | + params = {'title': "Special:OAuth/authenticate", |
| 95 | + 'oauth_token': request_token.key, |
| 96 | + 'oauth_consumer_key': consumer_token.key} |
| 97 | + |
| 98 | + return (mw_uri + "?" + urlencode(params), request_token) |
| 99 | + |
| 100 | + |
| 101 | +def process_request_token(content): |
| 102 | + text_content = force_unicode(content, "utf8") |
| 103 | + if text_content.startswith(force_unicode("Error: ")): |
| 104 | + raise OAuthException(text_content[len("Error: "):]) |
| 105 | + |
| 106 | + credentials = parse_qs(text_content) |
93 | 107 |
|
94 | 108 | if credentials is None or credentials == {}: |
95 | 109 | raise OAuthException( |
96 | 110 | "Expected x-www-form-urlencoded response from " + |
97 | 111 | "MediaWiki, but got something else: " + |
98 | | - "{0}".format(repr(r.content))) |
99 | | - |
100 | | - elif b('oauth_token') not in credentials or \ |
101 | | - b('oauth_token_secret') not in credentials: |
102 | | - |
| 112 | + "{0}".format(repr(text_content))) |
| 113 | + elif 'oauth_token' not in credentials or \ |
| 114 | + 'oauth_token_secret' not in credentials: |
103 | 115 | raise OAuthException( |
104 | 116 | "MediaWiki response lacks token information: " |
105 | 117 | "{0}".format(repr(credentials))) |
106 | | - |
107 | 118 | else: |
108 | | - |
109 | | - request_token = RequestToken( |
110 | | - credentials.get(b('oauth_token'))[0], |
111 | | - credentials.get(b('oauth_token_secret'))[0] |
| 119 | + return RequestToken( |
| 120 | + credentials.get('oauth_token')[0], |
| 121 | + credentials.get('oauth_token_secret')[0] |
112 | 122 | ) |
113 | 123 |
|
114 | | - params = {'title': "Special:OAuth/authenticate", |
115 | | - 'oauth_token': request_token.key, |
116 | | - 'oauth_consumer_key': consumer_token.key} |
117 | | - |
118 | | - return ( |
119 | | - mw_uri + "?" + urlencode(params), |
120 | | - request_token |
121 | | - ) |
122 | | - |
123 | 124 |
|
124 | 125 | def complete(mw_uri, consumer_token, request_token, response_qs, |
125 | 126 | user_agent=defaults.USER_AGENT): |
@@ -249,13 +250,13 @@ def identify(mw_uri, consumer_token, access_token, leeway=10.0, |
249 | 250 | resp = r.json() |
250 | 251 | if 'error' in resp: |
251 | 252 | raise OAuthException( |
252 | | - "A MediaWiki API error occurred: {0}".format(resp['message'])) |
253 | | - except ValueError: |
| 253 | + "A MediaWiki API error occurred: {0}" |
| 254 | + .format(resp['message'])) |
| 255 | + except ValueError as e: |
254 | 256 | raise OAuthException( |
255 | 257 | "An error occurred while trying to read json " + |
256 | 258 | "content: {0}".format(e)) |
257 | 259 |
|
258 | | - |
259 | 260 | # Decode json & stuff |
260 | 261 | try: |
261 | 262 | identity = jwt.decode(r.content, consumer_token.secret, |
|
0 commit comments