Skip to content

Commit

Permalink
Address the comments and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caroline-ttd committed May 30, 2024
1 parent 1d06065 commit ae288fb
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public enum ResponseStatus {
}

public enum PlatformType {
InApp,
HasOriginHeader,
Other
InApp, // Request containing the "X-UID2-Client-Version" header, typically originating from Android, iOS, or tvOS (Apple TV).
HasOriginHeader, // Request containing the "original" header, originating from the web.
Other // Everything else, such as requests originating from the server side.
}

public static void record(ISiteStore siteStore, Integer siteId, Endpoint endpoint, TokenVersion advertisingTokenVersion, ResponseStatus responseStatus, PlatformType platformType) {
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/uid2/operator/vertx/UIDOperatorVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public class UIDOperatorVerticle extends AbstractVerticle {
private final int optOutStatusMaxRequestSize;
private final boolean optOutStatusApiEnabled;

//"Android" is from https://github.com/IABTechLab/uid2-android-sdk/blob/ff93ebf597f5de7d440a84f7015a334ba4138ede/sdk/src/main/java/com/uid2/UID2Client.kt#L46
//"ios"/"tvos" is from https://github.com/IABTechLab/uid2-ios-sdk/blob/91c290d29a7093cfc209eca493d1fee80c17e16a/Sources/UID2/UID2Client.swift#L36-L38
private final Set<String> SUPPORTED_IN_APP = new HashSet<>(Arrays.asList("Android", "ios", "tvos"));

public UIDOperatorVerticle(JsonObject config,
boolean clientSideTokenGenerate,
ISiteStore siteProvider,
Expand Down Expand Up @@ -327,7 +331,6 @@ private void handleClientSideTokenGenerateImpl(RoutingContext rc) throws NoSuchA
final JsonObject body;
TokenResponseStatsCollector.PlatformType platformType = TokenResponseStatsCollector.PlatformType.Other;
try {
platformType = getPlatformType(rc);
body = rc.body().asJsonObject();
} catch (DecodeException ex) {
SendClientErrorResponseAndRecordStats(ResponseStatus.ClientError, 400, rc, "json payload is not valid",
Expand All @@ -342,6 +345,7 @@ private void handleClientSideTokenGenerateImpl(RoutingContext rc) throws NoSuchA
}

final CstgRequest request = body.mapTo(CstgRequest.class);
platformType = request.getAppName() != null ? TokenResponseStatsCollector.PlatformType.InApp : getPlatformType(rc);

final ClientSideKeypair clientSideKeypair = this.clientSideKeypairProvider.getSnapshot().getKeypair(request.getSubscriptionId());
if (clientSideKeypair == null) {
Expand Down Expand Up @@ -1795,16 +1799,16 @@ private RefreshResponse refreshIdentity(RoutingContext rc, String tokenStr) {
}

public static String getSiteName(ISiteStore siteStore, Integer siteId) {
if (siteId == null) return "X-UID2-Client-Version";
if (siteId == null) return "unknown";
if (siteStore == null) return "unknown"; //this is expected if CSTG is not enabled, eg for private operators

final Site site = siteStore.getSite(siteId);
return (site == null) ? "unknown" : site.getName();
}

private TokenResponseStatsCollector.PlatformType getPlatformType(RoutingContext rc) {
final String clientVersion = rc.request().getHeader("X-UID2-Client-Version");
if (clientVersion != null && (clientVersion.contains("Android") || clientVersion.contains("ios") || clientVersion.contains("tvos"))) {
final String clientVersionHeader = rc.request().getHeader("X-UID2-Client-Version");
if (clientVersionHeader != null && (SUPPORTED_IN_APP.stream().anyMatch(clientVersionHeader::contains))) {
return TokenResponseStatsCollector.PlatformType.InApp;
}

Expand Down
Loading

0 comments on commit ae288fb

Please sign in to comment.