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
@@ -1,5 +1,6 @@
|
||||
import { Schema, model, type Types } from "mongoose";
|
||||
import type { Notification, NotificationChannel } from "@/domain/notifications/notification.type.js";
|
||||
import { NtfyAuthTypes } from "@/domain/notifications/notification.type.js";
|
||||
|
||||
interface NotificationDocument extends Omit<Notification, "id" | "userId" | "teamId" | "createdAt" | "updatedAt"> {
|
||||
_id: Types.ObjectId;
|
||||
@@ -53,6 +54,8 @@ const NotificationSchema = new Schema<NotificationDocument>(
|
||||
accountSid: { type: String },
|
||||
twilioPhoneNumber: { type: String },
|
||||
topic: { type: String },
|
||||
ntfyAuthType: { type: String, enum: NtfyAuthTypes },
|
||||
ntfyUsername: { type: String },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
|
||||
@@ -14,6 +14,11 @@ export const NotificationChannels = [
|
||||
] as const;
|
||||
export type NotificationChannel = (typeof NotificationChannels)[number];
|
||||
|
||||
// ntfy servers accept either a bearer access token or HTTP basic credentials.
|
||||
// Both store their secret in `accessToken`; basic auth pairs it with `ntfyUsername`.
|
||||
export const NtfyAuthTypes = ["none", "token", "basic"] as const;
|
||||
export type NtfyAuthType = (typeof NtfyAuthTypes)[number];
|
||||
|
||||
export interface Notification {
|
||||
id: string;
|
||||
userId: string;
|
||||
@@ -28,6 +33,8 @@ export interface Notification {
|
||||
accountSid?: string;
|
||||
twilioPhoneNumber?: string;
|
||||
topic?: string;
|
||||
ntfyAuthType?: NtfyAuthType;
|
||||
ntfyUsername?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -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