From 51701c1eed7c1d9be91f9b509e0379244596c1f4 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 18 Feb 2015 21:27:01 +0200 Subject: [PATCH] Allow authentication on custom MusicBrainz servers using Basic HTTP Authentication. --- data/interfaces/default/config.html | 31 +++++++++++++++++++++++++++++ headphones/config.py | 3 +++ headphones/mb.py | 12 ++++++++--- headphones/webserve.py | 5 ++++- lib/musicbrainzngs/musicbrainz.py | 12 +++++++++-- 5 files changed, 57 insertions(+), 6 deletions(-) diff --git a/data/interfaces/default/config.html b/data/interfaces/default/config.html index 3b72169d9..ce0217ba7 100644 --- a/data/interfaces/default/config.html +++ b/data/interfaces/default/config.html @@ -1416,6 +1416,17 @@

MPC

+
+ +
+
+
+ +
+
+
+
+
@@ -1526,6 +1537,25 @@

MPC

$('#api_key').val(data); }); }); + if ($("#customauth").is(":checked")) + { + $("#customauth_options").show(); + } + else + { + $("#customauth_options").hide(); + } + + $("#customauth").click(function(){ + if ($("#customauth").is(":checked")) + { + $("#customauth_options").slideDown(); + } + else + { + $("#customauth_options").slideUp(); + } + }); if ($("#enable_https").is(":checked")) { $("#https_options").show(); @@ -2074,6 +2104,7 @@

MPC

initConfigCheckbox("#use_whatcd"); initConfigCheckbox("#api_enabled"); initConfigCheckbox("#enable_https"); + initConfigCheckbox("#customauth"); $('#twitterStep1').click(function () { diff --git a/headphones/config.py b/headphones/config.py index 4071edd7e..a3b390a89 100644 --- a/headphones/config.py +++ b/headphones/config.py @@ -44,9 +44,12 @@ def bool_int(value): 'CUE_SPLIT': (int, 'General', 1), 'CUE_SPLIT_FLAC_PATH': (str, 'General', ''), 'CUE_SPLIT_SHNTOOL_PATH': (str, 'General', ''), + 'CUSTOMAUTH': (int, 'General', 0), 'CUSTOMHOST': (str, 'General', 'localhost'), + 'CUSTOMPASS': (str, 'General', ''), 'CUSTOMPORT': (int, 'General', 5000), 'CUSTOMSLEEP': (int, 'General', 1), + 'CUSTOMUSER': (str, 'General', ''), 'DELETE_LOSSLESS_FILES': (int, 'General', 1), 'DESTINATION_DIR': (str, 'General', ''), 'DETECT_BITRATE': (int, 'General', 0), diff --git a/headphones/mb.py b/headphones/mb.py index 6caf0090f..66a9f3035 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -47,6 +47,8 @@ def startmb(): elif headphones.CONFIG.MIRROR == "custom": mbhost = headphones.CONFIG.CUSTOMHOST mbport = int(headphones.CONFIG.CUSTOMPORT) + mbuser = headphones.CONFIG.CUSTOMUSER + mbpass = headphones.CONFIG.CUSTOMPASS sleepytime = int(headphones.CONFIG.CUSTOMSLEEP) elif headphones.CONFIG.MIRROR == "headphones": mbhost = "144.76.94.239" @@ -69,12 +71,16 @@ def startmb(): mb_lock.minimum_delta = sleepytime # Add headphones credentials - if headphones.CONFIG.MIRROR == "headphones": - if not mbuser and mbpass: - logger.warn("No username or password set for VIP server") + if headphones.CONFIG.MIRROR == "headphones" or headphones.CONFIG.CUSTOMAUTH: + if not mbuser or not mbpass: + logger.warn("No username or password set for MusicBrainz server") else: musicbrainzngs.hpauth(mbuser, mbpass) + # Let us know if we disable custom authentication + if not headphones.CONFIG.CUSTOMAUTH: + musicbrainzngs.disable_hpauth(); + logger.debug('Using the following server values: MBHost: %s, MBPort: %i, Sleep Interval: %i', mbhost, mbport, sleepytime) return True diff --git a/headphones/webserve.py b/headphones/webserve.py index b3b67cac5..9cd03280e 100644 --- a/headphones/webserve.py +++ b/headphones/webserve.py @@ -1142,6 +1142,9 @@ def config(self): "customhost": headphones.CONFIG.CUSTOMHOST, "customport": headphones.CONFIG.CUSTOMPORT, "customsleep": headphones.CONFIG.CUSTOMSLEEP, + "customauth": checked(headphones.CONFIG.CUSTOMAUTH), + "customuser": headphones.CONFIG.CUSTOMUSER, + "custompass": headphones.CONFIG.CUSTOMPASS, "hpuser": headphones.CONFIG.HPUSER, "hppass": headphones.CONFIG.HPPASS, "songkick_enabled": checked(headphones.CONFIG.SONGKICK_ENABLED), @@ -1196,7 +1199,7 @@ def configUpdate(self, **kwargs): "nma_enabled", "nma_onsnatch", "pushalot_enabled", "pushalot_onsnatch", "synoindex_enabled", "pushover_enabled", "pushover_onsnatch", "pushbullet_enabled", "pushbullet_onsnatch", "subsonic_enabled", "twitter_enabled", "twitter_onsnatch", "osx_notify_enabled", "osx_notify_onsnatch", "boxcar_enabled", "boxcar_onsnatch", "songkick_enabled", "songkick_filter_enabled", - "mpc_enabled" + "mpc_enabled", "customauth" ] for checked_config in checked_configs: if checked_config not in kwargs: diff --git a/lib/musicbrainzngs/musicbrainz.py b/lib/musicbrainzngs/musicbrainz.py index 875eff6f3..d7e5e74f4 100644 --- a/lib/musicbrainzngs/musicbrainz.py +++ b/lib/musicbrainzngs/musicbrainz.py @@ -271,6 +271,7 @@ def _decorator(func): hostname = "musicbrainz.org" _client = "" _useragent = "" +mb_auth = False def auth(u, p): """Set the username and password to be used in subsequent queries to @@ -284,9 +285,16 @@ def hpauth(u, p): """Set the username and password to be used in subsequent queries to the MusicBrainz XML API that require authentication. """ - global hpuser, hppassword + global hpuser, hppassword, mb_auth hpuser = u hppassword = p + mb_auth = True + +def disable_hpauth(): + """Disable the authentication for MusicBrainz XML API + """ + global mb_auth + mb_auth = False def set_useragent(app, version, contact=None): """Set the User-Agent to be used for requests to the MusicBrainz webservice. @@ -635,7 +643,7 @@ def _mb_request(path, method='GET', auth_required=False, client_required=False, req.add_header('User-Agent', _useragent) # Add headphones credentials - if hostname == '144.76.94.239:8181': + if mb_auth: base64string = base64.encodestring('%s:%s' % (hpuser, hppassword)).replace('\n', '') req.add_header("Authorization", "Basic %s" % base64string)