2016-03-31 18:40:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
import xbmcvfs
|
|
|
|
|
|
|
|
import artwork
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
2016-06-16 05:43:36 +00:00
|
|
|
from utils import Logging, window, settings, language as lang
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class UserClient(threading.Thread):
|
|
|
|
|
|
|
|
# Borg - multiple instances, shared state
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
stopClient = False
|
|
|
|
auth = True
|
|
|
|
retry = 0
|
|
|
|
|
|
|
|
currUser = None
|
|
|
|
currUserId = None
|
|
|
|
currServer = None
|
|
|
|
currToken = None
|
|
|
|
HasAccess = True
|
|
|
|
AdditionalUser = []
|
|
|
|
|
|
|
|
userSettings = None
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2016-06-16 05:43:36 +00:00
|
|
|
global log
|
|
|
|
log = Logging(self.__class__.__name__).log
|
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
self.addon = xbmcaddon.Addon()
|
|
|
|
|
|
|
|
self.addonName = clientinfo.ClientInfo().getAddonName()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils()
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
|
|
|
|
def getAdditionalUsers(self):
|
|
|
|
|
2016-06-16 05:43:36 +00:00
|
|
|
additionalUsers = settings('additionalUsers')
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
if additionalUsers:
|
|
|
|
self.AdditionalUser = additionalUsers.split(',')
|
|
|
|
|
|
|
|
def getUsername(self):
|
|
|
|
|
2016-06-16 05:43:36 +00:00
|
|
|
username = settings('username')
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
if not username:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No username saved.", 2)
|
2016-03-31 18:40:47 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
return username
|
|
|
|
|
|
|
|
def getLogLevel(self):
|
|
|
|
|
|
|
|
try:
|
2016-06-16 05:43:36 +00:00
|
|
|
logLevel = int(settings('logLevel'))
|
2016-03-31 18:40:47 +00:00
|
|
|
except ValueError:
|
|
|
|
logLevel = 0
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
return logLevel
|
|
|
|
|
|
|
|
def getUserId(self):
|
|
|
|
|
|
|
|
username = self.getUsername()
|
|
|
|
w_userId = window('emby_currUser')
|
|
|
|
s_userId = settings('userId%s' % username)
|
|
|
|
|
|
|
|
# Verify the window property
|
|
|
|
if w_userId:
|
|
|
|
if not s_userId:
|
|
|
|
# Save access token if it's missing from settings
|
|
|
|
settings('userId%s' % username, value=w_userId)
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Returning userId from WINDOW for username: %s UserId: %s"
|
2016-03-31 18:40:47 +00:00
|
|
|
% (username, w_userId), 2)
|
|
|
|
return w_userId
|
|
|
|
# Verify the settings
|
|
|
|
elif s_userId:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Returning userId from SETTINGS for username: %s userId: %s"
|
2016-03-31 18:40:47 +00:00
|
|
|
% (username, s_userId), 2)
|
|
|
|
return s_userId
|
|
|
|
# No userId found
|
|
|
|
else:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No userId saved for username: %s." % username, 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
def getServer(self, prefix=True):
|
|
|
|
|
|
|
|
alternate = settings('altip') == "true"
|
|
|
|
if alternate:
|
|
|
|
# Alternate host
|
|
|
|
HTTPS = settings('secondhttps') == "true"
|
|
|
|
host = settings('secondipaddress')
|
|
|
|
port = settings('secondport')
|
|
|
|
else:
|
|
|
|
# Original host
|
|
|
|
HTTPS = settings('https') == "true"
|
|
|
|
host = settings('ipaddress')
|
|
|
|
port = settings('port')
|
|
|
|
|
|
|
|
server = host + ":" + port
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
if not host:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No server information saved.", 2)
|
2016-03-31 18:40:47 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# If https is true
|
|
|
|
if prefix and HTTPS:
|
|
|
|
server = "https://%s" % server
|
|
|
|
return server
|
|
|
|
# If https is false
|
|
|
|
elif prefix and not HTTPS:
|
|
|
|
server = "http://%s" % server
|
|
|
|
return server
|
|
|
|
# If only the host:port is required
|
|
|
|
elif not prefix:
|
|
|
|
return server
|
|
|
|
|
|
|
|
def getToken(self):
|
|
|
|
|
|
|
|
username = self.getUsername()
|
|
|
|
userId = self.getUserId()
|
|
|
|
w_token = window('emby_accessToken%s' % userId)
|
|
|
|
s_token = settings('accessToken')
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
# Verify the window property
|
|
|
|
if w_token:
|
|
|
|
if not s_token:
|
|
|
|
# Save access token if it's missing from settings
|
|
|
|
settings('accessToken', value=w_token)
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Returning accessToken from WINDOW for username: %s accessToken: %s"
|
2016-03-31 18:40:47 +00:00
|
|
|
% (username, w_token), 2)
|
|
|
|
return w_token
|
|
|
|
# Verify the settings
|
|
|
|
elif s_token:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Returning accessToken from SETTINGS for username: %s accessToken: %s"
|
2016-03-31 18:40:47 +00:00
|
|
|
% (username, s_token), 2)
|
|
|
|
window('emby_accessToken%s' % username, value=s_token)
|
|
|
|
return s_token
|
|
|
|
else:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No token found.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
def getSSLverify(self):
|
|
|
|
# Verify host certificate
|
|
|
|
s_sslverify = settings('sslverify')
|
|
|
|
if settings('altip') == "true":
|
|
|
|
s_sslverify = settings('secondsslverify')
|
|
|
|
|
|
|
|
if s_sslverify == "true":
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getSSL(self):
|
|
|
|
# Client side certificate
|
|
|
|
s_cert = settings('sslcert')
|
|
|
|
if settings('altip') == "true":
|
|
|
|
s_cert = settings('secondsslcert')
|
|
|
|
|
|
|
|
if s_cert == "None":
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return s_cert
|
|
|
|
|
|
|
|
def setUserPref(self):
|
|
|
|
|
|
|
|
doUtils = self.doUtils.downloadUrl
|
|
|
|
|
2016-03-31 18:55:25 +00:00
|
|
|
result = doUtils("{server}/emby/Users/{UserId}?format=json")
|
2016-03-31 18:40:47 +00:00
|
|
|
self.userSettings = result
|
|
|
|
# Set user image for skin display
|
|
|
|
if result.get('PrimaryImageTag'):
|
2016-06-16 05:43:36 +00:00
|
|
|
window('EmbyUserImage', value=artwork.Artwork().getUserArtwork(result['Id'], 'Primary'))
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
# Set resume point max
|
2016-03-31 18:55:25 +00:00
|
|
|
result = doUtils("{server}/emby/System/Configuration?format=json")
|
2016-06-16 05:43:36 +00:00
|
|
|
settings('markPlayed', value=str(result['MaxResumePct']))
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
def getPublicUsers(self):
|
|
|
|
# Get public Users
|
2016-06-16 05:43:36 +00:00
|
|
|
url = "%s/emby/Users/Public?format=json" % self.getServer()
|
|
|
|
result = self.doUtils.downloadUrl(url, authenticate=False)
|
2016-03-31 18:40:47 +00:00
|
|
|
if result != "":
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
# Server connection failed
|
|
|
|
return False
|
|
|
|
|
2016-03-31 18:55:25 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
def hasAccess(self):
|
|
|
|
# hasAccess is verified in service.py
|
2016-03-31 18:55:25 +00:00
|
|
|
result = self.doUtils.downloadUrl("{server}/emby/Users?format=json")
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
if result == False:
|
|
|
|
# Access is restricted, set in downloadutils.py via exception
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Access is restricted.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.HasAccess = False
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
elif window('emby_online') != "true":
|
|
|
|
# Server connection failed
|
|
|
|
pass
|
|
|
|
|
|
|
|
elif window('emby_serverStatus') == "restricted":
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Access is granted.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.HasAccess = True
|
|
|
|
window('emby_serverStatus', clear=True)
|
2016-06-21 01:57:29 +00:00
|
|
|
xbmcgui.Dialog().notification(lang(29999), lang(33007))
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
def loadCurrUser(self, authenticated=False):
|
|
|
|
|
|
|
|
doUtils = self.doUtils
|
|
|
|
username = self.getUsername()
|
|
|
|
userId = self.getUserId()
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
# Only to be used if token exists
|
|
|
|
self.currUserId = userId
|
|
|
|
self.currServer = self.getServer()
|
|
|
|
self.currToken = self.getToken()
|
|
|
|
self.ssl = self.getSSLverify()
|
|
|
|
self.sslcert = self.getSSL()
|
|
|
|
|
|
|
|
# Test the validity of current token
|
|
|
|
if authenticated == False:
|
|
|
|
url = "%s/emby/Users/%s?format=json" % (self.currServer, userId)
|
|
|
|
window('emby_currUser', value=userId)
|
|
|
|
window('emby_accessToken%s' % userId, value=self.currToken)
|
|
|
|
result = doUtils.downloadUrl(url)
|
|
|
|
|
|
|
|
if result == 401:
|
|
|
|
# Token is no longer valid
|
|
|
|
self.resetClient()
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Set to windows property
|
|
|
|
window('emby_currUser', value=userId)
|
|
|
|
window('emby_accessToken%s' % userId, value=self.currToken)
|
|
|
|
window('emby_server%s' % userId, value=self.currServer)
|
|
|
|
window('emby_server_%s' % userId, value=self.getServer(prefix=False))
|
|
|
|
|
|
|
|
# Set DownloadUtils values
|
|
|
|
doUtils.setUsername(username)
|
|
|
|
doUtils.setUserId(self.currUserId)
|
|
|
|
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()
|
|
|
|
self.getAdditionalUsers()
|
|
|
|
# Set user preferences in settings
|
|
|
|
self.currUser = username
|
|
|
|
self.setUserPref()
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
def authenticate(self):
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
dialog = xbmcgui.Dialog()
|
|
|
|
|
|
|
|
# Get /profile/addon_data
|
|
|
|
addondir = xbmc.translatePath(self.addon.getAddonInfo('profile')).decode('utf-8')
|
|
|
|
hasSettings = xbmcvfs.exists("%ssettings.xml" % addondir)
|
|
|
|
|
|
|
|
username = self.getUsername()
|
|
|
|
server = self.getServer()
|
|
|
|
|
|
|
|
# If there's no settings.xml
|
|
|
|
if not hasSettings:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No settings.xml found.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.auth = False
|
|
|
|
return
|
|
|
|
# If no user information
|
|
|
|
elif not server or not username:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Missing server information.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.auth = False
|
|
|
|
return
|
|
|
|
# If there's a token, load the user
|
|
|
|
elif self.getToken():
|
|
|
|
result = self.loadCurrUser()
|
|
|
|
|
2016-03-31 19:39:32 +00:00
|
|
|
if result is False:
|
2016-03-31 18:40:47 +00:00
|
|
|
pass
|
|
|
|
else:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Current user: %s" % self.currUser, 1)
|
|
|
|
log("Current userId: %s" % self.currUserId, 1)
|
|
|
|
log("Current accessToken: %s" % self.currToken, 2)
|
2016-03-31 18:40:47 +00:00
|
|
|
return
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
##### AUTHENTICATE USER #####
|
|
|
|
|
|
|
|
users = self.getPublicUsers()
|
|
|
|
password = ""
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
# 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 = dialog.input(
|
|
|
|
heading="%s %s" % (lang(33008), username.decode('utf-8')),
|
|
|
|
option=xbmcgui.ALPHANUM_HIDE_INPUT)
|
|
|
|
# If password dialog is cancelled
|
|
|
|
if not password:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("No password entered.", 0)
|
2016-03-31 18:40:47 +00:00
|
|
|
window('emby_serverStatus', value="Stop")
|
|
|
|
self.auth = False
|
|
|
|
return
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# Manual login, user is hidden
|
|
|
|
password = dialog.input(
|
|
|
|
heading="%s %s" % (lang(33008), username),
|
|
|
|
option=xbmcgui.ALPHANUM_HIDE_INPUT)
|
|
|
|
sha1 = hashlib.sha1(password)
|
2016-04-03 20:51:26 +00:00
|
|
|
sha1 = sha1.hexdigest()
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
# Authenticate username and password
|
|
|
|
data = {'username': username, 'password': sha1}
|
2016-06-16 05:43:36 +00:00
|
|
|
log(data, 2)
|
2016-03-31 18:40:47 +00:00
|
|
|
|
2016-06-16 05:43:36 +00:00
|
|
|
url = "%s/emby/Users/AuthenticateByName?format=json" % server
|
|
|
|
result = self.doUtils.downloadUrl(url, postBody=data, action_type="POST", authenticate=False)
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
try:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Auth response: %s" % result, 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
accessToken = result['AccessToken']
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
except (KeyError, TypeError):
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Failed to retrieve the api key.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
accessToken = None
|
|
|
|
|
|
|
|
if accessToken is not None:
|
|
|
|
self.currUser = username
|
2016-06-21 01:57:29 +00:00
|
|
|
dialog.notification(lang(29999),
|
2016-03-31 18:40:47 +00:00
|
|
|
"%s %s!" % (lang(33000), self.currUser.decode('utf-8')))
|
|
|
|
settings('accessToken', value=accessToken)
|
2016-03-31 19:39:32 +00:00
|
|
|
settings('userId%s' % username, value=result['User']['Id'])
|
2016-06-16 05:43:36 +00:00
|
|
|
log("User Authenticated: %s" % accessToken, 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.loadCurrUser(authenticated=True)
|
|
|
|
window('emby_serverStatus', clear=True)
|
|
|
|
self.retry = 0
|
|
|
|
else:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("User authentication failed.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
settings('accessToken', value="")
|
|
|
|
settings('userId%s' % username, value="")
|
|
|
|
dialog.ok(lang(33001), lang(33009))
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
# Give two attempts at entering password
|
|
|
|
if self.retry == 2:
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Too many retries. "
|
2016-03-31 18:40:47 +00:00
|
|
|
"You can retry by resetting attempts in the addon settings.", 1)
|
|
|
|
window('emby_serverStatus', value="Stop")
|
|
|
|
dialog.ok(lang(33001), lang(33010))
|
|
|
|
|
|
|
|
self.retry += 1
|
|
|
|
self.auth = False
|
|
|
|
|
|
|
|
def resetClient(self):
|
|
|
|
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Reset UserClient authentication.", 1)
|
2016-03-31 18:40:47 +00:00
|
|
|
if self.currToken is not None:
|
|
|
|
# In case of 401, removed saved token
|
2016-06-16 05:43:36 +00:00
|
|
|
settings('accessToken', value="")
|
|
|
|
window('emby_accessToken%s' % self.getUserId(), clear=True)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.currToken = None
|
2016-06-16 05:43:36 +00:00
|
|
|
log("User token has been removed.", 1)
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
self.auth = True
|
|
|
|
self.currUser = None
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
def run(self):
|
|
|
|
|
|
|
|
monitor = xbmc.Monitor()
|
2016-06-16 05:43:36 +00:00
|
|
|
log("----===## Starting UserClient ##===----", 0)
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
while not monitor.abortRequested():
|
|
|
|
|
|
|
|
status = window('emby_serverStatus')
|
|
|
|
if status:
|
|
|
|
# Verify the connection status to server
|
|
|
|
if status == "restricted":
|
|
|
|
# Parental control is restricting access
|
|
|
|
self.HasAccess = False
|
|
|
|
|
|
|
|
elif status == "401":
|
|
|
|
# Unauthorized access, revoke token
|
|
|
|
window('emby_serverStatus', value="Auth")
|
|
|
|
self.resetClient()
|
|
|
|
|
|
|
|
if self.auth and (self.currUser is None):
|
|
|
|
# Try to authenticate user
|
|
|
|
status = window('emby_serverStatus')
|
|
|
|
if not status or status == "Auth":
|
|
|
|
# Set auth flag because we no longer need
|
|
|
|
# to authenticate the user
|
|
|
|
self.auth = False
|
|
|
|
self.authenticate()
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
if not self.auth and (self.currUser is None):
|
|
|
|
# If authenticate failed.
|
|
|
|
server = self.getServer()
|
|
|
|
username = self.getUsername()
|
|
|
|
status = window('emby_serverStatus')
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
# The status Stop is for when user cancelled password dialog.
|
|
|
|
if server and username and status != "Stop":
|
|
|
|
# Only if there's information found to login
|
2016-06-16 05:43:36 +00:00
|
|
|
log("Server found: %s" % server, 2)
|
|
|
|
log("Username found: %s" % username, 2)
|
2016-03-31 18:40:47 +00:00
|
|
|
self.auth = True
|
|
|
|
|
|
|
|
|
|
|
|
if self.stopClient == True:
|
|
|
|
# If stopping the client didn't work
|
|
|
|
break
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
if monitor.waitForAbort(1):
|
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
break
|
2016-04-03 20:51:26 +00:00
|
|
|
|
2016-03-31 18:40:47 +00:00
|
|
|
self.doUtils.stopSession()
|
2016-06-16 05:43:36 +00:00
|
|
|
log("##===---- UserClient Stopped ----===##", 0)
|
2016-03-31 18:40:47 +00:00
|
|
|
|
|
|
|
def stopClient(self):
|
|
|
|
# When emby for kodi terminates
|
2015-12-24 20:07:00 +00:00
|
|
|
self.stopClient = True
|