Skip to content
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
7 changes: 6 additions & 1 deletion src/auth0/management/management_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def _enforce_custom_domain_whitelist(request: httpx.Request) -> None:
del request.headers[CUSTOM_DOMAIN_HEADER]


async def _enforce_custom_domain_whitelist_async(request: httpx.Request) -> None:
"""httpx event hook for httpx.AsyncClient, which awaits its request hooks."""
_enforce_custom_domain_whitelist(request)


def CustomDomainHeader(domain: str) -> RequestOptions:
"""Create request options that set the Auth0-Custom-Domain header for a single request.

Expand Down Expand Up @@ -522,7 +527,7 @@ def __init__(
if httpx_client is None:
httpx_client = httpx.AsyncClient(timeout=timeout or 60, follow_redirects=True)
httpx_client.event_hooks.setdefault("request", []).append(
_enforce_custom_domain_whitelist
_enforce_custom_domain_whitelist_async
)

# Encode client_info into Auth0-Client header to override default telemetry
Expand Down
40 changes: 40 additions & 0 deletions tests/management/test_management_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,46 @@ def test_non_whitelisted_paths_do_not_match(self, path):
"""Should not match non-whitelisted paths."""
assert _is_path_whitelisted(path) is False

def test_sync_request_enforces_whitelist(self):
"""Should send the header only on whitelisted paths for sync requests."""
sent = []

def handler(request):
sent.append(request)
return httpx.Response(200, json={})

client = ManagementClient(
domain="test.auth0.com",
token="my-token",
custom_domain="login.mycompany.com",
httpx_client=httpx.Client(transport=httpx.MockTransport(handler)),
)
client.users.get("auth0|abc123")
client.clients.get("abc123")

assert sent[0].headers[_CUSTOM_DOMAIN_HEADER] == "login.mycompany.com"
assert _CUSTOM_DOMAIN_HEADER not in sent[1].headers

async def test_async_request_enforces_whitelist(self):
"""Should send the header only on whitelisted paths for async requests."""
sent = []

def handler(request):
sent.append(request)
return httpx.Response(200, json={})

client = AsyncManagementClient(
domain="test.auth0.com",
token="my-token",
custom_domain="login.mycompany.com",
httpx_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
)
await client.users.get("auth0|abc123")
await client.clients.get("abc123")

assert sent[0].headers[_CUSTOM_DOMAIN_HEADER] == "login.mycompany.com"
assert _CUSTOM_DOMAIN_HEADER not in sent[1].headers


class TestImports:
"""Tests for module imports."""
Expand Down