Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-recaptcha-enterprise #8532

Merged
merged 81 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 73 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
b3672ed
docs(samples): add reCAPTCHA Enterprise code samples (#112)
Sita04 Oct 13, 2021
f2bec72
chore(deps): update all dependencies (#136)
renovate-bot Oct 13, 2021
e9e38c9
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Oct 25, 2021
13525e0
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Nov 2, 2021
c6ab1e1
docs(samples): removed assessment name in create_assessment sample (#…
Sita04 Nov 3, 2021
56d156a
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Nov 4, 2021
07ed100
chore: delete owlbot.py (#151)
parthea Nov 12, 2021
d90d9df
docs(samples): added sample and tests for annotate assessment API (#155)
Sita04 Nov 16, 2021
4045b5c
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Nov 18, 2021
e3055e1
docs(samples): added samples and test to migrate key and get metrics …
Sita04 Nov 19, 2021
2f7a4fc
chore(deps): update all dependencies (#177)
renovate-bot Jan 19, 2022
43ff382
chore(deps): update all dependencies (#186)
renovate-bot Feb 26, 2022
1b1d061
chore(deps): update all dependencies (#190)
renovate-bot Feb 28, 2022
474dd67
chore(deps): update all dependencies (#192)
renovate-bot Mar 1, 2022
9ce8584
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Mar 7, 2022
21e3146
chore(deps): update dependency selenium to v4.1.3 (#203)
renovate-bot Mar 9, 2022
4314004
chore(deps): update dependency pytest to v7.1.0 (#204)
renovate-bot Mar 13, 2022
cd20532
chore(deps): update dependency pytest to v7.1.1 (#206)
renovate-bot Mar 19, 2022
191c2c0
chore(python): use black==22.3.0 (#209)
gcf-owl-bot[bot] Mar 28, 2022
cbb798a
chore(deps): update dependency flask to v2.1.0 (#208)
renovate-bot Mar 30, 2022
54a96f4
chore(deps): update dependency flask to v2.1.1 (#212)
renovate-bot Apr 1, 2022
4a2567f
chore(python): add nox session to sort python imports (#220)
gcf-owl-bot[bot] Apr 21, 2022
7ff04db
chore(deps): update dependency pytest to v7.1.2 (#223)
renovate-bot Apr 25, 2022
b381127
chore(deps): update dependency flask to v2.1.2 (#225)
renovate-bot Apr 28, 2022
2234111
chore(deps): update dependency selenium to v4.1.5 (#231)
renovate-bot May 5, 2022
65bb52c
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot May 19, 2022
f347c33
chore(deps): update dependency selenium to v4.2.0 (#257)
renovate-bot May 27, 2022
48cb626
chore(deps): update all dependencies (#263)
renovate-bot Jul 13, 2022
9329fb6
chore(deps): update all dependencies (#275)
renovate-bot Aug 2, 2022
e7ae863
chore(deps): update all dependencies (#276)
renovate-bot Aug 5, 2022
7fe9912
chore(deps): update all dependencies (#277)
renovate-bot Aug 6, 2022
7afbd61
chore(deps): update all dependencies (#279)
renovate-bot Aug 9, 2022
30ac9ba
chore(deps): update all dependencies (#280)
renovate-bot Aug 9, 2022
8e2cf27
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Aug 15, 2022
21bba1b
chore(deps): update dependency selenium to v4.4.3 (#286)
renovate-bot Aug 23, 2022
a1c246c
chore(deps): update dependency pytest to v7.1.3 (#297)
renovate-bot Sep 6, 2022
b33f844
chore(deps): update dependency selenium to v4.5.0 (#305)
renovate-bot Oct 3, 2022
7074f70
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Oct 4, 2022
5579259
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Oct 10, 2022
09f7c85
chore(deps): update dependency pytest to v7.2.0 (#313)
renovate-bot Oct 26, 2022
42baf14
chore(deps): update dependency google-cloud-recaptcha-enterprise to v…
renovate-bot Oct 28, 2022
b5738d9
Merge remote-tracking branch 'migration/main' into python-recaptcha-e…
msampathkumar Nov 18, 2022
79ea7a5
Update copyright header
msampathkumar Nov 18, 2022
608bc03
Fix: selenium web-driver path issue
msampathkumar Nov 18, 2022
77779ca
Update CODEOWNERS and blunderbuss.yml
msampathkumar Nov 18, 2022
7235b42
Update copyright headers
msampathkumar Nov 18, 2022
c1a8d63
Update import order in test_create_assessment.py
msampathkumar Nov 18, 2022
76e0283
Update import order in test_create_assessment.py
msampathkumar Nov 18, 2022
38c3649
Update .github/CODEOWNERS
msampathkumar Nov 18, 2022
ac058cf
Update .github/blunderbuss.yml
dandhlee Nov 18, 2022
02e6ae5
Merge branch 'main' into python-recaptcha-enterprise-migration
msampathkumar Nov 21, 2022
b265d83
Merge branch 'main' into python-recaptcha-enterprise-migration
msampathkumar Nov 22, 2022
c7974d0
chore(samples): add Selenium resources for Docker
dandhlee Nov 22, 2022
0e69f28
Merge branch 'main' into python-recaptcha-enterprise-migration
msampathkumar Nov 22, 2022
a58e47b
Merge branch 'main' into python-recaptcha-enterprise-migration
dandhlee Nov 23, 2022
3331c03
Update blunderbuss.yml
msampathkumar Nov 24, 2022
c492f44
Update blunderbuss.yml
msampathkumar Nov 24, 2022
59011c7
Merge remote-tracking branch 'origin/python-recaptcha-enterprise-migr…
msampathkumar Nov 24, 2022
7c8ea4b
Update blunderbuss.yml
msampathkumar Nov 24, 2022
57c7864
Update .github/blunderbuss.yml
msampathkumar Nov 24, 2022
0915526
Update CODEOWNERS
msampathkumar Nov 28, 2022
feadb54
Update noxfile_config.py
msampathkumar Nov 29, 2022
36e17f3
Update noxfile_config.py
msampathkumar Nov 29, 2022
da16a09
Update blunderbuss.yml
msampathkumar Nov 29, 2022
7e79b72
Merge branch 'main' into python-recaptcha-enterprise-migration
Sita04 Jan 30, 2023
a13a9dd
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 3, 2023
e3ca918
Updates to CODEOWNERS and blunderbuss
rsamborski Feb 3, 2023
dca90dc
Fixed deprecation warnings in tests
rsamborski Feb 3, 2023
a23d79f
Fixed lint - import order
rsamborski Feb 3, 2023
03aeddf
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 3, 2023
e6732cb
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 6, 2023
a5b6ad6
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 8, 2023
8416a84
Merge branch 'main' into python-recaptcha-enterprise-migration
iennae Feb 10, 2023
37df22d
CODEOWNERS team rename aap-dpes -> torus-dpe
rsamborski Feb 10, 2023
60733fa
Merge branch 'main' into python-recaptcha-enterprise-migration
iennae Feb 10, 2023
4092007
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 13, 2023
c8b5b20
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 24, 2023
636f26b
Merge branch 'main' into python-recaptcha-enterprise-migration
msampathkumar Feb 24, 2023
708ad0a
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 28, 2023
ae267ac
CODEOWNERS clean-up
rsamborski Feb 28, 2023
6a7879e
Merge branch 'main' into python-recaptcha-enterprise-migration
rsamborski Feb 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
/billing/**/* @GoogleCloudPlatform/billing-samples-maintainers @GoogleCloudPlatform/python-samples-reviewers
/blog/**/* @GoogleCloudPlatform/python-samples-reviewers
/cdn/**/* @mpwarres @GoogleCloudPlatform/python-samples-reviewers
/cloudbuild/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/cloud-sql/**/* @GoogleCloudPlatform/infra-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/cloud_tasks/**/* @GoogleCloudPlatform/infra-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/cloudbuild/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/codelabs/**/* @GoogleCloudPlatform/python-samples-reviewers
/composer/**/* @leahecole @rachael-ds @GoogleCloudPlatform/cloud-dpes-composer @GoogleCloudPlatform/python-samples-reviewers
/compute/**/* @m-strzelczyk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -39,9 +39,11 @@
/contentwarehouse/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/data-science-onramp/ @leahecole @bradmiro @GoogleCloudPlatform/python-samples-reviewers
/datacatalog/**/* @GoogleCloudPlatform/python-samples-reviewers
/datacatalog/**/* @GoogleCloudPlatform/python-samples-reviewers
/dataflow/**/* @davidcavazos @GoogleCloudPlatform/python-samples-reviewers
/datalabeling/**/* @GoogleCloudPlatform/python-samples-reviewers @ivanmkc
/dataproc/**/* @GoogleCloudPlatform/python-samples-reviewers
/dataproc/**/** @GoogleCloudPlatform/cloud-dpes @GoogleCloudPlatform/python-samples-reviewers
/datastore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/dialogflow/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/dialogflow-cx/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -51,6 +53,7 @@
/enterpriseknowledgegraph/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/eventarc/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/error_reporting/**/* @GoogleCloudPlatform/python-samples-reviewers
/eventarc/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
rsamborski marked this conversation as resolved.
Show resolved Hide resolved
/firestore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/functions/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/functions/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -61,6 +64,7 @@
/iot/**/* @gcseh @GoogleCloudPlatform/api-iot @GoogleCloudPlatform/python-samples-reviewers
/jobs/**/* @GoogleCloudPlatform/python-samples-reviewers
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/kms/**/** @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/kubernetes_engine/**/* @GoogleCloudPlatform/python-samples-reviewers
/kubernetes_engine/django_tutorial/**/* @glasnt @GoogleCloudPlatform/python-samples-reviewers
/language/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -72,12 +76,13 @@
/monitoring/opencensus @yuriatgoogle @GoogleCloudPlatform/dee-observability @GoogleCloudPlatform/python-samples-reviewers
/monitoring/prometheus @yuriatgoogle @GoogleCloudPlatform/dee-observability @GoogleCloudPlatform/python-samples-reviewers
/notebooks/**/* @alixhami @GoogleCloudPlatform/python-samples-reviewers
/optimization/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/opencensus/**/* @GoogleCloudPlatform/python-samples-reviewers
/optimization/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/people-and-planet-ai/**/* @davidcavazos @GoogleCloudPlatform/python-samples-reviewers
/profiler/**/* @GoogleCloudPlatform/python-samples-reviewers
/pubsub/**/* @anguillanneuf @hongalex @GoogleCloudPlatform/python-samples-reviewers
/pubsublite/**/* @anguillanneuf @hongalex @GoogleCloudPlatform/python-samples-reviewers
/recaptcha_enterprise/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/retail/**/* @GoogleCloudPlatform/cloud-retail-team @GoogleCloudPlatform/python-samples-reviewers
/run/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/run/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -86,10 +91,10 @@
/speech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
/storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
/talent/**/* @GoogleCloudPlatform/python-samples-reviewers
/texttospeech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/trace/**/* @ymotongpoo @GoogleCloudPlatform/dee-observability @GoogleCloudPlatform/python-samples-reviewers
/translate/**/* @nicain @GoogleCloudPlatform/python-samples-reviewers
/talent/**/* @GoogleCloudPlatform/python-samples-reviewers
/vision/**/* @GoogleCloudPlatform/python-samples-reviewers
/workflows/**/* @GoogleCloudPlatform/python-samples-reviewers
/privateca/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
1 change: 1 addition & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ assign_issues_by:
- 'api: cloudkms'
- 'api: secretmanager'
- 'api: privateca'
- 'api: recaptchaenterprise'
to:
- GoogleCloudPlatform/dee-infra
- labels:
Expand Down
17 changes: 17 additions & 0 deletions .kokoro/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,21 @@ RUN useradd -d /h -u ${UID} ${USERNAME}
# Allow nopasswd sudo
RUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Install Chrome.
RUN curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
&& cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
&& echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
&& mkdir -p /usr/share/desktop-directories \
&& apt-get -y update && apt-get install -y google-chrome-stable

# Disable the SUID sandbox so that Chrome can launch without being in a privileged container.
RUN dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
&& echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
&& chmod 755 /opt/google/chrome/google-chrome

# Install Chrome Driver.
RUN mkdir -p /opt/selenium \
&& curl http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
&& cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;

CMD ["python3"]
46 changes: 46 additions & 0 deletions recaptcha_enterprise/snippets/annotate_assessment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python
# Copyright 2021 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# All Rights Reserved.

# [START recaptcha_enterprise_annotate_assessment]
from google.cloud import recaptchaenterprise_v1


def annotate_assessment(project_id: str, assessment_id: str) -> None:
"""Pre-requisite: Create an assessment before annotating.
Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
Args:
project_id: Google Cloud Project ID
assessment_id: Value of the 'name' field returned from the create_assessment() call.
"""

client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

assessment_name = f"projects/{project_id}/assessments/{assessment_id}"
# Build the annotation request.
# For more info on when/how to annotate, see:
# https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
request = recaptchaenterprise_v1.AnnotateAssessmentRequest()
request.name = assessment_name
request.annotation = request.Annotation.FRAUDULENT
request.reasons = [request.Reason.FAILED_TWO_FACTOR]

# Empty response is sent back.
client.annotate_assessment(request)
print("Annotated response sent successfully ! ")


# [END recaptcha_enterprise_annotate_assessment]
86 changes: 86 additions & 0 deletions recaptcha_enterprise/snippets/create_assessment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env python
# Copyright 2021 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# All Rights Reserved.

# [START recaptcha_enterprise_create_assessment]

from google.cloud import recaptchaenterprise_v1
from google.cloud.recaptchaenterprise_v1 import Assessment


def create_assessment(
project_id: str, recaptcha_site_key: str, token: str, recaptcha_action: str
) -> Assessment:
"""Create an assessment to analyze the risk of a UI action.
Args:
project_id: GCloud Project ID
recaptcha_site_key: Site key obtained by registering a domain/app to use recaptcha services.
token: The token obtained from the client on passing the recaptchaSiteKey.
recaptcha_action: Action name corresponding to the token.
"""

client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

# Set the properties of the event to be tracked.
event = recaptchaenterprise_v1.Event()
event.site_key = recaptcha_site_key
event.token = token

assessment = recaptchaenterprise_v1.Assessment()
assessment.event = event

project_name = f"projects/{project_id}"

# Build the assessment request.
request = recaptchaenterprise_v1.CreateAssessmentRequest()
request.assessment = assessment
request.parent = project_name

response = client.create_assessment(request)

# Check if the token is valid.
if not response.token_properties.valid:
print(
"The CreateAssessment call failed because the token was "
+ "invalid for for the following reasons: "
+ str(response.token_properties.invalid_reason)
)
return

# Check if the expected action was executed.
if response.token_properties.action != recaptcha_action:
print(
"The action attribute in your reCAPTCHA tag does"
+ "not match the action you are expecting to score"
)
return
else:
# Get the risk score and the reason(s)
# For more information on interpreting the assessment,
# see: https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
for reason in response.risk_analysis.reasons:
print(reason)
print(
"The reCAPTCHA score for this token is: "
+ str(response.risk_analysis.score)
)
# Get the assessment name (id). Use this to annotate the assessment.
assessment_name = client.parse_assessment_path(response.name).get("assessment")
print(f"Assessment name: {assessment_name}")
return response


# [END recaptcha_enterprise_create_assessment]
69 changes: 69 additions & 0 deletions recaptcha_enterprise/snippets/create_site_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# Copyright 2021 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# All Rights Reserved.

# [START recaptcha_enterprise_create_site_key]
from google.cloud import recaptchaenterprise_v1


def create_site_key(project_id: str, domain_name: str) -> str:
"""Create reCAPTCHA Site key which binds a domain name to a unique key.
Args:
project_id : GCloud Project ID.
domain_name: Specify the domain name in which the reCAPTCHA should be activated.
"""
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

# Set the type of the reCAPTCHA to be displayed.
# For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
web_settings = recaptchaenterprise_v1.WebKeySettings()
web_settings.allowed_domains.append(domain_name)
web_settings.allow_amp_traffic = False
web_settings.integration_type = web_settings.IntegrationType.SCORE

key = recaptchaenterprise_v1.Key()
key.display_name = "any descriptive name for the key"
key.web_settings = web_settings

# Create the request.
request = recaptchaenterprise_v1.CreateKeyRequest()
request.parent = f"projects/{project_id}"
request.key = key

# Get the name of the created reCAPTCHA site key.
response = client.create_key(request)
recaptcha_site_key = response.name.rsplit("/", maxsplit=1)[1]
print("reCAPTCHA Site key created successfully. Site Key: " + recaptcha_site_key)
return recaptcha_site_key


# [END recaptcha_enterprise_create_site_key]

if __name__ == "__main__":
import google.auth
import google.auth.exceptions

# TODO(developer): Replace the below variables before running
try:
default_project_id = google.auth.default()[1]
domain_name = "localhost"
except google.auth.exceptions.DefaultCredentialsError:
print(
"Please use `gcloud auth application-default login` "
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
)
else:
create_site_key(default_project_id, domain_name)
60 changes: 60 additions & 0 deletions recaptcha_enterprise/snippets/delete_site_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python
# Copyright 2021 Google, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# All Rights Reserved.

# [START recaptcha_enterprise_delete_site_key]
from google.cloud import recaptchaenterprise_v1


def delete_site_key(project_id: str, recaptcha_site_key: str) -> None:
"""Delete the given reCAPTCHA site key present under the project ID.

Args:
project_id : GCloud Project ID.
recaptcha_site_key: Specify the key ID to be deleted.
"""

client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

# Construct the key details.
key_name = f"projects/{project_id}/keys/{recaptcha_site_key}"

# Set the project ID and reCAPTCHA site key.
request = recaptchaenterprise_v1.DeleteKeyRequest()
request.name = key_name

client.delete_key(request)
print("reCAPTCHA Site key deleted successfully ! ")


# [END recaptcha_enterprise_delete_site_key]


if __name__ == "__main__":
import google.auth
import google.auth.exceptions

# TODO(developer): Replace the below variables before running
try:
default_project_id = google.auth.default()[1]
recaptcha_site_key = "recaptcha_site_key"
except google.auth.exceptions.DefaultCredentialsError:
print(
"Please use `gcloud auth application-default login` "
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
)
else:
delete_site_key(default_project_id, recaptcha_site_key)
Loading