Start user authentication

This commit is contained in:
tomkat83 2015-12-27 16:27:49 +01:00
parent b8395b8396
commit 6ea38c7dfb
4 changed files with 106 additions and 46 deletions

View file

@ -37,6 +37,7 @@ import utils
import downloadutils import downloadutils
import xbmcaddon import xbmcaddon
import xbmcgui import xbmcgui
import xbmc
import struct import struct
import time import time
@ -74,6 +75,7 @@ class PlexAPI():
# VARIABLES # VARIABLES
def __init__(self): def __init__(self):
self.__language__ = xbmcaddon.Addon().getLocalizedString
self.g_PMS = {} self.g_PMS = {}
client = clientinfo.ClientInfo() client = clientinfo.ClientInfo()
self.addonName = client.getAddonName() self.addonName = client.getAddonName()
@ -814,7 +816,81 @@ class PlexAPI():
dprint(__name__, 1, "====== MyPlex sign out XML finished ======") dprint(__name__, 1, "====== MyPlex sign out XML finished ======")
dprint(__name__, 0, 'MyPlex Sign Out done') dprint(__name__, 0, 'MyPlex Sign Out done')
def MyPlexSwitchHomeUser(self, id, pin, options, authtoken): def ChoosePlexHomeUser(self):
"""
Let's user choose from a list of Plex home users. Will switch to that
user accordingly.
Output:
username
authtoken
"""
string = self.__language__
plexToken = utils.settings('plexToken')
plexLogin = utils.settings('plexLogin')
self.logMsg("Getting user list.", 1)
# Get list of Plex home users self
users = self.MyPlexListHomeUsers(plexToken)
# Download users failed. Set username to Plex login
if not users:
utils.settings('username', value=plexLogin)
self.logMsg("User download failed. Set username = plexlogin", 1)
return
userlist = []
for user in users:
username = user['title']
userlist.append(username)
usertoken = ''
while not usertoken:
dialog = xbmcgui.Dialog()
user_select = dialog.select(string(30200), userlist)
if user_select > -1:
selected_user = userlist[user_select]
self.logMsg("Selected user: %s" % selected_user, 1)
utils.settings('username', value=selected_user)
else:
self.logMsg("No user selected.", 1)
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
return
# Ask for PIN, if protected:
if user['protected'] == '1':
dialog = xbmcgui.Dialog()
pin = dialog.input(
'Enter PIN for user %s' % selected_user,
type=xbmcgui.INPUT_NUMERIC,
option=xbmcgui.ALPHANUM_HIDE_INPUT
)
# Switch to this Plex Home user, if applicable
username, usertoken = self.MyPlexSwitchHomeUser(
user['User id'],
pin,
plexToken
)
if not username:
dialog = xbmcgui.Dialog()
dialog.ok(
self.addonName,
'Could not log in user %s' % selected_user,
'Please try again.'
)
def MyPlexSwitchHomeUser(self, id, pin, authtoken, options={}):
"""
Retrieves Plex home token for a Plex home user.
Input:
id id of the Plex home user
pin PIN of the Plex home user, if protected
authtoken token for plex.tv
options={} optional additional header options
Output:
username Plex home username
authtoken token for Plex home user
Returns empty strings if unsuccessful
"""
MyPlexHost = 'https://plex.tv' MyPlexHost = 'https://plex.tv'
MyPlexURL = MyPlexHost + '/api/home/users/' + id + '/switch' MyPlexURL = MyPlexHost + '/api/home/users/' + id + '/switch'
@ -822,18 +898,17 @@ class PlexAPI():
MyPlexURL += '?pin=' + pin MyPlexURL += '?pin=' + pin
xargs = {} xargs = {}
if options: xargs = self.getXArgsDeviceInfo(options)
xargs = getXArgsDeviceInfo(options)
xargs['X-Plex-Token'] = authtoken xargs['X-Plex-Token'] = authtoken
request = urllib2.Request(MyPlexURL, None, xargs) request = urllib2.Request(MyPlexURL, None, xargs)
request.get_method = lambda: 'POST' # turn into 'POST' - done automatically with data!=None. But we don't have data. request.get_method = lambda: 'POST'
response = urllib2.urlopen(request).read() response = urllib2.urlopen(request).read()
dprint(__name__, 1, "====== MyPlexHomeUser XML ======") self.logMsg("====== MyPlexHomeUser XML ======", 1)
dprint(__name__, 1, response) self.logMsg(response, 1)
dprint(__name__, 1, "====== MyPlexHomeUser XML finished ======") self.logMsg("====== MyPlexHomeUser XML finished ======", 1)
# analyse response # analyse response
XMLTree = etree.ElementTree(etree.fromstring(response)) XMLTree = etree.ElementTree(etree.fromstring(response))
@ -843,10 +918,9 @@ class PlexAPI():
authtoken = el_user.attrib.get('authenticationToken', '') authtoken = el_user.attrib.get('authenticationToken', '')
if username and authtoken: if username and authtoken:
dprint(__name__, 0, 'MyPlex switch HomeUser change successfull') self.logMsg('MyPlex switch HomeUser change successfull', 0)
else: else:
dprint(__name__, 0, 'MyPlex switch HomeUser change failed') self.logMsg('MyPlex switch HomeUser change failed', 0)
return (username, authtoken) return (username, authtoken)
def MyPlexListHomeUsers(self, authtoken): def MyPlexListHomeUsers(self, authtoken):

