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

feat(recaptcha): add mfa sample #8665

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
426d88f
feat(recaptcha): add mfa sample
Sita04 Sep 21, 2023
8c38c81
lint fix
Sita04 Sep 21, 2023
2fb725f
lint fix and change java version
Sita04 Sep 21, 2023
287b872
testing to check if tests run
Sita04 Sep 25, 2023
10839b8
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Sep 25, 2023
253b7fd
revert change and rename files
Sita04 Sep 25, 2023
99bb8d3
update pom
Sita04 Sep 26, 2023
b1b9f92
update pom
Sita04 Sep 26, 2023
5df777c
update pom
Sita04 Sep 26, 2023
160f8a7
refactor package name
Sita04 Sep 26, 2023
cfc8ce3
fix issue with template resolution
Sita04 Oct 2, 2023
a372434
add dependencies
Sita04 Oct 2, 2023
f8573c3
lint fix
Sita04 Oct 2, 2023
83f08fd
fix package name
Sita04 Oct 2, 2023
3c55dab
lint fix
Sita04 Oct 2, 2023
d9871d8
lint fix
Sita04 Oct 2, 2023
77a2e96
lint fix
Sita04 Oct 17, 2023
3db24af
check if chrome is installed
Sita04 Oct 17, 2023
b17a89c
refactor chrome driver installation
Sita04 Oct 17, 2023
f4242a8
comments to check installation
Sita04 Oct 17, 2023
88a99a8
fix path
Sita04 Oct 17, 2023
84aff14
update build command
Sita04 Oct 17, 2023
c310949
update command
Sita04 Oct 17, 2023
f767fff
revert directory
Sita04 Oct 17, 2023
57ae9fa
remove export command
Sita04 Oct 18, 2023
635c3b4
update acc to review
Sita04 Oct 18, 2023
ca79ff1
add symlink to chrome binary
Sita04 Oct 18, 2023
b20b23b
Update pom.xml
Sita04 Nov 9, 2023
cc11200
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 9, 2023
c66efed
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 23, 2023
949a761
Update pom.xml
Sita04 Nov 23, 2023
cae757b
Merge remote-tracking branch 'origin/main' into mfa-update
Sita04 Nov 23, 2023
10d9b67
commenting out to test installation from docker image
Sita04 Nov 23, 2023
0219627
add password check helper
Sita04 Nov 23, 2023
35dbe66
add version
Sita04 Nov 23, 2023
b4abf20
Merge remote-tracking branch 'origin/mfa-update' into mfa-update
Sita04 Nov 23, 2023
e5c585d
update deps and wire SB class file
Sita04 Nov 23, 2023
66a9201
Merge remote-tracking branch 'origin' into mfa-update
Sita04 Nov 23, 2023
a07e37b
testing update with selenium version
Sita04 Nov 24, 2023
4b9bf54
uncomment and override chrome installation
Sita04 Nov 24, 2023
ea95ad2
uncomment and override chrome installation
Sita04 Nov 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</parent>

<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>2.7.16</spring.boot.version>
</properties>
Expand Down Expand Up @@ -50,6 +50,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<mainClass>app.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</build>
Expand All @@ -61,6 +69,12 @@
<artifactId>google-cloud-recaptchaenterprise</artifactId>
</dependency>

<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>recaptcha-password-check-helpers</artifactId>
<version>1.0.1</version>
</dependency>

Sita04 marked this conversation as resolved.
Show resolved Hide resolved
<!-- [Start_Selenium_dependencies] -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
Expand All @@ -80,6 +94,16 @@
<artifactId>webdrivermanager</artifactId>
<version>5.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-http-jdk-client</artifactId>
<version>4.8.1</version>
</dependency>
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
<!-- [End_Selenium_dependencies] -->


Expand Down Expand Up @@ -119,6 +143,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- [End_Spring_dependencies] -->

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package app;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RestController
@RequestMapping
public class MainController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ public class AnnotateAssessment {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "project-id";
String projectId = "project-id";
String assessmentId = "assessment-id";
annotateAssessment(projectID, assessmentId);
annotateAssessment(projectId, assessmentId);
}

