1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import fastapi
from fastapi.middleware.cors import CORSMiddleware
import requests
from dotenv import load_dotenv
from os import getenv
print("Hello, world!")
load_dotenv()
app = fastapi.FastAPI(title="altafcreator.com", decscription="altafcreator.com API", version="1.0")
TURNSTILE_SECRET = getenv("CLOUDFLARE_TURNSTILE_SECRET").encode("utf-8")
origins = [
"http://localhost",
"https://altafcreator.com",
"https://backend.altafcreator.com"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/email")
def email(token):
if validate_turnstile(token):
return {
"status": True,
"message": "here's my email",
"business": "business@altafcreator.com",
"personal": "altaf@altafcreator.com",
}
else:
return {
"status": False,
"message": "Turnstile verification invalid",
}
# https://developers.cloudflare.com/turnstile/get-started/server-side-validation/, modified
# returns response if success, returns None if unsuccessful.
def validate_turnstile(token, remoteip=None) -> tuple:
url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
data = {
'secret': TURNSTILE_SECRET,
'response': token
}
if remoteip:
data['remoteip'] = remoteip
try:
response = requests.post(url, data=data, timeout=10)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
print(f"Turnstile validation error: {e}")
return None
|