-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIdentityMapV3Input.java
More file actions
162 lines (139 loc) · 6.95 KB
/
IdentityMapV3Input.java
File metadata and controls
162 lines (139 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.uid2.client;
import com.google.gson.annotations.SerializedName;
import java.util.*;
public class IdentityMapV3Input {
/**
* @param emails a list of normalized or unnormalized email addresses
* @return a IdentityMapV3Input instance, to be used in {@link IdentityMapV3Helper#createEnvelopeForIdentityMapRequest}
*/
public static IdentityMapV3Input fromEmails(List<String> emails) {
return new IdentityMapV3Input().withEmails(emails);
}
/**
* @param hashedEmails a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address
* @return an IdentityMapV3Input instance
*/
public static IdentityMapV3Input fromHashedEmails(List<String> hashedEmails) {
return new IdentityMapV3Input().withHashedEmails(hashedEmails);
}
/**
* @param phones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number
* @return an IdentityMapV3Input instance
*/
public static IdentityMapV3Input fromPhones(List<String> phones) {
return new IdentityMapV3Input().withPhones(phones);
}
/**
* @param hashedPhones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number
* @return an IdentityMapV3Input instance
*/
public static IdentityMapV3Input fromHashedPhones(List<String> hashedPhones) {
return new IdentityMapV3Input().withHashedPhones(hashedPhones);
}
// Transient as this should not be part of the serialized JSON payload we send to UID2 Operator
private transient final Map<String, List<String>> hashedDiiToRawDii = new HashMap<>();
@SerializedName("email_hash")
private final List<String> hashedEmails = new ArrayList<>();
@SerializedName("phone_hash")
private final List<String> hashedPhones = new ArrayList<>();
// We never send unhashed emails or phone numbers in the SDK, but they are required fields in the API request
@SerializedName("email")
private List<String> emails = Collections.unmodifiableList(new ArrayList<>());
@SerializedName("phone")
private List<String> phones = Collections.unmodifiableList(new ArrayList<>());
public IdentityMapV3Input() {}
/**
* @param hashedEmails a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withHashedEmails(List<String> hashedEmails) {
for (String hashedEmail : hashedEmails) {
withHashedEmail(hashedEmail);
}
return this;
}
/**
* @param hashedEmail a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withHashedEmail(String hashedEmail) {
this.hashedEmails.add(hashedEmail);
addToDiiMappings(hashedEmail, hashedEmail);
return this;
}
/**
* @param hashedPhones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withHashedPhones(List<String> hashedPhones) {
for (String hashedPhone : hashedPhones) {
withHashedPhone(hashedPhone);
}
return this;
}
/**
* @param hashedPhone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withHashedPhone(String hashedPhone) {
this.hashedPhones.add(hashedPhone);
addToDiiMappings(hashedPhone, hashedPhone);
return this;
}
/**
* @param emails a list of normalized or unnormalized email addresses
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withEmails(List<String> emails) {
for (String email : emails) {
withEmail(email);
}
return this;
}
/**
* @param email a normalized or unnormalized email address
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withEmail(String email) {
String hashedEmail = InputUtil.normalizeAndHashEmail(email);
this.hashedEmails.add(hashedEmail);
addToDiiMappings(hashedEmail, email);
return this;
}
/**
* @param phones a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withPhones(List<String> phones) {
for (String phone : phones) {
withPhone(phone);
}
return this;
}
/**
* @param phone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number
* @return this IdentityMapV3Input instance
*/
public IdentityMapV3Input withPhone(String phone) {
if (!InputUtil.isPhoneNumberNormalized(phone)) {
throw new IllegalArgumentException("phone number is not normalized: " + phone);
}
String hashedPhone = InputUtil.getBase64EncodedHash(phone);
this.hashedPhones.add(hashedPhone);
addToDiiMappings(hashedPhone, phone);
return this;
}
List<String> getInputDiis(String identityType, int i) {
return hashedDiiToRawDii.get(getHashedDii(identityType, i));
}
private void addToDiiMappings(String hashedDii, String rawDii) {
hashedDiiToRawDii.computeIfAbsent(hashedDii, k -> new ArrayList<>()).add(rawDii);
}
private String getHashedDii(String identityType, int i) {
switch (identityType) {
case "email_hash": return hashedEmails.get(i);
case "phone_hash": return hashedPhones.get(i);
}
throw new Uid2Exception("Unexpected identity type: " + identityType);
}
}