Update main.py
Browse files
main.py
CHANGED
@@ -18,11 +18,69 @@ app = FastAPI()
|
|
18 |
#router = APIRouter()
|
19 |
templates = Jinja2Templates(directory="templates")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Dependency for verifying the user's token
|
28 |
def get_current_user(token: str = Depends(verify_token)):
|
@@ -90,8 +148,26 @@ async def register_post(
|
|
90 |
email: str = Form(...),
|
91 |
password: str = Form(...),
|
92 |
confirm_password: str = Form(...),
|
|
|
93 |
db: Session = Depends(get_db)
|
94 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
if password != confirm_password:
|
96 |
# Return to the registration page with an error
|
97 |
return templates.TemplateResponse("register.html", {
|
|
|
18 |
#router = APIRouter()
|
19 |
templates = Jinja2Templates(directory="templates")
|
20 |
|
21 |
+
from google.cloud import recaptchaenterprise_v1
|
22 |
+
from google.cloud.recaptchaenterprise_v1 import Assessment
|
23 |
|
24 |
+
def create_assessment(
|
25 |
+
project_id: str, recaptcha_key: str, token: str, recaptcha_action: str
|
26 |
+
) -> Assessment:
|
27 |
+
"""Create an assessment to analyse the risk of a UI action.
|
28 |
+
Args:
|
29 |
+
project_id: Your Google Cloud project ID.
|
30 |
+
recaptcha_key: The reCAPTCHA key associated with the site/app
|
31 |
+
token: The generated token obtained from the client.
|
32 |
+
recaptcha_action: Action name corresponding to the token.
|
33 |
+
"""
|
34 |
+
|
35 |
+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
|
36 |
+
|
37 |
+
# Set the properties of the event to be tracked.
|
38 |
+
event = recaptchaenterprise_v1.Event()
|
39 |
+
event.site_key = recaptcha_key
|
40 |
+
event.token = token
|
41 |
+
|
42 |
+
assessment = recaptchaenterprise_v1.Assessment()
|
43 |
+
assessment.event = event
|
44 |
+
|
45 |
+
project_name = f"projects/{project_id}"
|
46 |
+
|
47 |
+
# Build the assessment request.
|
48 |
+
request = recaptchaenterprise_v1.CreateAssessmentRequest()
|
49 |
+
request.assessment = assessment
|
50 |
+
request.parent = project_name
|
51 |
+
|
52 |
+
response = client.create_assessment(request)
|
53 |
+
|
54 |
+
# Check if the token is valid.
|
55 |
+
if not response.token_properties.valid:
|
56 |
+
print(
|
57 |
+
"The CreateAssessment call failed because the token was "
|
58 |
+
+ "invalid for the following reasons: "
|
59 |
+
+ str(response.token_properties.invalid_reason)
|
60 |
+
)
|
61 |
+
return
|
62 |
+
|
63 |
+
# Check if the expected action was executed.
|
64 |
+
if response.token_properties.action != recaptcha_action:
|
65 |
+
print(
|
66 |
+
"The action attribute in your reCAPTCHA tag does"
|
67 |
+
+ "not match the action you are expecting to score"
|
68 |
+
)
|
69 |
+
return
|
70 |
+
else:
|
71 |
+
# Get the risk score and the reason(s).
|
72 |
+
# For more information on interpreting the assessment, see:
|
73 |
+
# https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
|
74 |
+
for reason in response.risk_analysis.reasons:
|
75 |
+
print(reason)
|
76 |
+
print(
|
77 |
+
"The reCAPTCHA score for this token is: "
|
78 |
+
+ str(response.risk_analysis.score)
|
79 |
+
)
|
80 |
+
# Get the assessment name (ID). Use this to annotate the assessment.
|
81 |
+
assessment_name = client.parse_assessment_path(response.name).get("assessment")
|
82 |
+
print(f"Assessment name: {assessment_name}")
|
83 |
+
return response
|
84 |
|
85 |
# Dependency for verifying the user's token
|
86 |
def get_current_user(token: str = Depends(verify_token)):
|
|
|
148 |
email: str = Form(...),
|
149 |
password: str = Form(...),
|
150 |
confirm_password: str = Form(...),
|
151 |
+
recaptcha_token: str = Form(...), # Add this line to accept the reCAPTCHA token
|
152 |
db: Session = Depends(get_db)
|
153 |
):
|
154 |
+
# Perform reCAPTCHA verification first
|
155 |
+
project_id = 'Loginauthc' # Replace with your project ID
|
156 |
+
recaptcha_key = '6LdaUQIpAAAAACQFcOxakEVXK9QHpaYbic6IClNO' # Replace with your site key
|
157 |
+
recaptcha_action = 'submit' # The action you're expecting
|
158 |
+
|
159 |
+
# Call the create_assessment function to validate the token
|
160 |
+
assessment = await create_assessment(
|
161 |
+
project_id, recaptcha_key, recaptcha_token, recaptcha_action
|
162 |
+
)
|
163 |
+
|
164 |
+
# Check the assessment result
|
165 |
+
if not assessment or assessment.risk_analysis.score < 0.5: # Use an appropriate risk score threshold
|
166 |
+
return templates.TemplateResponse("register.html", {
|
167 |
+
"request": request,
|
168 |
+
"error_message": "Captcha validation failed."
|
169 |
+
})
|
170 |
+
|
171 |
if password != confirm_password:
|
172 |
# Return to the registration page with an error
|
173 |
return templates.TemplateResponse("register.html", {
|