Skip to content

Commit e50bdab

Browse files
authored
add cloudflare turnstile verification endpoint (#78)
1 parent 4e4c59a commit e50bdab

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ export FIREBASE_CLIENT_X509_CERT_URL=''
1919

2020
# Skip token authentication on endpoints.
2121
export SKIP_TOKEN_AUTH_HEADER=''
22-
export SKIP_TOKEN_AUTH_KEY=''
22+
export SKIP_TOKEN_AUTH_KEY=''
23+
24+
# Cloudflare Turnstile Config
25+
export CLOUDFLARE_TURNSTILE_SECRET_KEY=''

server/server.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from functools import wraps
1515
from datadog import initialize, statsd
1616
import logging
17+
import requests
1718

1819
import boto3.data
1920
from onchain.pools.protocol import ProtocolRegistry
@@ -155,6 +156,39 @@ def handle_generic_error(e):
155156

156157
return jsonify({"error": str(e)}), 500
157158

159+
@app.route("/api/cloudflare/turnstile/v0/siteverify", methods=["POST"])
160+
def verify_cloudflare_turnstile_token():
161+
try:
162+
secret_key = os.getenv("CLOUDFLARE_TURNSTILE_SECRET_KEY")
163+
if not secret_key:
164+
raise Exception("CLOUDFLARE_TURNSTILE_SECRET_KEY environment variable is not set")
165+
166+
data = request.get_json()
167+
token = data.get('token')
168+
169+
if not token:
170+
return jsonify({"error": "Missing token"}), 400
171+
172+
# Make the request to Cloudflare Turnstile
173+
response = requests.post(
174+
'https://challenges.cloudflare.com/turnstile/v0/siteverify',
175+
data={
176+
'secret': secret_key,
177+
'response': token
178+
},
179+
headers={
180+
'content-type': 'application/x-www-form-urlencoded'
181+
}
182+
)
183+
184+
result = response.json()
185+
status_code = 200 if result.get('success') else 400
186+
return jsonify(result), status_code
187+
188+
except Exception as e:
189+
logging.error(f"Error verifying Cloudflare Turnstile token: {e}")
190+
return jsonify({"error": "Internal server error"}), 500
191+
158192
@app.route("/api/verify/solana", methods=["POST"])
159193
def verify_solana_signature():
160194
try:

0 commit comments

Comments
 (0)