Skip to content

Commit e1d930f

Browse files
authored
Remove references to deprecated hashed_account_id (GoogleCloudPlatform#8844)
The new recommended way to provide the user identifier is through the `userInfo.accountId` field in the assessment's `event`. In addition to the user's account id, new fields like phone number, email address and username can be specified too. Change-Id: Icd7bdecb8a783e9a148db2f2b50d1df3d222347c
1 parent dc18ac3 commit e1d930f

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

recaptcha_enterprise/snippets/src/main/java/recaptcha/account_defender/AccountDefenderAssessment.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import com.google.recaptchaenterprise.v1.ProjectName;
2828
import com.google.recaptchaenterprise.v1.RiskAnalysis.ClassificationReason;
2929
import com.google.recaptchaenterprise.v1.TokenProperties;
30+
import com.google.recaptchaenterprise.v1.UserId;
31+
import com.google.recaptchaenterprise.v1.UserInfo;
3032
import java.io.IOException;
3133
import java.nio.charset.StandardCharsets;
3234
import java.security.InvalidKeyException;
@@ -56,21 +58,16 @@ public static void main(String[] args)
5658
// recaptchaAction: The action name corresponding to the token.
5759
String recaptchaAction = "recaptcha-action";
5860

59-
// Unique ID of the customer, such as email, customer ID, etc.
60-
String userIdentifier = "default" + UUID.randomUUID().toString().split("-")[0];
61+
// Unique ID of the user, such as email, customer ID, etc.
62+
String accountId = "default" + UUID.randomUUID().toString().split("-")[0];
6163

62-
// Change this to a secret not shared with Google.
63-
final String HMAC_KEY = "SOME_INTERNAL_UNSHARED_KEY";
64+
// User phone number
65+
String phoneNumber = "555-987-XXXX";
6466

65-
// Get instance of Mac object implementing HmacSHA256, and initialize it with the above
66-
// secret key.
67-
Mac mac = Mac.getInstance("HmacSHA256");
68-
mac.init(new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
69-
"HmacSHA256"));
70-
byte[] hashBytes = mac.doFinal(userIdentifier.getBytes(StandardCharsets.UTF_8));
71-
ByteString hashedAccountId = ByteString.copyFrom(hashBytes);
67+
// User email address
68+
String emailAddress = "john.doe@example.com";
7269

73-
accountDefenderAssessment(projectId, recaptchaSiteKey, token, recaptchaAction, hashedAccountId);
70+
accountDefenderAssessment(projectId, recaptchaSiteKey, token, recaptchaAction, accountId, phoneNumber, emailAddress);
7471
}
7572

