Skip to content

Commit adbcb4d

Browse files
committed
fix: use create_storage_content_signature from shared package
1 parent 1e8faf6 commit adbcb4d

File tree

5 files changed

+180
-208
lines changed

5 files changed

+180
-208
lines changed

src/apify_client/_utils.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import asyncio
44
import base64
5-
import hashlib
6-
import hmac
75
import json
86
import random
9-
import string
107
import time
118
from collections.abc import Callable
129
from http import HTTPStatus
@@ -152,59 +149,3 @@ def encode_key_value_store_record_value(value: Any, content_type: str | None = N
152149
value = json.dumps(value, ensure_ascii=False, indent=2, allow_nan=False, default=str).encode('utf-8')
153150

154151
return (value, content_type)
155-
156-
157-
# TODO: will be removed once create_hmac_signature is moved to apify_shared.utils
158-
# https://github.com/apify/apify-shared-python/pull/44
159-
CHARSET = string.digits + string.ascii_letters
160-
161-
162-
def encode_base62(num: int) -> str:
163-
"""Encode the given number to base62."""
164-
if num == 0:
165-
return CHARSET[0]
166-
167-
res = ''
168-
while num > 0:
169-
num, remainder = divmod(num, 62)
170-
res = CHARSET[remainder] + res
171-
return res
172-
173-
174-
def create_hmac_signature(secret_key: str, message: str) -> str:
175-
"""Generate an HMAC signature and encodes it using Base62. Base62 encoding reduces the signature length.
176-
177-
HMAC signature is truncated to 30 characters to make it shorter.
178-
179-
Args:
180-
secret_key (str): Secret key used for signing signatures
181-
message (str): Message to be signed
182-
183-
Returns:
184-
str: Base62 encoded signature
185-
"""
186-
signature = hmac.new(secret_key.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()[:30]
187-
188-
decimal_signature = int(signature, 16)
189-
190-
return encode_base62(decimal_signature)
191-
192-
193-
def create_storage_signature(
194-
resource_id: str, url_signing_secret_key: str, expires_in_millis: int | None, version: int = 0
195-
) -> str:
196-
"""Create a storage signature for a resource, which can be used to generate signed URLs for accessing the resource.
197-
198-
The signature is created using HMAC with the provided secret key and includes
199-
the resource ID, expiration time, and version.
200-
201-
Note: expires_in_millis is optional. If not provided, the signature will not expire.
202-
203-
"""
204-
expires_at = int(time.time() * 1000) + expires_in_millis if expires_in_millis else 0
205-
206-
message_to_sign = f'{version}.{expires_at}.{resource_id}'
207-
hmac = create_hmac_signature(url_signing_secret_key, message_to_sign)
208-
209-
base64url_encoded_payload = base64.urlsafe_b64encode(f'{version}.{expires_at}.{hmac}'.encode())
210-
return base64url_encoded_payload.decode('utf-8')

src/apify_client/clients/resource_clients/dataset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from urllib.parse import urlencode, urlparse, urlunparse
77

88
from apify_shared.models import ListPage
9-
from apify_shared.utils import filter_out_none_values_recursively, ignore_docs
9+
from apify_shared.utils import create_storage_content_signature, filter_out_none_values_recursively, ignore_docs
1010

1111
from apify_client._errors import ApifyApiError
12-
from apify_client._utils import catch_not_found_or_throw, create_storage_signature, pluck_data
12+
from apify_client._utils import catch_not_found_or_throw, pluck_data
1313
from apify_client.clients.base import ResourceClient, ResourceClientAsync
1414

1515
if TYPE_CHECKING:
@@ -619,7 +619,7 @@ def create_items_public_url(
619619
)
620620

621621
if dataset and 'urlSigningSecretKey' in dataset:
622-
signature = create_storage_signature(
622+
signature = create_storage_content_signature(
623623
resource_id=dataset['id'],
624624
url_signing_secret_key=dataset['urlSigningSecretKey'],
625625
expires_in_millis=expires_in_millis,
@@ -1137,7 +1137,7 @@ async def create_items_public_url(
11371137
)
11381138

11391139
if dataset and 'urlSigningSecretKey' in dataset:
1140-
signature = create_storage_signature(
1140+
signature = create_storage_content_signature(
11411141
resource_id=dataset['id'],
11421142
url_signing_secret_key=dataset['urlSigningSecretKey'],
11431143
expires_in_millis=expires_in_millis,

src/apify_client/clients/resource_clients/key_value_store.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
from typing import TYPE_CHECKING, Any
77
from urllib.parse import urlencode, urlparse, urlunparse
88

9-
from apify_shared.utils import filter_out_none_values_recursively, ignore_docs, parse_date_fields
9+
from apify_shared.utils import (
10+
create_storage_content_signature,
11+
filter_out_none_values_recursively,
12+
ignore_docs,
13+
parse_date_fields,
14+
)
1015

1116
from apify_client._errors import ApifyApiError
1217
from apify_client._utils import (
1318
catch_not_found_or_throw,
14-
create_storage_signature,
1519
encode_key_value_store_record_value,
1620
pluck_data,
1721
)
@@ -326,7 +330,7 @@ def create_keys_public_url(
326330
)
327331

328332
if store and 'urlSigningSecretKey' in store:
329-
signature = create_storage_signature(
333+
signature = create_storage_content_signature(
330334
resource_id=store['id'],
331335
url_signing_secret_key=store['urlSigningSecretKey'],
332336
expires_in_millis=expires_in_millis,
@@ -623,7 +627,7 @@ async def create_keys_public_url(
623627
)
624628

625629
if store and 'urlSigningSecretKey' in store:
626-
signature = create_storage_signature(
630+
signature = create_storage_content_signature(
627631
resource_id=store['id'],
628632
url_signing_secret_key=store['urlSigningSecretKey'],
629633
expires_in_millis=expires_in_millis,
File renamed without changes.

0 commit comments

Comments
 (0)