Skip to content

Commit

Permalink
Merge pull request #73 from IABTechLab/mkc-UID2-3677-base64url-tokens
Browse files Browse the repository at this point in the history
Support base64-encoded v4 tokens
  • Loading branch information
mcollins-ttd authored Jul 30, 2024
2 parents adc1b67 + 0b1c000 commit 92c2a7a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 15 deletions.
21 changes: 18 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -65,11 +64,22 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<resources>
<resource>
Expand Down Expand Up @@ -193,6 +203,11 @@
</executions>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
</plugin>

</plugins>
</build>
</project>
10 changes: 8 additions & 2 deletions src/main/java/com/uid2/client/Uid2Encryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ static DecryptionResponse decrypt(String token, KeyContainer keys, Instant now,
}
else if (unsignedByte == AdvertisingTokenVersion.V4.value())
{
//same as V3 but use Base64URL encoding
return decryptV3(Uid2Base64UrlCoder.decode(token), keys, now, identityScope, domainName, clientType, 4);
// Accept either base64 or base64url encoding.
return decryptV3(Base64.getDecoder().decode(base64UrlToBase64(token)), keys, now, identityScope, domainName, clientType, 4);
}

return DecryptionResponse.makeError(DecryptionStatus.VERSION_NOT_SUPPORTED);
}

static String base64UrlToBase64(String value) {
// Base64 decoder doesn't require padding.
return value.replace('-', '+')
.replace('_', '/');
}

static DecryptionResponse decryptV2(byte[] encryptedId, KeyContainer keys, Instant now, String domainName, ClientType clientType) throws Exception {
try {
ByteBuffer rootReader = ByteBuffer.wrap(encryptedId);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/uid2/client/AdvertisingTokenBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ String build() throws Exception {
}


EncryptionTestsV4.validateAdvertisingToken(token, identityScope, identityType, version);
EncryptionV4Tests.validateAdvertisingToken(token, identityScope, identityType, version);
return token;
}
}
Expand Down
28 changes: 23 additions & 5 deletions src/test/java/com/uid2/client/BidstreamClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
import com.google.gson.JsonObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.params.provider.*;

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Base64;
import java.util.stream.Stream;

import static com.uid2.client.EncryptionTestsV4.validateAdvertisingToken;
import static com.uid2.client.EncryptionV4Tests.validateAdvertisingToken;
import static com.uid2.client.SharingClientTests.keySetToJsonForSharing;
import static com.uid2.client.TestData.*;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -288,6 +286,26 @@ public void tokenExpiryAndCustomNow() throws Exception {
assertEquals(EXAMPLE_UID, res.getUid());
}

@ParameterizedTest
@EnumSource(IdentityScope.class)
void decryptV4TokenEncodedAsBase64(IdentityScope identityScope) throws Exception {
refresh(keyBidstreamResponse(identityScope, MASTER_KEY, SITE_KEY));

String advertisingToken;
do {
advertisingToken = AdvertisingTokenBuilder.builder()
.withVersion(TokenVersionForTesting.V4)
.withScope(identityScope)
.build();

byte[] tokenAsBytes = Uid2Base64UrlCoder.decode(advertisingToken);
advertisingToken = Base64.getEncoder().encodeToString(tokenAsBytes);
}
while (!advertisingToken.contains("=") || !advertisingToken.contains("/") || !advertisingToken.contains("+"));

decryptAndAssertSuccess(advertisingToken, TokenVersionForTesting.V4);
}

private void refresh(String json) {
RefreshResponse refreshResponse = bidstreamClient.refreshJson(json);
assertTrue(refreshResponse.isSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Arrays;
import java.util.Base64;

public class EncryptionTestsV2 {
public class EncryptionV2Tests {
@Test
public void smokeTest() throws Exception {
UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, IdentityScope.UID2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Arrays;
import java.util.Base64;

public class EncryptionTestsV3 {
public class EncryptionV3Tests {
@Test
public void smokeTest() throws Exception {
UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, IdentityScope.UID2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Base64;
import java.util.stream.Collectors;

public class EncryptionTestsV4 {
public class EncryptionV4Tests {
// unit tests to ensure the base64url encoding and decoding are identical in all supported
// uid2 client sdks in different programming languages
@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/uid2/client/SharingClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Base64;

import static com.uid2.client.BidstreamClientTests.*;
import static com.uid2.client.EncryptionTestsV4.validateAdvertisingToken;
import static com.uid2.client.EncryptionV4Tests.validateAdvertisingToken;
import static com.uid2.client.TestData.*;
import static org.junit.jupiter.api.Assertions.*;

Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/uid2/client/test/IntegrationExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.util.TimerTask;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import static org.junit.jupiter.api.Assertions.*;

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

0 comments on commit 92c2a7a

Please sign in to comment.