Skip to content

[UID2-3242] Handle lifetime check for v2 tokens differently #36

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

Merged
merged 6 commits into from
Apr 29, 2024

Conversation

asloobq
Copy link
Contributor

@asloobq asloobq commented Apr 25, 2024

No description provided.

@asloobq asloobq marked this pull request as ready for review April 25, 2024 21:18
@@ -165,7 +167,7 @@ def _decrypt_token_v2(token_bytes, keys, domain_name, client_type, now):
established_ms = int.from_bytes(identity[idx:idx + 8], 'big')
established = dt.datetime.fromtimestamp(established_ms / 1000.0, tz=timezone.utc)

if not _token_has_valid_lifetime(keys, client_type, established, expires, now):
if not _token_has_valid_lifetime(keys, client_type, now, expires, now):
return DecryptedToken(DecryptionStatus.INVALID_TOKEN_LIFETIME, id_str, established, site_id, site_key.site_id,
Copy link
Contributor

@caroline-ttd caroline-ttd Apr 26, 2024

Choose a reason for hiding this comment

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

Probably it would be good to change the established in the error to now

Copy link
Contributor

Choose a reason for hiding this comment

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

it's correct currently because DecryptedToken has an established field. Recall that DecryptedToken is not just an error, but also shows the fields that were successfully extracted from the token.

site_payload += int.to_bytes(generated_at_timestamp, length=8, byteorder='big') # last refreshed/generated
site_payload += base64.b64decode(id_str)

master_payload = int.to_bytes(int(params.token_expiry.timestamp()) * 1000, length=8, byteorder='big') # expiry
master_payload += int.to_bytes(generated_at_timestamp, length=8, byteorder='big') # created
master_payload += int.to_bytes(generated_at_timestamp, length=8, byteorder='big') # generated
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick, do you want to write the same comment as C# SDK here, //identity refreshed, seems to be identical to TokenGenerated in Operator to provide more context.

Copy link
Contributor Author

@asloobq asloobq Apr 26, 2024

Choose a reason for hiding this comment

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

I think its understood from the # generated comment that its identical to last refreshed/generated in site payload :)

@@ -98,13 +99,15 @@ def generate_uid2_token_v4(id_str, master_key, site_id, site_key, params=default

@staticmethod
def generate_uid_token(id_str, master_key, site_id, site_key, identity_scope, token_version,
created_at=None, expires_at=None):
identity_established=None, token_generated=None, token_expiry=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why the default value is not datetime.now() but None

Copy link
Contributor

Choose a reason for hiding this comment

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


if not _token_has_valid_lifetime(keys, client_type, established, expires, now):
if not _token_has_valid_lifetime(keys, client_type, generated, expires, now):
return DecryptedToken(DecryptionStatus.INVALID_TOKEN_LIFETIME, None, established, site_id, site_key.site_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably it would be good to change the established in the error to generated

Copy link
Contributor

Choose a reason for hiding this comment

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

as above

identity_scope=IdentityScope.UID2.value, token_generated_at=dt.datetime.now(tz=timezone.utc)):
self.identity_scope = identity_scope
identity_scope=IdentityScope.UID2.value, token_generated=dt.datetime.now(tz=timezone.utc),
identity_established=dt.datetime.now(tz=timezone.utc)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't expiry, token_generated, and identity_established have a default value of None, and we call datetime.now in the function instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

agree, I assume the reason no test has failed is because we're not relying on these defaults other than in test code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch. Fixed it

@@ -66,15 +68,14 @@ def default_params():
class UID2TokenGenerator:

@staticmethod
def generate_uid2_token_v2(id_str, master_key, site_id, site_key, params = default_params(), version=2):
def generate_uid2_token_v2(id_str, master_key, site_id, site_key, params=default_params(), version=2):
Copy link
Contributor

Choose a reason for hiding this comment

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

default_params() will only be evaluated once, is that going to cause any problems?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got rid of this method

@@ -98,13 +99,15 @@ def generate_uid2_token_v4(id_str, master_key, site_id, site_key, params=default

@staticmethod
def generate_uid_token(id_str, master_key, site_id, site_key, identity_scope, token_version,
created_at=None, expires_at=None):
identity_established=None, token_generated=None, token_expiry=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -165,7 +167,7 @@ def _decrypt_token_v2(token_bytes, keys, domain_name, client_type, now):
established_ms = int.from_bytes(identity[idx:idx + 8], 'big')
established = dt.datetime.fromtimestamp(established_ms / 1000.0, tz=timezone.utc)

if not _token_has_valid_lifetime(keys, client_type, established, expires, now):
if not _token_has_valid_lifetime(keys, client_type, now, expires, now):
return DecryptedToken(DecryptionStatus.INVALID_TOKEN_LIFETIME, id_str, established, site_id, site_key.site_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

it's correct currently because DecryptedToken has an established field. Recall that DecryptedToken is not just an error, but also shows the fields that were successfully extracted from the token.


if not _token_has_valid_lifetime(keys, client_type, established, expires, now):
if not _token_has_valid_lifetime(keys, client_type, generated, expires, now):
return DecryptedToken(DecryptionStatus.INVALID_TOKEN_LIFETIME, None, established, site_id, site_key.site_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

as above

identity_scope=IdentityScope.UID2.value, token_generated_at=dt.datetime.now(tz=timezone.utc)):
self.identity_scope = identity_scope
identity_scope=IdentityScope.UID2.value, token_generated=dt.datetime.now(tz=timezone.utc),
identity_established=dt.datetime.now(tz=timezone.utc)):
Copy link
Contributor

Choose a reason for hiding this comment

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

agree, I assume the reason no test has failed is because we're not relying on these defaults other than in test code

expires_in_sec = IN_3_DAYS + dt.timedelta(minutes=1)
def test_token_lifetime_too_long_for_bidstream_but_remaining_lifetime_allowed(self): # TokenLifetimeTooLongForBidstreamButRemainingLifetimeAllowed
generated = YESTERDAY
expires_in_sec = generated + dt.timedelta(days=3) + dt.timedelta(minutes=1)
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 extract a variable for max bidstream lifetime from here, line 134, and line 33 so we can see the connection

self.decrypt_and_assert_success(token, expected_version, expected_scope)

def test_token_lifetime_too_long_for_sharing_but_remaining_lifetime_allowed(self): # TokenLifetimeTooLongForSharingButRemainingLifetimeAllowed
generated = YESTERDAY
expires_in_sec = generated + dt.timedelta(days=31)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, can we extract a variable for max sharing lifetime in this class

@asloobq asloobq merged commit 2d7002c into main Apr 29, 2024
@asloobq asloobq deleted the aaq-UID2-3242-dsp-check-token-lifetime-v2-handling branch April 29, 2024 21:11
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.

None yet

4 participants