/**
* Pre-requisite: Create an assessment before annotating.
* Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
*
* <p>Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
*
* @param projectID: GCloud Project id
* @param assessmentId: Value of the 'name' field returned from the CreateAssessment call.
* @param projectId : GCloud Project id
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param assessmentId : Value of the 'name' field returned from the CreateAssessment call.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void annotateAssessment(String projectID, String assessmentId) throws IOException {
public static void annotateAssessment(String projectId, String assessmentId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
Expand All @@ -54,7 +53,7 @@ public static void annotateAssessment(String projectID, String assessmentId) thr
// https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
AnnotateAssessmentRequest annotateAssessmentRequest =
AnnotateAssessmentRequest.newBuilder()
.setName(AssessmentName.of(projectID, assessmentId).toString())
.setName(AssessmentName.of(projectId, assessmentId).toString())
.setAnnotation(Annotation.FRAUDULENT)
.addReasons(Reason.FAILED_TWO_FACTOR)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ public class CreateAssessment {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "project-id";
String projectId = "project-id";
String recaptchaSiteKey = "recaptcha-site-key";
String token = "action-token";
String recaptchaAction = "action-name";

createAssessment(projectID, recaptchaSiteKey, token, recaptchaAction);
createAssessment(projectId, recaptchaSiteKey, token, recaptchaAction);
}

/**
* Create an assessment to analyze the risk of an UI action. Assessment approach is the same for
* both 'score' and 'checkbox' type recaptcha site keys.
*
* @param projectID : GCloud Project ID
* @param projectId : GCloud Project ID
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKey : Site key obtained by registering a domain/app to use recaptcha
* services. (score/ checkbox type)
* @param token : The token obtained from the client on passing the recaptchaSiteKey.
* @param recaptchaAction : Action name corresponding to the token.
*/
public static void createAssessment(
String projectID, String recaptchaSiteKey, String token, String recaptchaAction)
String projectId, String recaptchaSiteKey, String token, String recaptchaAction)
throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -63,7 +63,7 @@ public static void createAssessment(
// Build the assessment request.
CreateAssessmentRequest createAssessmentRequest =
CreateAssessmentRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setParent(ProjectName.of(projectId).toString())
.setAssessment(Assessment.newBuilder().setEvent(event).build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public class CreateSiteKey {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String domainName = "domain-name";

createSiteKey(projectID, domainName);
createSiteKey(projectId, domainName);
}

/**
* Create reCAPTCHA Site key which binds a domain name to a unique key.
*
* @param projectID : GCloud Project ID.
* @param projectId : GCloud Project ID.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param domainName : Specify the domain name in which the reCAPTCHA should be activated.
*/
public static String createSiteKey(String projectID, String domainName) throws IOException {
public static String createSiteKey(String projectId, String domainName) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
Expand All @@ -64,7 +64,7 @@ public static String createSiteKey(String projectID, String domainName) throws I

CreateKeyRequest createKeyRequest =
CreateKeyRequest.newBuilder()
.setParent(ProjectName.of(projectID).toString())
.setParent(ProjectName.of(projectId).toString())
.setKey(scoreKey)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ public class DeleteSiteKey {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String recaptchaSiteKey = "recaptcha-site-key";

deleteSiteKey(projectID, recaptchaSiteKey);
deleteSiteKey(projectId, recaptchaSiteKey);
}

/**
* Delete the given reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to be deleted.
* @param projectId : GCloud Project ID.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKey : Specify the site key to be deleted.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void deleteSiteKey(String projectID, String recaptchaSiteKey)
public static void deleteSiteKey(String projectId, String recaptchaSiteKey)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -54,7 +54,7 @@ public static void deleteSiteKey(String projectID, String recaptchaSiteKey)
// Set the project ID and reCAPTCHA site key.
DeleteKeyRequest deleteKeyRequest =
DeleteKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.setName(KeyName.of(projectId, recaptchaSiteKey).toString())
.build();

client.deleteKeyCallable().futureCall(deleteKeyRequest).get(5, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static void main(String[] args) throws IOException {
* 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.,
*
* @param projectId: Google Cloud Project Id.
* @param recaptchaSiteKey: Specify the site key to get metrics.
* @param projectId : Google Cloud Project id.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKey : Specify the site key to get metrics.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void getMetrics(String projectId, String recaptchaSiteKey) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public class GetSiteKey {
public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";
String recaptchaSiteKey = "recaptcha-site-key";

getSiteKey(projectID, recaptchaSiteKey);
getSiteKey(projectId, recaptchaSiteKey);
}

/**
* Get the reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to get the details.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No need for space

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The space is a mandatory lint check between params and the starting comment.

* @param projectId : GCloud Project ID.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKey : Specify the site key to get the details.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void getSiteKey(String projectID, String recaptchaSiteKey)
public static void getSiteKey(String projectId, String recaptchaSiteKey)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -56,7 +56,7 @@ public static void getSiteKey(String projectID, String recaptchaSiteKey)
// Construct the "GetSiteKey" request.
GetKeyRequest getKeyRequest =
GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKey).toString())
.setName(KeyName.of(projectId, recaptchaSiteKey).toString())
.build();

// Wait for the operation to complete.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ public class ListSiteKeys {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String projectId = "your-project-id";

listSiteKeys(projectID);
listSiteKeys(projectId);
}

/**
* List all keys present under the given project ID.
*
* @param projectID : GCloud Project ID.
* @param projectId : GCloud Project ID.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static ListKeysPagedResponse listSiteKeys(String projectID) throws IOException {
public static ListKeysPagedResponse listSiteKeys(String projectId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the `client.close()` method on the client to safely
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the project ID to list the keys present in it.
ListKeysRequest listKeysRequest =
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();
ListKeysRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).build();

ListKeysPagedResponse response = client.listKeys(listKeysRequest);
System.out.println("Listing reCAPTCHA site keys: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static void main(String[] args) throws IOException {
* to reCAPTCHA Enterprise. For more info, see:
* https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
*
* @param projectId: Google Cloud Project Id.
* @param recaptchaSiteKey: Specify the site key to migrate.
* @param projectId : Google Cloud Project Id.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKey : Specify the site key to migrate.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public class UpdateSiteKey {
public static void main(String[] args)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectID = "your-project-id";
String recaptchaSiteKeyID = "recaptcha-site-key-id";
String projectId = "your-project-id";
String recaptchaSiteKeyId = "recaptcha-site-key-id";
String domainName = "domain-name";

updateSiteKey(projectID, recaptchaSiteKeyID, domainName);
updateSiteKey(projectId, recaptchaSiteKeyId, domainName);
}

/**
* Update the properties of the given site key present under the project id.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKeyID: Specify the site key.
* @param domainName: Specify the domain name for which the settings should be updated.
* @param projectId : GCloud Project ID.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
* @param recaptchaSiteKeyId : Specify the site key.
* @param domainName : Specify the domain name for which the settings should be updated.
Sita04 marked this conversation as resolved.
Show resolved Hide resolved
*/
public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, String domainName)
public static void updateSiteKey(String projectId, String recaptchaSiteKeyId, String domainName)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
Expand All @@ -61,7 +61,7 @@ public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, St
.setKey(
Key.newBuilder()
.setDisplayName("any descriptive name for the key")
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString())
.setName(KeyName.of(projectId, recaptchaSiteKeyId).toString())
.setWebSettings(
WebKeySettings.newBuilder()
.setAllowAmpTraffic(true)
Expand All @@ -75,7 +75,7 @@ public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, St
// Check if the key has been updated.
GetKeyRequest getKeyRequest =
GetKeyRequest.newBuilder()
.setName(KeyName.of(projectID, recaptchaSiteKeyID).toString())
.setName(KeyName.of(projectId, recaptchaSiteKeyId).toString())
.build();
Key response = client.getKey(getKeyRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package account_defender;
package recaptcha.accountdefender;

// [START recaptcha_enterprise_account_defender_assessment]

Expand Down Expand Up @@ -60,12 +60,12 @@ public static void main(String[] args)
String userIdentifier = "default" + UUID.randomUUID().toString().split("-")[0];

// Change this to a secret not shared with Google.
final String HMAC_KEY = "SOME_INTERNAL_UNSHARED_KEY";
final String hmacKey = "SOME_INTERNAL_UNSHARED_KEY";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a constant. The original naming was correct. Please, rollback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed about the naming convention. But having a static string format within main() throws lint as its not the norm.


// Get instance of Mac object implementing HmacSHA256, and initialize it with the above
// secret key.
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
mac.init(new SecretKeySpec(hmacKey.getBytes(StandardCharsets.UTF_8),
"HmacSHA256"));
byte[] hashBytes = mac.doFinal(userIdentifier.getBytes(StandardCharsets.UTF_8));
ByteString hashedAccountId = ByteString.copyFrom(hashBytes);
Expand Down