From 879687e0afd397676b3cea20ec5e6def8424567b Mon Sep 17 00:00:00 2001 From: Fabian Sasse Date: Tue, 15 Nov 2022 11:31:53 +0100 Subject: [PATCH] authentication NTFY --- back/pialert.py | 21 +++++++++++++++++---- config/pialert.conf | 3 +++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 1c927b6c..7ea5adce 100644 --- a/back/pialert.py +++ b/back/pialert.py @@ -29,6 +29,7 @@ import smtplib import csv import json import requests +from base64 import b64encode #=============================================================================== @@ -1488,14 +1489,26 @@ def email_reporting (): closeDB() #------------------------------------------------------------------------------- def send_ntfy (_Text): - requests.post("{}/{}".format( NTFY_HOST,NTFY_TOPIC), - data=_Text, - headers={ + headers = { "Title": "Pi.Alert Notification", "Actions": "view, Open Dashboard, "+ REPORT_DASHBOARD_URL, "Priority": "urgent", "Tags": "warning" - }) + } + # if username and password are set generate hash and update header + if NTFY_USER and NTFY_PASSWORD: + # Generate hash for basic auth + usernamepassword = "{}:{}".format(NTFY_USER,NTFY_PASSWORD) + basichash = b64encode(bytes(NTFY_USER + ':' + NTFY_PASSWORD, "utf-8")).decode("ascii") + + # add authorization header with hash + headers["Authorization"] = "Basic {}".format(basichash) + + requests.post("{}/{}".format( NTFY_HOST,NTFY_TOPIC), + data=_Text, + headers=headers) + +send_ntfy("selfhosted ntfy test") def send_pushsafer (_Text): url = 'https://www.pushsafer.com/api' diff --git a/config/pialert.conf b/config/pialert.conf index ae253f36..295271b6 100644 --- a/config/pialert.conf +++ b/config/pialert.conf @@ -56,6 +56,9 @@ APPRISE_URL = 'mailto://smtp-relay.sendinblue.com:587?from=user@gmai REPORT_NTFY = False NTFY_HOST = 'https://ntfy.sh' NTFY_TOPIC = 'replace_my_secure_topicname_91h889f28' +# set username and password if authentication is used https://docs.ntfy.sh/config/#users-and-roles +NTFY_USER = '' +NTFY_PASSWORD = '' # PUSHSAFER (https://www.pushsafer.com/) settings # ----------------------