Skip to content

Commit cfbda5c

Browse files
committed
don't throw exception for encryption errors
1 parent c49be71 commit cfbda5c

File tree

7 files changed

+54
-46
lines changed

7 files changed

+54
-46
lines changed

tests/test_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_post_refresh_keys_response_with_no_default_keyset_key():
123123

124124
with self.assertRaises(EncryptionError) as context:
125125
client.encrypt(example_uid)
126-
self.assertEqual('No Keyset Key Found', str(context.exception))
126+
self.assertEqual("('No Keyset Key Found',)", str(context.exception))
127127

128128
def test_cannot_encrypt_if_theres_no_default_keyset_header(self, mock_refresh_keys_util):
129129
def get_post_refresh_keys_response_with_no_default_keyset_header():
@@ -137,7 +137,7 @@ def get_post_refresh_keys_response_with_no_default_keyset_header():
137137

138138
with self.assertRaises(EncryptionError) as context:
139139
client.encrypt(example_uid)
140-
self.assertEqual('No Keyset Key Found', str(context.exception))
140+
self.assertEqual("('No Keyset Key Found',)", str(context.exception))
141141

142142
def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util):
143143
def get_post_refresh_keys_response_with_token_expiry():
@@ -176,7 +176,7 @@ def get_post_refresh_keys_response_with_key_inactive():
176176

177177
with self.assertRaises(EncryptionError) as context:
178178
client.encrypt(example_uid)
179-
self.assertEqual('No Keyset Key Found', str(context.exception))
179+
self.assertEqual("('No Keyset Key Found',)", str(context.exception))
180180

181181
def test_encrypt_key_expired(self, mock_refresh_keys_util):
182182
def get_post_refresh_keys_response_with_key_expired():
@@ -190,4 +190,4 @@ def get_post_refresh_keys_response_with_key_expired():
190190

191191
with self.assertRaises(EncryptionError) as context:
192192
client.encrypt(example_uid)
193-
self.assertEqual('No Keyset Key Found', str(context.exception))
193+
self.assertEqual("('No Keyset Key Found',)", str(context.exception))

tests/test_sharing.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from unittest.mock import patch
23

34
from uid2_client import *
45
from test_utils import *
@@ -65,19 +66,21 @@ def test_multiple_keys_per_keyset(self):
6566

6667
self.assertEqual(example_uid, result.uid)
6768

68-
def test_cannot_encrypt_if_no_key_from_default_keyset(self):
69+
@patch('uid2_client.client.refresh_sharing_keys')
70+
def test_cannot_encrypt_if_no_key_from_default_keyset(self, mock_refresh_sharing_keys):
6971
client = Uid2Client("endpoint", "authkey", client_secret)
70-
json_body = key_set_to_json_for_sharing([master_key])
71-
keys = client.refresh_json(json_body)
72-
73-
self.assertRaises(EncryptionError, encrypt, example_uid, IdentityScope.UID2, keys)
72+
mock_refresh_sharing_keys.return_value = create_default_key_collection([master_key])
73+
client.refresh_keys()
74+
self.assertRaises(EncryptionError, client.encrypt, example_uid)
7475

75-
def test_cannot_encrypt_if_theres_no_default_keyset_header(self):
76+
@patch('uid2_client.client.refresh_sharing_keys')
77+
def test_cannot_encrypt_if_theres_no_default_keyset_header(self, mock_refresh_sharing_keys):
7678
client = Uid2Client("endpoint", "authkey", client_secret)
77-
json_body = key_set_to_json_for_sharing_with_header("", site_id, [master_key, site_key])
78-
keys = client.refresh_json(json_body)
79-
self.assertRaises(EncryptionError, encrypt, example_uid, IdentityScope.UID2, keys)
80-
79+
key_set = [master_key, site_key]
80+
mock_refresh_sharing_keys.return_value = EncryptionKeysCollection(key_set, IdentityScope.UID2, site_id, 1,
81+
"", 86400)
82+
client.refresh_keys()
83+
self.assertRaises(EncryptionError, client.encrypt, example_uid)
8184

8285
def test_expiry_in_token_matches_expiry_in_reponse(self):
8386
client = Uid2Client("endpoint", "authkey", client_secret)
@@ -92,14 +95,18 @@ def test_expiry_in_token_matches_expiry_in_reponse(self):
9295

9396
self.assertRaises(EncryptionError, decrypt, encrypted_data_response.encrypted_data, keys, now=now + dt.timedelta(seconds=3))
9497

95-
def test_encrypt_key_inactive(self):
98+
@patch('uid2_client.client.refresh_sharing_keys')
99+
def test_encrypt_key_inactive(self, mock_refresh_sharing_keys):
96100
client = Uid2Client("endpoint", "authkey", client_secret)
97101
key = EncryptionKey(245, site_id, now, now + dt.timedelta(days=1), now +dt.timedelta(days=2), site_secret, keyset_id=99999)
98-
keys = client.refresh_json(key_set_to_json_for_sharing([master_key, key]))
99-
self.assertRaises(EncryptionError, encrypt, example_uid, IdentityScope.UID2, keys)
102+
mock_refresh_sharing_keys.return_value = create_default_key_collection([master_key, key])
103+
client.refresh_keys()
104+
self.assertRaises(EncryptionError, client.encrypt, example_uid)
100105

