Skip to content

Commit

Permalink
fix: update references to deprecated hashed_account_id (#8974)
Browse files Browse the repository at this point in the history
* Update references to hashed_account_id to account_id in related accounts.

* Update references to hashed_account_id to account_id in related accounts.

* Add tests.

* Remove unused code.
  • Loading branch information
prashantxf94 committed Jan 17, 2024
1 parent 1c70654 commit eb80aaf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// [START recaptcha_enterprise_search_related_account_group_membership]

import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.protobuf.ByteString;
import com.google.recaptchaenterprise.v1.RelatedAccountGroupMembership;
import com.google.recaptchaenterprise.v1.SearchRelatedAccountGroupMembershipsRequest;
import java.io.IOException;
Expand All @@ -32,29 +31,29 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
// projectId: Google Cloud Project Id.
String projectId = "project-id";

// HMAC SHA-256 hashed unique id of the customer.
ByteString hashedAccountId = ByteString.copyFrom(new byte[] {});
// Unique id of the customer.
String accountId = "default" + UUID.randomUUID().toString().split("-")[0];

searchRelatedAccountGroupMemberships(projectId, hashedAccountId);
searchRelatedAccountGroupMemberships(projectId, accountId);
}

// List group memberships for the hashed account id.
// List group memberships for the account id.
public static void searchRelatedAccountGroupMemberships(
String projectId, ByteString hashedAccountId) throws IOException {
String projectId, String accountId) throws IOException {
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {

SearchRelatedAccountGroupMembershipsRequest request =
SearchRelatedAccountGroupMembershipsRequest.newBuilder()
.setProject(projectId)
.setHashedAccountId(hashedAccountId)
.setAccountId(accountId)
.build();

for (RelatedAccountGroupMembership groupMembership :
client.searchRelatedAccountGroupMemberships(request).iterateAll()) {
System.out.println(groupMembership.getName());
}
System.out.printf(
"Finished searching related account group memberships for %s!", hashedAccountId);
"Finished searching related account group memberships for %s!", accountId);
}
}
}
Expand Down
18 changes: 3 additions & 15 deletions recaptcha_enterprise/snippets/src/test/java/app/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -184,17 +183,6 @@ public void testCreateAnnotateAccountDefender()
String testURL = "http://localhost:" + randomServerPort + "/";
String accountId = "default-" + UUID.randomUUID().toString().split("-")[0];

// Secret not shared with Google.
String HMAC_KEY = "123456789";
// Get instance of Mac object implementing HmacSHA256, and initialize it with the above
// secret key.
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
"HmacSHA256");
mac.init(secretKeySpec);
byte[] hashBytes = mac.doFinal(accountId.getBytes(StandardCharsets.UTF_8));
asdfByteString hashedAccountId = ByteString.copyFrom(hashBytes);

// Create the assessment.
JSONObject createAssessmentResult =
createAssessment(testURL, accountId, AssessmentType.ACCOUNT_DEFENDER);
Expand All @@ -221,13 +209,13 @@ public void testCreateAnnotateAccountDefender()
ListRelatedAccountGroupMemberships.listRelatedAccountGroupMemberships(PROJECT_ID, "legitimate");
assertThat(stdOut.toString()).contains("Finished listing related account group memberships.");

// Search related group memberships for a hashed account id.
// Search related group memberships for a account id.
SearchRelatedAccountGroupMemberships.searchRelatedAccountGroupMemberships(
PROJECT_ID, hashedAccountId);
PROJECT_ID, accountId);
assertThat(stdOut.toString())
.contains(
String.format(
"Finished searching related account group memberships for %s", hashedAccountId));
"Finished searching related account group memberships for %s", accountId));
}

@Test
Expand Down

0 comments on commit eb80aaf

Please sign in to comment.