Skip to content

Commit a69ab65

Browse files
PR clean up
1 parent 6a46911 commit a69ab65

File tree

6 files changed

+86
-75
lines changed

6 files changed

+86
-75
lines changed

examples/sample_auto_refresh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from uid2_client import EncryptionKeysAutoRefresher
66
from uid2_client import Uid2Client
7-
from uid2_client import decrypt_token
7+
from uid2_client import decrypt
88

99

1010
def _usage():
@@ -26,7 +26,7 @@ def _usage():
2626
refresh_result = refresher.current_result()
2727
if refresh_result.ready:
2828
print('Keys are ready, last refreshed (UTC):', refresh_result.last_success_time, flush=True)
29-
result = decrypt_token(ad_token, refresh_result.keys)
29+
result = decrypt(ad_token, refresh_result.keys)
3030
print('UID2 =', result.uid2, flush=True)
3131
else:
3232
print('Keys are not ready yet, last error:', refresh_result.last_error[1], flush=True)

examples/sample_client.py

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

33
from uid2_client import Uid2Client
4-
from uid2_client import decrypt_token
4+
from uid2_client import decrypt
55

66

77
def _usage():
@@ -19,7 +19,7 @@ def _usage():
1919

2020
client = Uid2Client(base_url, auth_key, secret_key)
2121
keys = client.refresh_keys()
22-
result = decrypt_token(ad_token, keys)
22+
result = decrypt(ad_token, keys)
2323

2424
print('UID2 =', result.uid2)
2525
print('Established =', result.established)

tests/sharing_test.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
_site_key = EncryptionKey(_site_key_id, _site_id, _now - dt.timedelta(days=-1), _now, _now + dt.timedelta(days=1),
2424
_site_secret, keyset_id=99999)
2525

26-
_client_secret = "ywsvDNINiZOVSsfkHpLpSJzXzhr6Jx9Z/4Q0+lsEUvM="
26+
_client_secret = "ioG3wKxAokmp+rERx6A4kM/13qhyolUXIu14WN16Spo="
2727
_example_uid = "ywsvDNINiZOVSsfkHpLpSJzXzhr6Jx9Z/4Q0+lsEUvM="
2828

2929

@@ -33,7 +33,7 @@ def setup_sharing_and_encrypt(self, id_scope=IdentityScope.UID2):
3333
json = self._key_set_to_json_for_sharing([_master_key, _site_key])
3434
keys = client.refresh_json(json)
3535

36-
ad_token = encrypt_key(_example_uid, id_scope, keys)
36+
ad_token = encrypt(_example_uid, id_scope, keys)
3737

3838
return ad_token, keys
3939

@@ -70,7 +70,7 @@ def format_key(self, key: EncryptionKey):
7070

7171
def test_can_encrypt_and_decrypt_for_sharing(self):
7272
ad_token, keys = self.setup_sharing_and_encrypt()
73-
results = decrypt_token(ad_token, keys)
73+
results = decrypt(ad_token, keys)
7474
self.assertEqual(_example_uid, results.uid2)
7575

7676
def test_can_decrypt_another_clients_encrypted_token(self):
@@ -80,7 +80,7 @@ def test_can_decrypt_another_clients_encrypted_token(self):
8080

8181
receiving_keys = receiving_client.refresh_json(keys_json)
8282

83-
result = decrypt_token(ad_token, receiving_keys)
83+
result = decrypt(ad_token, receiving_keys)
8484
self.assertEqual(_example_uid, result.uid2)
8585

8686
def test_sharing_token_is_v4(self):
@@ -97,7 +97,7 @@ def test_euid_client_produces_euid_token(self):
9797
self.assertEqual("E", ad_token[0])
9898

9999
def _get_token_identity_type(self, uid2, keys):
100-
token = encrypt_key(uid2, IdentityScope.UID2, keys)
100+
token = encrypt(uid2, IdentityScope.UID2, keys)
101101

