-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
AccountDefenderAssessment.java
171 lines (147 loc) · 7.02 KB
/
AccountDefenderAssessment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package account_defender;
// [START recaptcha_enterprise_account_defender_assessment]
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.protobuf.ByteString;
import com.google.recaptchaenterprise.v1.AccountDefenderAssessment.AccountDefenderLabel;
import com.google.recaptchaenterprise.v1.Assessment;
import com.google.recaptchaenterprise.v1.CreateAssessmentRequest;
import com.google.recaptchaenterprise.v1.Event;
import com.google.recaptchaenterprise.v1.ProjectName;
import com.google.recaptchaenterprise.v1.RiskAnalysis.ClassificationReason;
import com.google.recaptchaenterprise.v1.TokenProperties;
import com.google.recaptchaenterprise.v1.UserId;
import com.google.recaptchaenterprise.v1.UserInfo;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class AccountDefenderAssessment {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
// TODO(developer): Replace these variables before running the sample.
// projectId: Google Cloud Project ID
String projectId = "project-id";
// recaptchaSiteKey: Site key obtained by registering a domain/app to use recaptcha
// services.
String recaptchaSiteKey = "recaptcha-site-key";
// token: The token obtained from the client on passing the recaptchaSiteKey.
// To get the token, integrate the recaptchaSiteKey with frontend. See,
// https://cloud.google.com/recaptcha-enterprise/docs/instrument-web-pages#frontend_integration_score
String token = "recaptcha-token";
// recaptchaAction: The action name corresponding to the token.
String recaptchaAction = "recaptcha-action";
// Unique ID of the user, such as email, customer ID, etc.
String accountId = "default" + UUID.randomUUID().toString().split("-")[0];
// User phone number
String phoneNumber = "555-987-XXXX";
// User email address
String emailAddress = "john.doe@example.com";
accountDefenderAssessment(projectId, recaptchaSiteKey, token, recaptchaAction, accountId, phoneNumber, emailAddress);
}
/**
* This assessment detects account takeovers. See,
* https://cloud.google.com/recaptcha-enterprise/docs/account-takeovers The input is the hashed
* account id. Result tells if the action represents an account takeover. You can optionally
* trigger a Multi-Factor Authentication based on the result.
*/
public static void accountDefenderAssessment(
String projectId,
String recaptchaSiteKey,
String token,
String recaptchaAction,
String accountId,
String phoneNumber,
String emailAddress)
throws IOException {
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the properties of the event to be tracked.
Event.Builder eventBuilder =
Event.newBuilder()
.setSiteKey(recaptchaSiteKey)
.setToken(token);
// Set the account id, email address and phone number (of the user).
eventBuilder.setUserInfo(
UserInfo.newBuilder()
.setAccountId(accountId)
.addUserIds(UserId.newBuilder().setEmail(emailAddress))
.addUserIds(UserId.newBuilder().setPhoneNumber(phoneNumber)));
Event event = eventBuilder.build();
// Build the assessment request.
CreateAssessmentRequest createAssessmentRequest =
CreateAssessmentRequest.newBuilder()
.setParent(ProjectName.of(projectId).toString())
.setAssessment(Assessment.newBuilder().setEvent(event).build())
.build();
Assessment response = client.createAssessment(createAssessmentRequest);
// Check integrity of the response token.
if (!checkTokenIntegrity(response.getTokenProperties(), recaptchaAction)) {
return;
}
// Get the reason(s) and the reCAPTCHA risk score.
// For more information on interpreting the assessment,
// see: https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
for (ClassificationReason reason : response.getRiskAnalysis().getReasonsList()) {
System.out.println(reason);
}
float recaptchaScore = response.getRiskAnalysis().getScore();
System.out.println("The reCAPTCHA score is: " + recaptchaScore);
String assessmentName = response.getName();
System.out.println(
"Assessment name: " + assessmentName.substring(assessmentName.lastIndexOf("/") + 1));
// Get the Account Defender result.
com.google.recaptchaenterprise.v1.AccountDefenderAssessment accountDefenderAssessment =
response.getAccountDefenderAssessment();
System.out.println(accountDefenderAssessment);
// Get Account Defender label.
List<AccountDefenderLabel> defenderResult =
response.getAccountDefenderAssessment().getLabelsList();
// Based on the result, can you choose next steps.
// If the 'defenderResult' field is empty, it indicates that Account Defender did not have
// anything to add to the score.
// Few result labels: ACCOUNT_DEFENDER_LABEL_UNSPECIFIED, PROFILE_MATCH,
// SUSPICIOUS_LOGIN_ACTIVITY, SUSPICIOUS_ACCOUNT_CREATION, RELATED_ACCOUNTS_NUMBER_HIGH.
// For more information on interpreting the assessment, see:
// https://cloud.google.com/recaptcha-enterprise/docs/account-defender#interpret-assessment-details
System.out.println("Account Defender Assessment Result: " + defenderResult);
}
}
private static boolean checkTokenIntegrity(
TokenProperties tokenProperties, String recaptchaAction) {
// Check if the token is valid.
if (!tokenProperties.getValid()) {
System.out.println(
"The Account Defender Assessment call failed because the token was: "
+ tokenProperties.getInvalidReason().name());
return false;
}
// Check if the expected action was executed.
if (!tokenProperties.getAction().equals(recaptchaAction)) {
System.out.printf(
"The action attribute in the reCAPTCHA tag '%s' does not match "
+ "the action '%s' you are expecting to score",
tokenProperties.getAction(), recaptchaAction);
return false;
}
return true;
}
}
// [END recaptcha_enterprise_account_defender_assessment]