Skip to content

Commit 92c2a7a

Browse files
authored
Merge pull request #73 from IABTechLab/mkc-UID2-3677-base64url-tokens
Support base64-encoded v4 tokens
2 parents adc1b67 + 0b1c000 commit 92c2a7a

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

pom.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
</dependency>
5454
<dependency>
5555
<groupId>org.junit.jupiter</groupId>
56-
<artifactId>junit-jupiter-engine</artifactId>
57-
<version>5.9.2</version>
56+
<artifactId>junit-jupiter</artifactId>
5857
<scope>test</scope>
5958
</dependency>
6059
<dependency>
@@ -65,11 +64,22 @@
6564
<dependency>
6665
<groupId>org.junit.jupiter</groupId>
6766
<artifactId>junit-jupiter-params</artifactId>
68-
<version>5.8.0</version>
6967
<scope>test</scope>
7068
</dependency>
7169
</dependencies>
7270

71+
<dependencyManagement>
72+
<dependencies>
73+
<dependency>
74+
<groupId>org.junit</groupId>
75+
<artifactId>junit-bom</artifactId>
76+
<version>5.10.3</version>
77+
<type>pom</type>
78+
<scope>import</scope>
79+
</dependency>
80+
</dependencies>
81+
</dependencyManagement>
82+
7383
<build>
7484
<resources>
7585
<resource>
@@ -193,6 +203,11 @@
193203
</executions>
194204
</plugin>
195205

206+
<plugin>
207+
<artifactId>maven-surefire-plugin</artifactId>
208+
<version>3.3.1</version>
209+
</plugin>
210+
196211
</plugins>
197212
</build>
198213
</project>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ static DecryptionResponse decrypt(String token, KeyContainer keys, Instant now,
4343
}
4444
else if (unsignedByte == AdvertisingTokenVersion.V4.value())
4545
{
46-
//same as V3 but use Base64URL encoding
47-
return decryptV3(Uid2Base64UrlCoder.decode(token), keys, now, identityScope, domainName, clientType, 4);
46+
// Accept either base64 or base64url encoding.
47+
return decryptV3(Base64.getDecoder().decode(base64UrlToBase64(token)), keys, now, identityScope, domainName, clientType, 4);
4848
}
4949

5050
return DecryptionResponse.makeError(DecryptionStatus.VERSION_NOT_SUPPORTED);
5151
}
5252

53+
static String base64UrlToBase64(String value) {
54+
// Base64 decoder doesn't require padding.
55+
return value.replace('-', '+')
56+
.replace('_', '/');
57+
}
58+
5359
static DecryptionResponse decryptV2(byte[] encryptedId, KeyContainer keys, Instant now, String domainName, ClientType clientType) throws Exception {
5460
try {
5561
ByteBuffer rootReader = ByteBuffer.wrap(encryptedId);

src/test/java/com/uid2/client/AdvertisingTokenBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ String build() throws Exception {
104104
}
105105

106106

107-
EncryptionTestsV4.validateAdvertisingToken(token, identityScope, identityType, version);
107+
EncryptionV4Tests.validateAdvertisingToken(token, identityScope, identityType, version);
108108
return token;
109109
}
110110
}

src/test/java/com/uid2/client/BidstreamClientTests.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
import com.google.gson.JsonObject;
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.params.ParameterizedTest;
7-
import org.junit.jupiter.params.provider.Arguments;
8-
import org.junit.jupiter.params.provider.CsvSource;
9-
import org.junit.jupiter.params.provider.MethodSource;
10-
import org.junit.jupiter.params.provider.ValueSource;
7+
import org.junit.jupiter.params.provider.*;
118

129
import java.time.Duration;
1310
import java.time.Instant;
1411
import java.time.temporal.ChronoUnit;
1512
import java.util.Arrays;
13+
import java.util.Base64;
1614
import java.util.stream.Stream;
1715

