VcamX has uploaded a new change for review.
https://gerrit.wikimedia.org/r/230159
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, 47 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/59/230159/1
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 4b8fcfc..8bc3124 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -241,6 +241,27 @@
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 or four
+ """
+ netloc = requests.utils.urlparse(uri).netloc
+ netloc_parts = netloc.split('.')
+ netlocs = [netloc] + ['.'.join(['*'] + netloc_parts[i + 1:])
+ for i in range(len(netloc_parts))]
+ auth_t = None
+ for path in netlocs:
+ if path in config.authenticate:
+ auth_t = config.authenticate[path]
+ break
+ return auth_t if auth_t is not None and len(auth_t) == 2 else None
+
+
def _http_process(session, http_request):
method = http_request.method
uri = http_request.uri
@@ -248,7 +269,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..6ed7da0 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -70,6 +70,26 @@
self.assertIsInstance(r, unicode)
self.assertIn('<html lang="mul"', r)
+ def test_get_authentication(self):
+ """Test http.get_authentication."""
+ 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'),
+ }
+ 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://www.wikiquote.org/': None,
+ }
+ for url, auth in pairs.items():
+ self.assertEqual(http.get_authentication(url), auth)
+ config.authenticate = authenticate
+
class HttpsCertificateTestCase(TestCase):
--
To view, visit https://gerrit.wikimedia.org/r/230159
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib426ca834839912c3707c66d41b814ece58be5a1
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: VcamX <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits