Skip to content

Commit d4eca84

Browse files
committed
Moved encrypt decrypt methods with timestamp to internal
1 parent 8812e6e commit d4eca84

4 files changed

Lines changed: 21 additions & 16 deletions

File tree

tests/test_bidstream_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ def test_token_expiry_custom_decryption_time(self, mock_refresh_bidstream_keys):
140140
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4,
141141
created_at=created_at, expires_at=expires_at)
142142
with self.assertRaises(EncryptionError) as context:
143-
self._client.decrypt_token_into_raw_uid(token, None, expires_at + dt.timedelta(seconds=1))
143+
self._client._decrypt_token_into_raw_uid(token, None, expires_at + dt.timedelta(seconds=1))
144144
self.assertEqual('token expired', str(context.exception))
145145

146-
result = self._client.decrypt_token_into_raw_uid(token, None, expires_at - dt.timedelta(seconds=1))
146+
result = self._client._decrypt_token_into_raw_uid(token, None, expires_at - dt.timedelta(seconds=1))
147147
self.assertIsNotNone(result)
148148
self.assertEqual(result.identity_scope, IdentityScope.UID2)
149149
self.assertEqual(result.advertising_token_version, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)

tests/test_sharing_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util
185185
mock_refresh_keys_util.return_value = create_default_key_collection([master_key, site_key])
186186
self._client.refresh_keys()
187187
token = self._client.encrypt_raw_uid_into_token(example_uid)
188-
self._client.decrypt_token_into_raw_uid(token, now + dt.timedelta(seconds=1))
188+
self._client._decrypt_token_into_raw_uid(token, now + dt.timedelta(seconds=1))
189189
with self.assertRaises(EncryptionError) as context:
190-
self._client.decrypt_token_into_raw_uid(token, now + dt.timedelta(seconds=3))
190+
self._client._decrypt_token_into_raw_uid(token, now + dt.timedelta(seconds=3))
191191
self.assertEqual('token expired', str(context.exception))
192192

193193
def test_encrypt_key_inactive(self, mock_refresh_keys_util): #EncryptKeyInactive

uid2_client/bid_stream_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ def __init__(self, base_url, auth_key, secret_key):
3636
self._auth_key = auth_key
3737
self._secret_key = base64.b64decode(secret_key)
3838

39-
def decrypt_token_into_raw_uid(self, token, domain_name, now=None):
39+
def _decrypt_token_into_raw_uid(self, token, domain_name, now=None):
40+
return decrypt_token(token, self._keys, domain_name, ClientType.Bidstream, now)
41+
42+
def decrypt_token_into_raw_uid(self, token, domain_name):
4043
"""Decrypt advertising token to extract UID2 details.
4144
4245
Args:
4346
token (str): advertising token to decrypt
4447
domain_name (str) : domain name from bid request
45-
now (datetime): date/time to use as "now" when doing token expiration check
4648
4749
Returns:
4850
DecryptedToken: details extracted from the advertising token
@@ -51,7 +53,7 @@ def decrypt_token_into_raw_uid(self, token, domain_name, now=None):
5153
EncryptionError: if token version is not supported, the token has expired,
5254
or no required decryption keys present in the keys collection
5355
"""
54-
return decrypt_token(token, self._keys, domain_name, ClientType.Bidstream, now)
56+
return self._decrypt_token_into_raw_uid(token, domain_name, dt.datetime.now(tz=dt.timezone.utc))
5557

5658
def refresh_keys(self):
5759
"""Get the latest encryption keys for advertising tokens.

uid2_client/sharing_client.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class SharingClient:
1717
1818
Methods:
1919
refresh_keys: Refresh encryption keys from UID2 servers
20-
encrypt_raw_uid_into_sharing_token: encrypt a raw UID2 into a sharing token
21-
decrypt_sharing_token_into_raw_uid: decrypt a sharing token
20+
encrypt_raw_uid_into_token: encrypt a raw UID2 into a sharing token
21+
decrypt_token_into_raw_uid: decrypt a sharing token
2222
"""
2323

2424
def __init__(self, base_url, auth_key, secret_key):
@@ -37,25 +37,28 @@ def __init__(self, base_url, auth_key, secret_key):
3737
self._auth_key = auth_key
3838
self._secret_key = base64.b64decode(secret_key)
3939

40-
def encrypt_raw_uid_into_token(self, uid2, keyset_id=None, now=None):
40+
def _encrypt_raw_uid_into_token(self, uid2, keyset_id=None, now=None):
41+
return encrypt(uid2, None, self._keys, keyset_id, now=now)
42+
43+
def _decrypt_token_into_raw_uid(self, token, now=None):
44+
return decrypt_token(token, self._keys, None, ClientType.Sharing, now)
45+
46+
def encrypt_raw_uid_into_token(self, uid2, keyset_id=None):
4147
""" Encrypt a UID2 into a sharing token
4248
4349
Args:
4450
uid2: the UID2 or EUID to be encrypted
45-
keys (EncryptionKeysCollection): collection of keys to choose from for encryption
4651
keyset_id (int) : An optional keyset id to use for the encryption. Will use default keyset if left blank
47-
now (Datetime): the datettime to use for now. Defaults to utc now
4852
4953
Returns (str): Sharing Token
5054
"""
51-
return encrypt(uid2, None, self._keys, keyset_id, now=now)
55+
return self._encrypt_raw_uid_into_token(uid2, keyset_id, dt.datetime.now(tz=dt.timezone.utc))
5256

53-
def decrypt_token_into_raw_uid(self, token, now=None):
57+
def decrypt_token_into_raw_uid(self, token):
5458
"""Decrypt sharing token to extract UID2 details.
5559
5660
Args:
5761
token (str): sharing token to decrypt
58-
now (datetime): date/time to use as "now" when doing token expiration check
5962
6063
Returns:
6164
DecryptedToken: details extracted from the sharing token
@@ -64,7 +67,7 @@ def decrypt_token_into_raw_uid(self, token, now=None):
6467
EncryptionError: if token version is not supported, the token has expired,
6568
or no required decryption keys present in the keys collection
6669
"""
67-
return decrypt_token(token, self._keys, None, ClientType.Sharing, now)
70+
return self._decrypt_token_into_raw_uid(token, dt.datetime.now(tz=dt.timezone.utc))
6871

6972
def refresh_keys(self):
7073
"""Get the latest encryption keys for sharing tokens.

0 commit comments

Comments
 (0)