@@ -461,10 +461,14 @@ private void post(Vertx vertx, String endpoint, JsonObject body, Handler<AsyncRe
461461 }
462462
463463 private void postV2 (ClientKey ck , Vertx vertx , String endpoint , JsonObject body , long nonce , String referer , Handler <AsyncResult <HttpResponse <Buffer >>> handler ) {
464- postV2 (ck , vertx , endpoint , body , nonce , referer , handler , Collections .emptyMap ());
464+ postV2 (ck , vertx , endpoint , body , nonce , referer , handler , Collections .emptyMap (), false );
465465 }
466466
467467 private void postV2 (ClientKey ck , Vertx vertx , String endpoint , JsonObject body , long nonce , String referer , Handler <AsyncResult <HttpResponse <Buffer >>> handler , Map <String , String > additionalHeaders ) {
468+ postV2 (ck , vertx , endpoint , body , nonce , referer , handler , additionalHeaders , false );
469+ }
470+
471+ private void postV2 (ClientKey ck , Vertx vertx , String endpoint , JsonObject body , long nonce , String referer , Handler <AsyncResult <HttpResponse <Buffer >>> handler , Map <String , String > additionalHeaders , boolean forceBase64RequestBody ) {
468472 WebClient client = WebClient .create (vertx );
469473 final String apiKey = ck == null ? "" : clientKey ;
470474 HttpRequest <Buffer > request = client .postAbs (getUrlForEndpoint (endpoint ))
@@ -492,7 +496,7 @@ private void postV2(ClientKey ck, Vertx vertx, String endpoint, JsonObject body,
492496 request .putHeader ("Referer" , referer );
493497 }
494498
495- if (request .headers ().contains (HttpHeaders .CONTENT_TYPE .toString (), HttpMediaType .APPLICATION_OCTET_STREAM .getType (), true )) {
499+ if (request .headers ().contains (HttpHeaders .CONTENT_TYPE .toString (), HttpMediaType .APPLICATION_OCTET_STREAM .getType (), true ) && ! forceBase64RequestBody ) {
496500 request .sendBuffer (bufBody , handler );
497501 } else {
498502 request .sendBuffer (Buffer .buffer (Utils .toBase64String (bufBody .getBytes ()).getBytes (StandardCharsets .UTF_8 )), handler );
@@ -962,6 +966,39 @@ void identityMapNewClientNoPolicySpecified(String apiVersion, String contentType
962966 }, Map .of (HttpHeaders .CONTENT_TYPE .toString (), contentType ));
963967 }
964968
969+ @ Test
970+ void fallbackToBase64DecodingIfBinaryEnvelopeDecodeFails (Vertx vertx , VertxTestContext testContext ) {
971+ final int clientSiteId = 201 ;
972+ fakeAuth (clientSiteId , newClientCreationDateTime , Role .MAPPER );
973+ setupSalts ();
974+ setupKeys ();
975+ ClientKey ck = (ClientKey ) clientKeyProvider .get ("" );
976+ long nonce = new BigInteger (Random .getBytes (8 )).longValue ();
977+
978+ JsonObject req = new JsonObject ();
979+ JsonArray emails = new JsonArray ();
980+ req .put ("email" , emails );
981+ emails .add ("test1@uid2.com" );
982+
983+ postV2 (ck , vertx , "v2/identity/map" , req , nonce , null , ar -> {
984+ assertTrue (ar .succeeded ());
985+ assertEquals (200 , ar .result ().statusCode ());
986+
987+ byte [] byteResp = Utils .decodeBase64String (ar .result ().bodyAsString ());
988+ byte [] decrypted = AesGcm .decrypt (byteResp , 0 , ck .getSecretBytes ());
989+
990+ JsonObject respJson = new JsonObject (new String (decrypted , 16 , decrypted .length - 16 , StandardCharsets .UTF_8 ));
991+ assertEquals ("success" , respJson .getString ("status" ));
992+
993+ // Check that response content type is text/plain
994+ assertEquals (HttpMediaType .TEXT_PLAIN .getType (), ar .result ().getHeader (HttpHeaders .CONTENT_TYPE .toString ()));
995+
996+ testContext .completeNow ();
997+
998+ // Set request content type header to application/octet-stream, but force a base64 encoded request envelope
999+ }, Map .of (HttpHeaders .CONTENT_TYPE .toString (), "application/octet-stream" ), true );
1000+ }
1001+
9651002 @ ParameterizedTest
9661003 @ MethodSource ("versionAndPolicy" )
9671004 void identityMapNewClientWrongPolicySpecified (String apiVersion , String policyParameterKey , Vertx vertx , VertxTestContext testContext ) {
0 commit comments