101-
def test_encrypt_key_expired(self):
106+
@patch('uid2_client.client.refresh_sharing_keys')
107+
def test_encrypt_key_expired(self, mock_refresh_sharing_keys):
102108
client = Uid2Client("endpoint", "authkey", client_secret)
103109
key = EncryptionKey(245, site_id, now, now, now - dt.timedelta(days=1), site_secret, keyset_id=99999)
104-
keys = client.refresh_json(key_set_to_json_for_sharing([master_key, key]))
105-
self.assertRaises(EncryptionError, encrypt, example_uid, IdentityScope.UID2, keys)
110+
mock_refresh_sharing_keys.return_value = create_default_key_collection([master_key, key])
111+
client.refresh_keys()
112+
self.assertRaises(EncryptionError, client.encrypt, example_uid)

tests/test_sharing_client.py

Lines changed: 10 additions & 14 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 SharingClient, EncryptionError, DecryptionStatus
5+
from uid2_client import SharingClient, DecryptionStatus
66
from uid2_client.encryption_status import EncryptionStatus
77

88

@@ -165,9 +165,8 @@ def get_post_refresh_keys_response_with_no_default_keyset_key():
165165
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_no_default_keyset_key()
166166
self._client.refresh()
167167

168-
with self.assertRaises(EncryptionError) as context:
169-
self._client.encrypt_raw_uid_into_token(example_uid)
170-
self.assertEqual('No Keyset Key Found', str(context.exception))
168+
result = self._client.encrypt_raw_uid_into_token(example_uid)
169+
self.assertEqual(result.status, EncryptionStatus.NOT_AUTHORIZED_FOR_KEY)
171170

172171
def test_cannot_encrypt_if_theres_no_default_keyset_header(self, mock_refresh_keys_util): #CannotEncryptIfTheresNoDefaultKeysetHeader
173172
def get_post_refresh_keys_response_with_no_default_keyset_header():
@@ -177,10 +176,10 @@ def get_post_refresh_keys_response_with_no_default_keyset_header():
177176

178177
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_no_default_keyset_header()
179178
self._client.refresh()
179+
self._client.encrypt_raw_uid_into_token(example_uid)
180180

181-
with self.assertRaises(EncryptionError) as context:
182-
self._client.encrypt_raw_uid_into_token(example_uid)
183-
self.assertEqual('No Keyset Key Found', str(context.exception))
181+
result = self._client.encrypt_raw_uid_into_token(example_uid)
182+
self.assertEqual(result.status, EncryptionStatus.NOT_AUTHORIZED_FOR_KEY)
184183

185184
def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util): # ExpiryInTokenMatchesExpiryInResponse
186185

@@ -198,7 +197,6 @@ def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util
198197
self.assertFalse(result.success)
199198
self.assertEqual(DecryptionStatus.TOKEN_EXPIRED, result.status)
200199

