From 03b110950b737120475820035ed62173fae3f7fc Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Mon, 17 Feb 2025 09:03:34 +1100 Subject: [PATCH] NTFY token support --- front/plugins/_publisher_ntfy/config.json | 28 +++++++++++++++++++++++ front/plugins/_publisher_ntfy/ntfy.py | 22 ++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/front/plugins/_publisher_ntfy/config.json b/front/plugins/_publisher_ntfy/config.json index eaabf3d2..e28c53a7 100755 --- a/front/plugins/_publisher_ntfy/config.json +++ b/front/plugins/_publisher_ntfy/config.json @@ -408,6 +408,34 @@ } ] }, + { + "function": "TOKEN", + "type": { + "dataType": "string", + "elements": [ + { + "elementType": "input", + "elementOptions": [], + "transformers": [] + } + ] + }, + "default_value": "", + "options": [], + "localized": ["name", "description"], + "name": [ + { + "language_code": "en_us", + "string": "NTFY token" + } + ], + "description": [ + { + "language_code": "en_us", + "string": "Enter a token if authentication is enabled for hosting an instance. If a token is provided, the username and password will be ignored." + } + ] + }, { "function": "USER", "type": { diff --git a/front/plugins/_publisher_ntfy/ntfy.py b/front/plugins/_publisher_ntfy/ntfy.py index 6cca1008..82611953 100755 --- a/front/plugins/_publisher_ntfy/ntfy.py +++ b/front/plugins/_publisher_ntfy/ntfy.py @@ -94,23 +94,29 @@ def send(html, text): response_text = '' response_status_code = '' + # settings + token = get_setting_value('NTFY_TOKEN') + user = get_setting_value('NTFY_USER') + pwd = get_setting_value('NTFY_PASSWORD') + # prepare request headers headers = { "Title": "NetAlertX Notification", "Actions": "view, Open Dashboard, "+ get_setting_value('REPORT_DASHBOARD_URL'), "Priority": get_setting_value('NTFY_PRIORITY'), "Tags": "warning" } - - # if username and password are set generate hash and update header - if get_setting_value('NTFY_USER') != "" and get_setting_value('NTFY_PASSWORD') != "": - # Generate hash for basic auth - # usernamepassword = "{}:{}".format(get_setting_value('NTFY_USER'),get_setting_value('NTFY_PASSWORD')) - basichash = b64encode(bytes(get_setting_value('NTFY_USER') + ':' + get_setting_value('NTFY_PASSWORD'), "utf-8")).decode("ascii") - # add authorization header with hash + # if token or username and password are set generate hash and update header + if token != '': + headers["Authorization"] = "Bearer {}".format(token) + elif user != "" and pwd != "": + # Generate hash for basic auth + basichash = b64encode(bytes(user + ':' + pwd, "utf-8")).decode("ascii") + # add authorization header with hash headers["Authorization"] = "Basic {}".format(basichash) + # call NTFY service try: response = requests.post("{}/{}".format( get_setting_value('NTFY_HOST'), get_setting_value('NTFY_TOPIC')), @@ -119,8 +125,6 @@ def send(html, text): response_status_code = response.status_code - - # Check if the request was successful (status code 200) if response_status_code == 200: response_text = response.text # This captures the response body/message