feat: add authentication support to ntfy notifications

ntfy servers that require auth rejected every Checkmate alert because the
provider posted without an Authorization header, so alerts to protected
topics failed silently.

Adds ntfyAuthType ("none" | "token" | "basic"):
- token: Authorization: Bearer <accessToken>
- basic: Authorization: Basic base64(ntfyUsername:accessToken)

Both types store their secret in the existing accessToken field rather than
introducing a new secret, so credential handling stays in one place.

Incomplete credentials fail the send instead of falling back to an anonymous
post, and validation rejects partial auth config up front so a channel can't
be saved looking authenticated while posting anonymously.

Backend only; frontend follows once this lands.

Closes #3717
This commit is contained in:
egeoztass committed 2026-08-14 14:01:06 +03:00
1 parent f97ca8565a
commit 1c9739d1da
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,