Skip to content

Commit 2451a25

Browse files
committed
refactor: clean up
1 parent 7f5d373 commit 2451a25

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/apify/_crypto.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import base64
44
import hashlib
55
import hmac
6+
import string
67
from typing import Any
78

89
from cryptography.exceptions import InvalidTag as InvalidTagException
@@ -157,7 +158,7 @@ def decrypt_input_secrets(private_key: rsa.RSAPrivateKey, input_data: Any) -> An
157158
return input_data
158159

159160

160-
CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
161+
CHARSET = string.digits + string.ascii_letters
161162

162163

163164
def encode_base62(num: int) -> str:
@@ -172,11 +173,12 @@ def encode_base62(num: int) -> str:
172173
return res
173174

174175

175-
# createHmacSignature
176176
@ignore_docs
177177
def create_hmac_signature(secret_key: str, message: str) -> str:
178178
"""Generates an HMAC signature and encodes it using Base62. Base62 encoding reduces the signature length.
179179
180+
HMAC signature is truncated to 30 characters to make it shorter.
181+
180182
Args:
181183
secret_key (str): Secret key used for signing signatures
182184
message (str): Message to be signed

src/apify/apify_storage_client/_key_value_store_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def get_public_url(self, key: str) -> str:
9494
public_api_url = self._api_public_base_url
9595
public_url = f'{public_api_url}/v2/key-value-stores/{self._client.resource_id}/records/{key}'
9696

97-
url_signing_secret_key = getattr(self.storage_object, 'url_signing_secret_key', None) # type: ignore[attr-defined]
97+
url_signing_secret_key = getattr(self.storage_object, 'url_signing_secret_key', None)
9898
if url_signing_secret_key:
9999
public_url += f'?signature={create_hmac_signature(url_signing_secret_key, key)}'
100100

tests/unit/test_crypto.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
_load_public_key,
99
create_hmac_signature,
1010
crypto_random_object_id,
11+
encode_base62,
1112
load_private_key,
1213
private_decrypt,
1314
public_encrypt,
@@ -114,7 +115,12 @@ def test_crypto_random_object_id_length_and_charset() -> None:
114115
assert char in 'abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789'
115116

116117

117-
# Check if the method is compatible with js version of the same method in:
118+
@pytest.mark.parametrize(('test_input', 'expected'), [(0, '0'), (10, 'a'), (999999999, '15FTGf')])
119+
def test_encode_base62(test_input: int, expected: str) -> None:
120+
assert encode_base62(test_input) == expected
121+
122+
123+
# This test ensures compatibility with the JavaScript version of the same method.
118124
# https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/hmac.ts
119125
def test_create_valid_hmac_signature() -> None:
120126
# This test uses the same secret key and message as in JS tests.
@@ -127,5 +133,5 @@ def test_create_same_hmac() -> None:
127133
# This test uses the same secret key and message as in JS tests.
128134
secret_key = 'hmac-same-secret-key'
129135
message = 'hmac-same-message-to-be-authenticated'
130-
for _ in range(5):
131-
assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
136+
assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'
137+
assert create_hmac_signature(secret_key, message) == 'FYMcmTIm3idXqleF1Sw5'

0 commit comments

Comments
 (0)