Skip to content

Commit 8812e6e

Browse files
committed
changed signatures to match C# SDK and fixed time bug
1 parent cb9ddd6 commit 8812e6e

9 files changed

Lines changed: 62 additions & 62 deletions

examples/sample_bidstream_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from uid2_client import BidStreamClient
3+
from uid2_client import BidstreamClient
44

55

66
# this sample client decrypts an advertising token into a raw UID2
@@ -11,7 +11,7 @@ def _usage():
1111
sys.exit(1)
1212

1313

14-
if len(sys.argv) <= 4:
14+
if len(sys.argv) <= 6:
1515
_usage()
1616

1717
base_url = sys.argv[1]
@@ -20,9 +20,9 @@ def _usage():
2020
domain_name = sys.argv[4]
2121
ad_token = sys.argv[5]
2222

23-
client = BidStreamClient(base_url, auth_key, secret_key)
23+
client = BidstreamClient(base_url, auth_key, secret_key)
2424
client.refresh_keys()
25-
decrypt_result = client.decrypt_ad_token_into_raw_uid(ad_token, domain_name)
25+
decrypt_result = client.decrypt_token_into_raw_uid(ad_token, domain_name)
2626

2727
print('UID2 =', decrypt_result.uid2)
2828
print('Established =', decrypt_result.established)

examples/sample_sharing_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ def _usage():
2121

2222
client = SharingClient(base_url, auth_key, secret_key)
2323
client.refresh_keys()
24-
new_sharing_token = client.encrypt_raw_uid_into_sharing_token(raw_uid)
24+
new_sharing_token = client.encrypt_raw_uid_into_token(raw_uid)
2525

2626
print('New Sharing Token =', new_sharing_token)
2727

28-
decrypt_result = client.decrypt_sharing_token_into_raw_uid(new_sharing_token)
28+
decrypt_result = client.decrypt_token_into_raw_uid(new_sharing_token)
2929

3030
print('Decrypted UID2 =', decrypt_result.uid2)
3131
print('Established =', decrypt_result.established)

tests/test_bidstream_client.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import patch
33

44
from test_utils import *
5-
from uid2_client import BidStreamClient, EncryptionError, Uid2Base64UrlCoder
5+
from uid2_client import BidstreamClient, EncryptionError, Uid2Base64UrlCoder
66

77

88
@patch('uid2_client.bid_stream_client.refresh_bidstream_keys')
@@ -12,15 +12,15 @@ class TestBidStreamClient(unittest.TestCase):
1212

1313
def setUp(self):
1414
self._key_collection = create_key_collection(IdentityScope.UID2)
15-
self._client = BidStreamClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
15+
self._client = BidstreamClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
1616

1717
def test_smoke_test(self, mock_refresh_bidstream_keys): # SmokeTest
1818
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
1919
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
2020
token = generate_uid_token(expected_scope, expected_version)
2121
mock_refresh_bidstream_keys.return_value = create_key_collection(expected_scope)
2222
self._client.refresh_keys()
23-
decrypted = self._client.decrypt_ad_token_into_raw_uid(token, None)
23+
decrypted = self._client.decrypt_token_into_raw_uid(token, None)
2424
self.assertEqual(decrypted.identity_scope, expected_scope)
2525
self.assertEqual(decrypted.advertising_token_version, expected_version)
2626

@@ -33,7 +33,7 @@ def test_phone_uids(self, mock_refresh_bidstream_keys): # PhoneTest
3333
self._client.refresh_keys()
3434
token = generate_uid_token(expected_scope, expected_version, phone_uid)
3535
self.assertEqual(IdentityType.Phone, get_identity_type(token))
36-
result = self._client.decrypt_ad_token_into_raw_uid(token, None)
36+
result = self._client.decrypt_token_into_raw_uid(token, None)
3737
self.assertIsNotNone(result)
3838
self.assertEqual(result.uid2, phone_uid)
3939
self.assertEqual(result.identity_scope, expected_scope)
@@ -51,7 +51,7 @@ def test_token_lifetime_too_long_for_bidstream(self, mock_refresh_bidstream_keys
5151
max_bidstream_lifetime_seconds=max_bidstream_lifetime)
5252
self._client.refresh_keys()
5353
with self.assertRaises(EncryptionError) as context:
54-
self._client.decrypt_ad_token_into_raw_uid(token, None)
54+
self._client.decrypt_token_into_raw_uid(token, None)
5555
self.assertEqual('invalid token lifetime', str(context.exception))
5656

