File size: 431 Bytes
bc0be9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// auth.ts
import { getToken } from "./utils.ts";
import { unauthorizedResponse } from "./response.ts";

export function validateAuth(req: Request): Response | null {
  const token = getToken();
  if (!token) return null; // 无需认证
  
  const auth = req.headers.get("authorization") ?? "";
  if (auth !== token) {
    return unauthorizedResponse("无效的客户端 API 密钥", 403);
  }
  
  return null; // 认证通过
}