Skip to content

Commit a8f0915

Browse files
committed
1. Renamed IdentityType to DiiType
2. In InputUtil, renamed Identity* member variable/methods to Dii* to indicate it's Dii 3. Change TokenUtils class getIdentityHash to getDiiHash and same for getDiiHashString
1 parent 6bb1fb9 commit a8f0915

20 files changed

+293
-298
lines changed

src/main/java/com/uid2/operator/model/IdentityType.java renamed to src/main/java/com/uid2/operator/model/identities/DiiType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package com.uid2.operator.model;
1+
package com.uid2.operator.model.identities;
22

33
import com.uid2.operator.vertx.ClientInputValidationException;
44

5-
public enum IdentityType {
5+
public enum DiiType {
66
Email(0), Phone(1);
77

88
public final int value;
99

10-
IdentityType(int value) { this.value = value; }
10+
DiiType(int value) { this.value = value; }
1111

12-
public static IdentityType fromValue(int value) {
12+
public static DiiType fromValue(int value) {
1313
switch (value) {
1414
case 0: return Email;
1515
case 1: return Phone;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package com.uid2.operator.model.identities;
22

3-
import com.uid2.operator.model.IdentityScope;
4-
import com.uid2.operator.model.IdentityType;
5-
63
import java.time.Instant;
74
import java.util.Arrays;
85

96
/**
107
* Contains a first level salted hash computed from Hashed DII (email/phone number)
118
* @param establishedAt for brand new token generation, it should be the time it is generated if the first level hash is from token/refresh call, it will be when the raw UID was originally created in the earliest token generation
129
*/
13-
public record FirstLevelHash(IdentityScope identityScope, IdentityType identityType, byte[] firstLevelHash,
10+
public record FirstLevelHash(IdentityScope identityScope, DiiType diiType, byte[] firstLevelHash,
1411
Instant establishedAt) {
1512

1613
// explicitly not checking establishedAt - this is only for making sure the first level hash matches a new input
1714
public boolean matches(FirstLevelHash that) {
1815
return this.identityScope.equals(that.identityScope) &&
19-
this.identityType.equals(that.identityType) &&
16+
this.diiType.equals(that.diiType) &&
2017
Arrays.equals(this.firstLevelHash, that.firstLevelHash);
2118
}
2219
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.uid2.operator.model.identities;
22

3-
import com.uid2.operator.model.IdentityScope;
4-
import com.uid2.operator.model.IdentityType;
5-
6-
// Contains a hash DII,
3+
// Contains a hash Directly Identifying Information (DII) (email or phone) see https://unifiedid.com/docs/ref-info/glossary-uid#gl-dii
74
// This hash can either be computed from a raw email/phone number DII input or provided by the UID Participant directly
8-
public record HashedDii(IdentityScope identityScope, IdentityType identityType, byte[] hashedDii) {
5+
//
6+
public record HashedDii(IdentityScope identityScope, DiiType diiType, byte[] hashedDii) {
97
}

src/main/java/com/uid2/operator/IdentityConst.java renamed to src/main/java/com/uid2/operator/model/identities/IdentityConst.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.uid2.operator;
1+
package com.uid2.operator.model.identities;
22

33
import com.uid2.operator.service.EncodingUtils;
44

src/main/java/com/uid2/operator/model/IdentityScope.java renamed to src/main/java/com/uid2/operator/model/identities/IdentityScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.uid2.operator.model;
1+
package com.uid2.operator.model.identities;
22

33
import com.uid2.operator.vertx.ClientInputValidationException;
44

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.uid2.operator.model.identities;
22

3-
import com.uid2.operator.model.IdentityScope;
4-
import com.uid2.operator.model.IdentityType;
5-
63
import java.util.Arrays;
74

85
// A raw UID is stored inside
9-
public record RawUid(IdentityScope identityScope, IdentityType identityType, byte[] rawUid) {
6+
public record RawUid(IdentityScope identityScope, DiiType diiType, byte[] rawUid) {
107

118
public boolean matches(RawUid that) {
129
return this.identityScope.equals(that.identityScope) &&
13-
this.identityType.equals(that.identityType) &&
10+
this.diiType.equals(that.diiType) &&
1411
Arrays.equals(this.rawUid, that.rawUid);
1512
}
1613
}

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.uid2.operator.service;
22

33
import com.uid2.operator.model.*;
4+
import com.uid2.operator.model.identities.DiiType;
45
import com.uid2.operator.model.identities.FirstLevelHash;
6+
import com.uid2.operator.model.identities.IdentityScope;
57
import com.uid2.operator.model.identities.RawUid;
68
import com.uid2.operator.util.PrivacyBits;
79
import com.uid2.operator.vertx.ClientInputValidationException;
@@ -69,7 +71,7 @@ private byte[] encodeIntoAdvertisingTokenV3(AdvertisingTokenRequest t, KeysetKey
6971
masterPayload.appendBytes(AesGcm.encrypt(sitePayload.getBytes(), siteKey).getPayload());
7072

7173
final Buffer b = Buffer.buffer(164);
72-
b.appendByte(encodeIdentityTypeV3(t.rawUid.identityScope(), t.rawUid.identityType()));
74+
b.appendByte(encodeIdentityTypeV3(t.rawUid.identityScope(), t.rawUid.diiType()));
7375
b.appendByte((byte) t.version.rawVersion);
7476
b.appendInt(masterKey.getId());
7577
b.appendBytes(AesGcm.encrypt(masterPayload.getBytes(), masterKey).getPayload());
@@ -129,7 +131,7 @@ private TokenRefreshRequest decodeRefreshTokenV2(Buffer b) {
129131
TokenVersion.V2, createdAt, validTill,
130132
new OperatorIdentity(0, OperatorType.Service, 0, 0),
131133
new SourcePublisher(siteId),
132-
new FirstLevelHash(IdentityScope.UID2, IdentityType.Email, identity,
134+
new FirstLevelHash(IdentityScope.UID2, DiiType.Email, identity,
133135
Instant.ofEpochMilli(establishedMillis)),
134136
privacyBits);
135137
}
@@ -152,19 +154,19 @@ private TokenRefreshRequest decodeRefreshTokenV3(Buffer b, byte[] bytes) {
152154
final PrivacyBits privacyBits = PrivacyBits.fromInt(b2.getInt(45));
153155
final Instant establishedAt = Instant.ofEpochMilli(b2.getLong(49));
154156
final IdentityScope identityScope = decodeIdentityScopeV3(b2.getByte(57));
155-
final IdentityType identityType = decodeIdentityTypeV3(b2.getByte(57));
157+
final DiiType diiType = decodeIdentityTypeV3(b2.getByte(57));
156158
final byte[] firstLevelHash = b2.getBytes(58, 90);
157159

158160
if (identityScope != decodeIdentityScopeV3(b.getByte(0))) {
159161
throw new ClientInputValidationException("Failed to decode refreshTokenV3: Identity scope mismatch");
160162
}
161-
if (identityType != decodeIdentityTypeV3(b.getByte(0))) {
163+
if (diiType != decodeIdentityTypeV3(b.getByte(0))) {
162164
throw new ClientInputValidationException("Failed to decode refreshTokenV3: Identity type mismatch");
163165
}
164166

165167
return new TokenRefreshRequest(
166168
TokenVersion.V3, createdAt, expiresAt, operatorIdentity, sourcePublisher,
167-
new FirstLevelHash(identityScope, identityType, firstLevelHash, establishedAt),
169+
new FirstLevelHash(identityScope, diiType, firstLevelHash, establishedAt),
168170
privacyBits);
169171
}
170172

@@ -233,7 +235,7 @@ public AdvertisingTokenRequest decodeAdvertisingTokenV2(Buffer b) {
233235
Instant.ofEpochMilli(expiresMillis),
234236
new OperatorIdentity(0, OperatorType.Service, 0, masterKeyId),
235237
new SourcePublisher(siteId, siteKeyId, 0),
236-
new RawUid(IdentityScope.UID2, IdentityType.Email, rawUid),
238+
new RawUid(IdentityScope.UID2, DiiType.Email, rawUid),
237239
privacyBits,
238240
Instant.ofEpochMilli(establishedMillis)
239241
);
@@ -262,21 +264,21 @@ public AdvertisingTokenRequest decodeAdvertisingTokenV3orV4(Buffer b, byte[] byt
262264
final Instant refreshedAt = Instant.ofEpochMilli(sitePayload.getLong(28));
263265
final byte[] rawUid = sitePayload.slice(36, sitePayload.length()).getBytes();
264266
final IdentityScope identityScope = rawUid.length == 32 ? IdentityScope.UID2 : decodeIdentityScopeV3(rawUid[0]);
265-
final IdentityType identityType = rawUid.length == 32 ? IdentityType.Email : decodeIdentityTypeV3(rawUid[0]);
267+
final DiiType diiType = rawUid.length == 32 ? DiiType.Email : decodeIdentityTypeV3(rawUid[0]);
266268

267269
if (rawUid.length > 32)
268270
{
269271
if (identityScope != decodeIdentityScopeV3(b.getByte(0))) {
270272
throw new ClientInputValidationException("Failed decoding advertisingTokenV3: Identity scope mismatch");
271273
}
272-
if (identityType != decodeIdentityTypeV3(b.getByte(0))) {
274+
if (diiType != decodeIdentityTypeV3(b.getByte(0))) {
273275
throw new ClientInputValidationException("Failed decoding advertisingTokenV3: Identity type mismatch");
274276
}
275277
}
276278

277279
return new AdvertisingTokenRequest(
278280
tokenVersion, createdAt, expiresAt, operatorIdentity, sourcePublisher,
279-
new RawUid(identityScope, identityType, rawUid),
281+
new RawUid(identityScope, diiType, rawUid),
280282
privacyBits, establishedAt
281283
);
282284
}
@@ -326,11 +328,11 @@ public byte[] encodeIntoRefreshTokenV3(TokenRefreshRequest t, KeysetKey serviceK
326328
encodePublisherRequesterV3(refreshPayload, t.sourcePublisher);
327329
refreshPayload.appendInt(t.privacyBits.getAsInt());
328330
refreshPayload.appendLong(t.firstLevelHash.establishedAt().toEpochMilli());
329-
refreshPayload.appendByte(encodeIdentityTypeV3(t.firstLevelHash.identityScope(), t.firstLevelHash.identityType()));
331+
refreshPayload.appendByte(encodeIdentityTypeV3(t.firstLevelHash.identityScope(), t.firstLevelHash.diiType()));
330332
refreshPayload.appendBytes(t.firstLevelHash.firstLevelHash());
331333

332334
final Buffer b = Buffer.buffer(124);
333-
b.appendByte(encodeIdentityTypeV3(t.firstLevelHash.identityScope(), t.firstLevelHash.identityType()));
335+
b.appendByte(encodeIdentityTypeV3(t.firstLevelHash.identityScope(), t.firstLevelHash.diiType()));
334336
b.appendByte((byte) t.version.rawVersion);
335337
b.appendInt(serviceKey.getId());
336338
b.appendBytes(AesGcm.encrypt(refreshPayload.getBytes(), serviceKey).getPayload());
@@ -402,17 +404,17 @@ private byte[] encryptIdentityV2(SourcePublisher sourcePublisher, byte[] id, Pri
402404
}
403405
}
404406

405-
static private byte encodeIdentityTypeV3(IdentityScope identityScope, IdentityType identityType) {
406-
return (byte) (TokenUtils.encodeIdentityScope(identityScope) | (identityType.value << 2) | 3);
407+
static private byte encodeIdentityTypeV3(IdentityScope identityScope, DiiType diiType) {
408+
return (byte) (TokenUtils.encodeIdentityScope(identityScope) | (diiType.value << 2) | 3);
407409
// "| 3" is used so that the 2nd char matches the version when V3 or higher. Eg "3" for V3 and "4" for V4
408410
}
409411

410412
static private IdentityScope decodeIdentityScopeV3(byte value) {
411413
return IdentityScope.fromValue((value & 0x10) >> 4);
412414
}
413415

414-
static private IdentityType decodeIdentityTypeV3(byte value) {
415-
return IdentityType.fromValue((value & 0xf) >> 2);
416+
static private DiiType decodeIdentityTypeV3(byte value) {
417+
return DiiType.fromValue((value & 0xf) >> 2);
416418
}
417419

418420
static void encodePublisherRequesterV3(Buffer b, SourcePublisher sourcePublisher) {

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.uid2.operator.service;
22

3-
import com.uid2.operator.model.IdentityScope;
4-
import com.uid2.operator.model.IdentityType;
3+
import com.uid2.operator.model.identities.IdentityScope;
4+
import com.uid2.operator.model.identities.DiiType;
55
import com.uid2.operator.model.identities.HashedDii;
66

77
public class InputUtil {
@@ -167,7 +167,7 @@ public static String normalizeEmailString(String email) {
167167
return addressPartToUse.append('@').append(domainPart).toString();
168168
}
169169

170-
public enum IdentityInputType {
170+
public enum DiiInputType {
171171
Raw,
172172
Hash
173173
}
@@ -183,62 +183,63 @@ private static enum EmailParsingState {
183183
public static class InputVal {
184184
private final String provided;
185185
private final String normalized;
186-
private final IdentityType identityType;
187-
private final IdentityInputType inputType;
186+
//Directly Identifying Information (DII) (email or phone) see https://unifiedid.com/docs/ref-info/glossary-uid#gl-dii
187+
private final DiiType diiType;
188+
private final DiiInputType inputType;
188189
private final boolean valid;
189-
private final byte[] identityInput;
190+
private final byte[] diiInput;
190191

191-
public InputVal(String provided, String normalized, IdentityType identityType, IdentityInputType inputType, boolean valid) {
192+
public InputVal(String provided, String normalized, DiiType diiType, DiiInputType inputType, boolean valid) {
192193
this.provided = provided;
193194
this.normalized = normalized;
194-
this.identityType = identityType;
195+
this.diiType = diiType;
195196
this.inputType = inputType;
196197
this.valid = valid;
197198
if (valid) {
198-
if (this.inputType == IdentityInputType.Raw) {
199-
this.identityInput = TokenUtils.getIdentityHash(this.normalized);
199+
if (this.inputType == DiiInputType.Raw) {
200+
this.diiInput = TokenUtils.getDiiHash(this.normalized);
200201
} else {
201-
this.identityInput = EncodingUtils.fromBase64(this.normalized);
202+
this.diiInput = EncodingUtils.fromBase64(this.normalized);
202203
}
203204
} else {
204-
this.identityInput = null;
205+
this.diiInput = null;
205206
}
206207
}
207208

208209
public static InputVal validEmail(String input, String normalized) {
209-
return new InputVal(input, normalized, IdentityType.Email, IdentityInputType.Raw, true);
210+
return new InputVal(input, normalized, DiiType.Email, DiiInputType.Raw, true);
210211
}
211212

212213
public static InputVal invalidEmail(String input) {
213-
return new InputVal(input, null, IdentityType.Email, IdentityInputType.Raw, false);
214+
return new InputVal(input, null, DiiType.Email, DiiInputType.Raw, false);
214215
}
215216

216217
public static InputVal validEmailHash(String input, String normalized) {
217-
return new InputVal(input, normalized, IdentityType.Email, IdentityInputType.Hash, true);
218+
return new InputVal(input, normalized, DiiType.Email, DiiInputType.Hash, true);
218219
}
219220

220221
public static InputVal invalidEmailHash(String input) {
221-
return new InputVal(input, null, IdentityType.Email, IdentityInputType.Hash, false);
222+
return new InputVal(input, null, DiiType.Email, DiiInputType.Hash, false);
222223
}
223224

224225
public static InputVal validPhone(String input, String normalized) {
225-
return new InputVal(input, normalized, IdentityType.Phone, IdentityInputType.Raw, true);
226+
return new InputVal(input, normalized, DiiType.Phone, DiiInputType.Raw, true);
226227
}
227228

228229
public static InputVal invalidPhone(String input) {
229-
return new InputVal(input, null, IdentityType.Phone, IdentityInputType.Raw, false);
230+
return new InputVal(input, null, DiiType.Phone, DiiInputType.Raw, false);
230231
}
231232

232233
public static InputVal validPhoneHash(String input, String normalized) {
233-
return new InputVal(input, normalized, IdentityType.Phone, IdentityInputType.Hash, true);
234+
return new InputVal(input, normalized, DiiType.Phone, DiiInputType.Hash, true);
234235
}
235236

236237
public static InputVal invalidPhoneHash(String input) {
237-
return new InputVal(input, null, IdentityType.Phone, IdentityInputType.Hash, false);
238+
return new InputVal(input, null, DiiType.Phone, DiiInputType.Hash, false);
238239
}
239240

240-
public byte[] getIdentityInput() {
241-
return this.identityInput;
241+
public byte[] getHashedDiiInput() {
242+
return this.diiInput;
242243
}
243244

244245
public String getProvided() {
@@ -249,21 +250,21 @@ public String getNormalized() {
249250
return normalized;
250251
}
251252

252-
public IdentityType getIdentityType() {
253-
return identityType;
253+
public DiiType getDiiType() {
254+
return diiType;
254255
}
255256

256-
public IdentityInputType getInputType() { return inputType; }
257+
public DiiInputType getInputType() { return inputType; }
257258

258259
public boolean isValid() {
259260
return valid;
260261
}
261262

262-
public HashedDii toHashedDiiIdentity(IdentityScope identityScope) {
263+
public HashedDii toHashedDii(IdentityScope identityScope) {
263264
return new HashedDii(
264265
identityScope,
265-
this.identityType,
266-
getIdentityInput());
266+
this.diiType,
267+
getHashedDiiInput());
267268
}
268269
}
269270

0 commit comments

Comments
 (0)