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:
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

View file

@ -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():

View file

@ -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()