7673
/**
@@ -84,19 +81,26 @@ public static void accountDefenderAssessment(
8481
String recaptchaSiteKey,
8582
String token,
8683
String recaptchaAction,
87-
ByteString hashedAccountId)
84+
String accountId,
85+
String phoneNumber,
86+
String emailAddress)
8887
throws IOException {
8988
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
9089

9190
// Set the properties of the event to be tracked.
92-
Event event =
91+
Event.Builder eventBuilder =
9392
Event.newBuilder()
9493
.setSiteKey(recaptchaSiteKey)
95-
.setToken(token)
96-
// Set the hashed account id (of the user).
97-
// Recommended approach: HMAC SHA256 along with salt (or secret key).
98-
.setHashedAccountId(hashedAccountId)
99-
.build();
94+
.setToken(token);
95+
96+
// Set the account id, email address and phone number (of the user).
97+
eventBuilder.setUserInfo(
98+
UserInfo.newBuilder()
99+
.setAccountId(accountId)
100+
.addUserIds(UserId.newBuilder().setEmail(emailAddress))
101+
.addUserIds(UserId.newBuilder().setPhoneNumber(phoneNumber)));
102+
103+
Event event = eventBuilder.build();
100104

101105
// Build the assessment request.
102106
CreateAssessmentRequest createAssessmentRequest =

recaptcha_enterprise/snippets/src/main/java/recaptcha/account_defender/AnnotateAccountDefenderAssessment.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.recaptchaenterprise.v1.AssessmentName;
2828
import java.io.IOException;
2929
import java.security.NoSuchAlgorithmException;
30+
import java.util.UUID;
3031

3132
public class AnnotateAccountDefenderAssessment {
3233

@@ -38,18 +39,18 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
3839
// assessmentId: Value of the 'name' field returned from the CreateAssessment call.
3940
String assessmentId = "account-defender-assessment-id";
4041

41-
// hashedAccountId: Set the hashedAccountId corresponding to the assessment id.
42-
ByteString hashedAccountId = ByteString.copyFrom(new byte[] {});
42+
// accountId: Set the accountId corresponding to the assessment id.
43+
String accountId = "default" + UUID.randomUUID().toString().split("-")[0];
4344

44-
annotateAssessment(projectID, assessmentId, hashedAccountId);
45+
annotateAssessment(projectID, assessmentId, accountId);
4546
}
4647

4748
/**
4849
* Pre-requisite: Create an assessment before annotating. Annotate an assessment to provide
4950
* feedback on the correctness of recaptcha prediction.
5051
*/
5152
public static void annotateAssessment(
52-
String projectID, String assessmentId, ByteString hashedAccountId) throws IOException {
53+
String projectID, String assessmentId, String accountId) throws IOException {
5354

5455
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
5556
// Build the annotation request.
@@ -60,7 +61,7 @@ public static void annotateAssessment(
6061
.setName(AssessmentName.of(projectID, assessmentId).toString())
6162
.setAnnotation(Annotation.LEGITIMATE)
6263
.addReasons(Reason.PASSED_TWO_FACTOR)
63-
.setHashedAccountId(hashedAccountId)
64+
.setAccountId(accountId)
6465
.build();
6566

6667
// Empty response is sent back.

recaptcha_enterprise/snippets/src/test/java/app/SnippetsIT.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public void testCreateAnnotateAccountDefender()
182182
ExecutionException, InvalidKeyException {
183183

184184
String testURL = "http://localhost:" + randomServerPort + "/";
185+
String accountId = "default-" + UUID.randomUUID().toString().split("-")[0];
185186

186187
// Secret not shared with Google.
187188
String HMAC_KEY = "123456789";
@@ -191,21 +192,19 @@ public void testCreateAnnotateAccountDefender()
191192
SecretKeySpec secretKeySpec = new SecretKeySpec(HMAC_KEY.getBytes(StandardCharsets.UTF_8),
192193
"HmacSHA256");
193194
mac.init(secretKeySpec);
194-
byte[] hashBytes = mac.doFinal(
195-
("default-" + UUID.randomUUID().toString().split("-")[0])
196-
.getBytes(StandardCharsets.UTF_8));
197-
ByteString hashedAccountId = ByteString.copyFrom(hashBytes);
195+
byte[] hashBytes = mac.doFinal(accountId.getBytes(StandardCharsets.UTF_8));
196+
asdfByteString hashedAccountId = ByteString.copyFrom(hashBytes);
198197

199198
// Create the assessment.
200199
JSONObject createAssessmentResult =
201-
createAssessment(testURL, hashedAccountId, AssessmentType.ACCOUNT_DEFENDER);
200+
createAssessment(testURL, accountId, AssessmentType.ACCOUNT_DEFENDER);
202201
String assessmentName = createAssessmentResult.getString("assessmentName");
203202
// Verify that the assessment name has been modified post the assessment creation.
204203
assertThat(assessmentName).isNotEmpty();
205204

206205
// Annotate the assessment.
207206
AnnotateAccountDefenderAssessment.annotateAssessment(
208-
PROJECT_ID, assessmentName, hashedAccountId);
207+
PROJECT_ID, assessmentName, accountId);
209208
assertThat(stdOut.toString()).contains("Annotated response sent successfully ! ");
210209

211210
// NOTE: The below assert statements have no significant effect,
@@ -247,7 +246,7 @@ public void testPasswordLeakAssessment()
247246
}
248247

249248
public JSONObject createAssessment(
250-
String testURL, ByteString hashedAccountId, AssessmentType assessmentType)
249+
String testURL, String accountId, AssessmentType assessmentType)
251250
throws IOException, JSONException, InterruptedException, ExecutionException {
252251

253252
// Setup the automated browser test and retrieve the token and action.
@@ -262,7 +261,7 @@ public JSONObject createAssessment(
262261
RECAPTCHA_SITE_KEY_1,
263262
tokenActionPair.getString("token"),
264263
tokenActionPair.getString("action"),
265-
hashedAccountId);
264+
accountId);
266265
break;
267266
}
268267
case ASSESSMENT:
@@ -280,7 +279,7 @@ public JSONObject createAssessment(
280279
String response = stdOut.toString();
281280
assertThat(response).contains("Assessment name: ");
282281
assertThat(response).contains("The reCAPTCHA score is: ");
283-
if (!hashedAccountId.isEmpty()) {
282+
if (!accountId.isEmpty()) {
284283
assertThat(response).contains("Account Defender Assessment Result: ");
285284
}
286285

0 commit comments

Comments
 (0)