Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename class/method/variable names to improve token generation code readability #1080

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

sunnywu
Copy link
Contributor

@sunnywu sunnywu commented Oct 14, 2024

…LevelHashIdentity and RawUidIdentity as UserIdentity is a very vague term and one needs to read function calls to really understand whether the UserIdentity class has hashed DII, first level hash or raw Uid.
2. Rename RefreshToken class to RefreshTokenInput
3. Rename createAdvertisingToken/createRefreshToken methods to createAdvertisingTokenInput/createRefreshTokenInput
…ii/FirstLevelHash/RawUidIdentity sub-classes
@sunnywu sunnywu changed the title Syw UI d2 4159 token gen code renaming Rename class/variable names to improve token generation code readability Oct 14, 2024
@sunnywu sunnywu changed the title Rename class/variable names to improve token generation code readability Rename class/method/variable names to improve token generation code readability Oct 14, 2024
Comment on lines 11 to 15
public IdentityScope identityScope;
public IdentityType identityType;
public int privacyBits;
public Instant establishedAt;
public Instant refreshedAt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make these final and set them in a protected ctor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 17 to 20
public IdentityScope GetIdentityScope() { return identityScope; }
public IdentityType GetIdentityType() { return identityType; }
public Instant GetEstablishedAt() { return establishedAt; };
public Instant GetIRefreshedAt() { return refreshedAt; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Should start with get
  • Why no getter for privacyBits?
  • Do we need getters if the fields are public? Or should we keep the getters and make the fields private?
  • Typo in GetIRefreshedAt

Comment on lines 245 to +248
this.identityV3Enabled
? TokenUtils.getAdvertisingIdV3(firstLevelHashIdentity.identityScope, firstLevelHashIdentity.identityType, firstLevelHashIdentity.id, rotatingSalt.getSalt())
: TokenUtils.getAdvertisingIdV2(firstLevelHashIdentity.id, rotatingSalt.getSalt()),
? TokenUtils.getRawUidV3(firstLevelHashIdentity.identityScope,
firstLevelHashIdentity.identityType, firstLevelHashIdentity.firstLevelHash, rotatingSalt.getSalt())
: TokenUtils.getRawUidV2(firstLevelHashIdentity.firstLevelHash, rotatingSalt.getSalt()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we introduce a variable here? This is hard to read.

public static final String ValidateIdentityForEmail = "validate@example.com";
public static final String ValidateIdentityForPhone = "+12345678901";
public static final byte[] ValidateIdentityForEmailHash = EncodingUtils.getSha256Bytes(IdentityConst.ValidateIdentityForEmail);
public static final byte[] ValidateIdentityForPhoneHash = EncodingUtils.getSha256Bytes(IdentityConst.ValidateIdentityForPhone);

// DIIs to use when you want to generate a optout response in token generation or identity map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add some similar comments for the above/below groups of variables too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


public IdentityScope GetIdentityScope() { return identityScope; }
public IdentityType GetIdentityType() { return identityType; }
public Instant GetEstablishedAt() { return establishedAt; };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra ; at the end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the get methods for now

public IdentityScope GetIdentityScope() { return identityScope; }
public IdentityType GetIdentityType() { return identityType; }
public Instant GetEstablishedAt() { return establishedAt; };
public Instant GetIRefreshedAt() { return refreshedAt; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra I after Get
i.e. this should be GetRefreshedAfterAt()


import java.time.Instant;

// Contains a hash computed from a raw email/phone number DII input or the hash is provided by the UID Participant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to split this into two lines as such:

// Contains a hashed DII
// The hash can either be computed from a raw email/phone number DII input or provided by the UID Participant directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

import java.time.Instant;

//base class for all other HshedDii/FirstLevelHash/RawUIDIdentity class and define the basic common fields
public class UserIdentity {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we're instantiating UserIdentity anymore, so I suggest making this an abstract class, i.e. public abstract class UserIdentity

As for the matches() method, you can also move matches() here as an abstract method and make other classes implement it, i.e. public abstract boolean matches(UserIdentity that);, or use generics on the class type so you can do public abstract boolean matches (T that); if you want to force the input parameter type to be the same as the derived class, but your call on this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have made UserIdentity abstract - regarding matches , let me see if i can refactor the codes of the various subclasses first


// Contains the computed raw UID and its bucket ID from identity/map request
public class RawUidResult {
public static RawUidResult OptoutIdentity = new RawUidResult(new byte[33], "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For public static, the naming convention is capitalized snake case, i.e. public static RawUidResult OPT_OUT_IDENTITY

On a separate note, not a really big deal because I think both make sense, but I noticed we sometimes refer to "optout" as "OptOut" and sometimes as "Optout" - do we want this standardized too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll keep the convention for now due to reason given above.

I'll note down the Optout/OptOut point - seems they are split 50/50 ish. Not too important in context of this ticket work tho

public static RefreshResponse Expired = new RefreshResponse(Status.Expired, IdentityTokens.LogoutToken);
public static RefreshResponse Deprecated = new RefreshResponse(Status.Deprecated, IdentityTokens.LogoutToken);
public static RefreshResponse NoActiveKey = new RefreshResponse(Status.NoActiveKey, IdentityTokens.LogoutToken);
public static RefreshResponse Invalid = new RefreshResponse(Status.Invalid, IdentityResponse.OptOutIdentityResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to the other public static comments, but it should be capitalized snake case here too

if (remoteApiHost == null) {
handler.handle(Future.failedFuture("remote api not set"));
return;
}

this.webClient.get(remoteApiPort, remoteApiHost, remoteApiPath).
addQueryParam("identity_hash", EncodingUtils.toBase64String(firstLevelHashIdentity.id))
addQueryParam("identity_hash", EncodingUtils.toBase64String(firstLevelHashIdentity.firstLevelHash))
.addQueryParam("advertising_id", EncodingUtils.toBase64String(advertisingId))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a comment here that advertising id is equivalent to raw UID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -214,18 +217,19 @@ public AdvertisingToken decodeAdvertisingTokenV2(Buffer b) {
final int siteId = b3.getInt(0);
final int length = b3.getInt(4);

final byte[] advertisingId = EncodingUtils.fromBase64(b3.slice(8, 8 + length).getBytes());
final byte[] getRawUid = EncodingUtils.fromBase64(b3.slice(8, 8 + length).getBytes());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just rawUid is fine here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed also renamed ``idtorawUid``` in decodeAdvertisingTokenV3orV4 method below

private byte[] encryptIdentityV2(SourcePublisher sourcePublisher, FirstLevelHashIdentity firstLevelHashIdentity, KeysetKey key) {
return encryptIdentityV2(sourcePublisher, firstLevelHashIdentity.firstLevelHash, firstLevelHashIdentity.privacyBits,
firstLevelHashIdentity.establishedAt, key);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

2. Renamed RawUidResult to RawUidResponse
3. rename some id variable to rawUid to make it clearer
4. more comments
…xed the UserIdentity/FirstLevelHashIdentity class uses to HashedDiiIdentity instead in IdentityMapBenchmark and TokenEndecBenchmark
   methods to encodeIntoAdvertisingToken or encodeIntoRefreshToken to
   make it clear what we are encoding into
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants