Skip to content

Commit

Permalink
Use Basic/Digest Auth from Requests
Browse files Browse the repository at this point in the history
As suggested in #52
Original auth mechanism introduced in #27
This change should retain backwards compability
  • Loading branch information
danmichaelo committed Nov 17, 2014
1 parent aa490ba commit 8bec560
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Expand Up @@ -19,6 +19,7 @@ This is the development version of mwclient.
Use 'simplified' continuation
[4262786](https://github.com/mwclient/mwclient/commit/4262786),
[#66](https://github.com/mwclient/mwclient/issues/66).
* [2014-11-16] Use Basic/Digest Auth from Requests

## Changes in version 0.7.0

Expand Down
14 changes: 9 additions & 5 deletions mwclient/client.py
Expand Up @@ -6,7 +6,6 @@
import random
import sys
import weakref
import base64

try:
# Python 2.7+
Expand All @@ -20,6 +19,7 @@
except ImportError:
import simplejson as json
import requests
from requests.auth import HTTPBasicAuth, AuthBase

import errors
import listing
Expand Down Expand Up @@ -62,13 +62,19 @@ def __init__(self, host, path='/w/', ext='.php', pool=None, retry_timeout=30,
self.ext = ext
self.credentials = None
self.compress = compress
self.httpauth = httpauth
self.retry_timeout = retry_timeout
self.max_retries = max_retries
self.wait_callback = wait_callback
self.max_lag = str(max_lag)
self.force_login = force_login

if isinstance(httpauth, (list, tuple)):
self.httpauth = HTTPBasicAuth(*httpauth)
elif httpauth is None or isinstance(httpauth, (AuthBase,)):
self.httpauth = httpauth
else:
raise RuntimeError('Authentication is not a tuple or an instance of AuthBase')

# The token string => token object mapping
self.wait_tokens = weakref.WeakKeyDictionary()

Expand All @@ -86,6 +92,7 @@ def __init__(self, host, path='/w/', ext='.php', pool=None, retry_timeout=30,
# Setup connection
if pool is None:
self.connection = requests.Session()
self.connection.auth = self.httpauth
self.connection.headers['User-Agent'] = 'MwClient/' + __ver__ + ' (https://github.com/mwclient/mwclient)'
if clients_useragent:
self.connection.headers['User-Agent'] = clients_useragent + ' - ' + self.connection.headers['User-Agent']
Expand Down Expand Up @@ -235,9 +242,6 @@ def raw_call(self, script, data, files=None):
headers = {}
if self.compress and gzip:
headers['Accept-Encoding'] = 'gzip'
if self.httpauth is not None:
credentials = base64.encodestring('%s:%s' % self.httpauth).replace('\n', '')
headers['Authorization'] = 'Basic %s' % credentials
token = self.wait_token((script, data))
while True:
scheme = 'http' # Should we move to 'https' as default?
Expand Down
17 changes: 17 additions & 0 deletions tests/test_client.py
Expand Up @@ -140,6 +140,23 @@ def test_basic_request(self):
assert 'action=query' in responses.calls[0].request.body
assert 'meta=siteinfo%7Cuserinfo' in responses.calls[0].request.body

@responses.activate
def test_httpauth_defaults_to_basic_auth(self):

self.httpShouldReturn(self.makeMetaResponse())

site = mwclient.Site('test.wikipedia.org', httpauth=('me', 'verysecret'))

assert isinstance(site.httpauth, requests.auth.HTTPBasicAuth)

@responses.activate
def test_httpauth_raise_error_on_invalid_type(self):

self.httpShouldReturn(self.makeMetaResponse())

with pytest.raises(RuntimeError):
site = mwclient.Site('test.wikipedia.org', httpauth=1)

@responses.activate
def test_api_disabled(self):
# Should raise APIDisabledError if API is not enabled
Expand Down

0 comments on commit 8bec560

Please sign in to comment.