From 4a14b6b01fa4d10b53aa2d75c7fd8bd9ce9dc645 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 1 Mar 2023 16:35:08 +0100 Subject: [PATCH] refactor(playout): update liquidsoap input auth handler --- .../liquidsoap/1.4/ls_script.liq | 36 ++++++--------- .../liquidsoap/liquidsoap_auth.py | 46 ++++++++++++------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/playout/libretime_playout/liquidsoap/1.4/ls_script.liq b/playout/libretime_playout/liquidsoap/1.4/ls_script.liq index 620f73377..6cc70dcbd 100644 --- a/playout/libretime_playout/liquidsoap/1.4/ls_script.liq +++ b/playout/libretime_playout/liquidsoap/1.4/ls_script.liq @@ -120,29 +120,19 @@ def input_main_on_disconnect() update_source_status("master_dj", false) end def input_show_on_connect(header) update_source_status("live_dj", true) end def input_show_on_disconnect() update_source_status("live_dj", false) end -# Auth function for live stream -# @Category LiveStream -# @param user Username to check against LibreTime API -# @param password Password to check against LibreTime API -# @param ~type Type of password to check, "dj" or "master, default: "master" -def input_check_auth(user="", password="", ~type="master") = - log("#{type} user #{user} connected",label="#{type}_source") +def make_input_auth_handler(input_name) + def auth_handler(user, password) + log("user '#{user}' connected", label="#{input_name}_input") - # Check auth based on return value from auth script - ret = test_process("python3 #{auth_path} --#{type} #{user} #{password}") + # Check auth based on return value from auth script + ret = test_process("python3 '#{auth_path}' '#{input_name}' '#{user}' '#{password}'") + if ret then + log("user '#{user}' authenticated", label="#{input_name}_input") + else + log("user '#{user}' auth failed", label="#{input_name}_input",level=2) + end - if ret then - log("#{type} user #{user} authenticated",label="#{type}_source") - else - log("#{type} user #{user} auth failed",label="#{type}_source",level=2) - end - - ret -end - -def make_input_auth_handler(source_name) - def auth_handler(user, password) = - input_check_auth(user, password, type=source_name) + ret end auth_handler end @@ -159,7 +149,7 @@ s = if input_show_port != 0 and input_show_mount != "" then input.harbor(id="harbor:input_show", input_show_mount, port=input_show_port, - auth=make_input_auth_handler("dj"), + auth=make_input_auth_handler("show"), max=40., on_connect=input_show_on_connect, on_disconnect=input_show_on_disconnect)) @@ -181,7 +171,7 @@ s = if input_main_port != 0 and input_main_mount != "" then input.harbor(id="harbor:input_main", input_main_mount, port=input_main_port, - auth=make_input_auth_handler("master"), + auth=make_input_auth_handler("main"), max=40., on_connect=input_main_on_connect, on_disconnect=input_main_on_disconnect)) diff --git a/playout/libretime_playout/liquidsoap/liquidsoap_auth.py b/playout/libretime_playout/liquidsoap/liquidsoap_auth.py index 28f922df5..4375fafa2 100644 --- a/playout/libretime_playout/liquidsoap/liquidsoap_auth.py +++ b/playout/libretime_playout/liquidsoap/liquidsoap_auth.py @@ -1,24 +1,36 @@ -import sys +from argparse import ArgumentParser +from typing import Literal from libretime_api_client.v1 import ApiClient as LegacyClient -legacy_client = LegacyClient() -dj_type = sys.argv[1] -username = sys.argv[2] -password = sys.argv[3] +def main(input_name: Literal["main", "show"], username: str, password: str) -> int: + legacy_client = LegacyClient() -source_type = "" -if dj_type == "--master": - source_type = "master" -elif dj_type == "--dj": - source_type = "dj" + input_name_map = {"main": "master", "show": "dj"} + response: dict = legacy_client.check_live_stream_auth( + username, + password, + input_name_map[input_name], + ) -response = legacy_client.check_live_stream_auth(username, password, source_type) + if response.get("msg", False) is True: + return 0 + return 1 -if "msg" in response and response["msg"] == True: - print(response["msg"]) - sys.exit(0) -else: - print(False) - sys.exit(1) + +if __name__ == "__main__": + parser = ArgumentParser() + + parser.add_argument("input_name", choices=["main", "show"]) + parser.add_argument("username") + parser.add_argument("password") + args = parser.parse_args() + + raise SystemExit( + main( + input_name=args.input_name, + username=args.username, + password=args.password, + ) + )