Skip to content

Commit 8254802

Browse files
msampathkumarSita04busunkim96partheagcf-owl-bot[bot]
authored
migrate code from googleapis/python-recaptcha-enterprise (#8532)
* docs(samples): add reCAPTCHA Enterprise code samples (#112) * docs(samples): added samples and tests for site key CRUD operation * docs(samples): added samples and tests for create assessment * docs(samples): modified requirements and test inputs * docs(samples): lint fix - adding copyright * fix(samples): added nox config and modified requirements-test * docs(samples): lint fix * refactor(samples): updated nox file * docs(samples): lint fix * docs(samples): added nox config * docs(samples): lint fix; modified nox config * refactor(samples): incorporated review comments * refactor(samples): lint fix * refactor(samples): lint fix * refactor(samples): lint fix * docs(samples): updated to use latest chrome version and lint fix * fix(samples): updated selenium to use chrome browser and added display name to update key * build: add placeholder dockerfile, update kokoro configs * Adding chrome installation in docker * refactor(samples): removed webdriver manager dependency. Included commands to install binary file in docker. * fix(samples): changed the chromedriver version to latest release. * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor(samples): included review comments Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update all dependencies (#136) * chore(deps): update all dependencies * fix: split selenium dependency on python 3.6 Co-authored-by: Bu Sun Kim <[email protected]> * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.3.0 (#139) Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.3.1 (#145) * docs(samples): removed assessment name in create_assessment sample (#147) * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.4.0 (#150) * chore: delete owlbot.py (#151) * chore: delete owlbot.py * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * use the latest post processor image * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * docs(samples): added sample and tests for annotate assessment API (#155) Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-recaptcha-enterprise/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #154 🦕 * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.4.1 (#159) * docs(samples): added samples and test to migrate key and get metrics (#161) Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-recaptcha-enterprise/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #160 🦕 * chore(deps): update all dependencies (#177) * chore(deps): update all dependencies * revert pin change for python 3.6 Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#186) * chore(deps): update all dependencies * chore: remove split pin on selenium We do not run tests on Python < 3.6. Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#190) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update all dependencies (#192) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.6.1 (#202) * chore(deps): update dependency selenium to v4.1.3 (#203) * chore(deps): update dependency pytest to v7.1.0 (#204) * chore(deps): update dependency pytest to v7.1.1 (#206) * chore(python): use black==22.3.0 (#209) Source-Link: googleapis/synthtool@6fab84a Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:7cffbc10910c3ab1b852c05114a08d374c195a81cdec1d4a67a1d129331d0bfe * chore(deps): update dependency flask to v2.1.0 (#208) * chore(deps): update dependency flask to v2.1.1 (#212) Co-authored-by: Anthonios Partheniou <[email protected]> * chore(python): add nox session to sort python imports (#220) Source-Link: googleapis/synthtool@1b71c10 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:00c9d764fd1cd56265f12a5ef4b99a0c9e87cf261018099141e2ca5158890416 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update dependency pytest to v7.1.2 (#223) * chore(deps): update dependency flask to v2.1.2 (#225) * chore(deps): update dependency selenium to v4.1.5 (#231) * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.7.0 (#234) * chore(deps): update dependency selenium to v4.2.0 (#257) * chore(deps): update all dependencies (#263) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#275) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert * test: find_element_by_id->find_element * test: add import from selenium.webdriver.common.by import By Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#276) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#277) * chore(deps): update all dependencies * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * revert Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update all dependencies (#279) * chore(deps): update all dependencies * revert * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update all dependencies (#280) * chore(deps): update all dependencies * revert * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.8.1 (#285) * chore(deps): update dependency selenium to v4.4.3 (#286) * chore(deps): update dependency pytest to v7.1.3 (#297) * chore(deps): update dependency selenium to v4.5.0 (#305) Co-authored-by: Anthonios Partheniou <[email protected]> * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.8.2 (#307) * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.8.3 (#310) * chore(deps): update dependency pytest to v7.2.0 (#313) * chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.9.0 (#314) * Update copyright header * Fix: selenium web-driver path issue * Update CODEOWNERS and blunderbuss.yml * Update copyright headers change "Google Inc" to "Google, Inc" * Update import order in test_create_assessment.py * Update import order in test_create_assessment.py * Update .github/CODEOWNERS Co-authored-by: Dan Lee <[email protected]> * Update .github/blunderbuss.yml * chore(samples): add Selenium resources for Docker * Update blunderbuss.yml * Update blunderbuss.yml * Update blunderbuss.yml * Update .github/blunderbuss.yml Co-authored-by: Dan Lee <[email protected]> * Update CODEOWNERS No addtions/deletions. Just sorted data. * Update noxfile_config.py Limit testing to latest versions only (3.9 and 3.10) * Update noxfile_config.py * Update blunderbuss.yml * Updates to CODEOWNERS and blunderbuss * Fixed deprecation warnings in tests * Fixed lint - import order * CODEOWNERS team rename aap-dpes -> torus-dpe * CODEOWNERS clean-up --------- Co-authored-by: Sita Lakshmi Sangameswaran <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Bu Sun Kim <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: WhiteSource Renovate <[email protected]> Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Dan Lee <[email protected]> Co-authored-by: Remigiusz Samborski <[email protected]> Co-authored-by: rsamborski <[email protected]> Co-authored-by: Jennifer Davis <[email protected]>
1 parent f4cd664 commit 8254802

17 files changed

+937
-9
lines changed

.github/CODEOWNERS

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
/.kokoro/ @GoogleCloudPlatform/python-samples-owners
1616
/* @GoogleCloudPlatform/python-samples-owners
1717

18-
/appengine/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
19-
/appengine/standard/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
20-
/appengine/flexible/django_cloudsql/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
18+
/appengine/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
19+
/appengine/standard/django/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
20+
/appengine/flexible/django_cloudsql/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
2121
/appengine/standard_python3/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
2222
/asset/**/* @GoogleCloudPlatform/python-samples-reviewers
2323
/auth/**/* @arithmetic1728 @GoogleCloudPlatform/python-samples-reviewers
@@ -35,7 +35,7 @@
3535
/composer/**/* @leahecole @rachael-ds @GoogleCloudPlatform/cloud-dpes-composer @GoogleCloudPlatform/python-samples-reviewers
3636
/compute/**/* @m-strzelczyk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
3737
/container/**/* @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/python-samples-reviewers
38-
/containeranalysis/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
38+
/containeranalysis/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
3939
/contentwarehouse/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
4040
/data-science-onramp/ @leahecole @bradmiro @GoogleCloudPlatform/python-samples-reviewers
4141
/datacatalog/**/* @GoogleCloudPlatform/python-samples-reviewers
@@ -49,10 +49,10 @@
4949
/documentai/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
5050
/endpoints/**/* @GoogleCloudPlatform/python-samples-reviewers
5151
/enterpriseknowledgegraph/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
52-
/eventarc/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
52+
/eventarc/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
5353
/error_reporting/**/* @GoogleCloudPlatform/python-samples-reviewers
5454
/firestore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
55-
/functions/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
55+
/functions/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
5656
/functions/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
5757
/healthcare/**/* @noerog @GoogleCloudPlatform/python-samples-reviewers
5858
/iam/api-client/**/* @GoogleCloudPlatform/python-samples-reviewers
@@ -78,10 +78,11 @@
7878
/profiler/**/* @GoogleCloudPlatform/python-samples-reviewers
7979
/pubsub/**/* @anguillanneuf @hongalex @GoogleCloudPlatform/python-samples-reviewers
8080
/pubsublite/**/* @anguillanneuf @hongalex @GoogleCloudPlatform/python-samples-reviewers
81+
/recaptcha_enterprise/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
8182
/retail/**/* @GoogleCloudPlatform/cloud-retail-team @GoogleCloudPlatform/python-samples-reviewers
82-
/run/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
83-
/run/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
84-
/secretmanager/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
83+
/run/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
84+
/run/django/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
85+
/secretmanager/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
8586
/securitycenter/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
8687
/speech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
8788
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ assign_issues_by:
156156
- 'api: cloudkms'
157157
- 'api: secretmanager'
158158
- 'api: privateca'
159+
- 'api: recaptchaenterprise'
159160
to:
160161
- GoogleCloudPlatform/dee-infra
161162
- labels:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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_annotate_assessment]
19+
from google.cloud import recaptchaenterprise_v1
20+
21+
22+
def annotate_assessment(project_id: str, assessment_id: str) -> None:
23+
"""Pre-requisite: Create an assessment before annotating.
24+
Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
25+
Args:
26+
project_id: Google Cloud Project ID
27+
assessment_id: Value of the 'name' field returned from the create_assessment() call.
28+
"""
29+
30+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
31+
32+
assessment_name = f"projects/{project_id}/assessments/{assessment_id}"
33+
# Build the annotation request.
34+
# For more info on when/how to annotate, see:
35+
# https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
36+
request = recaptchaenterprise_v1.AnnotateAssessmentRequest()
37+
request.name = assessment_name
38+
request.annotation = request.Annotation.FRAUDULENT
39+
request.reasons = [request.Reason.FAILED_TWO_FACTOR]
40+
41+
# Empty response is sent back.
42+
client.annotate_assessment(request)
43+
print("Annotated response sent successfully ! ")
44+
45+
46+
# [END recaptcha_enterprise_annotate_assessment]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_site_key]
19+
from google.cloud import recaptchaenterprise_v1
20+
21+
22+
def create_site_key(project_id: str, domain_name: str) -> str:
23+
"""Create reCAPTCHA Site key which binds a domain name to a unique key.
24+
Args:
25+
project_id : GCloud Project ID.
26+
domain_name: Specify the domain name in which the reCAPTCHA should be activated.
27+
"""
28+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
29+
30+
# Set the type of the reCAPTCHA to be displayed.
31+
# For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
32+
web_settings = recaptchaenterprise_v1.WebKeySettings()
33+
web_settings.allowed_domains.append(domain_name)
34+
web_settings.allow_amp_traffic = False
35+
web_settings.integration_type = web_settings.IntegrationType.SCORE
36+
37+
key = recaptchaenterprise_v1.Key()
38+
key.display_name = "any descriptive name for the key"
39+
key.web_settings = web_settings
40+
41+
# Create the request.
42+
request = recaptchaenterprise_v1.CreateKeyRequest()
43+
request.parent = f"projects/{project_id}"
44+
request.key = key
45+
46+
# Get the name of the created reCAPTCHA site key.
47+
response = client.create_key(request)
48+
recaptcha_site_key = response.name.rsplit("/", maxsplit=1)[1]
49+
print("reCAPTCHA Site key created successfully. Site Key: " + recaptcha_site_key)
50+
return recaptcha_site_key
51+
52+
53+
# [END recaptcha_enterprise_create_site_key]
54+
55+
if __name__ == "__main__":
56+
import google.auth
57+
import google.auth.exceptions
58+
59+
# TODO(developer): Replace the below variables before running
60+
try:
61+
default_project_id = google.auth.default()[1]
62+
domain_name = "localhost"
63+
except google.auth.exceptions.DefaultCredentialsError:
64+
print(
65+
"Please use `gcloud auth application-default login` "
66+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
67+
)
68+
else:
69+
create_site_key(default_project_id, domain_name)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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_delete_site_key]
19+
from google.cloud import recaptchaenterprise_v1
20+
21+
22+
def delete_site_key(project_id: str, recaptcha_site_key: str) -> None:
23+
"""Delete the given reCAPTCHA site key present under the project ID.
24+
25+
Args:
26+
project_id : GCloud Project ID.
27+
recaptcha_site_key: Specify the key ID to be deleted.
28+
"""
29+
30+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
31+
32+
# Construct the key details.
33+
key_name = f"projects/{project_id}/keys/{recaptcha_site_key}"
34+
35+
# Set the project ID and reCAPTCHA site key.
36+
request = recaptchaenterprise_v1.DeleteKeyRequest()
37+
request.name = key_name
38+
39+
client.delete_key(request)
40+
print("reCAPTCHA Site key deleted successfully ! ")
41+
42+
43+
# [END recaptcha_enterprise_delete_site_key]
44+
45+
46+
if __name__ == "__main__":
47+
import google.auth
48+
import google.auth.exceptions
49+
50+
# TODO(developer): Replace the below variables before running
51+
try:
52+
default_project_id = google.auth.default()[1]
53+
recaptcha_site_key = "recaptcha_site_key"
54+
except google.auth.exceptions.DefaultCredentialsError:
55+
print(
56+
"Please use `gcloud auth application-default login` "
57+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
58+
)
59+
else:
60+
delete_site_key(default_project_id, recaptcha_site_key)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_get_metrics_site_key]
19+
from google.cloud import recaptchaenterprise_v1
20+
21+
22+
def get_metrics(project_id: str, recaptcha_site_key: str) -> None:
23+
"""Get metrics specific to a recaptcha site key.
24+
E.g: score bucket count for a key or number of
25+
times the checkbox key failed/ passed etc.,
26+
Args:
27+
project_id: Google Cloud Project ID.
28+
recaptcha_site_key: Specify the site key to get metrics.
29+
"""
30+
31+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
32+
33+
metrics_name = f"projects/{project_id}/keys/{recaptcha_site_key}/metrics"
34+
request = recaptchaenterprise_v1.GetMetricsRequest()
35+
request.name = metrics_name
36+
37+
response = client.get_metrics(request)
38+
39+
# Retrieve the metrics you want from the key.
40+
# If the site key is checkbox type: then use response.challenge_metrics
41+
# instead of response.score_metrics
42+
for day_metric in response.score_metrics:
43+
# Each 'day_metric' is in the granularity of one day.
44+
score_bucket_count = day_metric.overall_metrics.score_buckets
45+
print(score_bucket_count)
46+
47+
print(f"Retrieved the bucket count for score based key: {recaptcha_site_key}")
48+
49+
50+
# [END recaptcha_enterprise_get_metrics_site_key]

0 commit comments

Comments
 (0)