Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ curl -X POST https://your-worker.workers.dev/api/notify \

`POST /api/notify` sends a push to the user's enabled notification channels; `title` defaults to `Notification` when omitted. Besides the API key, it also accepts a cookie login session.

Each key is limited to 120 requests/minute (`429` beyond that). Invalid, expired or revoked keys get `401`; keys stop working as soon as their user is disabled. `/api/notify` (including the legacy third-party token route `/api/notify/:token`) has an additional per-IP limit of 30 requests/minute, shared across both routes.
Each key is limited to 120 requests/minute (`429` beyond that). Invalid, expired or revoked keys get `401`; keys stop working as soon as their user is disabled. `/api/notify` has an additional per-IP limit of 30 requests/minute.

The legacy notify token (`/api/notify/<token>`) is retired and returns `410`; use an API key with `POST /api/notify` instead.

**3. List and revoke** (also requires a login session)

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ curl -X POST https://your-worker.workers.dev/api/notify \

`POST /api/notify` 会向该用户已启用的通知渠道发送推送;`title` 省略时默认为 `Notification`。该接口除了 API Key 也接受 cookie 登录态。

每个 key 限流 120 次/分钟,超出返回 `429`。key 无效、过期或已吊销时返回 `401`;对应用户被禁用后其 key 立即失效。`/api/notify`(含老的三方 token 路由 `/api/notify/:token`)另有一层按 IP 的 30 次/分钟限流,两条路由共享同一额度。
每个 key 限流 120 次/分钟,超出返回 `429`。key 无效、过期或已吊销时返回 `401`;对应用户被禁用后其 key 立即失效。`/api/notify` 另有一层按 IP 的 30 次/分钟限流。

旧的 notify token(`/api/notify/<token>`)已下线,返回 `410`,请改用 API Key 调用 `POST /api/notify`。

**3. 查看与吊销**(同样需要登录会话)

Expand Down
19 changes: 7 additions & 12 deletions worker/src/routes/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,17 @@ meRoutes.delete("/notification-history/:id", async (c) => {
return c.json({ success: true });
});

// Notify tokens are retired in favour of API keys. Both endpoints return 410
// without generating, looking up, or deleting any KV token record.
const NOTIFY_TOKEN_DEPRECATED =
"Notify tokens are deprecated. Use POST /api/notify with an API key (Authorization: Bearer enk_...) instead; create a key in Settings → API Keys.";

meRoutes.post("/notifications/token", async (c) => {
const userId = getEffectiveUserId(c);
const token = generateJti();
await c.env.KV.put(`notify_token:${token}`, userId, {
expirationTtl: 31536000,
});
return c.json({ token });
return c.json({ error: NOTIFY_TOKEN_DEPRECATED }, 410);
});

meRoutes.delete("/notifications/token/:token", async (c) => {
const userId = getEffectiveUserId(c);
const token = c.req.param("token");
const owner = await c.env.KV.get(`notify_token:${token}`);
if (owner !== userId) return c.json({ error: "Not found" }, 404);
await c.env.KV.delete(`notify_token:${token}`);
return c.json({ success: true });
return c.json({ error: NOTIFY_TOKEN_DEPRECATED }, 410);
});

// ── API keys ──────────────────────────────────────────────────────────────
Expand Down
45 changes: 10 additions & 35 deletions worker/src/routes/notify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Hono, type Context } from "hono";
import { Hono } from "hono";
import { getTablePrefix, type HonoEnv } from "../types";
import { getNotificationConfig } from "../db/queries/notifications";
import { findUserById } from "../db/queries/users";
import {
sendNotifications,
type NotifyMessage,
Expand All @@ -11,10 +10,8 @@ import { apiKeyOrCookieAuth } from "../middleware/apiKey";

export const notifyRoutes = new Hono<HonoEnv>();

notifyRoutes.use(
"/:token",
rateLimit({ max: 30, window: 60, keyPrefix: "notify_trigger" }),
);
const NOTIFY_TOKEN_DEPRECATED =
"Notify tokens are deprecated. Use POST /api/notify with an API key (Authorization: Bearer enk_...) instead; create a key in Settings → API Keys.";

// API-key or logged-in session trigger — exact POST /api/notify (no :token).
// The enk_... bearer is validated by apiKeyOrCookieAuth (its own 120/min per-key
Expand All @@ -27,34 +24,6 @@ notifyRoutes.use(

notifyRoutes.post("/", async (c) => {
const userId = c.get("userId");
return notifyByUserId(c, userId);
});

// Third-party trigger — authenticate via token in URL, header, or query param
notifyRoutes.post("/:token", async (c) => {
const token = c.req.param("token");
const prefix = getTablePrefix(c.env);

// Find user by their third-party API token stored in notification_configs
// Token format: userId:secret stored in KV
const userId = await c.env.KV.get(`notify_token:${token}`);
if (!userId) {
return c.json({ error: "Invalid token" }, 401);
}

const user = await findUserById(c.env.DB, prefix, userId);
if (!user?.is_active) {
return c.json({ error: "User not found or disabled" }, 401);
}

return notifyByUserId(c, userId);
});

/** Shared helper: resolve the user's notification config and dispatch the message. */
async function notifyByUserId(
c: Context<HonoEnv>,
userId: string,
): Promise<Response> {
const prefix = getTablePrefix(c.env);

const notifyConfig = await getNotificationConfig(c.env.DB, prefix, userId);
Expand All @@ -76,4 +45,10 @@ async function notifyByUserId(

const results = await sendNotifications(notifyConfig, message, c.env);
return c.json({ success: true, results });
}
});

// Third-party token trigger — deprecated in favour of API keys. Return 410
// without touching KV or the DB; legacy clients must migrate to POST /api/notify.
notifyRoutes.post("/:token", async (c) => {
return c.json({ error: NOTIFY_TOKEN_DEPRECATED }, 410);
});
Loading