5757
def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureToSimulateClockSkew
@@ -64,7 +64,7 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
6464
99999, 86400)
6565
self._client.refresh_keys()
6666
with self.assertRaises(EncryptionError) as context:
67-
self._client.decrypt_ad_token_into_raw_uid(token, None)
67+
self._client.decrypt_token_into_raw_uid(token, None)
6868
self.assertEqual('invalid token lifetime', str(context.exception))
6969

7070
def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew
@@ -76,7 +76,7 @@ def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refr
7676
expected_scope, site_id, 1,
7777
99999, 86400)
7878
self._client.refresh_keys()
79-
result = self._client.decrypt_ad_token_into_raw_uid(token, None)
79+
result = self._client.decrypt_token_into_raw_uid(token, None)
8080
self.assertIsNotNone(result)
8181
self.assertEqual(result.identity_scope, expected_scope)
8282
self.assertEqual(result.advertising_token_version, expected_version)
@@ -86,7 +86,7 @@ def test_empty_keys(self, mock_refresh_bidstream_keys): # EmptyKeyContainer
8686
mock_refresh_bidstream_keys.return_value = None
8787
self._client.refresh_keys()
8888
with self.assertRaises(EncryptionError) as context:
89-
self._client.decrypt_ad_token_into_raw_uid(token, None)
89+
self._client.decrypt_token_into_raw_uid(token, None)
9090
self.assertEqual('keys not initialized', str(context.exception))
9191

9292
def test_master_key_expired(self, mock_refresh_bidstream_keys): #ExpiredKeyContainer
@@ -101,7 +101,7 @@ def get_post_refresh_keys_response_with_key_expired():
101101
self._client.refresh_keys()
102102

103103
with self.assertRaises(EncryptionError) as context:
104-
self._client.decrypt_ad_token_into_raw_uid(example_uid, None)
104+
self._client.decrypt_token_into_raw_uid(example_uid, None)
105105

106106
self.assertEqual('no keys available or all keys have expired; refresh the latest keys from UID2 service', str(context.exception))
107107

@@ -116,7 +116,7 @@ def get_post_refresh_keys_response_with_key_expired():
116116
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
117117

118118
with self.assertRaises(EncryptionError) as context:
119-
self._client.decrypt_ad_token_into_raw_uid(token, None)
119+
self._client.decrypt_token_into_raw_uid(token, None)
120120

121121
self.assertEqual('not authorized for master key', str(context.exception))
122122

@@ -128,7 +128,7 @@ def test_invalid_payload(self, mock_refresh_bidstream_keys): #InvalidPayload
128128
bad_token = base64.urlsafe_b64encode(payload[:0])
129129

130130
with self.assertRaises(EncryptionError) as context:
131-
self._client.decrypt_ad_token_into_raw_uid(bad_token, None)
131+
self._client.decrypt_token_into_raw_uid(bad_token, None)
132132
self.assertEqual('invalid payload', str(context.exception))
133133

134134
def test_token_expiry_custom_decryption_time(self, mock_refresh_bidstream_keys):
@@ -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_ad_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_ad_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: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_smoke_test(self, mock_refresh_sharing_keys): # SmokeTest
2020
token = generate_uid_token(expected_scope, expected_version)
2121
mock_refresh_sharing_keys.return_value = create_key_collection(expected_scope)
2222
self._client.refresh_keys()
23-
decrypted = self._client.decrypt_sharing_token_into_raw_uid(token)
23+
decrypted = self._client.decrypt_token_into_raw_uid(token)
2424
self.assertEqual(decrypted.identity_scope, expected_scope)
2525
self.assertEqual(decrypted.advertising_token_version, expected_version)
2626

@@ -35,7 +35,7 @@ def test_token_lifetime_too_long_for_sharing(self, mock_refresh_sharing_keys):
3535
99999, 86400, max_sharing_lifetime)
3636
self._client.refresh_keys()
3737
with self.assertRaises(EncryptionError) as context:
38-
self._client.decrypt_sharing_token_into_raw_uid(token)
38+
self._client.decrypt_token_into_raw_uid(token)
3939
self.assertEqual('invalid token lifetime', str(context.exception))
4040

