4545import io .vertx .core .json .JsonArray ;
4646import io .vertx .core .json .JsonObject ;
4747import io .vertx .ext .web .AllowForwardHeaders ;
48- import io .vertx .ext .web .Route ;
4948import io .vertx .ext .web .Router ;
5049import io .vertx .ext .web .RoutingContext ;
5150import io .vertx .ext .web .handler .BodyHandler ;
@@ -129,6 +128,10 @@ public class UIDOperatorVerticle extends AbstractVerticle {
129128 //"Android" is from https://github.com/IABTechLab/uid2-android-sdk/blob/ff93ebf597f5de7d440a84f7015a334ba4138ede/sdk/src/main/java/com/uid2/UID2Client.kt#L46
130129 //"ios"/"tvos" is from https://github.com/IABTechLab/uid2-ios-sdk/blob/91c290d29a7093cfc209eca493d1fee80c17e16a/Sources/UID2/UID2Client.swift#L36-L38
131130 private final static List <String > SUPPORTED_IN_APP = Arrays .asList ("Android" , "ios" , "tvos" );
131+
132+ private static final String ERROR_INVALID_INPUT_WITH_PHONE_SUPPORT = "Required Parameter Missing: exactly one of [email, email_hash, phone, phone_hash] must be specified" ;
133+ private static final String ERROR_INVALID_INPUT_EMAIL_MISSING = "Required Parameter Missing: exactly one of email or email_hash must be specified" ;
134+ private static final String ERROR_INVALID_INPUT_EMAIL_TWICE = "Only one of email or email_hash can be specified" ;
132135 public final static String ORIGIN_HEADER = "Origin" ;
133136
134137 public UIDOperatorVerticle (JsonObject config ,
@@ -450,7 +453,7 @@ else if(emailHash != null) {
450453 input = InputUtil .normalizePhoneHash (phoneHash );
451454 }
452455
453- if (!checkForInvalidTokenInput (input , rc )) {
456+ if (!isTokenInputValid (input , rc )) {
454457 return ;
455458 }
456459
@@ -869,7 +872,7 @@ private void handleTokenRefreshV2(RoutingContext rc) {
869872 private void handleTokenValidateV1 (RoutingContext rc ) {
870873 try {
871874 final InputUtil .InputVal input = this .phoneSupport ? getTokenInputV1 (rc ) : getTokenInput (rc );
872- if (!checkForInvalidTokenInput (input , rc )) {
875+ if (!isTokenInputValid (input , rc )) {
873876 return ;
874877 }
875878 if ((Arrays .equals (ValidateIdentityForEmailHash , input .getIdentityInput ()) && input .getIdentityType () == IdentityType .Email )
@@ -900,7 +903,7 @@ private void handleTokenValidateV2(RoutingContext rc) {
900903 final JsonObject req = (JsonObject ) rc .data ().get ("request" );
901904
902905 final InputUtil .InputVal input = getTokenInputV2 (req );
903- if (!checkForInvalidTokenInput (input , rc )) {
906+ if (!isTokenInputValid (input , rc )) {
904907 return ;
905908 }
906909 if ((input .getIdentityType () == IdentityType .Email && Arrays .equals (ValidateIdentityForEmailHash , input .getIdentityInput ()))
@@ -932,17 +935,13 @@ private void handleTokenGenerateV1(RoutingContext rc) {
932935 try {
933936 final InputUtil .InputVal input = this .phoneSupport ? this .getTokenInputV1 (rc ) : this .getTokenInput (rc );
934937 platformType = getPlatformType (rc );
935- if (!checkForInvalidTokenInput (input , rc )) {
936- return ;
937- } else {
938+ if (isTokenInputValid (input , rc )) {
938939 final IdentityTokens t = this .idService .generateIdentity (
939940 new IdentityRequest (
940941 new PublisherIdentity (siteId , 0 , 0 ),
941942 input .toUserIdentity (this .identityScope , 1 , Instant .now ()),
942943 OptoutCheckPolicy .defaultPolicy ()));
943944
944- //Integer.parseInt(rc.queryParam("privacy_bits").get(0))));
945-
946945 ResponseUtil .Success (rc , toJsonV1 (t ));
947946 recordTokenResponseStats (siteId , TokenResponseStatsCollector .Endpoint .GenerateV1 , TokenResponseStatsCollector .ResponseStatus .Success , siteProvider , t .getAdvertisingTokenVersion (), platformType );
948947 }
@@ -959,9 +958,7 @@ private void handleTokenGenerateV2(RoutingContext rc) {
959958 platformType = getPlatformType (rc );
960959
961960 final InputUtil .InputVal input = this .getTokenInputV2 (req );
962- if (!checkForInvalidTokenInput (input , rc )) {
963- return ;
964- } else {
961+ if (isTokenInputValid (input , rc )) {
965962 final String apiContact = getApiContact (rc );
966963
967964 switch (validateUserConsent (req )) {
@@ -978,8 +975,9 @@ private void handleTokenGenerateV2(RoutingContext rc) {
978975 break ;
979976 }
980977 default : {
981- assert false : "Please update UIDOperatorVerticle.handleTokenGenerateV2 when changing UserConsentStatus" ;
982- break ;
978+ final String errorMsg = "Please update UIDOperatorVerticle.handleTokenGenerateV2 when changing UserConsentStatus" ;
979+ LOGGER .error (errorMsg );
980+ throw new IllegalStateException (errorMsg );
983981 }
984982 }
985983
@@ -1037,7 +1035,7 @@ private void handleTokenGenerate(RoutingContext rc) {
10371035 final InputUtil .InputVal input = this .getTokenInput (rc );
10381036 Integer siteId = null ;
10391037 if (input == null ) {
1040- SendClientErrorResponseAndRecordStats (ResponseStatus .ClientError , 400 , rc , "Required Parameter Missing: exactly one of email or email_hash must be specified" , siteId , TokenResponseStatsCollector .Endpoint .GenerateV0 , TokenResponseStatsCollector .ResponseStatus .BadPayload , siteProvider , TokenResponseStatsCollector .PlatformType .Other );
1038+ SendClientErrorResponseAndRecordStats (ResponseStatus .ClientError , 400 , rc , ERROR_INVALID_INPUT_EMAIL_MISSING , siteId , TokenResponseStatsCollector .Endpoint .GenerateV0 , TokenResponseStatsCollector .ResponseStatus .BadPayload , siteProvider , TokenResponseStatsCollector .PlatformType .Other );
10411039 return ;
10421040 }
10431041 else if (!input .isValid ()) {
@@ -1053,8 +1051,6 @@ else if (!input.isValid()) {
10531051 input .toUserIdentity (this .identityScope , 1 , Instant .now ()),
10541052 OptoutCheckPolicy .defaultPolicy ()));
10551053
1056- //Integer.parseInt(rc.queryParam("privacy_bits").get(0))));
1057-
10581054 recordTokenResponseStats (siteId , TokenResponseStatsCollector .Endpoint .GenerateV0 , TokenResponseStatsCollector .ResponseStatus .Success , siteProvider , t .getAdvertisingTokenVersion (), TokenResponseStatsCollector .PlatformType .Other );
10591055 sendJsonResponse (rc , toJson (t ));
10601056
@@ -1234,7 +1230,7 @@ private void handleBucketsV2(RoutingContext rc) {
12341230
12351231 private void handleIdentityMapV1 (RoutingContext rc ) {
12361232 final InputUtil .InputVal input = this .phoneSupport ? this .getTokenInputV1 (rc ) : this .getTokenInput (rc );
1237- if (!checkForInvalidTokenInput (input , rc )) {
1233+ if (!isTokenInputValid (input , rc )) {
12381234 return ;
12391235 }
12401236 try {
@@ -1254,13 +1250,7 @@ private void handleIdentityMap(RoutingContext rc) {
12541250 final InputUtil .InputVal input = this .getTokenInput (rc );
12551251
12561252 try {
1257- if (input == null ) {
1258- ResponseUtil .ClientError (rc , "Required Parameter Missing: exactly one of email or email_hash must be specified" );
1259- }
1260- else if (!input .isValid ()) {
1261- ResponseUtil .ClientError (rc , "Invalid email or email_hash" );
1262- }
1263- else {
1253+ if (isTokenInputValid (input , rc )) {
12641254 final Instant now = Instant .now ();
12651255 final MappedIdentity mappedIdentity = this .idService .map (input .toUserIdentity (this .identityScope , 0 , now ), now );
12661256 rc .response ().end (EncodingUtils .toBase64String (mappedIdentity .advertisingId ));
@@ -1363,9 +1353,9 @@ private InputUtil.InputVal getTokenInputV1(RoutingContext rc) {
13631353 return null ;
13641354 }
13651355
1366- private boolean checkForInvalidTokenInput (InputUtil .InputVal input , RoutingContext rc ) {
1356+ private boolean isTokenInputValid (InputUtil .InputVal input , RoutingContext rc ) {
13671357 if (input == null ) {
1368- 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" ;
1358+ String message = this .phoneSupport ? ERROR_INVALID_INPUT_WITH_PHONE_SUPPORT : ERROR_INVALID_INPUT_EMAIL_MISSING ;
13691359 ResponseUtil .ClientError (rc , message );
13701360 return false ;
13711361 } else if (!input .isValid ()) {
@@ -1381,11 +1371,11 @@ private InputUtil.InputVal[] getIdentityBulkInput(RoutingContext rc) {
13811371 final JsonArray emailHashes = obj .getJsonArray ("email_hash" );
13821372 // FIXME TODO. Avoid Double Iteration. Turn to a decorator pattern
13831373 if (emails == null && emailHashes == null ) {
1384- ResponseUtil .ClientError (rc , "Exactly one of email or email_hash must be specified" );
1374+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_EMAIL_MISSING );
13851375 return null ;
13861376 } else if (emails != null && !emails .isEmpty ()) {
13871377 if (emailHashes != null && !emailHashes .isEmpty ()) {
1388- ResponseUtil .ClientError (rc , "Only one of email or email_hash can be specified" );
1378+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_EMAIL_TWICE );
13891379 return null ;
13901380 }
13911381 return createInputList (emails , false );
@@ -1398,7 +1388,7 @@ private InputUtil.InputVal[] getIdentityBulkInput(RoutingContext rc) {
13981388 private InputUtil .InputVal [] getIdentityBulkInputV1 (RoutingContext rc ) {
13991389 final JsonObject obj = rc .body ().asJsonObject ();
14001390 if (obj .isEmpty ()) {
1401- ResponseUtil .ClientError (rc , "Exactly one of [email, email_hash, phone, phone_hash] must be specified" );
1391+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_WITH_PHONE_SUPPORT );
14021392 return null ;
14031393 }
14041394 final JsonArray emails = JsonParseUtils .parseArray (obj , "email" , rc );
@@ -1430,7 +1420,7 @@ private InputUtil.InputVal[] getIdentityBulkInputV1(RoutingContext rc) {
14301420 }
14311421
14321422 if (validInputs == 0 || nonEmptyInputs > 1 ) {
1433- ResponseUtil .ClientError (rc , "Exactly one of [email, email_hash, phone, phone_hash] must be specified" );
1423+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_WITH_PHONE_SUPPORT );
14341424 return null ;
14351425 }
14361426
@@ -1511,9 +1501,9 @@ private void handleIdentityMapV2(RoutingContext rc) {
15111501 final InputUtil .InputVal [] inputList = getIdentityMapV2Input (rc );
15121502 if (inputList == null ) {
15131503 if (this .phoneSupport )
1514- ResponseUtil .ClientError (rc , "Exactly one of [email, email_hash, phone, phone_hash] must be specified" );
1504+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_WITH_PHONE_SUPPORT );
15151505 else
1516- ResponseUtil .ClientError (rc , "Required Parameter Missing: exactly one of email or email_hash must be specified" );
1506+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_EMAIL_MISSING );
15171507 return ;
15181508 }
15191509
@@ -1579,11 +1569,11 @@ private void handleIdentityMapBatch(RoutingContext rc) {
15791569 final JsonArray emails = obj .getJsonArray ("email" );
15801570 final JsonArray emailHashes = obj .getJsonArray ("email_hash" );
15811571 if (emails == null && emailHashes == null ) {
1582- ResponseUtil .ClientError (rc , "Exactly one of email or email_hash must be specified" );
1572+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_EMAIL_MISSING );
15831573 return ;
15841574 } else if (emails != null && !emails .isEmpty ()) {
15851575 if (emailHashes != null && !emailHashes .isEmpty ()) {
1586- ResponseUtil .ClientError (rc , "Only one of email or email_hash can be specified" );
1576+ ResponseUtil .ClientError (rc , ERROR_INVALID_INPUT_EMAIL_TWICE );
15871577 return ;
15881578 }
15891579 inputList = createInputList (emails , false );
0 commit comments