1717from .encryption import _decrypt_gcm , _encrypt_gcm
1818from .keys import EncryptionKey , EncryptionKeysCollection
1919from .identity_scope import IdentityScope
20+ from .request_response_util import *
2021
2122
2223def _make_dt (timestamp ):
@@ -31,6 +32,9 @@ class Uid2Client:
3132
3233 Methods:
3334 refresh_keys: get the latest encryption keys for decrypting advertising tokens
35+ refresh_json: parse json to get encryption keys
36+ encrypt: encrypt a uid to a token
37+ decrypt: decrypt a token to a uid
3438
3539 Examples:
3640 Connect to the UID2 service and obtain the latest encryption keys:
@@ -47,6 +51,7 @@ def __init__(self, base_url, auth_key, secret_key, identity_scope):
4751 base_url (str): base URL for all requests to UID2 services (e.g. 'https://prod.uidapi.com')
4852 auth_key (str): authorization key for consuming the UID2 services
4953 secret_key (str): secret key for consuming the UID2 services
54+ identity_scope (IdentityScope): UID2 or EUID
5055
5156 Note:
5257 Your authorization key will determine which UID2 services you are allowed to use.
@@ -66,9 +71,9 @@ def refresh_keys(self):
6671 Returns:
6772 EncryptionKeysCollection containing the keys
6873 """
69- req , nonce = self ._make_v2_request ( dt .datetime .now (tz = timezone .utc ))
70- resp = self ._post ( '/v2/key/sharing' , headers = self ._auth_headers ( ), data = req )
71- resp_body = json .loads (self ._parse_v2_response ( resp .read (), nonce )).get ('body' )
74+ req , nonce = make_v2_request ( self ._secret_key , dt .datetime .now (tz = timezone .utc ))
75+ resp = post ( self ._base_url , '/v2/key/sharing' , headers = auth_headers ( self ._auth_key ), data = req )
76+ resp_body = json .loads (parse_v2_response ( self ._secret_key , resp .read (), nonce )).get ('body' )
7277 return self ._parse_keys_json (resp_body )
7378
7479 def refresh_json (self , json_str ):
@@ -124,33 +129,6 @@ def _parse_keys_json(self, resp_body):
124129 return EncryptionKeysCollection (keys , resp_body ["caller_site_id" ], resp_body ["master_keyset_id" ],
125130 resp_body .get ("default_keyset_id" , None ), resp_body ["token_expiry_seconds" ])
126131
127- def _make_url (self , path ):
128- return self ._base_url + path
129-
130- def _auth_headers (self ):
131- return {'Authorization' : 'Bearer ' + self ._auth_key ,
132- "X-UID2-Client-Version" : "uid2-client-python-" + pkg_resources .get_distribution ("uid2_client" ).version }
133-
134- def _make_v2_request (self , now ):
135- payload = int .to_bytes (int (now .timestamp () * 1000 ), 8 , 'big' )
136- nonce = os .urandom (8 )
137- payload += nonce
138-
139- envelope = int .to_bytes (1 , 1 , 'big' )
140- envelope += _encrypt_gcm (payload , None , self ._secret_key )
141-
142- return base64 .b64encode (envelope ), nonce
143-
144- def _parse_v2_response (self , encrypted , nonce ):
145- payload = _decrypt_gcm (base64 .b64decode (encrypted ), self ._secret_key )
146- if nonce != payload [8 :16 ]:
147- raise ValueError ("nonce mismatch" )
148- return payload [16 :]
149-
150- def _post (self , path , headers , data ):
151- req = request .Request (self ._make_url (path ), headers = headers , method = 'POST' , data = data )
152- return request .urlopen (req )
153-
154132
155133class Uid2ClientError (Exception ):
156134 """Raised for problems encountered while interacting with UID2 services."""
0 commit comments