Skip to content

Commit 0899c54

Browse files
committed
Add optional parameter for encrypt/decrypt to specify timestamp
1 parent a871524 commit 0899c54

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

uid2_client/bid_stream_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
>>> from uid2_client import BidStreamClient
33
"""
44
import base64
5+
import datetime as dt
56

6-
from .encryption import decrypt_token
77
from .client_type import ClientType
8+
from .encryption import decrypt_token
89
from .refresh_keys_util import refresh_bidstream_keys
910

1011

@@ -35,12 +36,13 @@ def __init__(self, base_url, auth_key, secret_key):
3536
self._auth_key = auth_key
3637
self._secret_key = base64.b64decode(secret_key)
3738

38-
def decrypt_ad_token_into_raw_uid(self, token, domain_name):
39+
def decrypt_ad_token_into_raw_uid(self, token, domain_name, now=dt.datetime.now(tz=dt.timezone.utc)):
3940
"""Decrypt advertising token to extract UID2 details.
4041
4142
Args:
4243
token (str): advertising token to decrypt
4344
domain_name (str) : domain name from bid request
45+
now (datetime): date/time to use as "now" when doing token expiration check
4446
4547
Returns:
4648
DecryptedToken: details extracted from the advertising token
@@ -49,7 +51,7 @@ def decrypt_ad_token_into_raw_uid(self, token, domain_name):
4951
EncryptionError: if token version is not supported, the token has expired,
5052
or no required decryption keys present in the keys collection
5153
"""
52-
return decrypt_token(token, self._keys, domain_name, ClientType.Bidstream)
54+
return decrypt_token(token, self._keys, domain_name, ClientType.Bidstream, now)
5355

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

uid2_client/encryption.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def decrypt_token(token, keys, domain_name, client_type, now=dt.datetime.now(tz=
8888

8989

9090
def _decrypt_token(token, keys, domain_name, client_type, now):
91+
if keys is None:
92+
raise EncryptionError('keys not initialized')
9193
if not keys.valid(now):
9294
raise EncryptionError('no keys available or all keys have expired; refresh the latest keys from UID2 service')
9395

uid2_client/sharing_client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
>>> from uid2_client import SharingClient
33
"""
44
import base64
5+
import datetime as dt
56

6-
from .encryption import encrypt, decrypt_token
77
from .client_type import ClientType
8+
from .encryption import encrypt, decrypt_token
89
from .refresh_keys_util import refresh_sharing_keys
910

1011

@@ -36,23 +37,25 @@ def __init__(self, base_url, auth_key, secret_key):
3637
self._auth_key = auth_key
3738
self._secret_key = base64.b64decode(secret_key)
3839

39-
def encrypt_raw_uid_into_sharing_token(self, uid2, keyset_id=None):
40+
def encrypt_raw_uid_into_sharing_token(self, uid2, keyset_id=None, now=dt.datetime.now(tz=dt.timezone.utc)):
4041
""" Encrypt a UID2 into a sharing token
4142
4243
Args:
4344
uid2: the UID2 or EUID to be encrypted
4445
keys (EncryptionKeysCollection): collection of keys to choose from for encryption
4546
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
4648
4749
Returns (str): Sharing Token
4850
"""
49-
return encrypt(uid2, None, self._keys, keyset_id)
51+
return encrypt(uid2, None, self._keys, keyset_id, now=now)
5052

51-
def decrypt_sharing_token_into_raw_uid(self, token):
53+
def decrypt_sharing_token_into_raw_uid(self, token, now=dt.datetime.now(tz=dt.timezone.utc)):
5254
"""Decrypt sharing token to extract UID2 details.
5355
5456
Args:
5557
token (str): sharing token to decrypt
58+
now (datetime): date/time to use as "now" when doing token expiration check
5659
5760
Returns:
5861
DecryptedToken: details extracted from the sharing token
@@ -61,7 +64,7 @@ def decrypt_sharing_token_into_raw_uid(self, token):
6164
EncryptionError: if token version is not supported, the token has expired,
6265
or no required decryption keys present in the keys collection
6366
"""
64-
return decrypt_token(token, self._keys, None, ClientType.Sharing)
67+
return decrypt_token(token, self._keys, None, ClientType.Sharing, now)
6568

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

0 commit comments

Comments
 (0)