Plex home user authentication done!

This commit is contained in:
tomkat83 2015-12-27 17:12:46 +01:00
parent 6ea38c7dfb
commit 48a248ff59
3 changed files with 14 additions and 46 deletions

View file

@ -823,6 +823,7 @@ class PlexAPI():
Output: Output:
username username
userid
authtoken authtoken
""" """
string = self.__language__ string = self.__language__
@ -835,7 +836,7 @@ class PlexAPI():
if not users: if not users:
utils.settings('username', value=plexLogin) utils.settings('username', value=plexLogin)
self.logMsg("User download failed. Set username = plexlogin", 1) self.logMsg("User download failed. Set username = plexlogin", 1)
return return ('', '', '')
userlist = [] userlist = []
for user in users: for user in users:
@ -852,7 +853,7 @@ class PlexAPI():
else: else:
self.logMsg("No user selected.", 1) self.logMsg("No user selected.", 1)
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId) xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
return return ('', '', '')
# Ask for PIN, if protected: # Ask for PIN, if protected:
if user['protected'] == '1': if user['protected'] == '1':
dialog = xbmcgui.Dialog() dialog = xbmcgui.Dialog()
@ -861,9 +862,11 @@ class PlexAPI():
type=xbmcgui.INPUT_NUMERIC, type=xbmcgui.INPUT_NUMERIC,
option=xbmcgui.ALPHANUM_HIDE_INPUT option=xbmcgui.ALPHANUM_HIDE_INPUT
) )
else:
pin = None
# Switch to this Plex Home user, if applicable # Switch to this Plex Home user, if applicable
username, usertoken = self.MyPlexSwitchHomeUser( username, usertoken = self.MyPlexSwitchHomeUser(
user['User id'], user['id'],
pin, pin,
plexToken plexToken
) )
@ -874,6 +877,7 @@ class PlexAPI():
'Could not log in user %s' % selected_user, 'Could not log in user %s' % selected_user,
'Please try again.' 'Please try again.'
) )
return (username, user['id'], usertoken)
def MyPlexSwitchHomeUser(self, id, pin, authtoken, options={}): def MyPlexSwitchHomeUser(self, id, pin, authtoken, options={}):
""" """
@ -933,7 +937,7 @@ class PlexAPI():
Output: Output:
List of users, where one entry is of the form: 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', "restricted": '1'/'0', "protected": '1'/'0',
"email": email, "title": title, "username": username, "email": email, "title": title, "username": username,
"thumb": thumb_url "thumb": thumb_url

View file

@ -15,6 +15,8 @@ import utils
import clientinfo import clientinfo
import downloadutils import downloadutils
import PlexAPI
################################################################################################## ##################################################################################################
@ -325,53 +327,16 @@ class UserClient(threading.Thread):
##### AUTHENTICATE USER ##### ##### AUTHENTICATE USER #####
users = self.getPublicUsers() # Choose Plex user login
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)
try: try:
self.logMsg("Auth response: %s" % result, 1) username, userId, accessToken = PlexAPI.PlexAPI().ChoosePlexHomeUser()
accessToken = result['AccessToken']
except (KeyError, TypeError): except (KeyError, TypeError):
self.logMsg("Failed to retrieve the api key.", 1) self.logMsg("Failed to retrieve the api key.", 1)
accessToken = None accessToken = None
if accessToken is not None: if accessToken is not None:
self.currUser = username self.currUser = username
xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % self.currUser) xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % username)
userId = result['User']['Id']
utils.settings('accessToken', value=accessToken) utils.settings('accessToken', value=accessToken)
utils.settings('userId%s' % username, value=userId) utils.settings('userId%s' % username, value=userId)
self.logMsg("User Authenticated: %s" % accessToken, 1) self.logMsg("User Authenticated: %s" % accessToken, 1)
@ -417,6 +382,7 @@ class UserClient(threading.Thread):
monitor = xbmc.Monitor() monitor = xbmc.Monitor()
self.logMsg("----===## Starting UserClient ##===----", 0) self.logMsg("----===## Starting UserClient ##===----", 0)
self.logMsg("self.auth=%s, self.currUser=%s" % (self.auth, self.currUser), 0)
while not monitor.abortRequested(): while not monitor.abortRequested():

View file

@ -98,8 +98,6 @@ class Service():
# Server auto-detect # Server auto-detect
initialsetup.InitialSetup().setup() initialsetup.InitialSetup().setup()
# Choose Plex user login
PlexAPI.PlexAPI().ChoosePlexHomeUser()
# Initialize important threads # Initialize important threads
user = userclient.UserClient() user = userclient.UserClient()