jenkins-bot has submitted this change and it was merged.
Change subject: Support wildcard character for config.authenticate ......................................................................
Support wildcard character for config.authenticate
Wildcard character '*' could be used as the prefix of urls in config.authenticate.
Bug: T108334 Change-Id: Ib426ca834839912c3707c66d41b814ece58be5a1 --- M pywikibot/comms/http.py M pywikibot/config2.py M tests/http_tests.py 3 files changed, 63 insertions(+), 2 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index 4b8fcfc..e39aae0 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -241,6 +241,29 @@ return r.content
+def get_authentication(uri): + """ + Retrieve authentication token. + + @param uri: the URI to access + @type uri: str + @return: authentication token + @rtype: None or tuple of two str + """ + parsed_uri = requests.utils.urlparse(uri) + netloc_parts = parsed_uri.netloc.split('.') + netlocs = [parsed_uri.netloc] + ['.'.join(['*'] + netloc_parts[i + 1:]) + for i in range(len(netloc_parts))] + for path in netlocs: + if path in config.authenticate: + if len(config.authenticate[path]) == 2: + return config.authenticate[path] + else: + warn('Invalid authentication tokens for %s ' + 'set in `config.authenticate`' % path) + return None + + def _http_process(session, http_request): method = http_request.method uri = http_request.uri @@ -248,7 +271,7 @@ headers = http_request.headers if PY2 and headers: headers = dict((key, str(value)) for key, value in headers.items()) - auth = config.authenticate.get(requests.utils.urlparse(uri).netloc, None) + auth = get_authentication(uri) timeout = config.socket_timeout try: response = session.request(method, uri, data=body, headers=headers, diff --git a/pywikibot/config2.py b/pywikibot/config2.py index 40a5031..0ef584b 100644 --- a/pywikibot/config2.py +++ b/pywikibot/config2.py @@ -151,13 +151,17 @@ # the following form: # # authenticate['en.wikipedia.org'] = ('John','XXXXX') +# authenticate['*.wikipedia.org'] = ('John','XXXXX') # # where John is your login name, and XXXXX your password. # Note: # 1. This is only for sites that use authentication in the form that gives # you a popup for name and password when you try to access any data, NOT # for, for example, wiki usernames -# 2. You must use the hostname of the site, not its family/language pair +# 2. You must use the hostname of the site, not its family/language pair. +# Pywikibot supports wildcard (*) in the prefix of hostname and select the +# best match authentication. So you can specify authentication not only for +# one site authenticate = {}
# diff --git a/tests/http_tests.py b/tests/http_tests.py index 8804345..d3d6027 100644 --- a/tests/http_tests.py +++ b/tests/http_tests.py @@ -71,6 +71,40 @@ self.assertIn('<html lang="mul"', r)
+class TestGetAuthenticationConfig(TestCase): + + """Test http.get_authentication.""" + + net = False + + def setUp(self): + """Set up test by configuring config.authenticate.""" + self._authenticate = config.authenticate + config.authenticate = { + 'zh.wikipedia.beta.wmflabs.org': ('1', '2'), + '*.wikipedia.beta.wmflabs.org': ('3', '4'), + '*.beta.wmflabs.org': ('5', '6'), + '*.wmflabs.org': ('7', '8'), + } + + def tearDown(self): + """Tear down test by resetting config.authenticate.""" + config.authenticate = self._authenticate + + def test_url_based_authentication(self): + """Test url-based authentication info.""" + pairs = { + 'https://zh.wikipedia.beta.wmflabs.org': ('1', '2'), + 'https://en.wikipedia.beta.wmflabs.org': ('3', '4'), + 'https://wiki.beta.wmflabs.org': ('5', '6'), + 'https://beta.wmflabs.org': ('7', '8'), + 'https://wmflabs.org': None, + 'https://www.wikiquote.org/': None, + } + for url, auth in pairs.items(): + self.assertEqual(http.get_authentication(url), auth) + + class HttpsCertificateTestCase(TestCase):
"""HTTPS certificate test."""
pywikibot-commits@lists.wikimedia.org