From: Michael Tremer Date: Fri, 29 Aug 2025 22:12:15 +0000 (+0000) Subject: system: Allow passing custom Authorization headers X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Fjson;p=ddns.git system: Allow passing custom Authorization headers Signed-off-by: Michael Tremer --- diff --git a/src/ddns/system.py b/src/ddns/system.py index 1999b46..e86b47b 100644 --- a/src/ddns/system.py +++ b/src/ddns/system.py @@ -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): """