-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator.java
More file actions
430 lines (358 loc) · 19 KB
/
Operator.java
File metadata and controls
430 lines (358 loc) · 19 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package app.component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.uid2.client.IdentityScope;
import com.uid2.client.*;
import com.uid2.shared.util.Mapper;
import common.*;
import lombok.Getter;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.time.Clock;
import java.time.Instant;
import java.util.Base64;
public class Operator extends App {
public enum Type {
PUBLIC("Public"),
PRIVATE("Private");
private final String name;
Type(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public enum CloudProvider {
PUBLIC(""),
AWS("aws-nitro"),
GCP("gcp-oidc"),
AZURE("azure-cc");
private final String name;
CloudProvider(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
private record V2Envelope(String envelope, byte[] nonce) {
}
// When running via the pipeline, environment variables are defined in the uid2-shared-actions repo.
// When running via IntelliJ, environment variables are defined in the uid2-dev-workspace repo under .idea/runConfigurations.
// Test data is defined in the uid2-admin repo.
private static final Logger LOGGER = LoggerFactory.getLogger(Operator.class);
private static final ObjectMapper OBJECT_MAPPER = Mapper.getInstance();
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private static final int TIMESTAMP_LENGTH = 8;
private static final int PUBLIC_KEY_PREFIX_LENGTH = 9;
private static final int AUTHENTICATION_TAG_LENGTH_BITS = 128;
private static final int IV_BYTES = 12;
private static final String TC_STRING = "CPhJRpMPhJRpMABAMBFRACBoALAAAEJAAIYgAKwAQAKgArABAAqAAA";
public static final String CLIENT_API_KEY = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_KEY);
public static final String CLIENT_API_SECRET = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_SECRET);
static {
// Log operator configuration at class initialization
String maskedKey = CLIENT_API_KEY != null && CLIENT_API_KEY.length() > 20
? CLIENT_API_KEY.substring(0, 10) + "..." + CLIENT_API_KEY.substring(CLIENT_API_KEY.length() - 10)
: "[null or too short]";
String maskedSecret = CLIENT_API_SECRET != null && CLIENT_API_SECRET.length() > 20
? CLIENT_API_SECRET.substring(0, 10) + "..." + CLIENT_API_SECRET.substring(CLIENT_API_SECRET.length() - 10)
: "[null or too short]";
LOGGER.info(() -> String.format(
"[OPERATOR CONFIG] Initialized with:%n" +
" CLIENT_API_KEY: %s (length: %d)%n" +
" CLIENT_API_SECRET: %s (length: %d)%n" +
" E2E_ENV: %s%n" +
" IDENTITY_SCOPE: %s",
maskedKey, CLIENT_API_KEY != null ? CLIENT_API_KEY.length() : 0,
maskedSecret, CLIENT_API_SECRET != null ? CLIENT_API_SECRET.length() : 0,
EnvUtil.getEnv(Const.Config.ENV, false),
EnvUtil.getEnv(Const.Config.IDENTITY_SCOPE, false)
));
}
// Local only - Sharing
public static final String CLIENT_API_KEY_SHARING_RECIPIENT = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_KEY_SHARING_RECIPIENT, EnabledCondition.isLocal());
public static final String CLIENT_API_SECRET_SHARING_RECIPIENT = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_SECRET_SHARING_RECIPIENT, EnabledCondition.isLocal());
public static final String CLIENT_API_KEY_NON_SHARING_RECIPIENT = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_KEY_NON_SHARING_RECIPIENT, EnabledCondition.isLocal());
public static final String CLIENT_API_SECRET_NON_SHARING_RECIPIENT = EnvUtil.getEnv(Const.Config.Operator.CLIENT_API_SECRET_NON_SHARING_RECIPIENT, EnabledCondition.isLocal());
// Local only - CSTG
public static final String CSTG_SUBSCRIPTION_ID = EnvUtil.getEnv(Const.Config.Operator.CSTG_SUBSCRIPTION_ID, EnabledCondition.isLocal());
public static final String CSTG_SERVER_PUBLIC_KEY = EnvUtil.getEnv(Const.Config.Operator.CSTG_SERVER_PUBLIC_KEY, EnabledCondition.isLocal());
public static final String CSTG_ORIGIN = EnvUtil.getEnv(Const.Config.Operator.CSTG_ORIGIN, EnabledCondition.isLocal());
public static final String CSTG_INVALID_ORIGIN = EnvUtil.getEnv(Const.Config.Operator.CSTG_INVALID_ORIGIN, EnabledCondition.isLocal());
@Getter
private final Type type;
private final PublisherUid2Client publisherClient;
private final UID2Client dspClient;
public Operator(String host, Integer port, String name, Type type) {
super(host, port, name);
this.type = type;
this.publisherClient = new PublisherUid2Client(
getBaseUrl(),
CLIENT_API_KEY,
CLIENT_API_SECRET
);
this.dspClient = new UID2Client(
getBaseUrl(),
CLIENT_API_KEY,
CLIENT_API_SECRET,
IDENTITY_SCOPE
);
}
public Operator(String host, String name, Type type) {
this(host, null, name, type);
}
public TokenGenerateResponse v2TokenGenerate(String type, String identity) {
TokenGenerateInput token;
if ("email".equals(type)) {
token = TokenGenerateInput.fromEmail(identity);
} else if ("phone".equals(type)) {
token = TokenGenerateInput.fromPhone(identity);
} else {
throw new IllegalArgumentException("Unsupported input type for token generation");
}
if (IDENTITY_SCOPE == IdentityScope.EUID) {
token = token.withTransparencyAndConsentString(TC_STRING);
}
token = token.doNotGenerateTokensForOptedOut();
return publisherClient.generateTokenResponse(token);
}
public JsonNode v2ClientSideTokenGenerate(String requestBody, boolean useValidOrigin) throws Exception {
final byte[] serverPublicKeyBytes = base64ToByteArray(CSTG_SERVER_PUBLIC_KEY.substring(PUBLIC_KEY_PREFIX_LENGTH));
final PublicKey serverPublicKey = KeyFactory.getInstance("EC")
.generatePublic(new X509EncodedKeySpec(serverPublicKeyBytes));
final KeyPair keyPair = generateKeyPair();
final SecretKey sharedSecret = generateSharedSecret(serverPublicKey, keyPair);
final JsonObject cstgEnvelope = createCstgEnvelope(requestBody, keyPair.getPublic(), sharedSecret);
final Request.Builder requestBuilder = new Request.Builder()
.url(getBaseUrl() + "/v2/token/client-generate")
.addHeader("Origin", useValidOrigin ? CSTG_ORIGIN : CSTG_INVALID_ORIGIN)
.post(RequestBody.create(cstgEnvelope.toString(), HttpClient.JSON));
final String encryptedResponse = HttpClient.execute(requestBuilder.build(), HttpClient.HttpMethod.POST);
return v2DecryptResponseWithoutNonce(encryptedResponse, sharedSecret.getEncoded());
}
private static KeyPair generateKeyPair() {
final KeyPairGenerator keyPairGenerator;
try {
keyPairGenerator = KeyPairGenerator.getInstance("EC");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
final ECGenParameterSpec ecParameterSpec = new ECGenParameterSpec("secp256r1");
try {
keyPairGenerator.initialize(ecParameterSpec);
} catch (InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
return keyPairGenerator.genKeyPair();
}
private static SecretKey generateSharedSecret(PublicKey serverPublicKey, KeyPair clientKeypair) {
try {
final KeyAgreement ka = KeyAgreement.getInstance("ECDH");
ka.init(clientKeypair.getPrivate());
ka.doPhase(serverPublicKey, true);
return new SecretKeySpec(ka.generateSecret(), "AES");
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
private static JsonObject createCstgEnvelope(String request, PublicKey clientPublicKey, SecretKey sharedSecret) {
final long now = Clock.systemUTC().millis();
final byte[] iv = new byte[IV_BYTES];
SECURE_RANDOM.nextBytes(iv);
final JsonArray aad = new JsonArray();
aad.add(now);
final byte[] payload = encryptCSTG(request.getBytes(StandardCharsets.UTF_8),
iv,
aad.toString().getBytes(StandardCharsets.UTF_8),
sharedSecret);
final JsonObject body = new JsonObject();
body.addProperty("payload", byteArrayToBase64(payload));
body.addProperty("iv", byteArrayToBase64(iv));
body.addProperty("public_key", byteArrayToBase64(clientPublicKey.getEncoded()));
body.addProperty("timestamp", now);
body.addProperty("subscription_id", CSTG_SUBSCRIPTION_ID);
return body;
}
private static byte[] encryptCSTG(byte[] plaintext, byte[] iv, byte[] aad, SecretKey key) {
final Cipher cipher;
try {
cipher = Cipher.getInstance("AES/GCM/NoPadding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException(e);
}
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(AUTHENTICATION_TAG_LENGTH_BITS, iv);
try {
cipher.init(Cipher.ENCRYPT_MODE, key, gcmParameterSpec);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
cipher.updateAAD(aad);
try {
return cipher.doFinal(plaintext);
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e);
}
}
public TokenRefreshResponse v2TokenRefresh(IdentityTokens identity) {
return publisherClient.refreshToken(identity);
}
public JsonNode v2TokenRefresh(String refreshToken, String refreshResponseKey) throws Exception {
String response = HttpClient.post(getBaseUrl() + "/v2/token/refresh", refreshToken, CLIENT_API_KEY);
return v2DecryptResponseWithoutNonce(response, base64ToByteArray(refreshResponseKey));
}
public JsonNode v2TokenValidate(String type, String identity, String advertisingToken) throws Exception {
String payload = "{\"%s\":\"%s\",\"token\":\"%s\"}".formatted(type, identity, advertisingToken);
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/token/validate", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
public JsonNode v2TokenLogout(String type, String identity) throws Exception {
String payload = "{\"%s\":\"%s\"}".formatted(type, identity);
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/token/logout", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
public DecryptionResponse v2TokenDecrypt(String token) throws UID2ClientException {
dspClient.refresh();
return dspClient.decrypt(token);
}
// Need to use the manual mapping for error cases - SDK won't allow creating input with bad emails or disable optout check
public JsonNode v2IdentityMap(String payload) throws Exception {
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/identity/map", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
public IdentityMapResponse v2IdentityMap(IdentityMapInput input) {
IdentityMapClient identityMapClient = new IdentityMapClient(getBaseUrl(), CLIENT_API_KEY, CLIENT_API_SECRET);
return identityMapClient.generateIdentityMap(input);
}
// Need to use the manual mapping for error cases - SDK won't allow creating input with bad emails
public JsonNode v3IdentityMap(String payload) throws Exception {
String baseUrl = getBaseUrl();
String endpoint = baseUrl + "/v3/identity/map";
LOGGER.info(() -> String.format(
"[v3IdentityMap] Preparing request:%n" +
" Operator Name: %s%n" +
" Operator Type: %s%n" +
" Base URL: %s%n" +
" Full Endpoint: %s%n" +
" CLIENT_API_KEY: %s (length: %d)%n" +
" CLIENT_API_SECRET: %s (length: %d)%n" +
" Payload (raw): %s",
getName(), type, baseUrl, endpoint,
CLIENT_API_KEY != null ? CLIENT_API_KEY.substring(0, Math.min(20, CLIENT_API_KEY.length())) + "..." : "[null]",
CLIENT_API_KEY != null ? CLIENT_API_KEY.length() : 0,
CLIENT_API_SECRET != null ? CLIENT_API_SECRET.substring(0, Math.min(20, CLIENT_API_SECRET.length())) + "..." : "[null]",
CLIENT_API_SECRET != null ? CLIENT_API_SECRET.length() : 0,
payload != null && payload.length() > 200 ? payload.substring(0, 200) + "..." : payload
));
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
LOGGER.info(() -> String.format(
"[v3IdentityMap] Created envelope:%n" +
" Envelope length: %d%n" +
" Nonce length: %d",
envelope.envelope().length(),
envelope.nonce().length
));
try {
String encryptedResponse = HttpClient.post(endpoint, envelope.envelope(), CLIENT_API_KEY);
LOGGER.info(() -> String.format(
"[v3IdentityMap] Request successful, response length: %d",
encryptedResponse != null ? encryptedResponse.length() : 0
));
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
} catch (Exception e) {
final String errorMsg = e.getMessage();
final String errorType = e.getClass().getName();
LOGGER.error(() -> String.format(
"[v3IdentityMap] Request failed:%n" +
" Endpoint: %s%n" +
" Error: %s%n" +
" Error Type: %s",
endpoint, errorMsg, errorType
));
throw e;
}
}
public IdentityMapV3Response v3IdentityMap(IdentityMapV3Input input) {
IdentityMapV3Client identityMapV3Client = new IdentityMapV3Client(getBaseUrl(), CLIENT_API_KEY, CLIENT_API_SECRET);
return identityMapV3Client.generateIdentityMap(input);
}
public JsonNode v2IdentityBuckets(String payload) throws Exception {
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/identity/buckets", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
public JsonNode v2OptOutStatus(String payload) throws Exception {
V2Envelope envelope = v2CreateEnvelope(payload, CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/optout/status", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
public JsonNode v2KeySharing() throws Exception {
V2Envelope envelope = v2CreateEnvelope("", CLIENT_API_SECRET);
String encryptedResponse = HttpClient.post(getBaseUrl() + "/v2/key/sharing", envelope.envelope(), CLIENT_API_KEY);
return v2DecryptEncryptedResponse(encryptedResponse, envelope.nonce(), CLIENT_API_SECRET);
}
private V2Envelope v2CreateEnvelope(String payload, String secret) throws Exception {
// Unencrypted envelope payload = timestamp + nonce + raw payload
Instant timestamp = Instant.now();
int nonceLength = 8;
byte[] nonce = new byte[nonceLength];
SECURE_RANDOM.nextBytes(nonce);
byte[] payloadBytes = payload.getBytes(StandardCharsets.UTF_8);
ByteBuffer writer = ByteBuffer.allocate(TIMESTAMP_LENGTH + nonce.length + payloadBytes.length);
writer.putLong(timestamp.toEpochMilli());
writer.put(nonce);
writer.put(payloadBytes);
// Encrypted envelope = 1 + iv + encrypted envelope payload + tag
byte envelopeVersion = 1;
byte[] encrypted = encryptGDM(writer.array(), base64ToByteArray(secret)); // iv + encrypted envelope payload + tag
ByteBuffer envelopeBuffer = ByteBuffer.allocate(1 + encrypted.length);
envelopeBuffer.put(envelopeVersion);
envelopeBuffer.put(encrypted);
return new V2Envelope(byteArrayToBase64(envelopeBuffer.array()), nonce);
}
private JsonNode v2DecryptEncryptedResponse(String encryptedResponse, byte[] nonceInRequest, String secret) throws Exception {
Constructor<Uid2Helper> cons = Uid2Helper.class.getDeclaredConstructor(String.class);
cons.setAccessible(true);
Uid2Helper uid2Helper = cons.newInstance(secret);
String decryptedResponse = uid2Helper.decrypt(encryptedResponse, nonceInRequest);
return OBJECT_MAPPER.readTree(decryptedResponse);
}
private JsonNode v2DecryptResponseWithoutNonce(String response, byte[] key) throws Exception {
Method decryptTokenRefreshResponseMethod = Uid2Helper.class.getDeclaredMethod("decryptTokenRefreshResponse", String.class, byte[].class);
decryptTokenRefreshResponseMethod.setAccessible(true);
String decryptedResponse = (String) decryptTokenRefreshResponseMethod.invoke(Uid2Helper.class, response, key);
return OBJECT_MAPPER.readTree(decryptedResponse);
}
private byte[] encryptGDM(byte[] b, byte[] secretBytes) throws Exception {
Class<?> clazz = Class.forName("com.uid2.client.Uid2Encryption");
Method encryptGDMMethod = clazz.getDeclaredMethod("encryptGCM", byte[].class, byte[].class, byte[].class);
encryptGDMMethod.setAccessible(true);
return (byte[]) encryptGDMMethod.invoke(clazz, b, null, secretBytes);
}
private static byte[] base64ToByteArray(String str) {
return Base64.getDecoder().decode(str);
}
private static String byteArrayToBase64(byte[] b) {
return Base64.getEncoder().encodeToString(b);
}
}