1717
1818public class EncryptedTokenEncoder implements ITokenEncoder {
1919 private final KeyManager keyManager ;
20+ private final IdentityEnvironment identityEnvironment ;
2021
21- public EncryptedTokenEncoder (KeyManager keyManager ) {
22+ public EncryptedTokenEncoder (KeyManager keyManager , IdentityEnvironment identityEnvironment ) {
2223 this .keyManager = keyManager ;
24+ this .identityEnvironment = identityEnvironment ;
2325 }
2426
2527 public byte [] encode (AdvertisingToken t , Instant asOf ) {
@@ -124,7 +126,7 @@ private RefreshToken decodeRefreshTokenV2(Buffer b) {
124126 TokenVersion .V2 , createdAt , validTill ,
125127 new OperatorIdentity (0 , OperatorType .Service , 0 , 0 ),
126128 new PublisherIdentity (siteId , 0 , 0 ),
127- new UserIdentity (IdentityScope .UID2 , IdentityType .Email , identity , privacyBits , Instant .ofEpochMilli (establishedMillis ), null ));
129+ new UserIdentity (IdentityScope .UID2 , IdentityType .Email , this . identityEnvironment , identity , privacyBits , Instant .ofEpochMilli (establishedMillis ), null ));
128130 }
129131
130132 private RefreshToken decodeRefreshTokenV3 (Buffer b , byte [] bytes ) {
@@ -157,7 +159,7 @@ private RefreshToken decodeRefreshTokenV3(Buffer b, byte[] bytes) {
157159
158160 return new RefreshToken (
159161 TokenVersion .V3 , createdAt , expiresAt , operatorIdentity , publisherIdentity ,
160- new UserIdentity (identityScope , identityType , id , privacyBits , establishedAt , null ));
162+ new UserIdentity (identityScope , identityType , this . identityEnvironment , id , privacyBits , establishedAt , null ));
161163 }
162164
163165 @ Override
@@ -225,13 +227,12 @@ public AdvertisingToken decodeAdvertisingTokenV2(Buffer b) {
225227 Instant .ofEpochMilli (expiresMillis ),
226228 new OperatorIdentity (0 , OperatorType .Service , 0 , masterKeyId ),
227229 new PublisherIdentity (siteId , siteKeyId , 0 ),
228- new UserIdentity (IdentityScope .UID2 , IdentityType .Email , advertisingId , privacyBits , Instant .ofEpochMilli (establishedMillis ), null )
230+ new UserIdentity (IdentityScope .UID2 , IdentityType .Email , this . identityEnvironment , advertisingId , privacyBits , Instant .ofEpochMilli (establishedMillis ), null )
229231 );
230232
231233 } catch (Exception e ) {
232234 throw new RuntimeException ("Couldn't decode advertisingTokenV2" , e );
233235 }
234-
235236 }
236237
237238 public AdvertisingToken decodeAdvertisingTokenV3orV4 (Buffer b , byte [] bytes , TokenVersion tokenVersion ) {
@@ -253,8 +254,7 @@ public AdvertisingToken decodeAdvertisingTokenV3orV4(Buffer b, byte[] bytes, Tok
253254 final IdentityScope identityScope = id .length == 32 ? IdentityScope .UID2 : decodeIdentityScopeV3 (id [0 ]);
254255 final IdentityType identityType = id .length == 32 ? IdentityType .Email : decodeIdentityTypeV3 (id [0 ]);
255256
256- if (id .length > 32 )
257- {
257+ if (id .length > 32 ) {
258258 if (identityScope != decodeIdentityScopeV3 (b .getByte (0 ))) {
259259 throw new ClientInputValidationException ("Failed decoding advertisingTokenV3: Identity scope mismatch" );
260260 }
@@ -265,7 +265,7 @@ public AdvertisingToken decodeAdvertisingTokenV3orV4(Buffer b, byte[] bytes, Tok
265265
266266 return new AdvertisingToken (
267267 tokenVersion , createdAt , expiresAt , operatorIdentity , publisherIdentity ,
268- new UserIdentity (identityScope , identityType , id , privacyBits , establishedAt , refreshedAt )
268+ new UserIdentity (identityScope , identityType , this . identityEnvironment , id , privacyBits , establishedAt , refreshedAt )
269269 );
270270 }
271271
@@ -338,7 +338,6 @@ public static String bytesToBase64Token(byte[] advertisingTokenBytes, TokenVersi
338338
339339 @ Override
340340 public IdentityTokens encode (AdvertisingToken advertisingToken , RefreshToken refreshToken , Instant refreshFrom , Instant asOf ) {
341-
342341 final byte [] advertisingTokenBytes = encode (advertisingToken , asOf );
343342 final String base64AdvertisingToken = bytesToBase64Token (advertisingTokenBytes , advertisingToken .version );
344343
@@ -367,16 +366,16 @@ private byte[] encryptIdentityV2(PublisherIdentity publisherIdentity, UserIdenti
367366 }
368367 }
369368
370- static private byte encodeIdentityTypeV3 (UserIdentity userIdentity ) {
369+ private static byte encodeIdentityTypeV3 (UserIdentity userIdentity ) {
371370 return (byte ) (TokenUtils .encodeIdentityScope (userIdentity .identityScope ) | (userIdentity .identityType .value << 2 ) | 3 );
372371 // "| 3" is used so that the 2nd char matches the version when V3 or higher. Eg "3" for V3 and "4" for V4
373372 }
374373
375- static private IdentityScope decodeIdentityScopeV3 (byte value ) {
374+ private static IdentityScope decodeIdentityScopeV3 (byte value ) {
376375 return IdentityScope .fromValue ((value & 0x10 ) >> 4 );
377376 }
378377
379- static private IdentityType decodeIdentityTypeV3 (byte value ) {
378+ private static IdentityType decodeIdentityTypeV3 (byte value ) {
380379 return IdentityType .fromValue ((value & 0xf ) >> 2 );
381380 }
382381
@@ -392,7 +391,7 @@ static PublisherIdentity decodePublisherIdentityV3(Buffer b, int offset) {
392391
393392 static void encodeOperatorIdentityV3 (Buffer b , OperatorIdentity operatorIdentity ) {
394393 b .appendInt (operatorIdentity .siteId );
395- b .appendByte ((byte )operatorIdentity .operatorType .value );
394+ b .appendByte ((byte ) operatorIdentity .operatorType .value );
396395 b .appendInt (operatorIdentity .operatorVersion );
397396 b .appendInt (operatorIdentity .operatorKeyId );
398397 }
0 commit comments