MCPcopy Create free account
hub / github.com/Hello-Application-XH/HelloGML / handleAdminAPIKey

Function handleAdminAPIKey

src/index.ts:256–287  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

254// ==================== Admin Handlers ====================
255
256async function handleAdminAPIKey(request: Request, env: Env): Promise<Response> {
257 const adminKey = request.headers.get("X-Admin-Key") || "";
258 if (env.ADMIN_KEY && adminKey !== env.ADMIN_KEY) {
259 return errorResponse("Unauthorized: invalid admin key", 401);
260 }
261
262 if (request.method === "POST") {
263 const body = (await request.json()) as any;
264 const apiKey = body.api_key;
265 if (!apiKey) return errorResponse("Missing api_key", 400);
266 await env.GLM_TOKENS.put(`ak:${apiKey}`, "1");
267 return jsonResponse({ success: true, message: "API key added successfully" });
268 }
269
270 if (request.method === "GET") {
271 const list = await env.GLM_TOKENS.list({ prefix: "ak:" });
272 const keys = list.keys.map((k) => ({
273 api_key: k.name.replace("ak:", ""),
274 }));
275 return jsonResponse({ keys });
276 }
277
278 if (request.method === "DELETE") {
279 const body = (await request.json()) as any;
280 const apiKey = body.api_key;
281 if (!apiKey) return errorResponse("Missing api_key", 400);
282 await env.GLM_TOKENS.delete(`ak:${apiKey}`);
283 return jsonResponse({ success: true, message: "API key deleted successfully" });
284 }
285
286 return errorResponse("Method not allowed", 405);
287}
288
289async function handleAdminToken(request: Request, env: Env): Promise<Response> {
290 const adminKey = request.headers.get("X-Admin-Key") || "";

Callers 1

fetchFunction · 0.85

Calls 2

errorResponseFunction · 0.85
jsonResponseFunction · 0.85

Tested by

no test coverage detected