From 14e0fd46ecba5a53613464cd6a3de164c0011bee Mon Sep 17 00:00:00 2001 From: Aaron <7063649+ahcodedev@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:07:33 -0800 Subject: [PATCH] Update PriceCharting clients and authentication security --- docs/index.md | 29 ++++ src/pypricecharting/_auth.py | 57 ++++++++ src/pypricecharting/asyncpricecharting.py | 38 ++++- src/pypricecharting/pricecharting.py | 39 ++++- tests/test_asyncpricecharting.py | 38 ++++- tests/test_auth.py | 167 ++++++++++++++++++++++ tests/test_pricecharting.py | 38 ++++- 7 files changed, 394 insertions(+), 12 deletions(-) create mode 100644 src/pypricecharting/_auth.py create mode 100644 tests/test_auth.py diff --git a/docs/index.md b/docs/index.md index 851d9ab..96c40b4 100644 --- a/docs/index.md +++ b/docs/index.md @@ -8,4 +8,33 @@ The package provides a synchronous client, `PriceCharting`, and an asynchronous The client is under development: the clients can be created and used as context managers, but their API methods are not implemented yet. +## Authentication + +Set your token in the `PRICECHARTING_TOKEN` environment variable and create a client without +arguments: + +```python +from pypricecharting import PriceCharting + +with PriceCharting() as client: + ... +``` + +You can also pass the token directly: + +```python +with PriceCharting("your-40-character-token") as client: + ... +``` + +The environment variable is recommended, because it keeps the token out of your code. Both +clients raise `ValueError` if no token is found or the token is not 40 characters. + +## Security + +The PriceCharting API requires the token in the query string of `GET` requests, so it is part of +the request URL. The clients redact it from the `httpx2` request log, which shows `t=REDACTED` +instead. Anything outside this library that records URLs can still see the token, such as +proxies or your own logging of requests. + See the [API Reference](api/clients.md) for the classes and types in the package. diff --git a/src/pypricecharting/_auth.py b/src/pypricecharting/_auth.py new file mode 100644 index 0000000..fa01339 --- /dev/null +++ b/src/pypricecharting/_auth.py @@ -0,0 +1,57 @@ +import logging +import re +from collections.abc import Generator +from urllib.parse import urlencode + +from httpx2 import Auth, Request, Response +from pydantic import SecretStr + +_TOKEN_PARAM = re.compile(r"([?&]t=)[^&\s\"']+") + + +class _TokenAuth(Auth): + """Add the PriceCharting token as `t` in the query string (GET) or form body (POST).""" + + requires_request_body = True + + def __init__(self, token: SecretStr): + self._token = token + + def auth_flow(self, request: Request) -> Generator[Request, Response]: + """Attach the token to the outgoing request.""" + token = self._token.get_secret_value() + + if request.method == "GET": + request.url = request.url.copy_set_param("t", token) + yield request + return + + param = urlencode({"t": token}).encode() + body = request.content + b"&" + param if request.content else param + headers = request.headers.copy() + headers.pop("Content-Length", None) # forces httpx2 to recalculate length + headers["Content-Type"] = "application/x-www-form-urlencoded" + + yield Request( + request.method, + request.url, + headers=headers, + content=body, + extensions=request.extensions, + ) + + +class _RedactTokenFilter(logging.Filter): + """Replace the `t` query parameter in httpx2 log records with `REDACTED`.""" + + def filter(self, record: logging.LogRecord) -> bool: + """Redact the token in place and always let the record through.""" + message = record.getMessage() + redacted = _TOKEN_PARAM.sub(r"\1REDACTED", message) + if redacted != message: + record.msg = redacted + record.args = None + return True + + +_REDACT_FILTER = _RedactTokenFilter() diff --git a/src/pypricecharting/asyncpricecharting.py b/src/pypricecharting/asyncpricecharting.py index c5dfa56..46a14e7 100644 --- a/src/pypricecharting/asyncpricecharting.py +++ b/src/pypricecharting/asyncpricecharting.py @@ -1,16 +1,46 @@ +import logging +import os from types import TracebackType from typing import Self from httpx2 import AsyncClient +from pydantic import SecretStr + +from pypricecharting._auth import _REDACT_FILTER, _TokenAuth class AsyncPriceCharting: """Asynchronous PriceCharting API client.""" - def __init__(self, credential: str): - """Create a client authenticated with a PriceCharting API token.""" - self.credential = credential - self.client = AsyncClient() + TOKEN_KEY = "PRICECHARTING_TOKEN" + + def __init__(self, token: str | None = None): + """Create a client authenticated with a PriceCharting API token. + + The client adds a logging filter to the `httpx2` logger that replaces the token in + request URLs with `REDACTED`. + + Args: + token: The 40-character PriceCharting API token. If omitted, it is read from the + `PRICECHARTING_TOKEN` environment variable. + + Raises: + ValueError: If no token is passed or set in the environment, or the token is not + 40 characters. + + """ + if token is not None and len(token) != 40: + raise ValueError("PriceCharting API token is not 40 characters") + token = token or os.environ.get(self.TOKEN_KEY) + + if token is None: + raise ValueError("PriceCharting API token not found") + if len(token) != 40: + raise ValueError("PriceCharting API token is not 40 characters") + + self._token = SecretStr(token) + self.client = AsyncClient(auth=_TokenAuth(self._token)) + logging.getLogger("httpx2").addFilter(_REDACT_FILTER) async def aclose(self): """Close the underlying HTTP client.""" diff --git a/src/pypricecharting/pricecharting.py b/src/pypricecharting/pricecharting.py index 5c8e9ab..36b12e4 100644 --- a/src/pypricecharting/pricecharting.py +++ b/src/pypricecharting/pricecharting.py @@ -1,16 +1,47 @@ +import logging +import os from types import TracebackType from typing import Self from httpx2 import Client +from pydantic import SecretStr + +from pypricecharting._auth import _REDACT_FILTER, _TokenAuth class PriceCharting: """Synchronous PriceCharting API client.""" - def __init__(self, credential: str): - """Create a client authenticated with a PriceCharting API token.""" - self.credential = credential - self.client = Client() + TOKEN_KEY = "PRICECHARTING_TOKEN" + + def __init__(self, token: str | None = None): + """Create a client authenticated with a PriceCharting API token. + + The client adds a logging filter to the `httpx2` logger that replaces the token in + request URLs with `REDACTED`. + + Args: + token: The 40-character PriceCharting API token. If omitted, it is read from the + `PRICECHARTING_TOKEN` environment variable. + + Raises: + ValueError: If no token is passed or set in the environment, or the token is not + 40 characters. + + """ + if token is not None and len(token) != 40: + raise ValueError("PriceCharting API token is not 40 characters") + + token = token or os.environ.get(self.TOKEN_KEY) + + if token is None: + raise ValueError("PriceCharting API token not found") + elif len(token) != 40: + raise ValueError("PriceCharting API token is not 40 characters") + + self._token = SecretStr(token) + self.client = Client(auth=_TokenAuth(self._token)) + logging.getLogger("httpx2").addFilter(_REDACT_FILTER) def close(self): """Close the underlying HTTP client.""" diff --git a/tests/test_asyncpricecharting.py b/tests/test_asyncpricecharting.py index 51f5e25..6d906f9 100644 --- a/tests/test_asyncpricecharting.py +++ b/tests/test_asyncpricecharting.py @@ -6,15 +6,49 @@ # Template tests: the API methods are placeholders, so these only check they can be called. # Replace the `is None` assertions once each method is implemented. -TOKEN = "test-token" +TOKEN = "test-token-12345678901234567890123456789" + + +def test_token_constant(): + assert AsyncPriceCharting.TOKEN_KEY == "PRICECHARTING_TOKEN" async def test_init_stores_credential(): client = AsyncPriceCharting(TOKEN) - assert client.credential == TOKEN + assert client._token.get_secret_value() == TOKEN await client.aclose() +async def test_init_raises_on_empty_token(): + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + AsyncPriceCharting("") + + +async def test_init_raises_on_incorrect_length_token(): + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + AsyncPriceCharting("a") + + +async def test_init_raises_on_missing_token(): + with pytest.raises(ValueError, match="PriceCharting API token not found"): + AsyncPriceCharting() + + +async def test_init_reads_env_variable(monkeypatch): + monkeypatch.setenv(AsyncPriceCharting.TOKEN_KEY, TOKEN) + + client = AsyncPriceCharting() + assert client._token.get_secret_value() == TOKEN + await client.aclose() + + +async def test_init_raises_on_incorrect_env_variable(monkeypatch): + monkeypatch.setenv(AsyncPriceCharting.TOKEN_KEY, "test") + + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + AsyncPriceCharting() + + async def test_init_creates_http_client(): client = AsyncPriceCharting(TOKEN) assert isinstance(client.client, AsyncClient) diff --git a/tests/test_auth.py b/tests/test_auth.py new file mode 100644 index 0000000..081e37d --- /dev/null +++ b/tests/test_auth.py @@ -0,0 +1,167 @@ +import logging +from urllib.parse import parse_qs + +import pytest +from httpx2 import AsyncClient, Client, MockTransport, Request, Response +from pydantic import SecretStr + +from pypricecharting import AsyncPriceCharting, PriceCharting +from pypricecharting._auth import _REDACT_FILTER, _RedactTokenFilter, _TokenAuth + +TOKEN = "test-token-12345678901234567890123456789" +URL = "https://example.com/api/product" + + +def _capture(sent: list[Request]): + def handler(request: Request) -> Response: + sent.append(request) + return Response(200) + + return MockTransport(handler) + + +def _client(sent: list[Request]) -> Client: + return Client(auth=_TokenAuth(SecretStr(TOKEN)), transport=_capture(sent)) + + +def _record(msg: str, *args: object) -> logging.LogRecord: + return logging.LogRecord("httpx2", logging.INFO, __file__, 1, msg, args, None) + + +# _TokenAuth + + +def test_token_auth_does_not_expose_token(): + auth = _TokenAuth(SecretStr(TOKEN)) + assert TOKEN not in repr(vars(auth)) + + +def test_get_adds_token_to_query(): + sent: list[Request] = [] + with _client(sent) as client: + client.get(URL, params={"id": "1"}) + + assert sent[0].url.params["t"] == TOKEN + assert sent[0].url.params["id"] == "1" + assert sent[0].content == b"" + + +def test_get_replaces_existing_token(): + sent: list[Request] = [] + with _client(sent) as client: + client.get(URL, params={"t": "other"}) + + assert sent[0].url.params.get_list("t") == [TOKEN] + + +def test_post_adds_token_to_form_body(): + sent: list[Request] = [] + with _client(sent) as client: + client.post(URL, data={"offer-id": "abc"}) + + request = sent[0] + assert parse_qs(request.content.decode()) == {"offer-id": ["abc"], "t": [TOKEN]} + assert "t" not in request.url.params + assert request.headers["Content-Type"] == "application/x-www-form-urlencoded" + assert request.headers["Content-Length"] == str(len(request.content)) + + +def test_post_without_body_sends_only_token(): + sent: list[Request] = [] + with _client(sent) as client: + client.post(URL) + + request = sent[0] + assert parse_qs(request.content.decode()) == {"t": [TOKEN]} + assert request.headers["Content-Type"] == "application/x-www-form-urlencoded" + assert request.headers["Content-Length"] == str(len(request.content)) + + +def test_post_keeps_other_headers(): + sent: list[Request] = [] + with _client(sent) as client: + client.post(URL, data={"offer-id": "abc"}, headers={"X-Test": "1"}) + + assert sent[0].headers["X-Test"] == "1" + + +async def test_async_get_adds_token_to_query(): + sent: list[Request] = [] + auth = _TokenAuth(SecretStr(TOKEN)) + async with AsyncClient(auth=auth, transport=_capture(sent)) as client: + await client.get(URL, params={"id": "1"}) + + assert sent[0].url.params["t"] == TOKEN + + +async def test_async_post_adds_token_to_form_body(): + sent: list[Request] = [] + auth = _TokenAuth(SecretStr(TOKEN)) + async with AsyncClient(auth=auth, transport=_capture(sent)) as client: + await client.post(URL, data={"offer-id": "abc"}) + + assert parse_qs(sent[0].content.decode()) == {"offer-id": ["abc"], "t": [TOKEN]} + + +# _RedactTokenFilter + + +@pytest.mark.parametrize( + ("url", "expected"), + [ + (f"{URL}?t={TOKEN}", f"{URL}?t=REDACTED"), + (f"{URL}?id=1&t={TOKEN}", f"{URL}?id=1&t=REDACTED"), + (f"{URL}?t={TOKEN}&id=1", f"{URL}?t=REDACTED&id=1"), + ], +) +def test_filter_redacts_token(url, expected): + record = _record('HTTP Request: %s %s "%s"', "GET", url, "HTTP/1.1 200 OK") + + assert _RedactTokenFilter().filter(record) is True + assert record.getMessage() == f'HTTP Request: GET {expected} "HTTP/1.1 200 OK"' + assert TOKEN not in record.getMessage() + + +def test_filter_leaves_other_messages_unchanged(): + record = _record("HTTP Request: %s %s", "GET", f"{URL}?id=1&type=t") + + assert _RedactTokenFilter().filter(record) is True + assert record.msg == "HTTP Request: %s %s" + assert record.args == ("GET", f"{URL}?id=1&type=t") + + +def test_filter_ignores_params_ending_in_t(): + record = _record(f"{URL}?format=json&cat=1") + + _RedactTokenFilter().filter(record) + assert record.getMessage() == f"{URL}?format=json&cat=1" + + +def test_httpx2_request_log_is_redacted(caplog): + sent: list[Request] = [] + with caplog.at_level(logging.INFO, logger="httpx2"): + logging.getLogger("httpx2").addFilter(_REDACT_FILTER) + with _client(sent) as client: + client.get(URL, params={"id": "1"}) + + assert "t=REDACTED" in caplog.text + assert TOKEN not in caplog.text + + +# Client installation + + +def test_client_installs_filter_once(): + PriceCharting(TOKEN).close() + PriceCharting(TOKEN).close() + + filters = logging.getLogger("httpx2").filters + assert filters.count(_REDACT_FILTER) == 1 + + +async def test_async_client_installs_filter_once(): + await AsyncPriceCharting(TOKEN).aclose() + await AsyncPriceCharting(TOKEN).aclose() + + filters = logging.getLogger("httpx2").filters + assert filters.count(_REDACT_FILTER) == 1 diff --git a/tests/test_pricecharting.py b/tests/test_pricecharting.py index 3d806bc..079964f 100644 --- a/tests/test_pricecharting.py +++ b/tests/test_pricecharting.py @@ -6,15 +6,49 @@ # Template tests: the API methods are placeholders, so these only check they can be called. # Replace the `is None` assertions once each method is implemented. -TOKEN = "test-token" +TOKEN = "test-token-12345678901234567890123456789" + + +def test_token_constant(): + assert PriceCharting.TOKEN_KEY == "PRICECHARTING_TOKEN" def test_init_stores_credential(): client = PriceCharting(TOKEN) - assert client.credential == TOKEN + assert client._token.get_secret_value() == TOKEN client.close() +def test_init_raises_on_empty_token(): + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + PriceCharting("") + + +def test_init_raises_on_incorrect_length_token(): + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + PriceCharting("a") + + +def test_init_raises_on_missing_token(): + with pytest.raises(ValueError, match="PriceCharting API token not found"): + PriceCharting() + + +def test_init_reads_env_variable(monkeypatch): + monkeypatch.setenv(PriceCharting.TOKEN_KEY, TOKEN) + + client = PriceCharting() + assert client._token.get_secret_value() == TOKEN + client.close() + + +def test_init_raises_on_incorrect_env_variable(monkeypatch): + monkeypatch.setenv(PriceCharting.TOKEN_KEY, "test") + + with pytest.raises(ValueError, match="PriceCharting API token is not 40 characters"): + PriceCharting() + + def test_init_creates_http_client(): client = PriceCharting(TOKEN) assert isinstance(client.client, Client)