解析鉴权头
(c: Context)
| 31 | |
| 32 | /** 解析鉴权头 */ |
| 33 | function parseAuth(c: Context): { mail: string; token: string } | null { |
| 34 | let mail = c.req.header("X-API-Mail") ?? ""; |
| 35 | let token = c.req.header("X-API-Token") ?? ""; |
| 36 | if (!mail || !token) { |
| 37 | const auth = c.req.header("Authorization") ?? ""; |
| 38 | if (auth.startsWith("Bearer ")) { |
| 39 | const body = auth.slice(7).trim(); |
| 40 | const idx = body.indexOf(":"); |
| 41 | if (idx > 0) { |
| 42 | mail = body.slice(0, idx); |
| 43 | token = body.slice(idx + 1); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | mail = mail.trim().toLowerCase(); |
| 48 | token = token.trim(); |
| 49 | if (!mail || !token) return null; |
| 50 | return {mail, token}; |
| 51 | } |
| 52 | |
| 53 | /** 内存限流器:Map<mail, { count, resetAt }> */ |
| 54 | const RATE_BUCKET = new Map<string, { count: number; resetAt: number }>(); |