2020-04-11 14:20:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# from flask import g
|
|
|
|
|
|
|
|
from flask_httpauth import HTTPBasicAuth
|
|
|
|
from itsdangerous import (TimedJSONWebSignatureSerializer
|
|
|
|
as Serializer, BadSignature, SignatureExpired)
|
|
|
|
from passlib.apps import custom_app_context as pwd_context
|
|
|
|
import uuid
|
|
|
|
from __main__ import config, app, g
|
2020-04-12 20:25:27 +00:00
|
|
|
from __main__ import auth_log as log
|
2020-04-25 16:20:46 +00:00
|
|
|
from jellyfin_accounts.jf_api import Jellyfin
|
|
|
|
from jellyfin_accounts.web_api import jf
|
2020-04-11 14:20:25 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:20:46 +00:00
|
|
|
def tempJF():
|
|
|
|
return Jellyfin(config['jellyfin']['server'],
|
|
|
|
config['jellyfin']['client'],
|
|
|
|
config['jellyfin']['version'],
|
|
|
|
config['jellyfin']['device'] + '_temp',
|
|
|
|
config['jellyfin']['device_id'] + '_temp')
|
|
|
|
|
2020-04-11 14:20:25 +00:00
|
|
|
class Account():
|
2020-04-25 16:20:46 +00:00
|
|
|
def __init__(self, username=None, password=None):
|
2020-04-11 14:20:25 +00:00
|
|
|
self.username = username
|
2020-04-25 16:20:46 +00:00
|
|
|
if password is not None:
|
|
|
|
self.password_hash = pwd_context.hash(password)
|
|
|
|
self.id = str(uuid.uuid4())
|
|
|
|
self.jf = False
|
|
|
|
elif username is not None:
|
|
|
|
jf.authenticate(config['jellyfin']['username'],
|
|
|
|
config['jellyfin']['password'])
|
|
|
|
self.id = jf.getUsers(self.username, public=False)['Id']
|
|
|
|
self.jf = True
|
2020-04-11 14:20:25 +00:00
|
|
|
def verify_password(self, password):
|
2020-04-25 16:20:46 +00:00
|
|
|
if not self.jf:
|
|
|
|
return pwd_context.verify(password, self.password_hash)
|
|
|
|
else:
|
|
|
|
temp_jf = tempJF()
|
|
|
|
try:
|
|
|
|
return temp_jf.authenticate(self.username, password)
|
|
|
|
except Jellyfin.AuthenticationError:
|
|
|
|
return False
|
2020-04-11 14:20:25 +00:00
|
|
|
def generate_token(self, expiration=1200):
|
|
|
|
s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
|
2020-04-25 16:20:46 +00:00
|
|
|
log.debug(self.id)
|
2020-04-11 14:20:25 +00:00
|
|
|
return s.dumps({ 'id': self.id })
|
|
|
|
|
|
|
|
@staticmethod
|
2020-04-25 16:20:46 +00:00
|
|
|
def verify_token(token, accounts):
|
|
|
|
log.debug(f'verifying token {token}')
|
2020-04-11 14:20:25 +00:00
|
|
|
s = Serializer(app.config['SECRET_KEY'])
|
|
|
|
try:
|
|
|
|
data = s.loads(token)
|
|
|
|
except SignatureExpired:
|
|
|
|
return None
|
|
|
|
except BadSignature:
|
|
|
|
return None
|
2020-04-25 16:20:46 +00:00
|
|
|
if config.getboolean('ui', 'jellyfin_login'):
|
|
|
|
for account in accounts:
|
|
|
|
if data['id'] == accounts[account].id:
|
|
|
|
return account
|
|
|
|
else:
|
|
|
|
return accounts['adminAccount']
|
|
|
|
|
|
|
|
|
2020-04-11 14:20:25 +00:00
|
|
|
|
|
|
|
auth = HTTPBasicAuth()
|
|
|
|
|
2020-04-25 16:20:46 +00:00
|
|
|
accounts = {}
|
2020-04-11 14:20:25 +00:00
|
|
|
|
2020-04-26 18:44:31 +00:00
|
|
|
if config.getboolean('ui', 'jellyfin_login'):
|
|
|
|
log.debug('Using jellyfin for admin authentication')
|
|
|
|
else:
|
|
|
|
log.debug('Using configured login details for admin authentication')
|
2020-04-25 16:20:46 +00:00
|
|
|
accounts['adminAccount'] = Account(config['ui']['username'],
|
|
|
|
config['ui']['password'])
|
2020-04-11 14:20:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth.verify_password
|
|
|
|
def verify_password(username, password):
|
2020-04-25 16:20:46 +00:00
|
|
|
user = None
|
|
|
|
verified = False
|
|
|
|
log.debug('Verifying auth')
|
|
|
|
if config.getboolean('ui', 'jellyfin_login'):
|
|
|
|
try:
|
|
|
|
jf_user = jf.getUsers(username, public=False)
|
|
|
|
id = jf_user['Id']
|
|
|
|
user = accounts[id]
|
|
|
|
except KeyError:
|
|
|
|
if config.getboolean('ui', 'admin_only'):
|
|
|
|
if jf_user['Policy']['IsAdministrator']:
|
|
|
|
user = Account(username)
|
|
|
|
accounts[id] = user
|
|
|
|
else:
|
|
|
|
log.debug(f'User {username} not admin.')
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
user = Account(username)
|
|
|
|
accounts[id] = user
|
|
|
|
except Jellyfin.UserNotFoundError:
|
|
|
|
user = Account().verify_token(username, accounts)
|
|
|
|
if user:
|
|
|
|
verified = True
|
|
|
|
if not user:
|
|
|
|
log.debug(f'User {username} not found on Jellyfin')
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
user = accounts['adminAccount']
|
|
|
|
verified = Account().verify_token(username, accounts)
|
|
|
|
|
|
|
|
if not verified:
|
|
|
|
if username == user.username and user.verify_password(password):
|
|
|
|
g.user = user
|
2020-04-12 20:25:27 +00:00
|
|
|
log.debug("HTTPAuth Allowed")
|
2020-04-11 14:20:25 +00:00
|
|
|
return True
|
|
|
|
else:
|
2020-04-12 20:25:27 +00:00
|
|
|
log.debug("HTTPAuth Denied")
|
2020-04-11 14:20:25 +00:00
|
|
|
return False
|
2020-04-25 16:20:46 +00:00
|
|
|
g.user = user
|
2020-04-12 20:25:27 +00:00
|
|
|
log.debug("HTTPAuth Allowed")
|
2020-04-11 14:20:25 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|