Skip to content

Commit 3277da3

Browse files
balogh.adam@icloud.combalogh.adam@icloud.com
authored andcommitted
captcha verification
1 parent 74ae74a commit 3277da3

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

server/fastapi_server.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ async def require_api_key(x_api_key: str = Header(None)):
174174
raise HTTPException(status_code=401, detail="Invalid or missing API key")
175175
return x_api_key
176176

177+
async def verify_captcha_token(captchaToken: str):
178+
secret_key = os.getenv("CLOUDFLARE_TURNSTILE_SECRET_KEY")
179+
if not secret_key:
180+
raise Exception(
181+
"CLOUDFLARE_TURNSTILE_SECRET_KEY environment variable is not set"
182+
)
183+
184+
async with aiohttp.ClientSession() as session:
185+
async with session.post(
186+
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
187+
data={"secret": secret_key, "response": captchaToken},
188+
headers={"content-type": "application/x-www-form-urlencoded"},
189+
) as response:
190+
result = await response.json()
191+
if result.get("success"):
192+
return True
193+
else:
194+
logging.error(f"Captcha verification failed: {result}")
195+
return False
196+
177197
# Routes
178198
@app.post("/api/cloudflare/turnstile/v0/siteverify")
179199
async def verify_cloudflare_turnstile_token(request: Request):
@@ -260,8 +280,8 @@ async def run_agent(
260280
agent_request = AgentChatRequest(**request_data)
261281

262282
if agent_request.captchaToken:
263-
# TODO: Verify captcha token
264-
pass
283+
if not await verify_captcha_token(agent_request.captchaToken):
284+
raise HTTPException(status_code=429, detail="Invalid captcha token")
265285

266286
# Increment message count, return 429 if limit reached
267287
if not await activity_tracker.increment_message_count(
@@ -299,8 +319,8 @@ async def run_suggestions(
299319
agent_request = AgentChatRequest(**request_data)
300320

301321
if agent_request.captchaToken:
302-
# TODO: Verify captcha token
303-
pass
322+
if not await verify_captcha_token(agent_request.captchaToken):
323+
raise HTTPException(status_code=429, detail="Invalid captcha token")
304324

305325
portfolio = Portfolio(holdings=[], total_value_usd=0)
306326
suggestions = await handle_suggestions_request(

0 commit comments

Comments
 (0)