Skip to content

Commit

Permalink
migrate code from googleapis/python-recaptcha-enterprise (#8532)
Browse files Browse the repository at this point in the history
* 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 <busunkim@google.com>
Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
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 <8822365+busunkim96@users.noreply.github.com>

* chore(deps): update dependency google-cloud-recaptcha-enterprise to v1.3.0 (#139)

Co-authored-by: Anthonios Partheniou <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <partheniou@google.com>

* 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 <partheniou@google.com>
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 <partheniou@google.com>
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 <partheniou@google.com>

* 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 <71398022+dandhlee@users.noreply.github.com>

* 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 <71398022+dandhlee@users.noreply.github.com>

* 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 <sita1996@gmail.com>
Co-authored-by: Bu Sun Kim <busunkim@google.com>
Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: WhiteSource Renovate <renovate@whitesourcesoftware.com>
Co-authored-by: WhiteSource Renovate <bot@renovateapp.com>
Co-authored-by: gcf-owl-bot[bot] <78513119+gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Dan Lee <71398022+dandhlee@users.noreply.github.com>
Co-authored-by: Remigiusz Samborski <rsamborski@users.noreply.github.com>
Co-authored-by: rsamborski <rsamborski@gmail.com>
Co-authored-by: Jennifer Davis <iennae@gmail.com>
  • Loading branch information
13 people authored Feb 28, 2023
1 parent f4cd664 commit 8254802
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 9 deletions.
19 changes: 10 additions & 9 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
/.kokoro/ @GoogleCloudPlatform/python-samples-owners
/* @GoogleCloudPlatform/python-samples-owners

/appengine/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/appengine/standard/django/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/appengine/flexible/django_cloudsql/**/* @glasnt @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/appengine/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/appengine/standard/django/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/appengine/flexible/django_cloudsql/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/appengine/standard_python3/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
/asset/**/* @GoogleCloudPlatform/python-samples-reviewers
/auth/**/* @arithmetic1728 @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -35,7 +35,7 @@
/composer/**/* @leahecole @rachael-ds @GoogleCloudPlatform/cloud-dpes-composer @GoogleCloudPlatform/python-samples-reviewers
/compute/**/* @m-strzelczyk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/container/**/* @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/python-samples-reviewers
/containeranalysis/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/containeranalysis/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/contentwarehouse/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/data-science-onramp/ @leahecole @bradmiro @GoogleCloudPlatform/python-samples-reviewers
/datacatalog/**/* @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -49,10 +49,10 @@
/documentai/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/endpoints/**/* @GoogleCloudPlatform/python-samples-reviewers
/enterpriseknowledgegraph/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/eventarc/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/eventarc/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/error_reporting/**/* @GoogleCloudPlatform/python-samples-reviewers
/firestore/**/* @GoogleCloudPlatform/cloud-native-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/functions/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/functions/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/functions/spanner/* @GoogleCloudPlatform/api-spanner-python @GoogleCloudPlatform/python-samples-reviewers
/healthcare/**/* @noerog @GoogleCloudPlatform/python-samples-reviewers
/iam/api-client/**/* @GoogleCloudPlatform/python-samples-reviewers
Expand All @@ -78,10 +78,11 @@
/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
/secretmanager/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
/run/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/run/django/**/* @glasnt @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/secretmanager/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/securitycenter/**/* @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
/speech/**/* @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/python-samples-reviewers
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
Expand Down
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
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)
50 changes: 50 additions & 0 deletions recaptcha_enterprise/snippets/get_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/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_get_metrics_site_key]
from google.cloud import recaptchaenterprise_v1


def get_metrics(project_id: str, recaptcha_site_key: str) -> None:
"""Get metrics specific to a recaptcha site key.
E.g: score bucket count for a key or number of
times the checkbox key failed/ passed etc.,
Args:
project_id: Google Cloud Project ID.
recaptcha_site_key: Specify the site key to get metrics.
"""

client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

metrics_name = f"projects/{project_id}/keys/{recaptcha_site_key}/metrics"
request = recaptchaenterprise_v1.GetMetricsRequest()
request.name = metrics_name

response = client.get_metrics(request)

# Retrieve the metrics you want from the key.
# If the site key is checkbox type: then use response.challenge_metrics
# instead of response.score_metrics
for day_metric in response.score_metrics:
# Each 'day_metric' is in the granularity of one day.
score_bucket_count = day_metric.overall_metrics.score_buckets
print(score_bucket_count)

print(f"Retrieved the bucket count for score based key: {recaptcha_site_key}")


# [END recaptcha_enterprise_get_metrics_site_key]
Loading

0 comments on commit 8254802

Please sign in to comment.