Skip to content

Commit 99b8d9f

Browse files
authored
Merge pull request #1989 from IABTechLab/gdm-UID2-5837-v4-uid
2 parents 9bc5696 + dbb152f commit 99b8d9f

29 files changed

+961
-556
lines changed

src/main/java/com/uid2/operator/model/IdentityEnvironment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.uid2.operator.vertx.ClientInputValidationException;
55

66
public enum IdentityEnvironment {
7-
TEST(0), INTEG(1), PROD(2);
7+
TEST(0),
8+
INTEG(1),
9+
PROD(2);
810

911
private final int value;
1012

@@ -34,4 +36,4 @@ public static IdentityEnvironment fromString(String value) {
3436
default -> throw new ClientInputValidationException("Invalid valid for IdentityEnvironment: " + value);
3537
};
3638
}
37-
}
39+
}

src/main/java/com/uid2/operator/model/IdentityRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public IdentityRequest(
1010
PublisherIdentity publisherIdentity,
1111
UserIdentity userIdentity,
1212
OptoutCheckPolicy tokenGeneratePolicy,
13-
IdentityEnvironment identityEnvironment)
14-
{
13+
IdentityEnvironment identityEnvironment) {
1514
this.publisherIdentity = publisherIdentity;
1615
this.userIdentity = userIdentity;
1716
this.optoutCheckPolicy = tokenGeneratePolicy;

src/main/java/com/uid2/operator/model/IdentityScope.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ public enum IdentityScope {
66
UID2(0),
77
EUID(1);
88

9-
public final int value;
9+
private final int value;
1010

11-
IdentityScope(int value) { this.value = value; }
11+
IdentityScope(int value) {
12+
this.value = value;
13+
}
14+
15+
public int getValue() {
16+
return value;
17+
}
1218

1319
public static IdentityScope fromValue(int value) {
14-
switch (value) {
15-
case 0: return UID2;
16-
case 1: return EUID;
17-
default: throw new ClientInputValidationException("Invalid value for IdentityScope: " + value);
18-
}
20+
return switch (value) {
21+
case 0 -> UID2;
22+
case 1 -> EUID;
23+
default -> throw new ClientInputValidationException("Invalid value for IdentityScope: " + value);
24+
};
1925
}
2026

2127
public static IdentityScope fromString(String str) {
22-
switch (str.toLowerCase()) {
23-
case "uid2": return UID2;
24-
case "euid": return EUID;
25-
default: throw new ClientInputValidationException("Invalid string for IdentityScope: " + str);
26-
}
28+
return switch (str.toLowerCase()) {
29+
case "uid2" -> UID2;
30+
case "euid" -> EUID;
31+
default -> throw new ClientInputValidationException("Invalid string for IdentityScope: " + str);
32+
};
2733
}
2834
}

src/main/java/com/uid2/operator/model/IdentityType.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
import com.uid2.operator.vertx.ClientInputValidationException;
44

55
public enum IdentityType {
6-
Email(0), Phone(1);
6+
Email(0),
7+
Phone(1);
78

8-
public final int value;
9+
private final int value;
910

10-
IdentityType(int value) { this.value = value; }
11+
IdentityType(int value) {
12+
this.value = value;
13+
}
14+
15+
public int getValue() {
16+
return value;
17+
}
1118

1219
public static IdentityType fromValue(int value) {
13-
switch (value) {
14-
case 0: return Email;
15-
case 1: return Phone;
16-
default: throw new ClientInputValidationException("Invalid valid for IdentityType: " + value);
17-
}
20+
return switch (value) {
21+
case 0 -> Email;
22+
case 1 -> Phone;
23+
default -> throw new ClientInputValidationException("Invalid valid for IdentityType: " + value);
24+
};
1825
}
1926
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.uid2.operator.model;
2+
3+
import com.uid2.operator.vertx.ClientInputValidationException;
4+
5+
public enum IdentityVersion {
6+
V2(-1), // V2 raw UIDs don't encode version
7+
V3(0),
8+
V4(1);
9+
10+
private final int value;
11+
12+
IdentityVersion(int value) {
13+
this.value = value;
14+
}
15+
16+
public int getValue() {
17+
return value;
18+
}
19+
20+
public static IdentityVersion fromValue(int value) {
21+
return switch (value) {
22+
case -1 -> V2;
23+
case 0 -> V3;
24+
case 1 -> V4;
25+
default -> throw new ClientInputValidationException("Invalid valid for IdentityVersion: " + value);
26+
};
27+
}
28+
}

src/main/java/com/uid2/operator/model/KeyManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.stream.Collectors;
1717

1818
public class KeyManager {
19-
private static final Logger LOGGER = LoggerFactory.getLogger(UIDOperatorVerticle.class);
19+
private static final Logger LOGGER = LoggerFactory.getLogger(KeyManager.class);
2020
private final IKeysetKeyStore keysetKeyStore;
2121
private final RotatingKeysetProvider keysetProvider;
2222

@@ -76,7 +76,6 @@ public KeysetKey getKey(int keyId) {
7676
return this.keysetKeyStore.getSnapshot().getKey(keyId);
7777
}
7878

79-
8079
public List<KeysetKey> getKeysForSharingOrDsps() {
8180
Map<Integer, Keyset> keysetMap = this.keysetProvider.getSnapshot().getAllKeysets();
8281
List<KeysetKey> keys = keysetKeyStore.getSnapshot().getAllKeysetKeys();

src/main/java/com/uid2/operator/model/MapRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public MapRequest(
1212
UserIdentity userIdentity,
1313
OptoutCheckPolicy optoutCheckPolicy,
1414
Instant asOf,
15-
IdentityEnvironment identityEnvironment)
16-
{
15+
IdentityEnvironment identityEnvironment) {
1716
this.userIdentity = userIdentity;
1817
this.optoutCheckPolicy = optoutCheckPolicy;
1918
this.asOf = asOf;

src/main/java/com/uid2/operator/model/UserIdentity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.time.Instant;
44
import java.util.Arrays;
5-
import java.util.Objects;
65

76
public class UserIdentity {
87
public final IdentityScope identityScope;

src/main/java/com/uid2/operator/service/EncodingUtils.java

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.util.Base64;
99
import java.util.UUID;
1010

11-
public class EncodingUtils {
11+
public final class EncodingUtils {
12+
private EncodingUtils() {
13+
}
1214

1315
public static String toBase64String(byte[] b) {
1416
return Base64.getEncoder().encodeToString(b);
@@ -18,9 +20,13 @@ public static byte[] toBase64(byte[] b) {
1820
return Base64.getEncoder().encode(b);
1921
}
2022

21-
public static byte[] fromBase64(String s) { return Base64.getDecoder().decode(s); }
23+
public static byte[] fromBase64(String s) {
24+
return Base64.getDecoder().decode(s);
25+
}
2226

23-
public static byte[] fromBase64(byte[] b) { return Base64.getDecoder().decode(b); }
27+
public static byte[] fromBase64(byte[] b) {
28+
return Base64.getDecoder().decode(b);
29+
}
2430

2531
public static String getSha256(String input, String salt) {
2632
return toBase64String(getSha256Bytes(input, salt));
@@ -34,10 +40,18 @@ public static byte[] getSha256Bytes(String input) {
3440
return getSha256Bytes(input, null);
3541
}
3642

43+
public static byte[] getSha256Bytes(byte[] input) {
44+
return getSha256Bytes(input, null);
45+
}
46+
3747
public static byte[] getSha256Bytes(String input, String salt) {
48+
return getSha256Bytes(input.getBytes(), salt);
49+
}
50+
51+
public static byte[] getSha256Bytes(byte[] input, String salt) {
3852
try {
3953
MessageDigest md = MessageDigest.getInstance("SHA-256");
40-
md.update(input.getBytes());
54+
md.update(input);
4155
if (salt != null) {
4256
md.update(salt.getBytes());
4357
}
@@ -47,34 +61,6 @@ public static byte[] getSha256Bytes(String input, String salt) {
4761
}
4862
}
4963

50-
public static String generateIdGuid(String value) {
51-
byte[] b = value.getBytes(Charset.forName("UTF8"));
52-
long high = 0L;
53-
high = high | (long) b[0] << (7 * 8);
54-
high = high | (long) b[1] << (6 * 8);
55-
high = high | (long) b[2] << (5 * 8);
56-
high = high | (long) b[3] << (5 * 8);
57-
high = high | (long) b[4] << (3 * 8);
58-
high = high | (long) b[5] << (2 * 8);
59-
high = high | (long) b[6] << (1 * 8);
60-
high = high | (long) b[7];
61-
62-
long low = 0;
63-
low = low | (long) b[8] << (7 * 8);
64-
low = low | (long) b[9] << (6 * 8);
65-
low = low | (long) b[10] << (5 * 8);
66-
low = low | (long) b[11] << (4 * 8);
67-
low = low | (long) b[12] << (3 * 8);
68-
low = low | (long) b[13] << (2 * 8);
69-
low = low | (long) b[14] << (1 * 8);
70-
low = low | (long) b[15];
71-
72-
String uid = new UUID(high, low).toString();
73-
74-
return uid;
75-
76-
}
77-
7864
public static Instant NowUTCMillis() {
7965
return Instant.now().truncatedTo(ChronoUnit.MILLIS);
8066
}
@@ -84,21 +70,21 @@ public static Instant NowUTCMillis(Clock clock) {
8470
}
8571

8672
public static byte[] fromHexString(String hs) throws NumberFormatException {
87-
if(hs.length() % 2 == 1) {
73+
if (hs.length() % 2 == 1) {
8874
throw new NumberFormatException("input " + hs.substring(0, 5) + "... is not a valid hex string - odd length");
8975
}
9076

9177
byte[] s = new byte[hs.length() / 2];
92-
for(int i = 0; i < hs.length(); i++) {
78+
for (int i = 0; i < hs.length(); i++) {
9379
int v; char c = hs.charAt(i);
94-
if(c >= '0' && c <= '9') v = c - '0';
95-
else if(c >= 'A' && c <= 'F') v = c - 'A' + 10;
96-
else if(c >= 'a' && c <= 'f') v = c - 'a' + 10;
80+
if (c >= '0' && c <= '9') v = c - '0';
81+
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
82+
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10;
9783
else throw new NumberFormatException("input " + hs.substring(0, 5) + "... is not a valid hex string - invalid character");
9884
if (i % 2 == 0) {
99-
s[i / 2] = (byte) (s[i / 2] | (byte)((v << 4) & 0xFF));
85+
s[i / 2] = (byte) (s[i / 2] | (byte) ((v << 4) & 0xFF));
10086
} else {
101-
s[i / 2] = (byte) (s[i / 2] | (byte)(v & 0xFF));
87+
s[i / 2] = (byte) (s[i / 2] | (byte) (v & 0xFF));
10288
}
10389
}
10490

src/main/java/com/uid2/operator/service/EncryptedTokenEncoder.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ public AdvertisingToken decodeAdvertisingTokenV2(Buffer b) {
231231
} catch (Exception e) {
232232
throw new RuntimeException("Couldn't decode advertisingTokenV2", e);
233233
}
234-
235234
}
236235

237236
public AdvertisingToken decodeAdvertisingTokenV3orV4(Buffer b, byte[] bytes, TokenVersion tokenVersion) {
@@ -253,8 +252,7 @@ public AdvertisingToken decodeAdvertisingTokenV3orV4(Buffer b, byte[] bytes, Tok
253252
final IdentityScope identityScope = id.length == 32 ? IdentityScope.UID2 : decodeIdentityScopeV3(id[0]);
254253
final IdentityType identityType = id.length == 32 ? IdentityType.Email : decodeIdentityTypeV3(id[0]);
255254

256-
if (id.length > 32)
257-
{
255+
if (id.length > 32) {
258256
if (identityScope != decodeIdentityScopeV3(b.getByte(0))) {
259257
throw new ClientInputValidationException("Failed decoding advertisingTokenV3: Identity scope mismatch");
260258
}
@@ -338,7 +336,6 @@ public static String bytesToBase64Token(byte[] advertisingTokenBytes, TokenVersi
338336

339337
@Override
340338
public IdentityTokens encode(AdvertisingToken advertisingToken, RefreshToken refreshToken, Instant refreshFrom, Instant asOf) {
341-
342339
final byte[] advertisingTokenBytes = encode(advertisingToken, asOf);
343340
final String base64AdvertisingToken = bytesToBase64Token(advertisingTokenBytes, advertisingToken.version);
344341

@@ -367,16 +364,16 @@ private byte[] encryptIdentityV2(PublisherIdentity publisherIdentity, UserIdenti
367364
}
368365
}
369366

370-
static private byte encodeIdentityTypeV3(UserIdentity userIdentity) {
371-
return (byte) (TokenUtils.encodeIdentityScope(userIdentity.identityScope) | (userIdentity.identityType.value << 2) | 3);
367+
private static byte encodeIdentityTypeV3(UserIdentity userIdentity) {
368+
return (byte) (TokenUtils.encodeIdentityScope(userIdentity.identityScope) | (userIdentity.identityType.getValue() << 2) | 3);
372369
// "| 3" is used so that the 2nd char matches the version when V3 or higher. Eg "3" for V3 and "4" for V4
373370
}
374371

375-
static private IdentityScope decodeIdentityScopeV3(byte value) {
372+
private static IdentityScope decodeIdentityScopeV3(byte value) {
376373
return IdentityScope.fromValue((value & 0x10) >> 4);
377374
}
378375

379-
static private IdentityType decodeIdentityTypeV3(byte value) {
376+
private static IdentityType decodeIdentityTypeV3(byte value) {
380377
return IdentityType.fromValue((value & 0xf) >> 2);
381378
}
382379

@@ -392,7 +389,7 @@ static PublisherIdentity decodePublisherIdentityV3(Buffer b, int offset) {
392389

393390
static void encodeOperatorIdentityV3(Buffer b, OperatorIdentity operatorIdentity) {
394391
b.appendInt(operatorIdentity.siteId);
395-
b.appendByte((byte)operatorIdentity.operatorType.value);
392+
b.appendByte((byte) operatorIdentity.operatorType.value);
396393
b.appendInt(operatorIdentity.operatorVersion);
397394
b.appendInt(operatorIdentity.operatorKeyId);
398395
}

0 commit comments

Comments
 (0)