From 78279a4b9e9b4f3fa549e1ca62fcb21f9c57da7a Mon Sep 17 00:00:00 2001 From: Todos Date: Sat, 17 Oct 2026 19:06:16 +0800 Subject: [PATCH] refactor(worker): retire notify token, use API key for /api/notify - POST /api/notify/:token now returns 410 Gone and no longer reads the KV notify_token record or queries the DB; legacy clients must migrate to POST /api/notify with an enk_... bearer key. - POST/DELETE /api/me/notifications/token(/:token) now return 410 Gone and no longer generate or delete any token. - Inlined the now single-caller notifyByUserId helper into POST /api/notify, removed the unused findUserById import, and dropped the :token rate limiter since the deprecated route no longer touches KV. - Log redaction for /api/notify/ (index.ts) is unchanged; README (zh/en) updated to document the retirement and the API-key migration path. Existing notify_token:* KV records are left to expire within their one-year TTL; no migration cleanup is needed. Checkpoint: 2IOucMQCOPHQph6u7Ww6n --- README.en.md | 4 +++- README.md | 4 +++- worker/src/routes/me.ts | 19 ++++++---------- worker/src/routes/notify.ts | 45 +++++++++---------------------------- 4 files changed, 23 insertions(+), 49 deletions(-) diff --git a/README.en.md b/README.en.md index 9047970..cf0ed46 100644 --- a/README.en.md +++ b/README.en.md @@ -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/`) is retired and returns `410`; use an API key with `POST /api/notify` instead. **3. List and revoke** (also requires a login session) diff --git a/README.md b/README.md index 05b2312..225a06d 100644 --- a/README.md +++ b/README.md @@ -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/`)已下线,返回 `410`,请改用 API Key 调用 `POST /api/notify`。 **3. 查看与吊销**(同样需要登录会话) diff --git a/worker/src/routes/me.ts b/worker/src/routes/me.ts index dcd7dac..0db2db1 100644 --- a/worker/src/routes/me.ts +++ b/worker/src/routes/me.ts @@ -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 ────────────────────────────────────────────────────────────── diff --git a/worker/src/routes/notify.ts b/worker/src/routes/notify.ts index 669f20c..7e11b53 100644 --- a/worker/src/routes/notify.ts +++ b/worker/src/routes/notify.ts @@ -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, @@ -11,10 +10,8 @@ import { apiKeyOrCookieAuth } from "../middleware/apiKey"; export const notifyRoutes = new Hono(); -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 @@ -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, - userId: string, -): Promise { const prefix = getTablePrefix(c.env); const notifyConfig = await getNotificationConfig(c.env.DB, prefix, userId); @@ -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); +});