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
@@ -109,4 +109,87 @@ describe("NtfyProvider", () => {
|
||||
expect(mockGotPost.mock.calls[0][1].body).toContain("/infrastructure/mon-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("authentication", () => {
|
||||
const authHeaderOf = () => mockGotPost.mock.calls[0][1].headers.Authorization;
|
||||
|
||||
it("sends no Authorization header when no auth type is configured", async () => {
|
||||
const { provider } = createProvider();
|
||||
await provider.sendMessage(makeNtfyNotification() as any, makeMessage());
|
||||
expect(authHeaderOf()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("sends no Authorization header when auth type is none", async () => {
|
||||
const { provider } = createProvider();
|
||||
await provider.sendMessage(makeNtfyNotification({ ntfyAuthType: "none" }) as any, makeMessage());
|
||||
expect(authHeaderOf()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("sends a bearer token for token auth", async () => {
|
||||
const { provider } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "token", accessToken: "tk_secret" });
|
||||
expect(await provider.sendMessage(notification as any, makeMessage())).toBe(true);
|
||||
expect(authHeaderOf()).toBe("Bearer tk_secret");
|
||||
});
|
||||
|
||||
it("sends base64 encoded credentials for basic auth", async () => {
|
||||
const { provider } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "basic", ntfyUsername: "alice", accessToken: "s3cret" });
|
||||
expect(await provider.sendMessage(notification as any, makeMessage())).toBe(true);
|
||||
expect(authHeaderOf()).toBe(`Basic ${Buffer.from("alice:s3cret").toString("base64")}`);
|
||||
});
|
||||
|
||||
it("authenticates test alerts as well as alerts", async () => {
|
||||
const { provider } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "token", accessToken: "tk_secret" });
|
||||
expect(await provider.sendTestAlert(notification)).toBe(true);
|
||||
expect(authHeaderOf()).toBe("Bearer tk_secret");
|
||||
});
|
||||
|
||||
it("preserves the notification headers alongside the Authorization header", async () => {
|
||||
const { provider } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "token", accessToken: "tk_secret" });
|
||||
await provider.sendMessage(notification as any, makeMessage());
|
||||
expect(mockGotPost.mock.calls[0][1].headers).toEqual(
|
||||
expect.objectContaining({
|
||||
Title: "Monitor Down: Test Monitor",
|
||||
Priority: "high",
|
||||
Tags: "rotating_light",
|
||||
Authorization: "Bearer tk_secret",
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// A misconfigured channel must fail loudly rather than post anonymously to a topic
|
||||
// the operator believes is protected.
|
||||
it("refuses to send unauthenticated when token auth has no access token", async () => {
|
||||
const { provider, logger } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "token", accessToken: "" });
|
||||
expect(await provider.sendMessage(notification as any, makeMessage())).toBe(false);
|
||||
expect(mockGotPost).not.toHaveBeenCalled();
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("refuses to send unauthenticated when basic auth has no username", async () => {
|
||||
const { provider, logger } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "basic", accessToken: "s3cret" });
|
||||
expect(await provider.sendMessage(notification as any, makeMessage())).toBe(false);
|
||||
expect(mockGotPost).not.toHaveBeenCalled();
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("refuses to send unauthenticated when basic auth has no password", async () => {
|
||||
const { provider, logger } = createProvider();
|
||||
const notification = makeNtfyNotification({ ntfyAuthType: "basic", ntfyUsername: "alice", accessToken: "" });
|
||||
expect(await provider.sendMessage(notification as any, makeMessage())).toBe(false);
|
||||
expect(mockGotPost).not.toHaveBeenCalled();
|
||||
expect(logger.warn).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("refuses to send an unauthenticated test alert when credentials are incomplete", async () => {
|
||||
const { provider } = createProvider();
|
||||
expect(await provider.sendTestAlert(makeNtfyNotification({ ntfyAuthType: "token", accessToken: "" }))).toBe(false);
|
||||
expect(mockGotPost).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -44,4 +44,69 @@ describe("notification validation", () => {
|
||||
expect(result.success).toBe(false);
|
||||
}
|
||||
);
|
||||
|
||||
describe("ntfy authentication", () => {
|
||||
const parseNtfy = (overrides: Record<string, unknown>) =>
|
||||
createNotificationBodyValidation.safeParse({
|
||||
notificationName: "ntfy alerts",
|
||||
type: "ntfy",
|
||||
address: "https://ntfy.example.com",
|
||||
topic: "checkmate-alerts",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
const pathsOf = (result: ReturnType<typeof parseNtfy>) => (result.success ? [] : result.error.issues.map((issue) => issue.path.join(".")));
|
||||
|
||||
it("accepts an unauthenticated channel", () => {
|
||||
expect(parseNtfy({}).success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts an explicit auth type of none", () => {
|
||||
expect(parseNtfy({ ntfyAuthType: "none" }).success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts token auth with an access token", () => {
|
||||
expect(parseNtfy({ ntfyAuthType: "token", accessToken: "tk_secret" }).success).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts basic auth with a username and password", () => {
|
||||
expect(parseNtfy({ ntfyAuthType: "basic", ntfyUsername: "alice", accessToken: "s3cret" }).success).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects an unknown auth type", () => {
|
||||
expect(parseNtfy({ ntfyAuthType: "oauth" }).success).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects token auth without an access token", () => {
|
||||
expect(pathsOf(parseNtfy({ ntfyAuthType: "token" }))).toContain("accessToken");
|
||||
});
|
||||
|
||||
it("rejects token auth carrying a username", () => {
|
||||
expect(pathsOf(parseNtfy({ ntfyAuthType: "token", accessToken: "tk_secret", ntfyUsername: "alice" }))).toContain("ntfyUsername");
|
||||
});
|
||||
|
||||
it("rejects basic auth without a username", () => {
|
||||
expect(pathsOf(parseNtfy({ ntfyAuthType: "basic", accessToken: "s3cret" }))).toContain("ntfyUsername");
|
||||
});
|
||||
|
||||
it("rejects basic auth without a password", () => {
|
||||
expect(pathsOf(parseNtfy({ ntfyAuthType: "basic", ntfyUsername: "alice" }))).toContain("accessToken");
|
||||
});
|
||||
|
||||
it("reports both fields when basic auth is entirely empty", () => {
|
||||
expect(pathsOf(parseNtfy({ ntfyAuthType: "basic" }))).toEqual(expect.arrayContaining(["ntfyUsername", "accessToken"]));
|
||||
});
|
||||
|
||||
// Credentials with auth off would be stored but never sent, which reads as protected
|
||||
// while posting anonymously.
|
||||
it("rejects credentials supplied without an auth type", () => {
|
||||
expect(pathsOf(parseNtfy({ accessToken: "tk_secret" }))).toContain("accessToken");
|
||||
expect(pathsOf(parseNtfy({ ntfyUsername: "alice" }))).toContain("ntfyUsername");
|
||||
});
|
||||
|
||||
it("still requires a topic and a valid server URL", () => {
|
||||
expect(parseNtfy({ topic: "" }).success).toBe(false);
|
||||
expect(parseNtfy({ address: "not-a-url" }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user