]> git.ipfire.org Git - ddns.git/commitdiff
system: Allow passing custom Authorization headers json
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Aug 2025 22:12:15 +0000 (22:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Aug 2025 22:12:15 +0000 (22:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/ddns/system.py

index 1999b46252b052e535ffe774a44a60464ed9e323..e86b47b125b2bb36678c6b1942f178bf8d0714a8 100644 (file)
@@ -122,8 +122,8 @@ class DDNSSystem(object):
 
                return self._guess_external_ip_address(url, **kwargs)
 
-       def send_request(self, url, method="GET", data=None, username=None, password=None,
-                       timeout=30, expect=bytes):
+       def send_request(self, url, method="GET", data=None, authorization=None,
+                       username=None, password=None, timeout=30, expect=bytes):
                # Add all arguments in the data dict to the URL and escape them properly
                if data:
                        if method == "GET":
@@ -145,9 +145,18 @@ class DDNSSystem(object):
 
                req = urllib.request.Request(url, method=method, data=data)
 
+               # Authenticate using HTTP Basic Auth
                if username and password:
-                       basic_auth_header = self._make_basic_auth_header(username, password)
-                       req.add_header("Authorization", "Basic %s" % basic_auth_header.decode())
+                       # Fail if too much authentication data has been passed
+                       if authorization:
+                               raise ValueError("You cannot pass an Authorization header as well as username and password")
+
+                       # Compose the Authorization: header
+                       authorizaton = self._make_basic_auth_header(username, password)
+
+               # Set the Authorization: header
+               if authorization:
+                       req.add_header("Authorization", authorization)
 
                # Set the user agent.
                req.add_header("User-Agent", self.USER_AGENT)
@@ -274,7 +283,7 @@ class DDNSSystem(object):
                # Encode authorization data in base64.
                authstring = base64.b64encode(authstring.encode())
 
-               return authstring
+               return "Basic %s" % authstring.decode()
 
        def get_address(self, proto):
                """