4141
def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureToSimulateClockSkew
@@ -48,7 +48,7 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
4848
99999, 86400)
4949
self._client.refresh_keys()
5050
with self.assertRaises(EncryptionError) as context:
51-
self._client.decrypt_sharing_token_into_raw_uid(token)
51+
self._client.decrypt_token_into_raw_uid(token)
5252
self.assertEqual('invalid token lifetime', str(context.exception))
5353

5454
def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew
@@ -60,7 +60,7 @@ def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refr
6060
expected_scope, site_id, 1,
6161
99999, 86400)
6262
self._client.refresh_keys()
63-
result = self._client.decrypt_sharing_token_into_raw_uid(token)
63+
result = self._client.decrypt_token_into_raw_uid(token)
6464
self.assertIsNotNone(result)
6565
self.assertEqual(result.identity_scope, expected_scope)
6666
self.assertEqual(result.advertising_token_version, expected_version)
@@ -74,7 +74,7 @@ def test_phone_uids(self, mock_refresh_sharing_keys): # PhoneTest
7474
self._client.refresh_keys()
7575
token = generate_uid_token(expected_scope, expected_version, phone_uid)
7676
self.assertEqual(IdentityType.Phone, get_identity_type(token))
77-
result = self._client.decrypt_sharing_token_into_raw_uid(token)
77+
result = self._client.decrypt_token_into_raw_uid(token)
7878
self.assertIsNotNone(result)
7979
self.assertEqual(result.uid2, phone_uid)
8080
self.assertEqual(result.identity_scope, expected_scope)
@@ -86,25 +86,25 @@ def test_sharing_client_produces_uid2_token(self, mock_refresh_keys_util): #Cli
8686
mock_refresh_keys_util.return_value = self._key_collection
8787
self._client.refresh_keys()
8888

89-
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
89+
token = self._client.encrypt_raw_uid_into_token(example_uid)
9090
self.assertEqual("A", token[0])
9191

9292
def test_sharing_client_produces_euid_token(self, mock_refresh_keys_util): #ClientProducesTokenWithCorrectPrefix
9393
mock_refresh_keys_util.return_value = create_key_collection(IdentityScope.EUID)
9494
self._client.refresh_keys()
9595

96-
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
96+
token = self._client.encrypt_raw_uid_into_token(example_uid)
9797
self.assertEqual("E", token[0])
9898

9999
def test_encrypt_decrypt(self, mock_refresh_sharing_keys): #CanEncryptAndDecryptForSharing
100100
key_collection = self._key_collection
101101
mock_refresh_sharing_keys.return_value = key_collection
102102

103103
self._client.refresh_keys()
104-
sharing_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid, key_collection.get_default_keyset_id())
104+
sharing_token = self._client.encrypt_raw_uid_into_token(example_uid, key_collection.get_default_keyset_id())
105105
self.assertIsNotNone(sharing_token)
106106
self.assertIsInstance(sharing_token, str)
107-
result = self._client.decrypt_sharing_token_into_raw_uid(sharing_token)
107+
result = self._client.decrypt_token_into_raw_uid(sharing_token)
108108
self.assertEqual(example_uid, result.uid2)
109109
mock_refresh_sharing_keys.assert_called_once()
110110

