Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support basic HTTP authentication in version info.

  • Loading branch information...
commit c1bb87994c5fb0e07a3621d6d5b88952f98527df 1 parent b42ffa3
@jone jone authored
View
3  docs/HISTORY.txt
@@ -5,6 +5,9 @@ Changelog
1.2.9
-----
+- Support basic HTTP authentication in version info.
+ [jone]
+
* Dependencycheck: do not mark packages as changed if only .mo-files
changed (since they are rebuilt in tag).
[jbaumann]
View
28 ftw/manager/commands/versioninfo.py
@@ -1,10 +1,12 @@
from ftw.manager.commands import basecommand
from ftw.manager.utils import output
from ftw.manager.utils import scm
+from ftw.manager.utils.http import HTTPRealmFinder
from ftw.manager.utils.memoize import memoize
from ftw.manager.utils.output import error
from pkg_resources import parse_version, Requirement
from setuptools import package_index
+from urlparse import urlparse, urlunparse
import ConfigParser
import distutils.core
import os
@@ -255,6 +257,32 @@ def _download_file(self, url):
# we need to keep a reference to the tempfile, otherwise it will be deleted
# imidiately
self._temporary_downloaded = []
+
+ if '@' in url:
+ # http basic auth in url
+
+ # remove credentials part from url
+ protocol, rest = url.split('://', 1)
+ protocol += '://'
+ credentials, rest = rest.split('@', 1)
+ url = protocol + rest
+
+ realm = HTTPRealmFinder(url).get()
+
+ # install a basic auth handler
+ if ':' in credentials:
+ user, password = credentials.split(':', 1)
+ else:
+ user, password = credentials, None
+
+ auth_handler = urllib2.HTTPBasicAuthHandler()
+ auth_handler.add_password(realm=realm,
+ uri=url,
+ user=user,
+ passwd=password)
+ opener = urllib2.build_opener(auth_handler)
+ urllib2.install_opener(opener)
+
request = urllib2.Request(url)
response = urllib2.urlopen(request)
data = response.read()
View
49 ftw/manager/utils/http.py
@@ -0,0 +1,49 @@
+import urllib2
+from urlparse import urlparse
+
+
+class HTTPRealmFinderHandler(urllib2.HTTPBasicAuthHandler):
+ def http_error_401(self, req, fp, code, msg, headers):
+ realm_string = headers['www-authenticate']
+
+ q1 = realm_string.find('"')
+ q2 = realm_string.find('"', q1+1)
+ realm = realm_string[q1+1:q2]
+
+ self.realm = realm
+
+
+class HTTPRealmFinder:
+ def __init__(self, url):
+ self.url = url
+ scheme, domain, path, x1, x2, x3 = urlparse(url)
+
+ handler = HTTPRealmFinderHandler()
+ handler.add_password(None, domain, 'foo', 'bar')
+ self.handler = handler
+
+ opener = urllib2.build_opener(handler)
+ urllib2.install_opener(opener)
+
+ def ping(self, url):
+ try:
+ urllib2.urlopen(url)
+ except urllib2.HTTPError:
+ pass
+
+ def get(self):
+ self.ping(self.url)
+ try:
+ realm = self.handler.realm
+ except AttributeError:
+ realm = None
+
+ return realm
+
+ def prt(self):
+ print self.get()
+
+
+def register_basic_auth_handler_for_url(url):
+ """Registers a basic auth handler for the url if it seems necessary.
+ """
Please sign in to comment.
Something went wrong with that request. Please try again.