Skip to content

Commit 43ddf4d

Browse files
committed
Using binary payload with IdentityMapV3
1 parent db70dee commit 43ddf4d

File tree

9 files changed

+100
-23
lines changed

9 files changed

+100
-23
lines changed

src/main/java/com/uid2/client/EnvelopeV2.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33

44
public class EnvelopeV2 {
5-
EnvelopeV2(String envelope, byte[] nonce) {
6-
this.envelope = envelope;
5+
EnvelopeV2(byte[] envelope, byte[] nonce) {
6+
this.binaryEnvelope = envelope;
77
this.nonce = nonce;
88
}
99

1010
/**
1111
* @return an encrypted request envelope which can be used in the POST body of a <a href="https://unifiedid.com/docs/endpoints/summary-endpoints">UID2 endpoint</a>.
1212
* See <a href="https://unifiedid.com/docs/getting-started/gs-encryption-decryption#encrypted-request-envelope">Encrypted Request Envelope</a>
1313
*/
14-
public String getEnvelope() { return envelope; }
14+
public String getEnvelope() { return InputUtil.byteArrayToBase64(binaryEnvelope); }
1515
byte[] getNonce() { return nonce;}
1616

17-
private final String envelope;
17+
public byte[] getBinaryEnvelope() {
18+
return binaryEnvelope;
19+
}
20+
21+
private final byte[] binaryEnvelope;
1822
private final byte[] nonce;
1923
}
2024

src/main/java/com/uid2/client/IdentityMapClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IdentityMapClient(String uid2BaseUrl, String clientApiKey, String base64S
1919
public IdentityMapResponse generateIdentityMap(IdentityMapInput identityMapInput) {
2020
EnvelopeV2 envelope = identityMapHelper.createEnvelopeForIdentityMapRequest(identityMapInput);
2121

22-
String responseString = uid2ClientHelper.makeRequest(envelope, "/v2/identity/map");
22+
String responseString = uid2ClientHelper.makeRequest("/v2/identity/map", envelope).getAsString();
2323
return identityMapHelper.createIdentityMapResponse(responseString, envelope, identityMapInput);
2424
}
2525

src/main/java/com/uid2/client/IdentityMapV3Client.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public IdentityMapV3Client(String uid2BaseUrl, String clientApiKey, String base6
1919
public IdentityMapV3Response generateIdentityMap(IdentityMapV3Input identityMapInput) {
2020
EnvelopeV2 envelope = identityMapHelper.createEnvelopeForIdentityMapRequest(identityMapInput);
2121

22-
String responseString = uid2ClientHelper.makeRequest(envelope, "/v3/identity/map");
23-
return identityMapHelper.createIdentityMapResponse(responseString, envelope, identityMapInput);
22+
Uid2Response response = uid2ClientHelper.makeBinaryRequest("/v3/identity/map", envelope);
23+
if (response.isBinary()) {
24+
return identityMapHelper.createIdentityMapResponse(response.getAsBytes(), envelope, identityMapInput);
25+
} else {
26+
return identityMapHelper.createIdentityMapResponse(response.getAsString(), envelope, identityMapInput);
27+
}
2428
}
2529

2630
private final IdentityMapV3Helper identityMapHelper;

src/main/java/com/uid2/client/IdentityMapV3Helper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ public IdentityMapV3Response createIdentityMapResponse(String responseString, En
3131
return new IdentityMapV3Response(decryptedResponseString, identityMapInput);
3232
}
3333

34+
public IdentityMapV3Response createIdentityMapResponse(byte[] response, EnvelopeV2 envelope, IdentityMapV3Input identityMapInput) {
35+
String decryptedResponseString = uid2Helper.decrypt(response, envelope.getNonce());
36+
return new IdentityMapV3Response(decryptedResponseString, identityMapInput);
37+
}
38+
3439
private final Uid2Helper uid2Helper;
3540
}

src/main/java/com/uid2/client/PublisherUid2Client.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public PublisherUid2Client(String uid2BaseUrl, String clientApiKey, String base6
2121
public IdentityTokens generateToken(TokenGenerateInput tokenGenerateInput) {
2222
EnvelopeV2 envelope = publisherUid2Helper.createEnvelopeForTokenGenerateRequest(tokenGenerateInput);
2323

24-
String responseString = uid2ClientHelper.makeRequest(envelope, "/v2/token/generate");
24+
String responseString = uid2ClientHelper.makeRequest("/v2/token/generate", envelope).getAsString();
2525
return publisherUid2Helper.createIdentityfromTokenGenerateResponse(responseString, envelope);
2626
}
2727

@@ -33,7 +33,7 @@ public IdentityTokens generateToken(TokenGenerateInput tokenGenerateInput) {
3333
public TokenGenerateResponse generateTokenResponse(TokenGenerateInput tokenGenerateInput) {
3434
EnvelopeV2 envelope = publisherUid2Helper.createEnvelopeForTokenGenerateRequest(tokenGenerateInput);
3535

36-
String responseString = uid2ClientHelper.makeRequest(envelope, "/v2/token/generate");
36+
String responseString = uid2ClientHelper.makeRequest("/v2/token/generate", envelope).getAsString();
3737
return publisherUid2Helper.createTokenGenerateResponse(responseString, envelope);
3838
}
3939

@@ -42,7 +42,7 @@ public TokenGenerateResponse generateTokenResponse(TokenGenerateInput tokenGener
4242
* @return the refreshed IdentityTokens instance (with a new advertising token and updated expiry times). Typically, this will be used to replace the current identity in the user's session
4343
*/
4444
public TokenRefreshResponse refreshToken(IdentityTokens currentIdentity) {
45-
String responseString = uid2ClientHelper.makeRequest(currentIdentity.getRefreshToken(), "/v2/token/refresh");
45+
String responseString = uid2ClientHelper.makeRequest("/v2/token/refresh", currentIdentity.getRefreshToken()).getAsString();
4646
return PublisherUid2Helper.createTokenRefreshResponse(responseString, currentIdentity);
4747
}
4848

src/main/java/com/uid2/client/TokenHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ EncryptionDataResponse encryptRawUidIntoToken(String rawUid, Instant now) {
4848
RefreshResponse refresh(String urlSuffix) {
4949
try{
5050
EnvelopeV2 envelope = uid2Helper.createEnvelopeV2("".getBytes());
51-
String responseString = uid2ClientHelper.makeRequest(envelope, urlSuffix);
51+
String responseString = uid2ClientHelper.makeRequest(urlSuffix, envelope).getAsString();
5252
byte[] response = uid2Helper.decrypt(responseString, envelope.getNonce()).getBytes();
5353
this.container.set(KeyParser.parse(new ByteArrayInputStream(response)));
5454
return RefreshResponse.makeSuccess();

src/main/java/com/uid2/client/Uid2ClientHelper.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import okhttp3.*;
44

55
import java.io.IOException;
6+
import java.util.Objects;
67

78
public class Uid2ClientHelper {
89
Uid2ClientHelper(String baseUrl, String clientApiKey) {
@@ -17,51 +18,73 @@ static Headers getHeaders(String clientApiKey) {
1718
.build();
1819
}
1920

20-
String makeRequest(EnvelopeV2 envelope, String urlSuffix) {
21-
return makeRequest(envelope.getEnvelope(), urlSuffix);
21+
Uid2Response makeRequest(String urlSuffix, EnvelopeV2 envelope) {
22+
return makeRequest(urlSuffix, getEnvelope(envelope));
2223
}
2324

24-
String makeRequest(String requestBody, String urlSuffix) {
25+
Uid2Response makeRequest(String urlSuffix, String payload) {
26+
return makeRequest(RequestBody.create(payload, FORM), urlSuffix);
27+
}
28+
29+
private static String getEnvelope(EnvelopeV2 envelope) {
30+
return envelope.getEnvelope();
31+
}
32+
33+
Uid2Response makeBinaryRequest(String urlSuffix, EnvelopeV2 envelope) {
34+
return makeRequest(RequestBody.create(envelope.getBinaryEnvelope(), BINARY), urlSuffix);
35+
}
36+
37+
Uid2Response makeRequest(RequestBody body, String urlSuffix) {
2538
Request request = new Request.Builder()
2639
.url(baseUrl + urlSuffix)
2740
.headers(headers)
28-
.post(RequestBody.create(requestBody, FORM))
41+
.post(body)
2942
.build();
3043

31-
3244
try (Response response = client.newCall(request).execute()) {
3345
if (!response.isSuccessful()) {
3446
throw new Uid2Exception("Unexpected code " + response);
3547
}
36-
3748
return getResponse(response);
3849
} catch (IOException e) {
3950
throw new Uid2Exception("error communicating with api endpoint", e);
4051
}
4152
}
4253

43-
private static String getResponse(Response response) {
44-
String responseString;
54+
private static Uid2Response getResponse(Response response) {
55+
Uid2Response uid2Response;
4556

4657
try {
4758
if (response == null) {
4859
throw new Uid2Exception("Response is null");
4960
}
5061
else {
51-
responseString = response.body() != null ? response.body().string() : response.toString();
62+
if (responseIsBinary(response)) {
63+
byte[] bytes = response.body() != null ? response.body().bytes() : new byte[0];
64+
uid2Response = Uid2Response.fromBytes(bytes);
65+
} else {
66+
String string = response.body() != null ? response.body().string() : response.toString();
67+
uid2Response = Uid2Response.fromString(string);
68+
}
69+
5270
if (!response.isSuccessful()) {
53-
throw new Uid2Exception("Unexpected code " + responseString);
71+
throw new Uid2Exception("Unexpected code " + response);
5472
}
5573
}
56-
return responseString;
74+
return uid2Response;
5775
} catch (IOException e) {
5876
throw new Uid2Exception("Error communicating with api endpoint", e);
5977
}
6078
}
6179

80+
private static boolean responseIsBinary(Response response) {
81+
return Objects.equals(response.headers().get("Content-Type"), "application/octet-stream");
82+
}
83+
6284
private final OkHttpClient client = new OkHttpClient();
6385
private final String baseUrl;
6486
private final Headers headers;
6587
private final static MediaType FORM = MediaType.get("application/x-www-form-urlencoded");
88+
private final static MediaType BINARY = MediaType.get("application/octet-stream");
6689

6790
}

src/main/java/com/uid2/client/Uid2Helper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.uid2.client;
22

3+
import org.jetbrains.annotations.NotNull;
4+
35
import java.io.IOException;
46
import java.io.InputStream;
57
import java.nio.ByteBuffer;
@@ -30,20 +32,28 @@ public EnvelopeV2 createEnvelopeV2(byte[] nonce, Instant timestamp, byte[] iv, b
3032
final byte envelopeVersion = 1;
3133
envelopeBuffer.put(envelopeVersion);
3234
envelopeBuffer.put(encrypted);
33-
return new EnvelopeV2(InputUtil.byteArrayToBase64(envelopeBuffer.array()), nonce);
35+
return new EnvelopeV2(envelopeBuffer.array(), nonce);
3436
}
3537

3638
public String decrypt(String response, byte[] nonceInRequest) {
3739
return decrypt(response, secretKey, false, nonceInRequest);
3840
}
3941

42+
public String decrypt(byte[] response, byte[] nonceInRequest) {
43+
return decrypt(response, secretKey, false, nonceInRequest);
44+
}
45+
4046
static String decryptTokenRefreshResponse(String response, byte[] secretKey) {
4147
return decrypt(response, secretKey, true, null);
4248
}
4349

4450
private static String decrypt(String response, byte[] secretKey, boolean isRefreshResponse, byte[] nonceInRequest) {
4551
//from parseV2Response
4652
byte[] responseBytes = InputUtil.base64ToByteArray(response);
53+
return decrypt(responseBytes, secretKey, isRefreshResponse, nonceInRequest);
54+
}
55+
56+
private static String decrypt(byte[] responseBytes, byte[] secretKey, boolean isRefreshResponse, byte[] nonceInRequest) {
4757
byte[] payload = Uid2Encryption.decryptGCM(responseBytes, 0, secretKey);
4858

4959
byte[] resultBytes;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.uid2.client;
2+
3+
public class Uid2Response {
4+
String asString;
5+
byte[] asBytes;
6+
7+
private Uid2Response(String asString, byte[] asBytes) {
8+
this.asString = asString;
9+
this.asBytes = asBytes;
10+
}
11+
12+
public static Uid2Response fromString(String asString) {
13+
return new Uid2Response(asString, null);
14+
}
15+
16+
public static Uid2Response fromBytes(byte[] asBytes) {
17+
return new Uid2Response(null, asBytes);
18+
}
19+
20+
public String getAsString() {
21+
return asString;
22+
}
23+
24+
public byte[] getAsBytes() {
25+
return asBytes;
26+
}
27+
28+
public boolean isBinary() {
29+
return asBytes != null;
30+
}
31+
}

0 commit comments

Comments
 (0)