Skip to content

Commit 87cd0e4

Browse files
Sita04Cory-Kramerfaube
authored
feat: add recaptcha demosite v1 (#7722)
* docs(recaptcha-app): init add webapp * mapping exposed Docker port to app * update port * Change default port from 8000 to 8080. (#7633) * refactor: Demosite alignment with new UI * add UI files * updated static file references * refactoring * updated dockerfiles to exclude checkbox key * fixed templating issue * refactor to demo site_key best practice * lint fix * remove unrelated file * skip checkstyle plugin * fix typo * updated acc to review comments * Add an optional DOCKER_COMPOSE_USER environment variable. This can be helpful such that files get created by the same user that is running docker * updated assessment verdict, added README and comment in dockerfiles * add test file * Demosite app recaptcha changes (#7706) * Specify a container_name to simplify docker operations. * Remove unhelpful from the response. * refactor test file * fix xpath for testcases * fix year in header * updated acc to review comments * docs(recaptcha-app): init add webapp * update port * Change default port from 8000 to 8080. (#7633) * refactor: Demosite alignment with new UI * add UI files * refactoring * refactor to demo site_key best practice * remove unrelated file * moving pom back as this change is unrelated to demosite * readme typo fix * updated acc to review comments * remove local css, update json parsing, errors * modify response entity to be idiomatic * update getElement to use shadowRoot * update browser test selectors * lint * add libnss3 * test * test: adding libnss to verify if the chrome driver starts * update libnss install command * fix dependencies * Update run_tests.sh * Update run_tests.sh * setup driver in docker * remove drivermanager and set driver path * update driver path * set driver path with chromeoptions * debug installation * temporarily ignore tests --------- Co-authored-by: Cory Kramer <coryakramer@gmail.com> Co-authored-by: François Aubé <faube@google.com>
1 parent e6621bb commit 87cd0e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+8363
-62
lines changed

.kokoro/tests/run_tests.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,30 @@ fi
117117
# Install Chrome and chrome driver for recaptcha tests
118118
if [[ "$file" == *"recaptcha_enterprise/"* ]]; then
119119

120+
# Based on this content: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-unix
121+
# https://github.com/alixaxel/chrome-aws-lambda/issues/164
122+
apt install libnss3
123+
apt install libnss3-dev libgdk-pixbuf2.0-dev libgtk-3-dev libxss-dev libgconf-2-4
124+
120125
# Install Chrome.
121126
curl https://dl-ssl.google.com/linux/linux_signing_key.pub -o /tmp/google.pub \
122127
&& cat /tmp/google.pub | apt-key add -; rm /tmp/google.pub \
123128
&& echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' > /etc/apt/sources.list.d/google.list \
124129
&& mkdir -p /usr/share/desktop-directories \
125130
&& apt-get -y update && apt-get install -y google-chrome-stable
126-
131+
127132
# Disable the SUID sandbox so that Chrome can launch without being in a privileged container.
128133
dpkg-divert --add --rename --divert /opt/google/chrome/google-chrome.real /opt/google/chrome/google-chrome \
129134
&& echo "#!/bin/bash\nexec /opt/google/chrome/google-chrome.real --no-sandbox --disable-setuid-sandbox \"\$@\"" > /opt/google/chrome/google-chrome \
130135
&& chmod 755 /opt/google/chrome/google-chrome
131-
136+
132137
# Install chrome driver.
133138
mkdir -p /opt/selenium \
134139
&& curl http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -o /opt/selenium/chromedriver_linux64.zip \
135140
&& cd /opt/selenium; unzip /opt/selenium/chromedriver_linux64.zip; rm -rf chromedriver_linux64.zip; ln -fs /opt/selenium/chromedriver /usr/local/bin/chromedriver;
136141

142+
export CHROME_DRIVER_PATH="$PWD/chromedriver"
143+
echo "Installing chrome and driver. Path to installation: $CHROME_DRIVER_PATH"
137144
fi
138145

139146
btlr_args=(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2023 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+
# This file is only used for packaging and deployment purposes.
16+
FROM maven:3-openjdk-11-slim as builder
17+
18+
ENV PORT 8080
19+
20+
ARG GOOGLE_CLOUD_PROJECT
21+
ENV GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}
22+
23+
ARG SITE_KEY
24+
ENV SITE_KEY=${SITE_KEY}
25+
26+
# Copy local code to the container image.
27+
ENV APP_HOME /app
28+
WORKDIR $APP_HOME
29+
COPY . ./
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Demosite - Google Cloud reCAPTCHA Enterprise
2+
3+
Google [Cloud reCAPTCHA Enterprise](https://cloud.google.com/recaptcha-enterprise) helps protect your website from fraudulent activity, spam, and abuse without creating friction.
4+
5+
This application demonstrates how to integrate your client and server code with reCAPTCHA Enterprise - Java Client library.
6+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2023 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+
# This file is only used for packaging and deployment purposes.
16+
version: "3"
17+
services:
18+
livereload:
19+
image: demosite-livereload
20+
container_name: demosite-livereload
21+
build:
22+
context: .
23+
dockerfile: Dockerfile
24+
args:
25+
- "GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT}"
26+
- "SITE_KEY=${SITE_KEY}"
27+
command: mvn spring-boot:run
28+
user: "${DOCKER_COMPOSE_USER}"
29+
ports: [ "8080:8080" ]
30+
volumes:
31+
- "./:/app"
32+
restart: always
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.demosite.app</groupId>
7+
<artifactId>demosite</artifactId>
8+
<name>demosite</name>
9+
<version>0.0.1-SNAPSHOT</version>
10+
11+
<!--
12+
The parent pom defines common style checks and testing strategies for our samples.
13+
Removing or replacing it should not affect the execution of the samples in anyway.
14+
-->
15+
<parent>
16+
<groupId>com.google.cloud.samples</groupId>
17+
<artifactId>shared-configuration</artifactId>
18+
<version>1.2.0</version>
19+
</parent>
20+
21+
<dependencyManagement>
22+
<dependencies>
23+
<dependency>
24+
<!-- Import dependency management from Spring Boot -->
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-dependencies</artifactId>
27+
<version>2.7.4</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
<properties>
34+
<maven.compiler.target>11</maven.compiler.target>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
<app.mainclass>app.SpringbootMain</app.mainclass>
38+
</properties>
39+
40+
<dependencies>
41+
42+
<dependency>
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>google-cloud-recaptchaenterprise</artifactId>
45+
<version>3.2.0</version>
46+
</dependency>
47+
48+
<!-- https://mvnrepository.com/artifact/org.json/json -->
49+
<dependency>
50+
<groupId>org.json</groupId>
51+
<artifactId>json</artifactId>
52+
<version>20220320</version>
53+
</dependency>
54+
55+
56+
<!-- start_Selenium_dependencies -->
57+
<dependency>
58+
<groupId>org.seleniumhq.selenium</groupId>
59+
<artifactId>selenium-java</artifactId>
60+
<version>4.7.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.seleniumhq.selenium</groupId>
65+
<artifactId>selenium-chrome-driver</artifactId>
66+
<version>4.7.0</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<!-- end_Selenium_dependencies -->
70+
71+
72+
<!-- start_test_dependencies -->
73+
<dependency>
74+
<groupId>junit</groupId>
75+
<artifactId>junit</artifactId>
76+
<version>4.13.2</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.google.truth</groupId>
81+
<artifactId>truth</artifactId>
82+
<version>1.1.3</version>
83+
<scope>test</scope>
84+
</dependency>
85+
<!-- end_test_dependencies -->
86+
87+
88+
<!-- start_Spring_dependencies -->
89+
<dependency>
90+
<groupId>org.springframework.boot</groupId>
91+
<artifactId>spring-boot-starter-web</artifactId>
92+
<version>2.7.0</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-starter-test</artifactId>
97+
<version>2.7.0</version>
98+
<scope>test</scope>
99+
</dependency>
100+
<dependency>
101+
<groupId>org.springframework.boot</groupId>
102+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
103+
<version>2.7.0</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>com.google.api</groupId>
107+
<artifactId>api-common</artifactId>
108+
<version>2.1.5</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.springframework.boot</groupId>
112+
<artifactId>spring-boot</artifactId>
113+
<version>2.7.0</version>
114+
<scope>compile</scope>
115+
</dependency>
116+
<!-- end_Spring_dependencies -->
117+
118+
</dependencies>
119+
120+
<build>
121+
<plugins>
122+
<plugin>
123+
<groupId>org.springframework.boot</groupId>
124+
<artifactId>spring-boot-maven-plugin</artifactId>
125+
<version>2.7.4</version>
126+
<configuration>
127+
<mainClass>${app.mainclass}</mainClass>
128+
</configuration>
129+
</plugin>
130+
<!-- The checkstyle plugin is skipped to avoid Javadoc errors thrown in
131+
the files. Adding the Javadoc comments in the Controller file is against
132+
the best interest of Demosite project of simpler& straight code access -->
133+
<plugin>
134+
<groupId>org.apache.maven.plugins</groupId>
135+
<artifactId>maven-checkstyle-plugin</artifactId>
136+
<configuration>
137+
<skip>true</skip>
138+
</configuration>
139+
</plugin>
140+
<plugin>
141+
<groupId>org.apache.maven.plugins</groupId>
142+
<artifactId>maven-surefire-plugin</artifactId>
143+
<version>3.0.0-M7</version>
144+
<configuration>
145+
<includes>
146+
<include>**/*IT.java</include>
147+
</includes>
148+
<skip>false</skip>
149+
</configuration>
150+
</plugin>
151+
<plugin>
152+
<groupId>org.apache.maven.plugins</groupId>
153+
<artifactId>maven-failsafe-plugin</artifactId>
154+
<version>3.0.0-M7</version>
155+
<configuration>
156+
<skip>false</skip>
157+
</configuration>
158+
<dependencies>
159+
<dependency>
160+
<groupId>org.junit.vintage</groupId>
161+
<artifactId>junit-vintage-engine</artifactId>
162+
<version>5.8.2</version>
163+
</dependency>
164+
</dependencies>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
169+
</project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
17+
package app;
18+
19+
import java.util.HashMap;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.MediaType;
25+
import org.springframework.http.ResponseEntity;
26+
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.PostMapping;
28+
import org.springframework.web.bind.annotation.RequestBody;
29+
import org.springframework.web.bind.annotation.RequestMapping;
30+
import org.springframework.web.bind.annotation.ResponseBody;
31+
import org.springframework.web.bind.annotation.RestController;
32+
import org.springframework.web.servlet.ModelAndView;
33+
import recaptcha.CreateAssessment;
34+
35+
@RestController
36+
@RequestMapping
37+
public class MainController {
38+
39+
private static final LinkedHashMap<String, String> CONTEXT = new LinkedHashMap<>();
40+
41+
static {
42+
CONTEXT.put("project_id", System.getenv("GOOGLE_CLOUD_PROJECT"));
43+
CONTEXT.put("site_key", System.getenv("SITE_KEY"));
44+
}
45+
46+
@GetMapping(value = "/")
47+
public static ModelAndView home() {
48+
return new ModelAndView("home", CONTEXT);
49+
}
50+
51+
@GetMapping(value = "/store")
52+
public static ModelAndView store() {
53+
return new ModelAndView("store", CONTEXT);
54+
}
55+
56+
@GetMapping(value = "/login")
57+
public static ModelAndView login() {
58+
return new ModelAndView("login", CONTEXT);
59+
}
60+
61+
@GetMapping(value = "/comment")
62+
public static ModelAndView comment() {
63+
return new ModelAndView("comment", CONTEXT);
64+
}
65+
66+
@GetMapping(value = "/signup")
67+
public static ModelAndView signup() {
68+
return new ModelAndView("signup", CONTEXT);
69+
}
70+
71+
@GetMapping(value = "/game")
72+
public static ModelAndView game() {
73+
return new ModelAndView("game", CONTEXT);
74+
}
75+
76+
@PostMapping(value = "/create_assessment", produces = "application/json")
77+
public static @ResponseBody
78+
ResponseEntity<HashMap<String, HashMap<String, String>>> createAssessment(
79+
@RequestBody Map<String, HashMap<String, String>> credentials) {
80+
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
81+
final HttpHeaders httpHeaders = new HttpHeaders();
82+
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
83+
HashMap<String, HashMap<String, String>> result;
84+
85+
try {
86+
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Starts -->
87+
result = CreateAssessment.createAssessment(projectId,
88+
CONTEXT.get("site_key"),
89+
credentials.get("recaptcha_cred").get("token"),
90+
credentials.get("recaptcha_cred").get("action"));
91+
// <!-- ATTENTION: reCAPTCHA Example (Server Part 1/2) Ends -->
92+
} catch (Exception e) {
93+
result = new HashMap<>() {{
94+
put("data", new HashMap<>() {{
95+
put("error_msg", e.toString());
96+
}});
97+
}};
98+
return new ResponseEntity<>(result, httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
99+
}
100+
101+
return new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
102+
}
103+
}

0 commit comments

Comments
 (0)