2020-06-16 19:07:47 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-06-27 14:52:03 +00:00
|
|
|
__version__ = "0.2.2"
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
import secrets
|
|
|
|
import configparser
|
|
|
|
import shutil
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
from flask import Flask, g
|
|
|
|
from jellyfin_accounts.data_store import JSONStorage
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
parser = argparse.ArgumentParser(description="jellyfin-accounts")
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
parser.add_argument("-c", "--config", help="specifies path to configuration file.")
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--data",
|
|
|
|
help=("specifies directory to store data in. " + "defaults to ~/.jf-accounts."),
|
|
|
|
)
|
|
|
|
parser.add_argument("--host", help="address to host web ui on.")
|
|
|
|
parser.add_argument("-p", "--port", help="port to host web ui on.")
|
|
|
|
parser.add_argument(
|
|
|
|
"-g",
|
|
|
|
"--get_defaults",
|
|
|
|
help=(
|
|
|
|
"tool to grab a JF users "
|
|
|
|
+ "policy (access, perms, etc.) and "
|
|
|
|
+ "homescreen layout and "
|
|
|
|
+ "output it as json to be used as a user template."
|
|
|
|
),
|
|
|
|
action="store_true",
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
args, leftovers = parser.parse_known_args()
|
|
|
|
|
|
|
|
if args.data is not None:
|
|
|
|
data_dir = Path(args.data)
|
|
|
|
else:
|
2020-06-21 19:29:53 +00:00
|
|
|
data_dir = Path.home() / ".jf-accounts"
|
2020-06-16 19:07:47 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
local_dir = (Path(__file__).parent / "data").resolve()
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
first_run = False
|
2020-06-21 19:29:53 +00:00
|
|
|
if data_dir.exists() is False or (data_dir / "config.ini").exists() is False:
|
2020-06-16 19:07:47 +00:00
|
|
|
if not data_dir.exists():
|
|
|
|
Path.mkdir(data_dir)
|
2020-06-21 19:29:53 +00:00
|
|
|
print(f"Config dir not found, so created at {str(data_dir)}")
|
2020-06-16 19:07:47 +00:00
|
|
|
if args.config is None:
|
2020-06-21 19:29:53 +00:00
|
|
|
config_path = data_dir / "config.ini"
|
|
|
|
shutil.copy(str(local_dir / "config-default.ini"), str(config_path))
|
2020-06-16 19:07:47 +00:00
|
|
|
print("Setup through the web UI, or quit and edit the configuration manually.")
|
|
|
|
first_run = True
|
|
|
|
else:
|
|
|
|
config_path = Path(args.config)
|
2020-06-21 19:29:53 +00:00
|
|
|
print(f"config.ini can be found at {str(config_path)}")
|
2020-06-16 19:07:47 +00:00
|
|
|
else:
|
2020-06-21 19:29:53 +00:00
|
|
|
config_path = data_dir / "config.ini"
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
config = configparser.RawConfigParser()
|
|
|
|
config.read(config_path)
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
def create_log(name):
|
|
|
|
log = logging.getLogger(name)
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
2020-06-21 19:29:53 +00:00
|
|
|
if config.getboolean("ui", "debug"):
|
2020-06-16 19:07:47 +00:00
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
handler.setLevel(logging.DEBUG)
|
|
|
|
else:
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
handler.setLevel(logging.INFO)
|
2020-06-21 19:29:53 +00:00
|
|
|
fmt = " %(name)s - %(levelname)s - %(message)s"
|
2020-06-16 19:07:47 +00:00
|
|
|
format = logging.Formatter(fmt)
|
|
|
|
handler.setFormatter(format)
|
|
|
|
log.addHandler(handler)
|
|
|
|
log.propagate = False
|
|
|
|
return log
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
|
|
|
|
log = create_log("main")
|
|
|
|
web_log = create_log("waitress")
|
2020-06-16 19:07:47 +00:00
|
|
|
if not first_run:
|
2020-06-21 19:29:53 +00:00
|
|
|
email_log = create_log("emails")
|
|
|
|
auth_log = create_log("auth")
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
if args.host is not None:
|
2020-06-21 19:29:53 +00:00
|
|
|
log.debug(f"Using specified host {args.host}")
|
|
|
|
config["ui"]["host"] = args.host
|
2020-06-16 19:07:47 +00:00
|
|
|
if args.port is not None:
|
2020-06-21 19:29:53 +00:00
|
|
|
log.debug(f"Using specified port {args.port}")
|
|
|
|
config["ui"]["port"] = args.port
|
2020-06-16 19:07:47 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
for key in config["files"]:
|
|
|
|
if config["files"][key] == "":
|
|
|
|
if key != "custom_css":
|
|
|
|
log.debug(f"Using default {key}")
|
|
|
|
config["files"][key] = str(data_dir / (key + ".json"))
|
2020-06-16 19:07:47 +00:00
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
for key in ["user_configuration", "user_displayprefs"]:
|
|
|
|
if key not in config["files"]:
|
|
|
|
log.debug(f"Using default {key}")
|
|
|
|
config["files"][key] = str(data_dir / (key + ".json"))
|
2020-06-16 19:07:47 +00:00
|
|
|
|
2020-06-28 23:35:51 +00:00
|
|
|
if "no_username" not in config["email"]:
|
|
|
|
config["email"]["no_username"] = "false"
|
|
|
|
log.debug("Set no_username to false")
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
with open(config["files"]["invites"], "r") as f:
|
2020-06-16 19:07:47 +00:00
|
|
|
temp_invites = json.load(f)
|
2020-06-21 19:29:53 +00:00
|
|
|
if "invites" in temp_invites:
|
2020-06-16 19:07:47 +00:00
|
|
|
new_invites = {}
|
2020-06-21 19:29:53 +00:00
|
|
|
log.info("Converting invites.json to new format, temporary.")
|
|
|
|
for el in temp_invites["invites"]:
|
|
|
|
i = {"valid_till": el["valid_till"]}
|
|
|
|
if "email" in el:
|
|
|
|
i["email"] = el["email"]
|
|
|
|
new_invites[el["code"]] = i
|
|
|
|
with open(config["files"]["invites"], "w") as f:
|
2020-06-16 19:07:47 +00:00
|
|
|
f.write(json.dumps(new_invites, indent=4, default=str))
|
|
|
|
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
data_store = JSONStorage(
|
|
|
|
config["files"]["emails"],
|
|
|
|
config["files"]["invites"],
|
|
|
|
config["files"]["user_template"],
|
|
|
|
config["files"]["user_displayprefs"],
|
|
|
|
config["files"]["user_configuration"],
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def default_css():
|
|
|
|
css = {}
|
2020-06-21 19:29:53 +00:00
|
|
|
css[
|
|
|
|
"href"
|
|
|
|
] = "https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
|
|
|
|
css[
|
|
|
|
"integrity"
|
|
|
|
] = "sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
|
|
|
|
css["crossorigin"] = "anonymous"
|
2020-06-16 19:07:47 +00:00
|
|
|
return css
|
|
|
|
|
|
|
|
|
|
|
|
css = {}
|
|
|
|
css = default_css()
|
2020-06-21 19:29:53 +00:00
|
|
|
if "custom_css" in config["files"]:
|
|
|
|
if config["files"]["custom_css"] != "":
|
2020-06-16 19:07:47 +00:00
|
|
|
try:
|
2020-06-21 19:29:53 +00:00
|
|
|
shutil.copy(
|
|
|
|
config["files"]["custom_css"], (local_dir / "static" / "bootstrap.css")
|
|
|
|
)
|
|
|
|
log.debug("Loaded custom CSS")
|
|
|
|
css["href"] = "/bootstrap.css"
|
|
|
|
css["integrity"] = ""
|
|
|
|
css["crossorigin"] = ""
|
2020-06-16 19:07:47 +00:00
|
|
|
except FileNotFoundError:
|
2020-06-21 19:29:53 +00:00
|
|
|
log.error(
|
|
|
|
f'Custom CSS {config["files"]["custom_css"]} not found, using default.'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
"email_html" not in config["password_resets"]
|
|
|
|
or config["password_resets"]["email_html"] == ""
|
|
|
|
):
|
|
|
|
log.debug("Using default password reset email HTML template")
|
|
|
|
config["password_resets"]["email_html"] = str(local_dir / "email.html")
|
|
|
|
if (
|
|
|
|
"email_text" not in config["password_resets"]
|
|
|
|
or config["password_resets"]["email_text"] == ""
|
|
|
|
):
|
|
|
|
log.debug("Using default password reset email plaintext template")
|
|
|
|
config["password_resets"]["email_text"] = str(local_dir / "email.txt")
|
|
|
|
|
|
|
|
if (
|
|
|
|
"email_html" not in config["invite_emails"]
|
|
|
|
or config["invite_emails"]["email_html"] == ""
|
|
|
|
):
|
|
|
|
log.debug("Using default invite email HTML template")
|
|
|
|
config["invite_emails"]["email_html"] = str(local_dir / "invite-email.html")
|
|
|
|
if (
|
|
|
|
"email_text" not in config["invite_emails"]
|
|
|
|
or config["invite_emails"]["email_text"] == ""
|
|
|
|
):
|
|
|
|
log.debug("Using default invite email plaintext template")
|
|
|
|
config["invite_emails"]["email_text"] = str(local_dir / "invite-email.txt")
|
|
|
|
if (
|
|
|
|
"public_server" not in config["jellyfin"]
|
|
|
|
or config["jellyfin"]["public_server"] == ""
|
|
|
|
):
|
|
|
|
config["jellyfin"]["public_server"] = config["jellyfin"]["server"]
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
if args.get_defaults:
|
|
|
|
import json
|
|
|
|
from jellyfin_accounts.jf_api import Jellyfin
|
2020-06-21 19:29:53 +00:00
|
|
|
|
|
|
|
jf = Jellyfin(
|
|
|
|
config["jellyfin"]["server"],
|
|
|
|
config["jellyfin"]["client"],
|
|
|
|
config["jellyfin"]["version"],
|
|
|
|
config["jellyfin"]["device"],
|
|
|
|
config["jellyfin"]["device_id"],
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
print("NOTE: This can now be done through the web ui.")
|
2020-06-21 19:29:53 +00:00
|
|
|
print(
|
|
|
|
"""
|
2020-06-16 19:07:47 +00:00
|
|
|
This tool lets you grab various settings from a user,
|
|
|
|
so that they can be applied every time a new account is
|
2020-06-21 19:29:53 +00:00
|
|
|
created. """
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
print("Step 1: User Policy.")
|
2020-06-21 19:29:53 +00:00
|
|
|
print(
|
|
|
|
"""
|
2020-06-21 19:21:33 +00:00
|
|
|
A user policy stores a users permissions (e.g access rights and
|
2020-06-16 19:07:47 +00:00
|
|
|
most of the other settings in the 'Profile' and 'Access' tabs
|
2020-06-21 19:29:53 +00:00
|
|
|
of a user). """
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
success = False
|
2020-06-21 19:21:33 +00:00
|
|
|
msg = "Get public users only or all users? (requires auth) [public/all]: "
|
2020-06-16 19:07:47 +00:00
|
|
|
public = False
|
|
|
|
while not success:
|
|
|
|
choice = input(msg)
|
2020-06-21 19:29:53 +00:00
|
|
|
if choice == "public":
|
2020-06-16 19:07:47 +00:00
|
|
|
public = True
|
|
|
|
print("Make sure the user is publicly visible!")
|
|
|
|
success = True
|
2020-06-21 19:29:53 +00:00
|
|
|
elif choice == "all":
|
|
|
|
jf.authenticate(
|
|
|
|
config["jellyfin"]["username"], config["jellyfin"]["password"]
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
public = False
|
|
|
|
success = True
|
|
|
|
users = jf.getUsers(public=public)
|
|
|
|
for index, user in enumerate(users):
|
|
|
|
print(f'{index+1}) {user["Name"]}')
|
|
|
|
success = False
|
|
|
|
while not success:
|
|
|
|
try:
|
2020-06-21 19:29:53 +00:00
|
|
|
user_index = int(input(">: ")) - 1
|
|
|
|
policy = users[user_index]["Policy"]
|
2020-06-16 19:07:47 +00:00
|
|
|
success = True
|
|
|
|
except (ValueError, IndexError):
|
|
|
|
pass
|
|
|
|
data_store.user_template = policy
|
|
|
|
print(f'Policy written to "{config["files"]["user_template"]}".')
|
2020-06-21 19:29:53 +00:00
|
|
|
print("In future, this policy will be copied to all new users.")
|
|
|
|
print("Step 2: Homescreen Layout")
|
|
|
|
print(
|
|
|
|
"""
|
2020-06-16 19:07:47 +00:00
|
|
|
You may want to customize the default layout of a new user's
|
|
|
|
home screen. These settings can be applied to an account through
|
2020-06-21 19:29:53 +00:00
|
|
|
the 'Home' section in a user's settings. """
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
success = False
|
|
|
|
while not success:
|
|
|
|
choice = input("Grab the chosen user's homescreen layout? [y/n]: ")
|
2020-06-21 19:29:53 +00:00
|
|
|
if choice.lower() == "y":
|
|
|
|
user_id = users[user_index]["Id"]
|
|
|
|
configuration = users[user_index]["Configuration"]
|
2020-06-16 19:07:47 +00:00
|
|
|
display_prefs = jf.getDisplayPreferences(user_id)
|
|
|
|
data_store.user_configuration = configuration
|
2020-06-21 19:29:53 +00:00
|
|
|
print(
|
|
|
|
f'Configuration written to "{config["files"]["user_configuration"]}".'
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
data_store.user_displayprefs = display_prefs
|
2020-06-21 19:29:53 +00:00
|
|
|
print(
|
|
|
|
f'Display Prefs written to "{config["files"]["user_displayprefs"]}".'
|
|
|
|
)
|
2020-06-16 19:07:47 +00:00
|
|
|
success = True
|
2020-06-21 19:29:53 +00:00
|
|
|
elif choice.lower() == "n":
|
2020-06-16 19:07:47 +00:00
|
|
|
success = True
|
|
|
|
|
|
|
|
else:
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
def signal_handler(sig, frame):
|
2020-06-21 19:29:53 +00:00
|
|
|
print("Quitting...")
|
2020-06-16 19:07:47 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
global app
|
|
|
|
app = Flask(__name__, root_path=str(local_dir))
|
2020-06-21 19:29:53 +00:00
|
|
|
app.config["DEBUG"] = config.getboolean("ui", "debug")
|
|
|
|
app.config["SECRET_KEY"] = secrets.token_urlsafe(16)
|
2020-06-16 19:07:47 +00:00
|
|
|
|
|
|
|
from waitress import serve
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
if first_run:
|
|
|
|
import jellyfin_accounts.setup
|
2020-06-21 19:29:53 +00:00
|
|
|
|
|
|
|
host = config["ui"]["host"]
|
|
|
|
port = config["ui"]["port"]
|
|
|
|
log.info("Starting web UI for first run setup...")
|
|
|
|
serve(app, host=host, port=port)
|
2020-06-16 19:07:47 +00:00
|
|
|
else:
|
|
|
|
import jellyfin_accounts.web_api
|
|
|
|
import jellyfin_accounts.web
|
2020-06-21 19:29:53 +00:00
|
|
|
|
|
|
|
host = config["ui"]["host"]
|
|
|
|
port = config["ui"]["port"]
|
|
|
|
log.info(f"Starting web UI on {host}:{port}")
|
|
|
|
if config.getboolean("password_resets", "enabled"):
|
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
def start_pwr():
|
|
|
|
import jellyfin_accounts.pw_reset
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
jellyfin_accounts.pw_reset.start()
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-06-16 19:07:47 +00:00
|
|
|
pwr = threading.Thread(target=start_pwr, daemon=True)
|
2020-06-21 19:29:53 +00:00
|
|
|
log.info("Starting email thread")
|
2020-06-16 19:07:47 +00:00
|
|
|
pwr.start()
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
serve(app, host=host, port=int(port))
|