Skip to content

Commit 3c3cf97

Browse files
committed
Address review comments
1 parent 8896006 commit 3c3cf97

File tree

8 files changed

+40
-37
lines changed

8 files changed

+40
-37
lines changed

examples/sample_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _usage():
3030
client.refresh_keys()
3131
decrypt_result = client.decrypt(ad_token)
3232

33-
print('UID =', decrypt_result.uid)
33+
print('UID2 =', decrypt_result.uid2)
3434
print('Established =', decrypt_result.established)
3535
print('Site ID =', decrypt_result.site_id)
3636
print('Site Key Site ID =', decrypt_result.site_key_site_id)

examples/sample_sharing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _usage():
2727
# client = EuidClientFactory.create(base_url, auth_key, secret_key)
2828
# for UID2 use:
2929
client = Uid2ClientFactory.create(base_url, auth_key, secret_key)
30-
client.refresh()
30+
client.refresh_keys()
3131
new_ad_token = client.encrypt(raw_uid)
3232

3333
print('New Ad Token =', new_ad_token)

tests/test_encryption.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,24 @@ def _generate_v4_token(self, expires_in_seconds):
184184
def test_decrypt_token_2_invalid_lifetime_exception(self):
185185
test_cases = [
186186
# expires 30s AFTER max
187-
[self._generate_v2_token(dt.timedelta(seconds=60)), 30, 3600, ClientType.Bidstream],
188-
[self._generate_v4_token(dt.timedelta(seconds=60)), 30, 3600, ClientType.Bidstream],
189-
[self._generate_v2_token(dt.timedelta(seconds=60)), 3600, 30, ClientType.Sharing],
190-
[self._generate_v4_token(dt.timedelta(seconds=60)), 3600, 30, ClientType.Sharing],
187+
[self._generate_v2_token(dt.timedelta(seconds=60)), 30, 3600, ClientType.BIDSTREAM],
188+
[self._generate_v4_token(dt.timedelta(seconds=60)), 30, 3600, ClientType.BIDSTREAM],
189+
[self._generate_v2_token(dt.timedelta(seconds=60)), 3600, 30, ClientType.SHARING],
190+
[self._generate_v4_token(dt.timedelta(seconds=60)), 3600, 30, ClientType.SHARING],
191191
# expires 1s AFTER max
192-
[self._generate_v2_token(dt.timedelta(seconds=60)), 59, 3600, ClientType.Bidstream],
193-
[self._generate_v4_token(dt.timedelta(seconds=60)), 59, 3600, ClientType.Bidstream],
194-
[self._generate_v2_token(dt.timedelta(seconds=60)), 3600, 59, ClientType.Sharing],
195-
[self._generate_v4_token(dt.timedelta(seconds=60)), 3600, 59, ClientType.Sharing],
192+
[self._generate_v2_token(dt.timedelta(seconds=60)), 59, 3600, ClientType.BIDSTREAM],
193+
[self._generate_v4_token(dt.timedelta(seconds=60)), 59, 3600, ClientType.BIDSTREAM],
194+
[self._generate_v2_token(dt.timedelta(seconds=60)), 3600, 59, ClientType.SHARING],
195+
[self._generate_v4_token(dt.timedelta(seconds=60)), 3600, 59, ClientType.SHARING],
196196
# expires 1 day AFTER max
197197
[self._generate_v2_token(dt.timedelta(days=3)), dt.timedelta(days=2).seconds, dt.timedelta(days=4).seconds,
198-
ClientType.Bidstream],
198+
ClientType.BIDSTREAM],
199199
[self._generate_v4_token(dt.timedelta(days=3)), dt.timedelta(days=2).seconds,
200-
dt.timedelta(days=4).seconds, ClientType.Bidstream],
200+
dt.timedelta(days=4).seconds, ClientType.BIDSTREAM],
201201
[self._generate_v2_token(dt.timedelta(days=3)), dt.timedelta(days=4).seconds, dt.timedelta(days=2).seconds,
202-
ClientType.Sharing],
202+
ClientType.SHARING],
203203
[self._generate_v4_token(dt.timedelta(days=3)), dt.timedelta(days=4).seconds,
204-
dt.timedelta(days=2).seconds, ClientType.Sharing]
204+
dt.timedelta(days=2).seconds, ClientType.SHARING]
205205
]
206206
for token, max_bidstream_lifetime_seconds, max_sharing_lifetime_seconds, client_type in test_cases:
207207
with self.subTest(token=token,
@@ -218,15 +218,15 @@ def test_decrypt_token_invalid_lifetime_pass(self):
218218
seconds_since_established = 3600 # from UID2TokenGenerator.generate_uid2_token_v4
219219
test_cases = [
220220
# expires 30s before max
221-
[self._generate_v2_token(dt.timedelta(seconds=30)), seconds_since_established + 60, 60, ClientType.Bidstream],
222-
[self._generate_v4_token(dt.timedelta(seconds=30)), seconds_since_established + 60, 60, ClientType.Bidstream],
223-
[self._generate_v2_token(dt.timedelta(seconds=30)), 30, seconds_since_established + 30, ClientType.Sharing],
224-
[self._generate_v4_token(dt.timedelta(seconds=30)), 30, seconds_since_established + 30, ClientType.Sharing],
221+
[self._generate_v2_token(dt.timedelta(seconds=30)), seconds_since_established + 60, 60, ClientType.BIDSTREAM],
222+
[self._generate_v4_token(dt.timedelta(seconds=30)), seconds_since_established + 60, 60, ClientType.BIDSTREAM],
223+
[self._generate_v2_token(dt.timedelta(seconds=30)), 30, seconds_since_established + 30, ClientType.SHARING],
224+
[self._generate_v4_token(dt.timedelta(seconds=30)), 30, seconds_since_established + 30, ClientType.SHARING],
225225
# expires exactly at max
226-
[self._generate_v2_token(dt.timedelta(seconds=30)), seconds_since_established + 30, 30, ClientType.Bidstream],
227-
[self._generate_v4_token(dt.timedelta(seconds=30)), seconds_since_established + 30, 30, ClientType.Bidstream],
228-
[self._generate_v2_token(dt.timedelta(seconds=30)), 60, seconds_since_established + 60, ClientType.Sharing],
229-
[self._generate_v4_token(dt.timedelta(seconds=30)), 60, seconds_since_established + 60, ClientType.Sharing]
226+
[self._generate_v2_token(dt.timedelta(seconds=30)), seconds_since_established + 30, 30, ClientType.BIDSTREAM],
227+
[self._generate_v4_token(dt.timedelta(seconds=30)), seconds_since_established + 30, 30, ClientType.BIDSTREAM],
228+
[self._generate_v2_token(dt.timedelta(seconds=30)), 60, seconds_since_established + 60, ClientType.SHARING],
229+
[self._generate_v4_token(dt.timedelta(seconds=30)), 60, seconds_since_established + 60, ClientType.SHARING]
230230
]
231231
for token, max_bidstream_lifetime_seconds, max_sharing_lifetime_seconds, client_type in test_cases:
232232
with self.subTest(token=token,

uid2_client/bidstream_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BidstreamClient:
1818
1919
Methods:
2020
refresh_keys: Refresh encryption keys from UID2 servers
21-
decrypt_ad_token_into_raw_uid: decrypt an advertising token
21+
decrypt_token_into_raw_uid: decrypt an advertising token
2222
"""
2323

2424
def __init__(self, base_url, auth_key, secret_key):
@@ -38,7 +38,7 @@ def __init__(self, base_url, auth_key, secret_key):
3838
self._secret_key = base64.b64decode(secret_key)
3939

4040
def _decrypt_token_into_raw_uid(self, token, domain_name, now=None):
41-
return decrypt_token(token, self._keys, domain_name, ClientType.Bidstream, now)
41+
return decrypt_token(token, self._keys, domain_name, ClientType.BIDSTREAM, now)
4242

4343
def decrypt_token_into_raw_uid(self, token, domain_name):
4444
"""Decrypt advertising token to extract UID2 details.
@@ -61,7 +61,7 @@ def refresh(self):
6161
6262
This will synchronously connect to the corresponding UID2 service and fetch the latest
6363
set of encryption keys which can then be used to decrypt advertising tokens using
64-
the decrypt_token function.
64+
the decrypt_token_into_raw_uid() function.
6565
6666
Returns:
6767
EncryptionKeysCollection containing the keys

uid2_client/client_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33

44
class ClientType(Enum):
5-
Sharing = 1,
6-
Bidstream = 2,
7-
LegacyWithoutDomainCheck = 3
5+
SHARING = 1,
6+
BIDSTREAM = 2,
7+
LEGACY_WITHOUT_DOMAIN_CHECK = 3

uid2_client/encryption.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class _PayloadType(Enum):
3838
base64_url_special_chars = {"-", "_"}
3939

4040

41-
# DEPRECATED, DO NOT CALL DIRECTLY. For DSPs PLEASE USE BidStreamClient's decrypt_ad_token_into_raw_uid()
42-
# for Sharers USE SharingClient's decrypt_sharing_token_into_raw_uid()
41+
# DEPRECATED, DO NOT CALL DIRECTLY. For DSPs PLEASE USE BidstreamClient's decrypt_token_into_raw_uid()
42+
# for Sharers USE SharingClient's decrypt_token_into_raw_uid()
4343
def decrypt(token, keys, now=None):
4444
"""Decrypt advertising token to extract UID2 details.
4545
@@ -57,7 +57,7 @@ def decrypt(token, keys, now=None):
5757
"""
5858

5959
try:
60-
decrypted_token = _decrypt_token(token, keys, None, ClientType.LegacyWithoutDomainCheck, now)
60+
decrypted_token = _decrypt_token(token, keys, None, ClientType.LEGACY_WITHOUT_DOMAIN_CHECK, now)
6161
if decrypted_token.status != DecryptionStatus.SUCCESS:
6262
raise EncryptionError(str(decrypted_token.status))
6363
else:
@@ -88,7 +88,7 @@ def decrypt_token(token, keys, domain_name, client_type, now=None):
8888

8989
try:
9090
decrypted_token = _decrypt_token(token, keys, domain_name, client_type, now)
91-
if client_type == ClientType.LegacyWithoutDomainCheck and decrypted_token.status != DecryptionStatus.SUCCESS:
91+
if client_type == ClientType.LEGACY_WITHOUT_DOMAIN_CHECK and decrypted_token.status != DecryptionStatus.SUCCESS:
9292
raise EncryptionError(str(decrypted_token.status))
9393
else:
9494
return decrypted_token
@@ -125,9 +125,9 @@ def _decrypt_token(token, keys, domain_name, client_type, now):
125125

126126

127127
def _token_has_valid_lifetime(keys, client_type, established, expires, now):
128-
if client_type is ClientType.Bidstream:
128+
if client_type is ClientType.BIDSTREAM:
129129
max_life_time_seconds = keys.get_max_bidstream_lifetime_seconds()
130-
elif client_type is ClientType.Sharing:
130+
elif client_type is ClientType.SHARING:
131131
max_life_time_seconds = keys.get_max_sharing_lifetime_seconds()
132132
else:
133133
return True # Skip check for legacy clients
@@ -567,6 +567,10 @@ def __init__(self, status, uid, established, site_id, site_key_site_id, identity
567567
def success(self):
568568
return self.status == DecryptionStatus.SUCCESS
569569

570+
@property
571+
def uid2(self): # for backward compatibility
572+
return self.uid
573+
570574
@staticmethod
571575
def make_error(decryption_status):
572576
return DecryptedToken(decryption_status, None, None, None, None, None, None, None, None, None)

uid2_client/encryption_data_response.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def make_error(encryption_status):
1818
@property
1919
def encrypted_data(self):
2020
return self._encrypted_data
21-
#uid = encrypted_data
2221

2322
@property
2423
def status(self):
25-
return self._encryption_status
24+
return self._encryption_status

uid2_client/sharing_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _encrypt_raw_uid_into_token(self, uid2, keyset_id=None, now=None):
4242
return encrypt(uid2, None, self._keys, keyset_id, now=now)
4343

4444
def _decrypt_token_into_raw_uid(self, token, now=None):
45-
return decrypt_token(token, self._keys, None, ClientType.Sharing, now)
45+
return decrypt_token(token, self._keys, None, ClientType.SHARING, now)
4646

4747
def encrypt_raw_uid_into_token(self, uid2, keyset_id=None):
4848
""" Encrypt a UID2 into a sharing token

0 commit comments

Comments
 (0)