Skip to content

Commit 74754b2

Browse files
committed
feat(crypto): decrypt secret objects
1 parent a0a54c8 commit 74754b2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/apify/_crypto.py

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

@@ -154,6 +155,23 @@ def decrypt_input_secrets(private_key: rsa.RSAPrivateKey, input_data: Any) -> An
154155
encrypted_value,
155156
private_key=private_key,
156157
)
158+
# TODO reuse the decryption logic for string and objects
159+
elif isinstance(value, dict) and isinstance(value.get("secret"), str):
160+
match = ENCRYPTED_INPUT_VALUE_REGEXP.fullmatch(value["secret"])
161+
if match:
162+
encrypted_password = match.group(1)
163+
encrypted_value = match.group(2)
164+
try:
165+
decrypted_json_str = private_decrypt(
166+
encrypted_password,
167+
encrypted_value,
168+
private_key=private_key,
169+
)
170+
input_data[key] = json.loads(decrypted_json_str)
171+
except Exception as err:
172+
raise ValueError(
173+
f'The input field "{key}" could not be parsed as JSON after decryption: {err}'
174+
)
157175

158176
return input_data
159177

0 commit comments

Comments
 (0)