View file

@ -351,7 +351,7 @@ class DownloadUtils():
# Parental control - access restricted # Parental control - access restricted
utils.window('emby_serverStatus', value="restricted") utils.window('emby_serverStatus', value="restricted")
xbmcgui.Dialog().notification( xbmcgui.Dialog().notification(
heading="Emby server", heading=self.addonName,
message="Access restricted.", message="Access restricted.",
icon=xbmcgui.NOTIFICATION_ERROR, icon=xbmcgui.NOTIFICATION_ERROR,
time=5000) time=5000)
@ -366,8 +366,8 @@ class DownloadUtils():
utils.window('emby_serverStatus', value="401") utils.window('emby_serverStatus', value="401")
self.logMsg("HTTP Error: %s" % e, 0) self.logMsg("HTTP Error: %s" % e, 0)
xbmcgui.Dialog().notification( xbmcgui.Dialog().notification(
heading="Error connecting", heading=self.addonName,
message="Unauthorized.", message="Error connecting: Unauthorized.",
icon=xbmcgui.NOTIFICATION_ERROR) icon=xbmcgui.NOTIFICATION_ERROR)
return 401 return 401

View file

@ -211,26 +211,3 @@ class InitialSetup():
if musicAccess: if musicAccess:
self.logMsg("User opted to direct stream music.", 1) self.logMsg("User opted to direct stream music.", 1)
utils.settings('streamMusic', value="true") utils.settings('streamMusic', value="true")
##### USER INFO #####
self.logMsg("Getting user list.", 1)
# Get list of Plex home users
users = self.plx.MyPlexListHomeUsers(plexToken)
# Download users failed. Set username to Plex login
if not users:
utils.settings('username', value=plexLogin)
self.logMsg("User download failed. Set username = plexlogin", 1)
else:
userlist = []
for user in users:
username = user['title']
userlist.append(username)
dialog = xbmcgui.Dialog()
user_select = dialog.select(string(30200), userlist)
if user_select > -1:
selected_user = userlist[user_select]
self.logMsg("Selected user: %s" % selected_user, 1)
utils.settings('username', value=selected_user)
else:
self.logMsg("No user selected.", 1)
xbmc.executebuiltin('Addon.OpenSettings(%s)' % addonId)

View file

@ -31,6 +31,8 @@ import utils
import videonodes import videonodes
import websocket_client as wsc import websocket_client as wsc
import PlexAPI
################################################################################################# #################################################################################################
@ -96,6 +98,8 @@ 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()
@ -213,11 +217,16 @@ class Service():
# or Kodi is shut down. # or Kodi is shut down.
while not monitor.abortRequested(): while not monitor.abortRequested():
if user.getServer() == False: server = user.getServer()
plexToken = utils.settings('plexToken')
if server == False:
# No server info set in add-on settings # No server info set in add-on settings
pass pass
elif user.getPublicUsers() == False: elif PlexAPI.PlexAPI().CheckConnection(
server,
plexToken
) != 200:
# Server is offline. # Server is offline.
# Alert the user and suppress future warning # Alert the user and suppress future warning
if self.server_online: if self.server_online: