This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
docs(samples): added recaptcha code samples and tests #112
Merged
Merged
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
419e129
docs(samples): added samples and tests for site key CRUD operation
Sita04 d4da932
docs(samples): added samples and tests for create assessment
Sita04 517292f
docs(samples): modified requirements and test inputs
Sita04 3aa68d2
docs(samples): lint fix - adding copyright
Sita04 45eb4e0
fix(samples): added nox config and modified requirements-test
Sita04 a0df10c
docs(samples): lint fix
Sita04 e0fd66e
refactor(samples): updated nox file
Sita04 c9dfe99
docs(samples): lint fix
Sita04 c255bd0
docs(samples): added nox config
Sita04 1a7c7a7
docs(samples): lint fix; modified nox config
Sita04 dab4e91
refactor(samples): incorporated review comments
Sita04 432dba9
refactor(samples): lint fix
Sita04 4707aac
Merge branch 'main' into master
Sita04 8d13190
refactor(samples): lint fix
Sita04 3203ad2
Merge remote-tracking branch 'origin/master'
Sita04 8dcba2b
refactor(samples): lint fix
Sita04 371292f
docs(samples): updated to use latest chrome version and lint fix
Sita04 3219ea7
fix(samples): updated selenium to use chrome browser and added displa…
Sita04 b571090
build: add placeholder dockerfile, update kokoro configs
busunkim96 907a99a
Merge branch 'main' into master
busunkim96 4f0be2e
Adding chrome installation in docker
Sita04 13a735b
refactor(samples): removed webdriver manager dependency. Included com…
Sita04 a0c4eeb
fix(samples): changed the chromedriver version to latest release.
Sita04 f88deea
Merge branch 'main' into master
parthea 0496f83
🦉 Updates from OwlBot
gcf-owl-bot[bot] a20395b
refactor(samples): included review comments
Sita04 1602811
Merge branch 'main' into master
Sita04 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# 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. | ||
|
||
# Use the python-docs-samples iamge as a base: | ||
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.kokoro/docker/Dockerfile | ||
FROM gcr.io/cloud-devrel-kokoro-resources/python-samples-testing-docker | ||
|
||
# TODO: Add Chrome Installation Here | ||
|
||
CMD ["python3.6"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
# [START recaptcha_enterprise_create_assessment] | ||
from google.cloud import recaptchaenterprise_v1 | ||
|
||
|
||
def create_assessment( | ||
project_id: str, recaptcha_site_key: str, token: str, recaptcha_action: str | ||
) -> None: | ||
""" 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. | ||
""" | ||
|
||
# TODO(developer): Replace these variables before running the sample. | ||
# Specify a name for this assessment. | ||
assessment_name = "assessment_name" | ||
|
||
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 | ||
assessment.name = assessment_name | ||
|
||
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) | ||
) | ||
else: | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Check if the expected action was executed. | ||
if response.token_properties.action == recaptcha_action: | ||
# 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) | ||
) | ||
else: | ||
Sita04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print( | ||
"The action attribute in your reCAPTCHA tag does" | ||
+ "not match the action you are expecting to score" | ||
) | ||
|
||
|
||
# [END recaptcha_enterprise_create_assessment] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2021 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
# [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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sita04 I've updated your branch with the config changes necessary to use this Dockerfile. Please fill this with whatever additional packages you need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much Bu Sun!