Fix generation of the basic auth http header.
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 9 Jun 2014 19:34:03 +0000 (21:34 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Mon, 9 Jun 2014 19:34:03 +0000 (21:34 +0200)
src/ddns/system.py

index 6057ef4..8fa5c42 100644 (file)
@@ -19,6 +19,7 @@
 #                                                                             #
 ###############################################################################
 
+import base64
 import re
 import urllib
 import urllib2
@@ -86,7 +87,7 @@ class DDNSSystem(object):
 
                        return match.group(1)
 
-       def send_request(self, url, method="GET", data=None, timeout=30):
+       def send_request(self, url, method="GET", data=None, username=None, password=None, timeout=30):
                assert method in ("GET", "POST")
 
                # Add all arguments in the data dict to the URL and escape them properly.
@@ -102,6 +103,11 @@ class DDNSSystem(object):
 
                req = urllib2.Request(url, data=data)
 
+               if username and password:
+                       basic_auth_header = self._make_basic_auth_header(username, password)
+                       print repr(basic_auth_header)
+                       req.add_header("Authorization", "Basic %s" % basic_auth_header)
+
                # Set the user agent.
                req.add_header("User-Agent", self.USER_AGENT)
 
@@ -144,6 +150,17 @@ class DDNSSystem(object):
 
                return "&".join(args)
 
+       def _make_basic_auth_header(self, username, password):
+               authstring = "%s:%s" % (username, password)
+
+               # Encode authorization data in base64.
+               authstring = base64.encodestring(authstring)
+
+               # Remove any newline characters.
+               authstring = authstring.replace("\n", "")
+
+               return authstring
+
        def get_address(self, proto):
                assert proto in ("ipv6", "ipv4")