201-
202200
def test_encrypt_key_inactive(self, mock_refresh_keys_util): #EncryptKeyInactive
203201
def get_post_refresh_keys_response_with_key_inactive():
204202
inactive_key = EncryptionKey(245, site_id, now, TOMORROW, IN_2_DAYS,
@@ -208,9 +206,8 @@ def get_post_refresh_keys_response_with_key_inactive():
208206
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_key_inactive()
209207
self._client.refresh()
210208

211-
with self.assertRaises(EncryptionError) as context:
212-
self._client.encrypt_raw_uid_into_token(example_uid)
213-
self.assertEqual('No Keyset Key Found', str(context.exception))
209+
result = self._client.encrypt_raw_uid_into_token(example_uid)
210+
self.assertEqual(result.status, EncryptionStatus.NOT_AUTHORIZED_FOR_KEY)
214211

215212
def test_encrypt_key_expired(self, mock_refresh_keys_util): #EncryptKeyExpired
216213
def get_post_refresh_keys_response_with_key_expired():
@@ -221,9 +218,8 @@ def get_post_refresh_keys_response_with_key_expired():
221218
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_key_expired()
222219
self._client.refresh()
223220

224-
with self.assertRaises(EncryptionError) as context:
225-
self._client.encrypt_raw_uid_into_token(example_uid)
226-
self.assertEqual('No Keyset Key Found', str(context.exception))
221+
result = self._client.encrypt_raw_uid_into_token(example_uid)
222+
self.assertEqual(result.status, EncryptionStatus.NOT_AUTHORIZED_FOR_KEY)
227223

228224
def test_refresh_keys(self, mock_refresh_sharing_keys):
229225
key_collection = create_default_key_collection([master_key])

uid2_client/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
from .publisher_client import *
2020
from .bid_stream_client import *
2121
from .sharing_client import *
22+
from .decryption_status import *
23+
from .encryption_status import *
24+
from .encryption_data_response import *
2225

2326

uid2_client/client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"""
77

88
import datetime as dt
9-
from datetime import timezone
109
import json
10+
from datetime import timezone
1111

12-
from uid2_client import encryption
13-
from .client_type import ClientType
14-
from .keys import EncryptionKey, EncryptionKeysCollection
12+
#from uid2_client import encryption, EncryptionError, EncryptionStatus
13+
from .encryption import *
1514
from .identity_scope import IdentityScope
15+
from .keys import EncryptionKeysCollection
1616
from .refresh_keys_util import refresh_sharing_keys, parse_keys_json
1717
from .request_response_util import *
1818

@@ -98,7 +98,11 @@ def encrypt(self, uid2, keyset_id=None):
9898
9999
Returns (str): Sharing Token
100100
"""
101-
return encryption.encrypt(uid2, self._identity_scope, self._keys, keyset_id).encrypted_data
101+
result = encrypt(uid2, self._identity_scope, self._keys, keyset_id)
102+
if result.status == EncryptionStatus.SUCCESS:
103+
return result.encrypted_data
104+
else:
105+
raise EncryptionError(result.status.value)
102106

103107
def decrypt(self, token):
104108
"""Decrypt advertising token to extract UID2 details.
@@ -115,7 +119,7 @@ def decrypt(self, token):
115119
EncryptionError: if token version is not supported, the token has expired,
116120
or no required decryption keys present in the keys collection
117121
"""
118-
return encryption.decrypt(token, self._keys)
122+
return decrypt(token, self._keys)
119123

120124
class Uid2ClientError(Exception):
121125
"""Raised for problems encountered while interacting with UID2 services."""

uid2_client/encryption.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from uid2_client.client_type import ClientType
1717
from uid2_client.decryption_status import DecryptionStatus
1818
from uid2_client.encryption_data_response import EncryptionDataResponse
19+
from uid2_client.encryption_status import EncryptionStatus
1920
from uid2_client.uid2_base64_url_coder import Uid2Base64UrlCoder
2021
from uid2_client.identity_type import IdentityType
2122
from uid2_client.identity_scope import IdentityScope
@@ -102,13 +103,10 @@ def _decrypt_token(token, keys, domain_name, client_type, now):
102103
now = dt.datetime.now(tz=dt.timezone.utc)
103104
if keys is None:
104105
return DecryptedToken.make_error(DecryptionStatus.NOT_INITIALIZED)
105-
# raise EncryptionError('keys not initialized')
106106
if not keys.valid(now):
107107
return DecryptedToken.make_error(DecryptionStatus.KEYS_NOT_SYNCED)
108-
# raise EncryptionError('no keys available or all keys have expired; refresh the latest keys from UID2 service')
109108
if len(token) < 4:
110109
return DecryptedToken.make_error(DecryptionStatus.INVALID_PAYLOAD)
111-
# raise EncryptionError('invalid payload')
112110

113111
header_str = token[0:4]
114112
index = next((i for i, ch in enumerate(header_str) if ch in base64_url_special_chars), None)
@@ -124,7 +122,6 @@ def _decrypt_token(token, keys, domain_name, client_type, now):
124122
return _decrypt_token_v3(Uid2Base64UrlCoder.decode(token), keys, domain_name, client_type, now, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
125123
else:
126124
return DecryptedToken.make_error(DecryptionStatus.VERSION_NOT_SUPPORTED)
127-
# raise EncryptionError('token version not supported')
128125

129126

130127
def _token_has_valid_lifetime(keys, client_type, established, expires, now):
@@ -312,7 +309,7 @@ def encrypt(uid2, identity_scope, keys, keyset_id=None, **kwargs):
312309
return
313310

314311
if key is None:
315-
raise EncryptionError("No Keyset Key Found")
312+
return EncryptionDataResponse.make_error(EncryptionStatus.NOT_AUTHORIZED_FOR_KEY)
316313
if identity_scope is None:
317314
identity_scope = keys.get_identity_scope()
318315
return _encrypt_token(uid2, identity_scope, master_key, key, site_id, now, token_expiry, ad_token_version)
@@ -405,6 +402,7 @@ def encrypt_data(data, identity_scope, **kwargs):
405402
def _encrypt_data_v1(data, key, iv):
406403
return int.to_bytes(key.key_id, 4, 'big') + iv + _encrypt(data, iv, key)
407404

405+
408406
# DEPRECATED, DO NOT CALL
409407
def decrypt_data(encrypted_data, keys):
410408
"""Decrypt data encrypted with encrypt_data().

uid2_client/encryption_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class EncryptionStatus(Enum):
55
SUCCESS = "success",
6-
# NOT_AUTHORIZED_FOR_KEY = "not_authorized_for_key",
6+
NOT_AUTHORIZED_FOR_KEY = "No Keyset Key Found",
77
# NOT_AUTHORIZED_FOR_MASTER_KEY = "not_authorized_for_master_key",
88
# NOT_INITIALIZED = "not_initialized",
99
# KEYS_NOT_SYNCED = "keys_not_synced",

0 commit comments

Comments
 (0)