Skip to content

Commit

Permalink
Now support Basic Auth Header for the login page
Browse files Browse the repository at this point in the history
  • Loading branch information
ukdtom committed Feb 15, 2016
1 parent 130391f commit 32d7d5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
Binary file modified Contents/Code/Docs/webtools-README_DEVS.odt
Binary file not shown.
7 changes: 6 additions & 1 deletion Contents/Code/plextvhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ def __init__(self):
self.myHeader['X-Plex-Platform'] = Platform.OS

# Login to Plex.tv
def login(self, req):
def login(self, user, pwd):
Log.Info('Start to auth towards plex.tv')

'''
user = req.get_argument('user', '')
if user == '':
Log.Error('Missing username')
Expand All @@ -41,6 +43,9 @@ def login(self, req):
req.set_status(412)
req.finish("<html><body>Missing password</body></html>")
return req
'''


# Got what we needed, so let's logon
authString = String.Base64Encode('%s:%s' % (user, pwd))
self.myHeader['Authorization'] = 'Basic ' + authString
Expand Down
34 changes: 29 additions & 5 deletions Contents/Code/webSrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,41 @@ def get(self):

def post(self):
global AUTHTOKEN

# Check for an auth header, in case a frontend wanted to use that
# Header has precedence compared to params
auth_header = self.request.headers.get('Authorization', None)
if auth_header is None or not auth_header.startswith('Basic '):
Log.Info('No Basic Auth header, so looking for params')
user = self.get_argument('user', '')
if user == '':
if plexTV().auth2myPlex():
Log.Info('Missing username')
self.clear()
self.set_status(412)
self.finish("<html><body>Missing username</body></html>")
pwd = self.get_argument('pwd', '')
if pwd == '':
Log.Info('Missing password')
self.clear()
self.set_status(412)
self.finish("<html><body>Missing password</body></html>")
else:
Log.Info('Auth header found')
auth_decoded = String.Base64Decode(auth_header[6:])
user, pwd = auth_decoded.split(':', 2)
Log.Info('User is: ' + user)
# Allow no password when in debug mode
if DEBUGMODE:
self.allow()
Log.Info('All is good, we are authenticated')
self.redirect('/')

# Let's start by checking if the server is online
if plexTV().auth2myPlex():
token = ''
try:
# Authenticate
retVal = plexTV().isServerOwner(plexTV().login(self))
retVal = plexTV().isServerOwner(plexTV().login(user, pwd))
self.clear()
if retVal == 0:
# All is good
Expand All @@ -140,6 +163,7 @@ def post(self):
Log.Critical('Unknown error, when authenticating')
self.set_status(403)
except Ex.HTTPError, e:
Log.Critical('Exception in Login: ' + str(e))
self.clear()
self.set_status(e.code)
self.finish(e)
Expand All @@ -149,16 +173,16 @@ def post(self):
# Server is offline
if Dict['password'] == '':
Log.Info('First local login, so we need to set the local password')
Dict['password'] = self.get_argument("pwd")
Dict['password'] = pwd
Dict['pwdset'] = True
Dict.Save
self.allow()
self.redirect('/')
elif Dict['password'] == self.get_argument("pwd"):
elif Dict['password'] == pwd:
self.allow()
Log.Info('Local password accepted')
self.redirect('/')
elif Dict['password'] != self.get_argument("pwd"):
elif Dict['password'] != pwd:
Log.Critical('Either local login failed, or PMS lost connection to plex.tv')
self.clear()
self.set_status(401)
Expand Down

0 comments on commit 32d7d5e

Please sign in to comment.