Skip to content

Commit b3672ed

Browse files
Sita04busunkim96partheagcf-owl-bot[bot]
authored
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>
0 parents  commit b3672ed

12 files changed

+716
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_create_assessment]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def create_assessment(
20+
project_id: str, recaptcha_site_key: str, token: str, recaptcha_action: str
21+
) -> None:
22+
""" Create an assessment to analyze the risk of a UI action.
23+
Args:
24+
project_id: GCloud Project ID
25+
recaptcha_site_key: Site key obtained by registering a domain/app to use recaptcha services.
26+
token: The token obtained from the client on passing the recaptchaSiteKey.
27+
recaptcha_action: Action name corresponding to the token.
28+
"""
29+
30+
# TODO(developer): Replace these variables before running the sample.
31+
# Specify a name for this assessment.
32+
assessment_name = "assessment_name"
33+
34+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
35+
36+
# Set the properties of the event to be tracked.
37+
event = recaptchaenterprise_v1.Event()
38+
event.site_key = recaptcha_site_key
39+
event.token = token
40+
41+
assessment = recaptchaenterprise_v1.Assessment()
42+
assessment.event = event
43+
assessment.name = assessment_name
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+
81+
82+
# [END recaptcha_enterprise_create_assessment]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_create_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def create_site_key(project_id: str, domain_name: str) -> str:
20+
"""Create reCAPTCHA Site key which binds a domain name to a unique key.
21+
Args:
22+
project_id : GCloud Project ID.
23+
domain_name: Specify the domain name in which the reCAPTCHA should be activated.
24+
"""
25+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
26+
27+
# Set the type of the reCAPTCHA to be displayed.
28+
# For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
29+
web_settings = recaptchaenterprise_v1.WebKeySettings()
30+
web_settings.allowed_domains.append(domain_name)
31+
web_settings.allow_amp_traffic = False
32+
web_settings.integration_type = web_settings.IntegrationType.SCORE
33+
34+
key = recaptchaenterprise_v1.Key()
35+
key.display_name = "any descriptive name for the key"
36+
key.web_settings = web_settings
37+
38+
# Create the request.
39+
request = recaptchaenterprise_v1.CreateKeyRequest()
40+
request.parent = f"projects/{project_id}"
41+
request.key = key
42+
43+
# Get the name of the created reCAPTCHA site key.
44+
response = client.create_key(request)
45+
recaptcha_site_key = response.name.rsplit("/", maxsplit=1)[1]
46+
print("reCAPTCHA Site key created successfully. Site Key: " + recaptcha_site_key)
47+
return recaptcha_site_key
48+
49+
50+
# [END recaptcha_enterprise_create_site_key]
51+
52+
if __name__ == "__main__":
53+
import google.auth
54+
import google.auth.exceptions
55+
56+
# TODO(developer): Replace the below variables before running
57+
try:
58+
default_project_id = google.auth.default()[1]
59+
domain_name = "localhost"
60+
except google.auth.exceptions.DefaultCredentialsError:
61+
print(
62+
"Please use `gcloud auth application-default login` "
63+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
64+
)
65+
else:
66+
create_site_key(default_project_id, domain_name)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_delete_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def delete_site_key(project_id: str, recaptcha_site_key: str) -> None:
20+
""" Delete the given reCAPTCHA site key present under the project ID.
21+
22+
Args:
23+
project_id : GCloud Project ID.
24+
recaptcha_site_key: Specify the key ID to be deleted.
25+
"""
26+
27+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
28+
29+
# Construct the key details.
30+
key_name = f"projects/{project_id}/keys/{recaptcha_site_key}"
31+
32+
# Set the project ID and reCAPTCHA site key.
33+
request = recaptchaenterprise_v1.DeleteKeyRequest()
34+
request.name = key_name
35+
36+
client.delete_key(request)
37+
print("reCAPTCHA Site key deleted successfully ! ")
38+
39+
40+
# [END recaptcha_enterprise_delete_site_key]
41+
42+
43+
if __name__ == "__main__":
44+
import google.auth
45+
import google.auth.exceptions
46+
47+
# TODO(developer): Replace the below variables before running
48+
try:
49+
default_project_id = google.auth.default()[1]
50+
recaptcha_site_key = "recaptcha_site_key"
51+
except google.auth.exceptions.DefaultCredentialsError:
52+
print(
53+
"Please use `gcloud auth application-default login` "
54+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
55+
)
56+
else:
57+
delete_site_key(default_project_id, recaptcha_site_key)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_get_site_key]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def get_site_key(project_id: str, recaptcha_site_key: str) -> None:
20+
"""
21+
Get the reCAPTCHA site key present under the project ID.
22+
23+
Args:
24+
project_id: GCloud Project ID.
25+
recaptcha_site_key: Specify the site key to get the details.
26+
"""
27+
28+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
29+
30+
# Construct the key details.
31+
key_name = f"projects/{project_id}/keys/{recaptcha_site_key}"
32+
33+
request = recaptchaenterprise_v1.GetKeyRequest()
34+
request.name = key_name
35+
36+
key = client.get_key(request)
37+
print("Successfully obtained the key !" + key.name)
38+
39+
40+
# [END recaptcha_enterprise_get_site_key]
41+
42+
43+
if __name__ == "__main__":
44+
import google.auth
45+
import google.auth.exceptions
46+
47+
# TODO(developer): Replace the below variables before running
48+
try:
49+
default_project_id = google.auth.default()[1]
50+
recaptcha_site_key = "recaptcha_site_key"
51+
except google.auth.exceptions.DefaultCredentialsError:
52+
print(
53+
"Please use `gcloud auth application-default login` "
54+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
55+
)
56+
else:
57+
get_site_key(default_project_id, recaptcha_site_key)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2021 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START recaptcha_enterprise_list_site_keys]
16+
from google.cloud import recaptchaenterprise_v1
17+
18+
19+
def list_site_keys(project_id: str) -> None:
20+
""" List all keys present under the given project ID.
21+
22+
Args:
23+
project_id: GCloud Project ID.
24+
"""
25+
26+
project_name = f"projects/{project_id}"
27+
28+
client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()
29+
30+
# Set the project id to list the keys present in it.
31+
request = recaptchaenterprise_v1.ListKeysRequest()
32+
request.parent = project_name
33+
34+
response = client.list_keys(request)
35+
print("Listing reCAPTCHA site keys: ")
36+
for i, key in enumerate(response):
37+
print(f"{str(i)}. {key.name}")
38+
39+
40+
# [END recaptcha_enterprise_list_site_keys]
41+
42+
43+
if __name__ == "__main__":
44+
import google.auth
45+
import google.auth.exceptions
46+
47+
# TODO(developer): Replace the below variables before running
48+
try:
49+
default_project_id = google.auth.default()[1]
50+
except google.auth.exceptions.DefaultCredentialsError:
51+
print(
52+
"Please use `gcloud auth application-default login` "
53+
"or set GOOGLE_APPLICATION_CREDENTIALS to use this script."
54+
)
55+
else:
56+
list_site_keys(default_project_id)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be imported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": True,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
# "gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
35+
# A dictionary you want to inject into your test. Don't put any
36+
# secrets here. These values will override predefined values.
37+
"envs": {},
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
selenium==3.141.0
2+
Flask==2.0.1
3+
pytest==6.2.4
4+
pytest-flask==1.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-recaptcha-enterprise==1.0.0

0 commit comments

Comments
 (0)