fix: aksepter api-key header i tillegg til X-MCP-Key (Perplexity MCP connector)

This commit is contained in:
chrischristiansen-glitch 2026-06-29 01:35:05 +02:00
parent cec811d033
commit 34c7b77a9f

View File

@ -5,7 +5,7 @@ import os
import logging import logging
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException, Depends, Header from fastapi import FastAPI, HTTPException, Depends, Header, Request
from pydantic import BaseModel from pydantic import BaseModel
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
@ -19,8 +19,10 @@ MCP_SECRET = os.environ.get("MCP_SECRET", "")
BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609") BUILD_TRIGGER_ID = os.environ.get("CLOUD_BUILD_TRIGGER_ID", "38423976-91ff-4ff4-859e-1f262344c609")
def verify_token(x_mcp_key: str = Header(default="")): def verify_token(request: Request, x_mcp_key: str = Header(default="")):
if MCP_SECRET and x_mcp_key != MCP_SECRET: # Aksepter både X-MCP-Key og api-key (Perplexity MCP connector bruker api-key)
token = x_mcp_key or request.headers.get("api-key", "")
if MCP_SECRET and token != MCP_SECRET:
raise HTTPException(status_code=401, detail="Ugyldig MCP-nøkkel") raise HTTPException(status_code=401, detail="Ugyldig MCP-nøkkel")
return True return True
@ -52,15 +54,13 @@ def _parse_ts(ts):
from datetime import datetime, timezone from datetime import datetime, timezone
if ts is None: if ts is None:
return None return None
# Hvis det allerede er et datetime-objekt (inkl. DatetimeWithNanoseconds)
if hasattr(ts, 'year'): if hasattr(ts, 'year'):
if getattr(ts, 'tzinfo', None) is None: if getattr(ts, 'tzinfo', None) is None:
return ts.replace(tzinfo=timezone.utc) return ts.replace(tzinfo=timezone.utc)
return ts return ts
# Konverter til string og parse RFC3339
ts = str(ts).rstrip("Z") ts = str(ts).rstrip("Z")
try: try:
clean_ts = ts[:19] # YYYY-MM-DDTHH:MM:SS clean_ts = ts[:19]
if "." in ts: if "." in ts:
frac = ts[20:26].split("+")[0].split("-")[0] if len(ts) > 20 else "000000" frac = ts[20:26].split("+")[0].split("-")[0] if len(ts) > 20 else "000000"
clean_ts = ts[:19] + "." + frac.ljust(6, "0")[:6] clean_ts = ts[:19] + "." + frac.ljust(6, "0")[:6]