|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2021 Google, Inc |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +# All Rights Reserved. |
| 17 | + |
| 18 | +# [START recaptcha_enterprise_create_assessment] |
| 19 | + |
| 20 | +from google.cloud import recaptchaenterprise_v1 |
| 21 | +from google.cloud.recaptchaenterprise_v1 import Assessment |
| 22 | + |
| 23 | + |
| 24 | +def create_assessment( |
| 25 | + project_id: str, recaptcha_site_key: str, token: str, recaptcha_action: str |
| 26 | +) -> Assessment: |
| 27 | + """Create an assessment to analyze the risk of a UI action. |
| 28 | + Args: |
| 29 | + project_id: GCloud Project ID |
| 30 | + recaptcha_site_key: Site key obtained by registering a domain/app to use recaptcha services. |
| 31 | + token: The token obtained from the client on passing the recaptchaSiteKey. |
| 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_site_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 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, |
| 73 | + # see: 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 | + |
| 86 | +# [END recaptcha_enterprise_create_assessment] |
0 commit comments