forked from FriendlyCaptcha/friendly-captcha-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (74 loc) · 3.03 KB
/
main.py
File metadata and controls
90 lines (74 loc) · 3.03 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
from friendly_captcha_client.client import FriendlyCaptchaClient, FriendlyCaptchaResult
app = FastAPI()
templates = Jinja2Templates(directory="./templates/")
FRC_SITEKEY = os.getenv("FRC_SITEKEY")
FRC_APIKEY = os.getenv("FRC_APIKEY")
# Optionally we can pass in custom endpoints to be used, such as "eu".
FRC_SITEVERIFY_ENDPOINT = os.getenv("FRC_SITEVERIFY_ENDPOINT")
FRC_WIDGET_ENDPOINT = os.getenv("FRC_WIDGET_ENDPOINT")
if not FRC_SITEKEY or not FRC_APIKEY:
print(
"Please set the FRC_SITEKEY and FRC_APIKEY environment values before running this example to your Friendly Captcha sitekey and API key respectively."
)
exit(1)
frc_client = FriendlyCaptchaClient(
api_key=FRC_APIKEY,
sitekey=FRC_SITEKEY,
siteverify_endpoint=FRC_SITEVERIFY_ENDPOINT, # Optional, defaults to "global"
strict=False,
)
@app.get("/")
def read_root(request: Request):
return templates.TemplateResponse(
"demo.html",
{
"request": request,
"message": "",
"sitekey": FRC_SITEKEY,
"widget_endpoint": FRC_WIDGET_ENDPOINT,
},
)
@app.post("/")
def post_form(
request: Request,
subject: str = Form(None),
message: str = Form(None),
frc_captcha_response: str = Form(..., alias="frc-captcha-response"),
):
result: FriendlyCaptchaResult = frc_client.verify_captcha_response(
frc_captcha_response
)
if not result.was_able_to_verify:
# In this case we were not actually able to verify the response embedded in the form, but we may still want to accept it.
# It could mean there is a network issue or that the service is down. In those cases you generally want to accept submissions anyhow
# That's why we use `shouldAccept()` below to actually accept or reject the form submission. It will return true in these cases.
if result.is_client_error:
# Something is wrong with our configuration, check your API key!
# Send yourself an alert to fix this! Your site is unprotected until you fix this.
print("CAPTCHA CONFIG ERROR: ", result.error)
else:
print("Failed to verify captcha response: ", result.error)
if not result.should_accept:
return templates.TemplateResponse(
"demo.html",
{
"request": request,
"message": "❌ Anti-robot check failed, please try again.",
"sitekey": FRC_SITEKEY,
"widget_endpoint": FRC_WIDGET_ENDPOINT,
},
)
# The captcha was OK, process the form.
subject, message # Normally we would use the form data here and submit it to our database.
return templates.TemplateResponse(
"demo.html",
{
"request": request,
"message": "✅ Your message has been submitted successfully.",
"sitekey": FRC_SITEKEY,
"widget_endpoint": FRC_WIDGET_ENDPOINT,
},
)