Skip to content

Commit

Permalink
Add encrypt_response functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nanomad committed Sep 27, 2024
1 parent 5e9657d commit a6b62fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "saic_ismart_client_ng"
homepage = "https://github.com/SAIC-iSmart-API/saic-python-client-ng"
version = "0.2.3"
version = "0.3.0"
description = "SAIC next gen client library (MG iSMART)"
authors = [
"Giovanni Condello <[email protected]>",
Expand Down
51 changes: 50 additions & 1 deletion src/saic_ismart_client_ng/net/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def encrypt_request(
request_content,
user_token
)
original_request_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
original_request_headers["APP-VERIFICATION-STRING"] = app_verification_string
original_request_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
return new_content, original_request_headers


Expand Down Expand Up @@ -142,6 +142,55 @@ def decrypt_request(
return original_request_content.encode(charset)


def encrypt_response(
*,
original_request_url: str,
original_response_headers: dict,
original_response_content: str,
response_timestamp_ms: int,
base_uri: str,
tenant_id: str,
user_token: '',

Check failure on line 153 in src/saic_ismart_client_ng/net/crypto.py

View workflow job for this annotation

GitHub Actions / build (3.11)

Ruff (F722)

src/saic_ismart_client_ng/net/crypto.py:153:21: F722 Syntax error in forward annotation: ``

Check failure on line 153 in src/saic_ismart_client_ng/net/crypto.py

View workflow job for this annotation

GitHub Actions / build (3.12)

Ruff (F722)

src/saic_ismart_client_ng/net/crypto.py:153:21: F722 Syntax error in forward annotation: ``
):
request_content = ""
request_path = str(original_request_url).replace(base_uri, "/")
original_content_type = original_response_headers.get("Content-Type") # 'application/x-www-form-urlencoded'
if not original_content_type:
modified_content_type = "application/json"
else:
modified_content_type = original_content_type # 'application/x-www-form-urlencoded'
current_ts = str(response_timestamp_ms)
request_body = original_response_content
new_content = original_response_content
if request_body and "multipart" not in original_content_type:
modified_content_type = normalize_content_type(original_content_type)
request_content = request_body.strip()
if request_content:
key_hex = md5_hex_digest(
current_ts + "1" + modified_content_type,
False
)
iv_hex = md5_hex_digest(current_ts, False)
if key_hex and iv_hex:
new_content = encrypt_aes_cbc_pkcs5_padding(request_content, key_hex, iv_hex).encode('utf-8')

original_response_headers["Content-Type"] = f"{modified_content_type};charset=utf-8"
original_response_headers["APP-CONTENT-ENCRYPTED"] = "1"
original_response_headers["APP-SEND-DATE"] = current_ts
original_response_headers["ORIGINAL-CONTENT-TYPE"] = modified_content_type
app_verification_string = get_app_verification_string(
'',
request_path,
current_ts,
tenant_id,
modified_content_type,
request_content,
user_token
)
original_response_headers["APP-VERIFICATION-STRING"] = app_verification_string
return new_content, original_response_headers


def decrypt_response(
*,
original_response_content: str,
Expand Down

0 comments on commit a6b62fe

Please sign in to comment.