Prompt to login to plex.tv if no token available
This commit is contained in:
parent
3a514365ee
commit
a4dd5d8711
3 changed files with 89 additions and 68 deletions
|
@ -619,7 +619,7 @@ msgid "Invalid username or password"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
msgctxt "#33010"
|
msgctxt "#33010"
|
||||||
msgid "Failed to authenticate too many times. Reset in the settings."
|
msgid "Your Plex token is no longer valid. Logging-out of plex.tv"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
# Dialog before playback
|
# Dialog before playback
|
||||||
|
|
|
@ -61,6 +61,14 @@ class InitialSetup(object):
|
||||||
if self.plex_token:
|
if self.plex_token:
|
||||||
LOG.debug('Found a plex.tv token in the settings')
|
LOG.debug('Found a plex.tv token in the settings')
|
||||||
|
|
||||||
|
def write_credentials_to_settings(self):
|
||||||
|
"""
|
||||||
|
Writes Plex username, token to plex.tv and Plex id to PKC settings
|
||||||
|
"""
|
||||||
|
utils.settings('username', value=self.plex_login or '')
|
||||||
|
utils.settings('userid', value=self.plexid or '')
|
||||||
|
utils.settings('plexToken', value=self.plex_token or '')
|
||||||
|
|
||||||
def enter_new_pms_address(self):
|
def enter_new_pms_address(self):
|
||||||
# "Enter your Plex Media Server's IP or URL. Examples are:"
|
# "Enter your Plex Media Server's IP or URL. Examples are:"
|
||||||
utils.messageDialog(utils.lang(29999),
|
utils.messageDialog(utils.lang(29999),
|
||||||
|
|
|
@ -35,72 +35,6 @@ WINDOW_PROPERTIES = (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def authenticate():
|
|
||||||
"""
|
|
||||||
Authenticate the current user or prompt to log-in
|
|
||||||
|
|
||||||
Returns True if successful, False if not. 'aborted' if user chose to
|
|
||||||
abort
|
|
||||||
"""
|
|
||||||
LOG.info('Authenticating user')
|
|
||||||
if app.ACCOUNT.plex_username and not app.ACCOUNT.force_login:
|
|
||||||
# Found a user in the settings, try to authenticate
|
|
||||||
LOG.info('Trying to authenticate with old settings')
|
|
||||||
res = PF.check_connection(app.CONN.server,
|
|
||||||
token=app.ACCOUNT.pms_token,
|
|
||||||
verifySSL=app.CONN.verify_ssl_cert)
|
|
||||||
if res is False:
|
|
||||||
LOG.error('Something went wrong while checking connection')
|
|
||||||
return False
|
|
||||||
elif res == 401:
|
|
||||||
LOG.error('User token no longer valid. Sign user out')
|
|
||||||
app.ACCOUNT.clear()
|
|
||||||
return False
|
|
||||||
elif res >= 400:
|
|
||||||
LOG.error('Answer from PMS is not as expected')
|
|
||||||
return False
|
|
||||||
LOG.info('Successfully authenticated using old settings')
|
|
||||||
app.ACCOUNT.set_authenticated()
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Could not use settings - try to get Plex user list from plex.tv
|
|
||||||
if app.ACCOUNT.plex_token:
|
|
||||||
LOG.info("Trying to connect to plex.tv to get a user list")
|
|
||||||
user, _ = userselect.start()
|
|
||||||
if not user:
|
|
||||||
LOG.info('No user received')
|
|
||||||
app.CONN.pms_status = 'Stop'
|
|
||||||
return False
|
|
||||||
username = user.title
|
|
||||||
user_id = user.id
|
|
||||||
token = user.authToken
|
|
||||||
else:
|
|
||||||
LOG.info("Trying to authenticate without a token")
|
|
||||||
username = ''
|
|
||||||
user_id = ''
|
|
||||||
token = ''
|
|
||||||
res = PF.check_connection(app.CONN.server,
|
|
||||||
token=token,
|
|
||||||
verifySSL=app.CONN.verify_ssl_cert)
|
|
||||||
if res is False:
|
|
||||||
LOG.error('Something went wrong while checking connection')
|
|
||||||
return False
|
|
||||||
elif res == 401:
|
|
||||||
LOG.error('Token not valid')
|
|
||||||
return False
|
|
||||||
elif res >= 400:
|
|
||||||
LOG.error('Answer from PMS is not as expected')
|
|
||||||
return False
|
|
||||||
LOG.info('Successfully authenticated')
|
|
||||||
# Got new values that need to be saved
|
|
||||||
utils.settings('username', value=username)
|
|
||||||
utils.settings('userid', value=user_id)
|
|
||||||
utils.settings('accessToken', value=token)
|
|
||||||
app.ACCOUNT.load()
|
|
||||||
app.ACCOUNT.set_authenticated()
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class Service():
|
class Service():
|
||||||
ws = None
|
ws = None
|
||||||
sync = None
|
sync = None
|
||||||
|
@ -220,6 +154,85 @@ class Service():
|
||||||
LOG.info('Login to plex.tv')
|
LOG.info('Login to plex.tv')
|
||||||
return self.setup.plex_tv_sign_in()
|
return self.setup.plex_tv_sign_in()
|
||||||
|
|
||||||
|
def authenticate(self):
|
||||||
|
"""
|
||||||
|
Authenticate the current user or prompt to log-in
|
||||||
|
|
||||||
|
Returns True if successful, False if not. 'aborted' if user chose to
|
||||||
|
abort
|
||||||
|
"""
|
||||||
|
LOG.info('Authenticating user')
|
||||||
|
if app.ACCOUNT.plex_username and not app.ACCOUNT.force_login:
|
||||||
|
# Found a user in the settings, try to authenticate
|
||||||
|
LOG.info('Trying to authenticate with old settings')
|
||||||
|
res = PF.check_connection(app.CONN.server,
|
||||||
|
token=app.ACCOUNT.pms_token,
|
||||||
|
verifySSL=app.CONN.verify_ssl_cert)
|
||||||
|
if res is False:
|
||||||
|
LOG.error('Something went wrong while checking connection')
|
||||||
|
return False
|
||||||
|
elif res == 401:
|
||||||
|
LOG.error('User token no longer valid. Sign user out')
|
||||||
|
app.ACCOUNT.clear()
|
||||||
|
return False
|
||||||
|
elif res >= 400:
|
||||||
|
LOG.error('Answer from PMS is not as expected')
|
||||||
|
return False
|
||||||
|
LOG.info('Successfully authenticated using old settings')
|
||||||
|
app.ACCOUNT.set_authenticated()
|
||||||
|
return True
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# Could not use settings - try to get Plex user list from plex.tv
|
||||||
|
if app.ACCOUNT.plex_token:
|
||||||
|
LOG.info("Trying to connect to plex.tv to get a user list")
|
||||||
|
user, _ = userselect.start()
|
||||||
|
if not user:
|
||||||
|
LOG.info('No user received')
|
||||||
|
app.CONN.pms_status = 'Stop'
|
||||||
|
return False
|
||||||
|
username = user.title
|
||||||
|
user_id = user.id
|
||||||
|
token = user.authToken
|
||||||
|
else:
|
||||||
|
LOG.info("Trying to authenticate without a token")
|
||||||
|
username = ''
|
||||||
|
user_id = ''
|
||||||
|
token = ''
|
||||||
|
res = PF.check_connection(app.CONN.server,
|
||||||
|
token=token,
|
||||||
|
verifySSL=app.CONN.verify_ssl_cert)
|
||||||
|
if res is False:
|
||||||
|
LOG.error('Something went wrong while checking connection')
|
||||||
|
return False
|
||||||
|
elif res == 401:
|
||||||
|
if app.ACCOUNT.plex_token:
|
||||||
|
LOG.error('Token no longer valid')
|
||||||
|
# "Your Plex token is no longer valid. Logging-out of plex.tv"
|
||||||
|
utils.messageDialog(utils.lang(29999), utils.lang(33010))
|
||||||
|
app.ACCOUNT.clear()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
# "Failed to authenticate. Did you login to plex.tv?"
|
||||||
|
utils.messageDialog(utils.lang(29999), utils.lang(39023))
|
||||||
|
if self.setup.plex_tv_sign_in():
|
||||||
|
self.setup.write_credentials_to_settings()
|
||||||
|
app.ACCOUNT.load()
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
elif res >= 400:
|
||||||
|
LOG.error('Answer from PMS is not as expected')
|
||||||
|
return False
|
||||||
|
LOG.info('Successfully authenticated')
|
||||||
|
# Got new values that need to be saved
|
||||||
|
utils.settings('username', value=username)
|
||||||
|
utils.settings('userid', value=user_id)
|
||||||
|
utils.settings('accessToken', value=token)
|
||||||
|
app.ACCOUNT.load()
|
||||||
|
app.ACCOUNT.set_authenticated()
|
||||||
|
return True
|
||||||
|
|
||||||
def ServiceEntryPoint(self):
|
def ServiceEntryPoint(self):
|
||||||
# Important: Threads depending on abortRequest will not trigger
|
# Important: Threads depending on abortRequest will not trigger
|
||||||
# if profile switch happens more than once.
|
# if profile switch happens more than once.
|
||||||
|
@ -324,7 +337,7 @@ class Service():
|
||||||
if not app.ACCOUNT.authenticated:
|
if not app.ACCOUNT.authenticated:
|
||||||
LOG.info('Not yet authenticated')
|
LOG.info('Not yet authenticated')
|
||||||
# Do authentication
|
# Do authentication
|
||||||
if not authenticate():
|
if not self.authenticate():
|
||||||
continue
|
continue
|
||||||
# Start up events
|
# Start up events
|
||||||
if welcome_msg is True:
|
if welcome_msg is True:
|
||||||
|
|
Loading…
Reference in a new issue