From: Jaroslav Kysela Date: Mon, 26 Nov 2018 15:23:11 +0000 (+0100) Subject: tvh-json.py: add proper digest/plain authentication, fixes #5350 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d1269210587e3423add62aa0d7aead654eb36ded;p=thirdparty%2Ftvheadend.git tvh-json.py: add proper digest/plain authentication, fixes #5350 --- diff --git a/lib/api/python/tvh-json.py b/lib/api/python/tvh-json.py index fd5f67025..62df9fa02 100755 --- a/lib/api/python/tvh-json.py +++ b/lib/api/python/tvh-json.py @@ -7,7 +7,6 @@ import os import sys import json -import base64 import traceback try: # Python 3 @@ -27,6 +26,9 @@ DEBUG=False TVH_API=env('TVH_API_URL', 'http://localhost:9981/api') TVH_USER=env('TVH_USER', None) TVH_PASS=env('TVH_PASS', None) +TVH_AUTH=env('TVH_AUTH', 'digest') +PWD_MGR = urllib.HTTPPasswordMgrWithDefaultRealm() +PWD_MGR.add_password(None, TVH_API, TVH_USER, TVH_PASS) class Response(object): def __init__(self, response): @@ -55,14 +57,19 @@ class TVHeadend(object): def __init__(self, path, headers=None): self._headers = headers or {} self._path = path or [] - a = '%s:%s' % (TVH_USER, TVH_PASS) - self._auth = b'Basic ' + base64.b64encode(a.encode('utf-8')) def opener(self): - if DEBUG: - return urllib.build_opener(urllib.HTTPSHandler(debuglevel=1)) + handlers = [] + if TVH_AUTH == 'digest': + handlers.append(urllib.HTTPDigestAuthHandler(PWD_MGR)) + elif TVH_AUTH == 'basic': + handlers.append(urllib.HTTPBasicAuthHandler(PWD_MGR)) else: - return urllib.build_opener() + handlers.append(urllib.HTTPDigestAuthHandler(PWD_MGR)) + handlers.append(urllib.HTTPBasicAuthHandler(PWD_MGR)) + if DEBUG: + handlers.append(urllib.HTTPSHandler(debuglevel=1)) + return urllib.build_opener(*handlers) def _push(self, data, binary=None, method='PUT'): content_type = None @@ -76,7 +83,6 @@ class TVHeadend(object): request = urllib.Request(TVH_API + path, data=data) if content_type: request.add_header('Content-Type', content_type) - request.add_header('Authorization', self._auth) request.get_method = lambda: method try: r = Response(opener.open(request))