-
Notifications
You must be signed in to change notification settings - Fork 3
[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
[UID2-3242] Handle lifetime check for v2 tokens differently #36
Conversation
@@ -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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above
uid2_client/uid2_token_generator.py
Outdated
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)): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. Fixed it
uid2_client/uid2_token_generator.py
Outdated
@@ -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): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as above
uid2_client/uid2_token_generator.py
Outdated
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)): |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
No description provided.