102102
first_char = token[0]
103103
if ('A' == first_char or 'E' == first_char):
@@ -125,9 +125,9 @@ def test_multiple_keys_per_keyset(self):
125125
json_body = self._key_set_to_json_for_sharing([_master_key, master_key2, _site_key, site_key2])
126126
keys = client.refresh_json(json_body)
127127

128-
ad_token = encrypt_key(_example_uid, IdentityScope.UID2, keys)
128+
ad_token = encrypt(_example_uid, IdentityScope.UID2, keys)
129129

130-
result = decrypt_token(ad_token, keys)
130+
result = decrypt(ad_token, keys)
131131

132132
self.assertEqual(_example_uid, result.uid2)
133133

@@ -136,31 +136,38 @@ def test_cannot_encrypt_if_no_key_from_default_keyset(self):
136136
json_body = self._key_set_to_json_for_sharing([_master_key])
137137
keys = client.refresh_json(json_body)
138138

139-
self.assertRaises(EncryptionError, encrypt_key, _example_uid, IdentityScope.UID2, keys)
139+
self.assertRaises(EncryptionError, encrypt, _example_uid, IdentityScope.UID2, keys)
140+
141+
def test_cannot_encrypt_if_theres_to_default_keyset_header(self):
142+
client = Uid2Client("endpoint", "authkey", _client_secret)
143+
json_body = self._key_set_to_json_for_sharing_with_header("", _site_id, [_master_key, _site_key])
144+
keys = client.refresh_json(json_body)
145+
self.assertRaises(EncryptionError, encrypt, _example_uid, IdentityScope.UID2, keys)
146+
140147

141148
def test_expiry_in_token_matches_expiry_in_reponse(self):
142149
client = Uid2Client("endpoint", "authkey", _client_secret)
143150
json_body = self._key_set_to_json_for_sharing_with_header('"default_keyset_id": 99999, "token_expiry_seconds": 2,', 99999, [_master_key, _site_key])
144151
keys = client.refresh_json(json_body)
145152

146153
now = dt.datetime.now(tz=timezone.utc)
147-
ad_token = encrypt_key(_example_uid, IdentityScope.UID2, keys)
154+
ad_token = encrypt(_example_uid, IdentityScope.UID2, keys)
148155

149-
result = decrypt_token(ad_token, keys, now=now+dt.timedelta(seconds=1))
156+
result = decrypt(ad_token, keys, now=now + dt.timedelta(seconds=1))
150157
self.assertEqual(_example_uid, result.uid2)
151158

152-
self.assertRaises(EncryptionError, decrypt_token, ad_token, keys, now=now+dt.timedelta(seconds=3))
159+
self.assertRaises(EncryptionError, decrypt, ad_token, keys, now=now + dt.timedelta(seconds=3))
153160

154161
def test_encrypt_key_inactive(self):
155162
client = Uid2Client("endpoint", "authkey", _client_secret)
156163
key = EncryptionKey(245, _site_id, _now, _now + dt.timedelta(days=1), _now +dt.timedelta(days=2), _site_secret, keyset_id=99999)
157164
keys = client.refresh_json(self._key_set_to_json_for_sharing([_master_key, key]))
158-
self.assertRaises(EncryptionError, encrypt_key, _example_uid, IdentityScope.UID2, keys)
165+
self.assertRaises(EncryptionError, encrypt, _example_uid, IdentityScope.UID2, keys)
159166

160167
def test_encrypt_key_expired(self):
161168
client = Uid2Client("endpoint", "authkey", _client_secret)
162169
key = EncryptionKey(245, _site_id, _now, _now, _now - dt.timedelta(days=1), _site_secret, keyset_id=99999)
163170
keys = client.refresh_json(self._key_set_to_json_for_sharing([_master_key, key]))
164-
self.assertRaises(EncryptionError, encrypt_key, _example_uid, IdentityScope.UID2, keys)
171+
self.assertRaises(EncryptionError, encrypt, _example_uid, IdentityScope.UID2, keys)
165172

166173

0 commit comments

Comments
 (0)