Skip to content

Commit a645e2a

Browse files
authored
Merge pull request #633 from IABTechLab/ccm-UID2-3590-call-check-token-input-cstg
Respond with 400 instead of 500 when CSTG request validation fails
2 parents fc22065 + 9dfcc5d commit a645e2a

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/main/java/com/uid2/operator/vertx/UIDOperatorVerticle.java

+12-18
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ else if(emailHash != null) {
453453
input = InputUtil.normalizePhoneHash(phoneHash);
454454
}
455455

456+
if (!checkForInvalidTokenInput(input, rc)) {
457+
return;
458+
}
459+
456460
PrivacyBits privacyBits = new PrivacyBits();
457461
privacyBits.setLegacyBit();
458462
privacyBits.setClientSideTokenGenerate();
@@ -893,7 +897,7 @@ private void handleTokenRefreshV2(RoutingContext rc) {
893897
private void handleTokenValidateV1(RoutingContext rc) {
894898
try {
895899
final InputUtil.InputVal input = this.phoneSupport ? getTokenInputV1(rc) : getTokenInput(rc);
896-
if (this.phoneSupport ? !checkTokenInputV1(input, rc) : !checkTokenInput(input, rc)) {
900+
if (!checkForInvalidTokenInput(input, rc)) {
897901
return;
898902
}
899903
if ((Arrays.equals(ValidateIdentityForEmailHash, input.getIdentityInput()) && input.getIdentityType() == IdentityType.Email)
@@ -924,7 +928,7 @@ private void handleTokenValidateV2(RoutingContext rc) {
924928
final JsonObject req = (JsonObject) rc.data().get("request");
925929

926930
final InputUtil.InputVal input = getTokenInputV2(req);
927-
if (this.phoneSupport ? !checkTokenInputV1(input, rc) : !checkTokenInput(input, rc)) {
931+
if (!checkForInvalidTokenInput(input, rc)) {
928932
return;
929933
}
930934
if ((input.getIdentityType() == IdentityType.Email && Arrays.equals(ValidateIdentityForEmailHash, input.getIdentityInput()))
@@ -956,7 +960,7 @@ private void handleTokenGenerateV1(RoutingContext rc) {
956960
try {
957961
final InputUtil.InputVal input = this.phoneSupport ? this.getTokenInputV1(rc) : this.getTokenInput(rc);
958962
platformType = getPlatformType(rc);
959-
if (this.phoneSupport ? !checkTokenInputV1(input, rc) : !checkTokenInput(input, rc)) {
963+
if (!checkForInvalidTokenInput(input, rc)) {
960964
return;
961965
} else {
962966
final IdentityTokens t = this.idService.generateIdentity(
@@ -983,7 +987,7 @@ private void handleTokenGenerateV2(RoutingContext rc) {
983987
platformType = getPlatformType(rc);
984988

985989
final InputUtil.InputVal input = this.getTokenInputV2(req);
986-
if (this.phoneSupport ? !checkTokenInputV1(input, rc) : !checkTokenInput(input, rc)) {
990+
if (!checkForInvalidTokenInput(input, rc)) {
987991
return;
988992
} else {
989993
final String apiContact = getApiContact(rc);
@@ -1258,7 +1262,7 @@ private void handleBucketsV2(RoutingContext rc) {
12581262

12591263
private void handleIdentityMapV1(RoutingContext rc) {
12601264
final InputUtil.InputVal input = this.phoneSupport ? this.getTokenInputV1(rc) : this.getTokenInput(rc);
1261-
if (this.phoneSupport ? !checkTokenInputV1(input, rc) : !checkTokenInput(input, rc)) {
1265+
if (!checkForInvalidTokenInput(input, rc)) {
12621266
return;
12631267
}
12641268
try {
@@ -1387,20 +1391,10 @@ private InputUtil.InputVal getTokenInputV1(RoutingContext rc) {
13871391
return null;
13881392
}
13891393

1390-
private boolean checkTokenInput(InputUtil.InputVal input, RoutingContext rc) {
1391-
if (input == null) {
1392-
ResponseUtil.ClientError(rc, "Required Parameter Missing: exactly one of email or email_hash must be specified");
1393-
return false;
1394-
} else if (!input.isValid()) {
1395-
ResponseUtil.ClientError(rc, "Invalid Identifier");
1396-
return false;
1397-
}
1398-
return true;
1399-
}
1400-
1401-
private boolean checkTokenInputV1(InputUtil.InputVal input, RoutingContext rc) {
1394+
private boolean checkForInvalidTokenInput(InputUtil.InputVal input, RoutingContext rc) {
14021395
if (input == null) {
1403-
ResponseUtil.ClientError(rc, "Required Parameter Missing: exactly one of [email, email_hash, phone, phone_hash] must be specified");
1396+
String message = this.phoneSupport ? "Required Parameter Missing: exactly one of [email, email_hash, phone, phone_hash] must be specified" : "Required Parameter Missing: exactly one of email or email_hash must be specified";
1397+
ResponseUtil.ClientError(rc, message);
14041398
return false;
14051399
} else if (!input.isValid()) {
14061400
ResponseUtil.ClientError(rc, "Invalid Identifier");

src/test/java/com/uid2/operator/UIDOperatorVerticleTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -4162,6 +4162,34 @@ void cstgNoActiveKey(Vertx vertx, VertxTestContext testContext) throws NoSuchAlg
41624162
});
41634163
}
41644164

4165+
@ParameterizedTest
4166+
@CsvSource({
4167+
"email_hash,random@unifiedid.com",
4168+
"phone_hash,1234567890",
4169+
})
4170+
void cstgInvalidInput(String identityType, String rawUID, Vertx vertx, VertxTestContext testContext) throws NoSuchAlgorithmException, InvalidKeyException {
4171+
setupCstgBackend("cstg.co.uk");
4172+
setupKeys(true);
4173+
4174+
JsonObject identity = new JsonObject();
4175+
identity.put(identityType, getSha256(rawUID) + getSha256(rawUID));
4176+
identity.put("optout_check", 1);
4177+
Tuple.Tuple2<JsonObject, SecretKey> data = createClientSideTokenGenerateRequestWithPayload(identity, Instant.now().toEpochMilli(), null);
4178+
4179+
sendCstg(vertx,
4180+
"v2/token/client-generate",
4181+
"http://cstg.co.uk",
4182+
data.getItem1(),
4183+
data.getItem2(),
4184+
400,
4185+
testContext,
4186+
respJson -> {
4187+
assertFalse(respJson.containsKey("body"));
4188+
assertEquals("Invalid Identifier", respJson.getString("message"));
4189+
testContext.completeNow();
4190+
});
4191+
}
4192+
41654193
private void assertAreClientSideGeneratedTokens(AdvertisingToken advertisingToken, RefreshToken refreshToken, int siteId, IdentityType identityType, String identity,
41664194
boolean expectClientSideTokenGenerateOptoutResponse) {
41674195
assertAreClientSideGeneratedTokens(advertisingToken,

0 commit comments

Comments
 (0)