From 48a248ff594980e920e9b37db5ea3e93c6957131 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sun, 27 Dec 2015 17:12:46 +0100 Subject: [PATCH] Plex home user authentication done! --- resources/lib/PlexAPI.py | 12 ++++++---- resources/lib/userclient.py | 46 +++++-------------------------------- service.py | 2 -- 3 files changed, 14 insertions(+), 46 deletions(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index 3b518b87..05da6d3e 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -823,6 +823,7 @@ class PlexAPI(): Output: username + userid authtoken """ string = self.__language__ @@ -835,7 +836,7 @@ class PlexAPI(): if not users: utils.settings('username', value=plexLogin) self.logMsg("User download failed. Set username = plexlogin", 1) - return + return ('', '', '') userlist = [] for user in users: @@ -852,7 +853,7 @@ class PlexAPI(): else: self.logMsg("No user selected.", 1) xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId) - return + return ('', '', '') # Ask for PIN, if protected: if user['protected'] == '1': dialog = xbmcgui.Dialog() @@ -861,9 +862,11 @@ class PlexAPI(): type=xbmcgui.INPUT_NUMERIC, option=xbmcgui.ALPHANUM_HIDE_INPUT ) + else: + pin = None # Switch to this Plex Home user, if applicable username, usertoken = self.MyPlexSwitchHomeUser( - user['User id'], + user['id'], pin, plexToken ) @@ -874,6 +877,7 @@ class PlexAPI(): 'Could not log in user %s' % selected_user, 'Please try again.' ) + return (username, user['id'], usertoken) def MyPlexSwitchHomeUser(self, id, pin, authtoken, options={}): """ @@ -933,7 +937,7 @@ class PlexAPI(): Output: List of users, where one entry is of the form: { - "User id": userId, "admin": '1'/'0', "guest": '1'/'0', + "id": userId, "admin": '1'/'0', "guest": '1'/'0', "restricted": '1'/'0', "protected": '1'/'0', "email": email, "title": title, "username": username, "thumb": thumb_url diff --git a/resources/lib/userclient.py b/resources/lib/userclient.py index e42b4b43..c51299e7 100644 --- a/resources/lib/userclient.py +++ b/resources/lib/userclient.py @@ -15,6 +15,8 @@ import utils import clientinfo import downloadutils +import PlexAPI + ################################################################################################## @@ -325,53 +327,16 @@ class UserClient(threading.Thread): ##### AUTHENTICATE USER ##### - users = self.getPublicUsers() - password = "" - - # Find user in list - for user in users: - name = user['Name'] - - if username.decode('utf-8') in name: - # If user has password - if user['HasPassword'] == True: - password = xbmcgui.Dialog().input( - heading="Enter password for user: %s" % username, - option=xbmcgui.ALPHANUM_HIDE_INPUT) - # If password dialog is cancelled - if not password: - self.logMsg("No password entered.", 0) - utils.window('emby_serverStatus', value="Stop") - self.auth = False - return - break - else: - # Manual login, user is hidden - password = xbmcgui.Dialog().input( - heading="Enter password for user: %s" % username, - option=xbmcgui.ALPHANUM_HIDE_INPUT) - sha1 = hashlib.sha1(password) - sha1 = sha1.hexdigest() - - # Authenticate username and password - url = "%s/emby/Users/AuthenticateByName?format=json" % server - data = {'username': username, 'password': sha1} - self.logMsg(data, 2) - - result = self.doUtils.downloadUrl(url, postBody=data, type="POST", authenticate=False) - + # Choose Plex user login try: - self.logMsg("Auth response: %s" % result, 1) - accessToken = result['AccessToken'] - + username, userId, accessToken = PlexAPI.PlexAPI().ChoosePlexHomeUser() except (KeyError, TypeError): self.logMsg("Failed to retrieve the api key.", 1) accessToken = None if accessToken is not None: self.currUser = username - xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % self.currUser) - userId = result['User']['Id'] + xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % username) utils.settings('accessToken', value=accessToken) utils.settings('userId%s' % username, value=userId) self.logMsg("User Authenticated: %s" % accessToken, 1) @@ -417,6 +382,7 @@ class UserClient(threading.Thread): monitor = xbmc.Monitor() self.logMsg("----===## Starting UserClient ##===----", 0) + self.logMsg("self.auth=%s, self.currUser=%s" % (self.auth, self.currUser), 0) while not monitor.abortRequested(): diff --git a/service.py b/service.py index 43ea429a..34182578 100644 --- a/service.py +++ b/service.py @@ -98,8 +98,6 @@ class Service(): # Server auto-detect initialsetup.InitialSetup().setup() - # Choose Plex user login - PlexAPI.PlexAPI().ChoosePlexHomeUser() # Initialize important threads user = userclient.UserClient()