Skip to content

Commit d4f93c4

Browse files
Merge pull request #996 from VWS-Python/allow-strings
Allow utf-8 encoded strings as content
2 parents 197a054 + a8c8900 commit d4f93c4

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/vws_auth_tools/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def authorization_header(
2626
access_key: str,
2727
secret_key: str,
2828
method: str,
29-
content: bytes,
29+
content: str | bytes | None,
3030
content_type: str,
3131
date: str,
3232
request_path: str,
@@ -59,6 +59,13 @@ def authorization_header(
5959
"""
6060
# Ignore a warning that MD5 is insecure - VWS requires it.
6161
hashed = hashlib.md5() # noqa: S324
62+
63+
if content is None:
64+
content = b""
65+
66+
if isinstance(content, str):
67+
content = content.encode(encoding="utf-8")
68+
6269
hashed.update(content)
6370
content_md5_hex = hashed.hexdigest()
6471

tests/test_vws_auth_tools.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
from zoneinfo import ZoneInfo
55

6+
import pytest
67
import vws_auth_tools
78
from freezegun import freeze_time
89

@@ -36,7 +37,8 @@ def test_rfc_1123_date() -> None:
3637
assert result == "Thu, 05 Feb 2015 14:55:12 GMT"
3738

3839

39-
def test_authorization_header() -> None:
40+
@pytest.mark.parametrize("content", [b"some_bytes", "some_bytes"])
41+
def test_authorization_header(content: bytes | str) -> None:
4042
"""The Authorization header is constructed as documented.
4143
4244
This example has been run on known-working code and so any refactor should
@@ -46,7 +48,6 @@ def test_authorization_header() -> None:
4648
# Ignore "Possible hardcoded password" as it is appropriate here.
4749
secret_key = "my_secret_key" # noqa: S105
4850
method = "HTTPMETHOD"
49-
content = b"some_bytes"
5051
content_type = "some/content/type"
5152
date = "some_date_string"
5253
request_path = "/foo"
@@ -62,3 +63,29 @@ def test_authorization_header() -> None:
6263
)
6364

6465
assert result == "VWS my_access_key:8Uy6SKuO5sSBY2X8/znlPFmDF/k="
66+
67+
68+
@pytest.mark.parametrize("content", [b"", None])
69+
def test_authorization_header_none_content(content: bytes | None) -> None:
70+
"""
71+
The Authorization header is the same whether the content is None or b"".
72+
"""
73+
access_key = "my_access_key"
74+
# Ignore "Possible hardcoded password" as it is appropriate here.
75+
secret_key = "my_secret_key" # noqa: S105
76+
method = "HTTPMETHOD"
77+
content_type = "some/content/type"
78+
date = "some_date_string"
79+
request_path = "/foo"
80+
81+
result = vws_auth_tools.authorization_header(
82+
access_key=access_key,
83+
secret_key=secret_key,
84+
method=method,
85+
content=content,
86+
content_type=content_type,
87+
date=date,
88+
request_path=request_path,
89+
)
90+
91+
assert result == "VWS my_access_key:XXvKyRyMkwS8/1P1WLQ0duqNpKs="

0 commit comments

Comments
 (0)