Skip to content

Commit a6b62fe

Browse files
committed
Add encrypt_response functionality
1 parent 5e9657d commit a6b62fe

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.poetry]
22
name = "saic_ismart_client_ng"
33
homepage = "https://github.com/SAIC-iSmart-API/saic-python-client-ng"
4-
version = "0.2.3"
4+
version = "0.3.0"
55
description = "SAIC next gen client library (MG iSMART)"
66
authors = [
77
"Giovanni Condello <[email protected]>",

src/saic_ismart_client_ng/net/crypto.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def encrypt_request(
107107
request_content,
108108
user_token
109109
)
110-
original_request_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
111110
original_request_headers["APP-VERIFICATION-STRING"] = app_verification_string
111+
original_request_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
112112
return new_content, original_request_headers
113113

114114

@@ -142,6 +142,55 @@ def decrypt_request(
142142
return original_request_content.encode(charset)
143143

144144

145+
def encrypt_response(
146+
*,
147+
original_request_url: str,
148+
original_response_headers: dict,
149+
original_response_content: str,
150+
response_timestamp_ms: int,
151+
base_uri: str,
152+
tenant_id: str,
153+
user_token: '',
154+
):
155+
request_content = ""
156+
request_path = str(original_request_url).replace(base_uri, "/")
157+
original_content_type = original_response_headers.get("Content-Type") # 'application/x-www-form-urlencoded'
158+
if not original_content_type:
159+
modified_content_type = "application/json"
160+
else:
161+
modified_content_type = original_content_type # 'application/x-www-form-urlencoded'
162+
current_ts = str(response_timestamp_ms)
163+
request_body = original_response_content
164+
new_content = original_response_content
165+
if request_body and "multipart" not in original_content_type:
166+
modified_content_type = normalize_content_type(original_content_type)
167+
request_content = request_body.strip()
168+
if request_content:
169+
key_hex = md5_hex_digest(
170+
current_ts + "1" + modified_content_type,
171+
False
172+
)
173+
iv_hex = md5_hex_digest(current_ts, False)
174+
if key_hex and iv_hex:
175+
new_content = encrypt_aes_cbc_pkcs5_padding(request_content, key_hex, iv_hex).encode('utf-8')
176+
177+
original_response_headers["Content-Type"] = f"{modified_content_type};charset=utf-8"
178+
original_response_headers["APP-CONTENT-ENCRYPTED"] = "1"
179+
original_response_headers["APP-SEND-DATE"] = current_ts
180+
original_response_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
181+
app_verification_string = get_app_verification_string(
182+
'',
183+
request_path,
184+
current_ts,
185+
tenant_id,
186+
modified_content_type,
187+
request_content,
188+
user_token
189+
)
190+
original_response_headers["APP-VERIFICATION-STRING"] = app_verification_string
191+
return new_content, original_response_headers
192+
193+
145194
def decrypt_response(
146195
*,
147196
original_response_content: str,

0 commit comments

Comments
 (0)