Reworked the userclient with possibilities

Revoked token was actually broken, handle access schedule better,
restart/start properly. This is to stabilize things.
This commit is contained in:
angelblue05 2015-05-13 23:48:35 -05:00
parent 696586e952
commit 11e4a70e25
4 changed files with 51 additions and 21 deletions

View file

@ -124,7 +124,6 @@ class DownloadUtils():
self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
self.logMsg("Requests session started on: %s" % self.server)
self.postCapabilities(self.deviceId)
def imageUrl(self, id, type, index, width, height):
# To move to API.py
@ -279,13 +278,14 @@ class DownloadUtils():
# Unauthorized
status = WINDOW.getProperty("Server_status")
if 'x-application-error-code' in r.headers:
if r.headers['X-Application-Error-Code'] == "ParentalControl":
# Parental control - access restricted
WINDOW.setProperty("Server_status", "restricted")
xbmcgui.Dialog().notification("Emby server", "Access restricted.", xbmcgui.NOTIFICATION_ERROR, time=5000)
return False
elif (status == "401") or (status == "Auth"):
if (status == "401") or (status == "Auth"):
pass
else:
@ -293,6 +293,7 @@ class DownloadUtils():
WINDOW.setProperty("Server_status", "401")
self.logMsg("HTTP Error: %s" % e, 0)
xbmcgui.Dialog().notification("Error connecting", "Unauthorized.", xbmcgui.NOTIFICATION_ERROR)
return 401
elif (r.status_code == 301) or (r.status_code == 302):
# Redirects

View file

@ -178,6 +178,9 @@ class UserClient(threading.Thread):
self.logMsg("Access is restricted.")
self.HasAccess = False
return
elif self.WINDOW.getProperty('Server_online') != "true":
# Server connection failed
return
if self.WINDOW.getProperty("Server_status") == "restricted":
self.logMsg("Access is granted.")
@ -186,7 +189,7 @@ class UserClient(threading.Thread):
xbmcgui.Dialog().notification("Emby server", "Access is enabled.")
return
def loadCurrUser(self):
def loadCurrUser(self, authenticated=False):
WINDOW = self.WINDOW
doUtils = self.doUtils
@ -199,6 +202,17 @@ class UserClient(threading.Thread):
self.ssl = self.getSSLverify()
self.sslcert = self.getSSL()
# Test the validity of current token
if authenticated == False:
url = "%s/mediabrowser/Users/%s" % (self.currServer, self.currUserId)
WINDOW.setProperty("currUser", username)
WINDOW.setProperty("accessToken%s" % username, self.currToken)
result = doUtils.downloadUrl(url, type="POST")
if result == 401:
# Token is no longer valid
self.resetClient()
return False
# Set to windows property
WINDOW.setProperty("currUser", username)
WINDOW.setProperty("accessToken%s" % username, self.currToken)
@ -212,6 +226,8 @@ class UserClient(threading.Thread):
doUtils.setServer(self.currServer)
doUtils.setToken(self.currToken)
doUtils.setSSL(self.ssl, self.sslcert)
# parental control - let's verify if access is restricted
self.hasAccess()
# Start DownloadUtils session
doUtils.startSession()
@ -239,12 +255,14 @@ class UserClient(threading.Thread):
return
# If there's a token
if (self.getToken() != ""):
self.loadCurrUser()
result = self.loadCurrUser()
if result == False:
pass
else:
self.logMsg("Current user: %s" % self.currUser, 0)
self.logMsg("Current userId: %s" % self.currUserId, 0)
self.logMsg("Current accessToken: %s" % self.currToken, 0)
# parental control - let's verify if access is restricted
self.hasAccess()
return
users = self.getPublicUsers()
@ -292,11 +310,12 @@ class UserClient(threading.Thread):
if (result != None and accessToken != None):
self.currUser = username
xbmcgui.Dialog().notification("Emby server", "Welcome %s!" % self.currUser)
userId = result[u'User'][u'Id']
addon.setSetting("accessToken", accessToken)
addon.setSetting("userId%s" % username, userId)
self.logMsg("User Authenticated: %s" % accessToken)
self.loadCurrUser()
self.loadCurrUser(authenticated=True)
self.WINDOW.setProperty("Server_status", "")
self.retry = 0
return
@ -318,10 +337,12 @@ class UserClient(threading.Thread):
def resetClient(self):
username = self.getUsername()
self.logMsg("Reset UserClient authentication.", 1)
if (self.currToken != None):
# In case of 401, removed saved token
self.addon.setSetting("accessToken%s" % self.currUser, "")
self.WINDOW.setProperty("accessToken%s" % self.currUser, "")
self.addon.setSetting("accessToken", "")
self.WINDOW.setProperty("accessToken%s" % username, "")
self.currToken = None
self.logMsg("User token has been removed.", 1)

View file

@ -28,6 +28,7 @@ class WebSocketThread(threading.Thread):
_shared_state = {}
doUtils = DownloadUtils()
clientInfo = ClientInformation()
KodiMonitor = KodiMonitor.Kodi_Monitor()
addonName = clientInfo.getAddonName()
@ -258,9 +259,13 @@ class WebSocketThread(threading.Thread):
on_close = self.on_close)
self.client.on_open = self.on_open
self.doUtils.postCapabilities(deviceId)
while not self.KodiMonitor.abortRequested():
if WINDOW.getProperty("Server_online") == "true":
# Server came back online, repost capabilities
self.doUtils.postCapabilities(deviceId)
self.client.run_forever()
if (self.keepRunning):

View file

@ -143,7 +143,6 @@ class Service():
if self.KodiMonitor.waitForAbort(1):
# Abort was requested while waiting. We should exit
break
#WebSocketThread().processPendingActions()
else:
if self.warn_auth:
@ -155,6 +154,10 @@ class Service():
WINDOW.setProperty("Emby_Service_Timestamp", str(int(time.time())))
user.hasAccess()
if WINDOW.getProperty('Server_online') != "true":
# Server went offline
break
if self.KodiMonitor.waitForAbort(5):
# Abort was requested while waiting. We should exit
break