Skip to content

Commit

Permalink
NTFY token support
Browse files Browse the repository at this point in the history
  • Loading branch information
jokob-sk committed Feb 16, 2025
1 parent 7d9e846 commit 03b1109
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
28 changes: 28 additions & 0 deletions front/plugins/_publisher_ntfy/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 13 additions & 9 deletions front/plugins/_publisher_ntfy/ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand All @@ -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
Expand Down

0 comments on commit 03b1109

Please sign in to comment.