@@ -113,35 +113,35 @@ def test_can_decrypt_another_clients_encrypted_token(self, mock_refresh_keys_uti
113113
sending_client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
114114
sending_client.refresh_keys()
115115

116-
token = sending_client.encrypt_raw_uid_into_sharing_token(example_uid)
116+
token = sending_client.encrypt_raw_uid_into_token(example_uid)
117117

118118
receiving_client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
119119
receiving_client.refresh_keys()
120120

121-
result = receiving_client.decrypt_sharing_token_into_raw_uid(token)
121+
result = receiving_client.decrypt_token_into_raw_uid(token)
122122
self.assertEqual(example_uid, result.uid2)
123123

124124
def test_sharing_token_is_v4(self, mock_refresh_keys_util):
125125
mock_refresh_keys_util.return_value = self._key_collection
126126
self._client.refresh_keys()
127127

128-
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
128+
token = self._client.encrypt_raw_uid_into_token(example_uid)
129129
contains_base_64_special_chars = "+" in token or "/" in token or "=" in token
130130
self.assertFalse(contains_base_64_special_chars)
131131

132132
def test_raw_uid_produces_correct_identity_type_in_token(self, mock_refresh_keys_util): #RawUidProducesCorrectIdentityTypeInToken
133133
mock_refresh_keys_util.return_value = self._key_collection
134134
self._client.refresh_keys()
135135

136-
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_sharing_token(
136+
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_token(
137137
"Q4bGug8t1xjsutKLCNjnb5fTlXSvIQukmahYDJeLBtk=")))
138-
self.assertEqual(IdentityType.Phone, get_identity_type(self._client.encrypt_raw_uid_into_sharing_token(
138+
self.assertEqual(IdentityType.Phone, get_identity_type(self._client.encrypt_raw_uid_into_token(
139139
"BEOGxroPLdcY7LrSiwjY52+X05V0ryELpJmoWAyXiwbZ")))
140-
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_sharing_token(
140+
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_token(
141141
"oKg0ZY9ieD/CGMEjAA0kcq+8aUbLMBG0MgCT3kWUnJs=")))
142-
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_sharing_token(
142+
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_token(
143143
"AKCoNGWPYng/whjBIwANJHKvvGlGyzARtDIAk95FlJyb")))
144-
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_sharing_token(
144+
self.assertEqual(IdentityType.Email, get_identity_type(self._client.encrypt_raw_uid_into_token(
145145
"EKCoNGWPYng/whjBIwANJHKvvGlGyzARtDIAk95FlJyb")))
146146

147147
def test_multiple_keys_per_keyset(self, mock_refresh_keys_util): # MultipleKeysPerKeyset
@@ -151,9 +151,9 @@ def get_post_refresh_keys_response_with_multiple_keys():
151151
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_multiple_keys()
152152
self._client.refresh_keys()
153153

154-
sharing_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
154+
sharing_token = self._client.encrypt_raw_uid_into_token(example_uid)
155155

156-
result = self._client.decrypt_sharing_token_into_raw_uid(sharing_token)
156+
result = self._client.decrypt_token_into_raw_uid(sharing_token)
157157
self.assertEqual(example_uid, result.uid2)
158158

159159
def test_cannot_encrypt_if_no_key_from_default_keyset(self, mock_refresh_keys_util): #CannotEncryptIfNoKeyFromTheDefaultKeyset
@@ -164,7 +164,7 @@ def get_post_refresh_keys_response_with_no_default_keyset_key():
164164
self._client.refresh_keys()
165165

166166
with self.assertRaises(EncryptionError) as context:
167-
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
167+
self._client.encrypt_raw_uid_into_token(example_uid)
168168
self.assertEqual('No Keyset Key Found', str(context.exception))
169169

170170
def test_cannot_encrypt_if_theres_no_default_keyset_header(self, mock_refresh_keys_util): #CannotEncryptIfTheresNoDefaultKeysetHeader
@@ -177,17 +177,17 @@ def get_post_refresh_keys_response_with_no_default_keyset_header():
177177
self._client.refresh_keys()
178178

179179
with self.assertRaises(EncryptionError) as context:
180-
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
180+
self._client.encrypt_raw_uid_into_token(example_uid)
181181
self.assertEqual('No Keyset Key Found', str(context.exception))
182182

183183
def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util): # ExpiryInTokenMatchesExpiryInResponse
184184

185185
mock_refresh_keys_util.return_value = create_default_key_collection([master_key, site_key])
186186
self._client.refresh_keys()
187-
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
188-
self._client.decrypt_sharing_token_into_raw_uid(token, now + dt.timedelta(seconds=1))
187+
token = self._client.encrypt_raw_uid_into_token(example_uid)
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_sharing_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
@@ -200,7 +200,7 @@ def get_post_refresh_keys_response_with_key_inactive():
200200
self._client.refresh_keys()
201201

202202
with self.assertRaises(EncryptionError) as context:
203-
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
203+
self._client.encrypt_raw_uid_into_token(example_uid)
204204
self.assertEqual('No Keyset Key Found', str(context.exception))
205205

206206
def test_encrypt_key_expired(self, mock_refresh_keys_util): #EncryptKeyExpired
@@ -213,7 +213,7 @@ def get_post_refresh_keys_response_with_key_expired():
213213
self._client.refresh_keys()
214214

215215
with self.assertRaises(EncryptionError) as context:
216-
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
216+
self._client.encrypt_raw_uid_into_token(example_uid)
217217
self.assertEqual('No Keyset Key Found', str(context.exception))
218218

219219
def test_refresh_keys(self, mock_refresh_sharing_keys):

0 commit comments

Comments
 (0)