Skip to content

[auth] Add custom header injection to JsonRpcBase (API key support) #828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ dmypy.json

# MacOS artifacts
.DS_Store
.jrb/ ..
Empty file.
77 changes: 77 additions & 0 deletions tests/unit/asyn/clients/test_json_rpc_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from unittest.mock import AsyncMock, patch

import pytest
from httpx import Response

from xrpl.asyncio.clients.exceptions import XRPLAuthenticationException
from xrpl.asyncio.clients.json_rpc_base import JsonRpcBase
from xrpl.models.requests import ServerInfo


@pytest.mark.asyncio
async def test_global_headers_are_sent():
client = JsonRpcBase(
"https://xrpl.fake", headers={"Authorization": "Bearer testtoken"}
)

with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = Response(
status_code=200,
json={"result": {"status": "success"}, "id": 1},
)

await client._request_impl(ServerInfo())

headers_sent = mock_post.call_args.kwargs["headers"]
assert headers_sent["Authorization"] == "Bearer testtoken"
assert headers_sent["Content-Type"] == "application/json"


@pytest.mark.asyncio
async def test_per_request_headers_override_global():
client = JsonRpcBase(
"https://xrpl.fake", headers={"Authorization": "Bearer default"}
)

with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = Response(
status_code=200,
json={"result": {"status": "success"}, "id": 1},
)

await client._request_impl(
ServerInfo(), headers={"Authorization": "Bearer override"}
)

headers_sent = mock_post.call_args.kwargs["headers"]
assert headers_sent["Authorization"] == "Bearer override"


@pytest.mark.asyncio
async def test_no_headers_does_not_crash():
client = JsonRpcBase("https://xrpl.fake")

with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = Response(
status_code=200,
json={"result": {"status": "success"}, "id": 1},
)

await client._request_impl(ServerInfo())

headers_sent = mock_post.call_args.kwargs["headers"]
assert headers_sent["Content-Type"] == "application/json"


@pytest.mark.asyncio
async def test_raises_on_401_403():
client = JsonRpcBase("https://xrpl.fake")

for code in [401, 403]:
with patch("httpx.AsyncClient.post", new_callable=AsyncMock) as mock_post:
mock_post.return_value = Response(status_code=code, text="Unauthorized")

with pytest.raises(
XRPLAuthenticationException, match="Authentication failed"
):
await client._request_impl(ServerInfo())
81 changes: 0 additions & 81 deletions xrpl/asyncio/clients/client.py

This file was deleted.

6 changes: 6 additions & 0 deletions xrpl/asyncio/clients/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ class XRPLWebsocketException(XRPLException):
"""

pass


class XRPLAuthenticationException(XRPLRequestFailureException):
"""Raised when authentication with the XRPL node fails (401 or 403)."""

pass
56 changes: 0 additions & 56 deletions xrpl/asyncio/clients/json_rpc_base.py

This file was deleted.

Loading