18-
import static com.uid2.client.EncryptionTestsV4.validateAdvertisingToken;
16+
import static com.uid2.client.EncryptionV4Tests.validateAdvertisingToken;
1917
import static com.uid2.client.SharingClientTests.keySetToJsonForSharing;
2018
import static com.uid2.client.TestData.*;
2119
import static org.junit.jupiter.api.Assertions.*;
@@ -288,6 +286,26 @@ public void tokenExpiryAndCustomNow() throws Exception {
288286
assertEquals(EXAMPLE_UID, res.getUid());
289287
}
290288

289+
@ParameterizedTest
290+
@EnumSource(IdentityScope.class)
291+
void decryptV4TokenEncodedAsBase64(IdentityScope identityScope) throws Exception {
292+
refresh(keyBidstreamResponse(identityScope, MASTER_KEY, SITE_KEY));
293+
294+
String advertisingToken;
295+
do {
296+
advertisingToken = AdvertisingTokenBuilder.builder()
297+
.withVersion(TokenVersionForTesting.V4)
298+
.withScope(identityScope)
299+
.build();
300+
301+
byte[] tokenAsBytes = Uid2Base64UrlCoder.decode(advertisingToken);
302+
advertisingToken = Base64.getEncoder().encodeToString(tokenAsBytes);
303+
}
304+
while (!advertisingToken.contains("=") || !advertisingToken.contains("/") || !advertisingToken.contains("+"));
305+
306+
decryptAndAssertSuccess(advertisingToken, TokenVersionForTesting.V4);
307+
}
308+
291309
private void refresh(String json) {
292310
RefreshResponse refreshResponse = bidstreamClient.refreshJson(json);
293311
assertTrue(refreshResponse.isSuccess());

src/test/java/com/uid2/client/EncryptionTestsV2.java renamed to src/test/java/com/uid2/client/EncryptionV2Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Arrays;
1111
import java.util.Base64;
1212

13-
public class EncryptionTestsV2 {
13+
public class EncryptionV2Tests {
1414
@Test
1515
public void smokeTest() throws Exception {
1616
UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, IdentityScope.UID2);

src/test/java/com/uid2/client/EncryptionTestsV3.java renamed to src/test/java/com/uid2/client/EncryptionV3Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.Arrays;
99
import java.util.Base64;
1010

11-
public class EncryptionTestsV3 {
11+
public class EncryptionV3Tests {
1212
@Test
1313
public void smokeTest() throws Exception {
1414
UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, IdentityScope.UID2);

src/test/java/com/uid2/client/EncryptionTestsV4.java renamed to src/test/java/com/uid2/client/EncryptionV4Tests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.Base64;
1414
import java.util.stream.Collectors;
1515

16-
public class EncryptionTestsV4 {
16+
public class EncryptionV4Tests {
1717
// unit tests to ensure the base64url encoding and decoding are identical in all supported
1818
// uid2 client sdks in different programming languages
1919
@Test

src/test/java/com/uid2/client/SharingClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.Base64;
1515

1616
import static com.uid2.client.BidstreamClientTests.*;
17-
import static com.uid2.client.EncryptionTestsV4.validateAdvertisingToken;
17+
import static com.uid2.client.EncryptionV4Tests.validateAdvertisingToken;
1818
import static com.uid2.client.TestData.*;
1919
import static org.junit.jupiter.api.Assertions.*;
2020

src/test/java/com/uid2/client/test/IntegrationExamples.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import java.util.TimerTask;
77

88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
10+
911
import static org.junit.jupiter.api.Assertions.*;
1012

13+
@EnabledIfEnvironmentVariable(named = "UID2_BASE_URL", matches = "\\S+")
1114
public class IntegrationExamples {
1215
final String TEST_ENDPOINT = System.getenv("UID2_BASE_URL");
1316
final String TEST_API_KEY = System.getenv("UID2_API_KEY");

0 commit comments

Comments
 (0)