Merge pull request #3839 from egeoztass/feat/ntfy-authentication
feat: add authentication support to ntfy notifications
This commit is contained in:
8 files changed
+289
-8
No files matched your search
@@ -11,12 +11,18 @@ export class NtfyProvider extends NotificationProvider {
|
||||
return false;
|
||||
}
|
||||
|
||||
const authHeaders = this.buildAuthHeaders(notification, "sendTestAlert");
|
||||
if (authHeaders === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await got.post(this.buildTopicUrl(notification.address, notification.topic), {
|
||||
body: getTestMessage(),
|
||||
headers: {
|
||||
Title: "Checkmate test notification",
|
||||
Priority: "default",
|
||||
...authHeaders,
|
||||
},
|
||||
...this.gotRequestOptions(),
|
||||
});
|
||||
@@ -38,6 +44,11 @@ export class NtfyProvider extends NotificationProvider {
|
||||
return false;
|
||||
}
|
||||
|
||||
const authHeaders = this.buildAuthHeaders(notification, "sendMessage");
|
||||
if (authHeaders === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await got.post(this.buildTopicUrl(notification.address, notification.topic), {
|
||||
body: this.buildNtfyText(message),
|
||||
@@ -45,6 +56,7 @@ export class NtfyProvider extends NotificationProvider {
|
||||
Title: message.content.title,
|
||||
Priority: this.mapPriority(message.severity),
|
||||
Tags: this.mapTags(message.severity),
|
||||
...authHeaders,
|
||||
},
|
||||
...this.gotRequestOptions(),
|
||||
});
|
||||
@@ -70,6 +82,42 @@ export class NtfyProvider extends NotificationProvider {
|
||||
return `${address.replace(/\/+$/, "")}/${encodeURIComponent(topic)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Authorization header for the configured auth type, or null when the
|
||||
* credentials are incomplete. Null is treated as a send failure rather than falling
|
||||
* back to an anonymous post, so a misconfigured channel surfaces instead of quietly
|
||||
* publishing to a topic the server may leave world-readable.
|
||||
*/
|
||||
private buildAuthHeaders(notification: Partial<Notification>, method: string): Record<string, string> | null {
|
||||
const { ntfyAuthType, accessToken, ntfyUsername } = notification;
|
||||
|
||||
if (!ntfyAuthType || ntfyAuthType === "none") {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (ntfyAuthType === "token") {
|
||||
if (!accessToken) {
|
||||
this.logMissingCredentials("access token", method);
|
||||
return null;
|
||||
}
|
||||
return { Authorization: `Bearer ${accessToken}` };
|
||||
}
|
||||
|
||||
if (!ntfyUsername || !accessToken) {
|
||||
this.logMissingCredentials("username and password", method);
|
||||
return null;
|
||||
}
|
||||
return { Authorization: `Basic ${Buffer.from(`${ntfyUsername}:${accessToken}`).toString("base64")}` };
|
||||
}
|
||||
|
||||
private logMissingCredentials(missing: string, method: string): void {
|
||||
this.logger.warn({
|
||||
message: `ntfy notification is configured for authentication but is missing its ${missing}`,
|
||||
service: SERVICE_NAME,
|
||||
method,
|
||||
});
|
||||
}
|
||||
|
||||
private buildNtfyText(message: NotificationMessage): string {
|
||||
const lines = [
|
||||
message.content.summary,
|
||||
|
||||
Reference in new issue
Block a user