From f8c8419d9eec9ccb95ccd87174459879152973fc Mon Sep 17 00:00:00 2001 From: Jacob Henner Date: Fri, 25 Sep 2026 17:00:18 -0400 Subject: [PATCH 1/3] Differentiate async auth Differentiate async auth, to ensure that any blocking IO performed during GSSAPI authentication (e.g. network interactions with a KDC) does not block the event loop. --- httpx_gssapi/gssapi_.py | 82 +++++++++++++++++++++++++++++------------ tests/test_mocked.py | 59 +++++++++++++++++++++++++++-- 2 files changed, 115 insertions(+), 26 deletions(-) diff --git a/httpx_gssapi/gssapi_.py b/httpx_gssapi/gssapi_.py index 0f5a99b..b477ac6 100644 --- a/httpx_gssapi/gssapi_.py +++ b/httpx_gssapi/gssapi_.py @@ -1,8 +1,9 @@ +import asyncio import re import logging from itertools import chain from functools import wraps -from typing import Generator, Optional, List, Any, Union +from typing import AsyncGenerator, Generator, Optional, List, Any, Union from base64 import b64encode, b64decode @@ -17,6 +18,7 @@ log = logging.getLogger(__name__) FlowGen = Generator[Request, Response, None] +AsyncFlowGen = AsyncGenerator[Request, Response] # Different types of mutual authentication: # with mutual_authentication set to REQUIRED, all responses will be @@ -98,8 +100,7 @@ def _gss_to_spnego_error(message: str, *args: Any, **kwargs: Any): """Helper function to _handle_gsserror to raise SPNEGOExchangeErrors.""" try: request = next( - a for a in chain(args, kwargs.values()) - if isinstance(a, Request) + a for a in chain(args, kwargs.values()) if isinstance(a, Request) ) except StopIteration: # sanity check raise RuntimeError("No request in arguments!") @@ -136,14 +137,16 @@ class HTTPSPNEGOAuth(Auth): """ - def __init__(self, - mutual_authentication: int = DISABLED, - target_name: Optional[Union[str, gssapi.Name]] = "HTTP", - delegate: bool = False, - opportunistic_auth: bool = False, - creds: Optional[gssapi.Credentials] = None, - mech: Optional[Union[bytes, gssapi.OID]] = SPNEGO, - sanitize_mutual_error_response: bool = True): + def __init__( + self, + mutual_authentication: int = DISABLED, + target_name: Optional[Union[str, gssapi.Name]] = "HTTP", + delegate: bool = False, + opportunistic_auth: bool = False, + creds: Optional[gssapi.Credentials] = None, + mech: Optional[Union[bytes, gssapi.OID]] = SPNEGO, + sanitize_mutual_error_response: bool = True, + ): self.mutual_authentication = mutual_authentication self.target_name = target_name self.delegate = delegate @@ -152,7 +155,7 @@ def __init__(self, self.mech = mech self.sanitize_mutual_error_response = sanitize_mutual_error_response - def auth_flow(self, request: Request) -> FlowGen: + def sync_auth_flow(self, request: Request) -> FlowGen: if self.opportunistic_auth: # add Authorization header before we receive a 401 ctx = self.set_auth_header(request) @@ -162,9 +165,42 @@ def auth_flow(self, request: Request) -> FlowGen: response = yield request yield from self.handle_response(response, ctx) - def handle_response(self, - response: Response, - ctx: Optional[SecurityContext] = None) -> FlowGen: + async def async_auth_flow(self, request: Request) -> AsyncFlowGen: + if self.opportunistic_auth: + # GSSAPI may perform blocking I/O while acquiring credentials. + ctx = await asyncio.to_thread(self.set_auth_header, request) + else: + ctx = None + + response = yield request + num_401s = 0 + while response.status_code == 401 and num_401s < 2: + num_401s += 1 + log.debug("Handling 401 response, total seen: %d", num_401s) + + if _negotiate_value(response) is None: + log.debug("GSSAPI is not supported") + break + + log.debug("Generating user authentication header") + try: + ctx = await asyncio.to_thread( + self.set_auth_header, response.request, response + ) + except SPNEGOExchangeError: + log.debug("Failed to generate authentication header") + + response = yield response.request + + if response.status_code == 401 or ctx is None: + log.debug("Failed to authenticate, returning 401 response") + return + + await asyncio.to_thread(self.handle_mutual_auth, response, ctx) + + def handle_response( + self, response: Response, ctx: Optional[SecurityContext] = None + ) -> FlowGen: num_401s = 0 while response.status_code == 401 and num_401s < 2: num_401s += 1 @@ -221,8 +257,10 @@ def handle_mutual_auth(self, response: Response, ctx: SecurityContext): " on %d response", response.status_code, ) - if (self.mutual_authentication == REQUIRED - and self.sanitize_mutual_error_response): + if ( + self.mutual_authentication == REQUIRED + and self.sanitize_mutual_error_response + ): _sanitize_response(response) else: # Unable to attempt mutual authentication when mutual auth is @@ -232,9 +270,9 @@ def handle_mutual_auth(self, response: Response, ctx: SecurityContext): raise MutualAuthenticationError(response=response) @_handle_gsserror(gss_stage='stepping', result=_gss_to_spnego_error) - def set_auth_header(self, - request: Request, - response: Optional[Response] = None) -> SecurityContext: + def set_auth_header( + self, request: Request, response: Optional[Response] = None + ) -> SecurityContext: """ Create a new security context, generate the GSSAPI authentication token, and insert it into the request header. The new security context @@ -257,9 +295,7 @@ def set_auth_header(self, return ctx @_handle_gsserror(gss_stage="stepping", result=False) - def authenticate_server(self, - response: Response, - ctx: SecurityContext) -> bool: + def authenticate_server(self, response: Response, ctx: SecurityContext) -> bool: """ Uses GSSAPI to authenticate the server by extracting the negotiate value from the response and stepping the security context. diff --git a/tests/test_mocked.py b/tests/test_mocked.py index 770bfc3..5500331 100644 --- a/tests/test_mocked.py +++ b/tests/test_mocked.py @@ -1,7 +1,9 @@ #!/usr/bin/env python """Tests for httpx_gssapi.""" +import asyncio import logging +import threading from base64 import b64encode from unittest.mock import Mock, patch @@ -103,7 +105,7 @@ def test_force_preemptive(patched_ctx): request = null_request() - flow = auth.auth_flow(request) + flow = auth.sync_auth_flow(request) next(flow) # Move to first request yield assert 'Authorization' in request.headers @@ -115,7 +117,7 @@ def test_no_force_preemptive(patched_ctx): request = null_request() - flow = auth.auth_flow(request) + flow = auth.sync_auth_flow(request) next(flow) # Move to first request yield assert 'Authorization' not in request.headers @@ -363,13 +365,64 @@ def test_opportunistic_auth(patched_ctx): request = null_request() - flow = auth.auth_flow(request) + flow = auth.sync_auth_flow(request) assert next(flow) is request assert 'Authorization' in request.headers assert request.headers.get('Authorization') == b64_negotiate_response +def test_async_auth_flow_offloads_gssapi_work(): + auth = httpx_gssapi.HTTPSPNEGOAuth(opportunistic_auth=True) + event_loop_thread = threading.get_ident() + gssapi_thread = None + + def set_auth_header(request, response=None): + nonlocal gssapi_thread + gssapi_thread = threading.get_ident() + request.headers['Authorization'] = b64_negotiate_response + return object() + + async def request(): + transport = httpx.MockTransport( + lambda request: httpx.Response(200, request=request) + ) + with patch.object(auth, 'set_auth_header', side_effect=set_auth_header): + async with httpx.AsyncClient(auth=auth, transport=transport) as client: + response = await client.get('http://www.example.org/') + assert response.status_code == 200 + + asyncio.run(request()) + assert gssapi_thread is not None + assert gssapi_thread != event_loop_thread + + +def test_async_auth_flow_handles_negotiate_challenge(patched_ctx): + auth = httpx_gssapi.HTTPSPNEGOAuth() + request_count = 0 + + def transport(request): + nonlocal request_count + request_count += 1 + if request_count == 1: + return httpx.Response(401, headers=neg_token, request=request) + assert request.headers['Authorization'] == b64_negotiate_response + return httpx.Response(200, request=request) + + async def request(): + async with httpx.AsyncClient( + auth=auth, + transport=httpx.MockTransport(transport), + ) as client: + response = await client.get('http://www.example.org/') + assert response.status_code == 200 + + asyncio.run(request()) + assert request_count == 2 + check_init() + fake_resp.assert_called_with(b'token') + + def test_explicit_creds(patched_creds, patched_ctx): response = null_response(headers=neg_token) creds = gssapi.Credentials() From 1ee1b15ce54580487c1b982b179a01316f1747eb Mon Sep 17 00:00:00 2001 From: Roger Aiudi Date: Mon, 28 Sep 2026 21:46:29 -0400 Subject: [PATCH 2/3] Use threads to step the auth flow generator --- httpx_gssapi/gssapi_.py | 59 +++++++++++----------------------- tests/test_mocked.py | 70 ++++++++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 65 deletions(-) diff --git a/httpx_gssapi/gssapi_.py b/httpx_gssapi/gssapi_.py index b477ac6..d599eff 100644 --- a/httpx_gssapi/gssapi_.py +++ b/httpx_gssapi/gssapi_.py @@ -2,8 +2,8 @@ import re import logging from itertools import chain -from functools import wraps -from typing import AsyncGenerator, Generator, Optional, List, Any, Union +from functools import partial, wraps +from typing import Any, AsyncGenerator, Generator, List, Optional, Union from base64 import b64encode, b64decode @@ -17,6 +17,7 @@ from .exceptions import MutualAuthenticationError, SPNEGOExchangeError log = logging.getLogger(__name__) + FlowGen = Generator[Request, Response, None] AsyncFlowGen = AsyncGenerator[Request, Response] @@ -155,23 +156,13 @@ def __init__( self.mech = mech self.sanitize_mutual_error_response = sanitize_mutual_error_response - def sync_auth_flow(self, request: Request) -> FlowGen: + def auth_flow(self, request: Request) -> FlowGen: if self.opportunistic_auth: # add Authorization header before we receive a 401 ctx = self.set_auth_header(request) else: ctx = None - response = yield request - yield from self.handle_response(response, ctx) - - async def async_auth_flow(self, request: Request) -> AsyncFlowGen: - if self.opportunistic_auth: - # GSSAPI may perform blocking I/O while acquiring credentials. - ctx = await asyncio.to_thread(self.set_auth_header, request) - else: - ctx = None - response = yield request num_401s = 0 while response.status_code == 401 and num_401s < 2: @@ -184,46 +175,34 @@ async def async_auth_flow(self, request: Request) -> AsyncFlowGen: log.debug("Generating user authentication header") try: - ctx = await asyncio.to_thread( - self.set_auth_header, response.request, response - ) + ctx = self.set_auth_header(response.request, response) except SPNEGOExchangeError: log.debug("Failed to generate authentication header") + # Try request again, hopefully with a new auth header response = yield response.request if response.status_code == 401 or ctx is None: log.debug("Failed to authenticate, returning 401 response") return - await asyncio.to_thread(self.handle_mutual_auth, response, ctx) + self.handle_mutual_auth(response, ctx) - def handle_response( - self, response: Response, ctx: Optional[SecurityContext] = None - ) -> FlowGen: - num_401s = 0 - while response.status_code == 401 and num_401s < 2: - num_401s += 1 - log.debug("Handling 401 response, total seen: %d", num_401s) + async def async_auth_flow(self, request: Request) -> AsyncFlowGen: + to_thread = partial(asyncio.get_running_loop().run_in_executor, None) - if _negotiate_value(response) is None: - log.debug("GSSAPI is not supported") - break + flow = self.auth_flow(request) - log.debug("Generating user authentication header") + def _send(r: Response) -> Union[Request, None]: try: - ctx = self.set_auth_header(response.request, response) - except SPNEGOExchangeError: - log.debug("Failed to generate authentication header") - - # Try request again, hopefully with a new auth header - response = yield response.request - - if response.status_code == 401 or ctx is None: - log.debug("Failed to authenticate, returning 401 response") - return - - self.handle_mutual_auth(response, ctx) + return flow.send(r) + except StopIteration: # catch here, can't pass through Future + return None + + req: Union[Request, None] = await to_thread(next, flow) + while req is not None: + resp = yield req + req = await to_thread(_send, resp) def handle_mutual_auth(self, response: Response, ctx: SecurityContext): """ diff --git a/tests/test_mocked.py b/tests/test_mocked.py index 5500331..302c5a0 100644 --- a/tests/test_mocked.py +++ b/tests/test_mocked.py @@ -5,6 +5,7 @@ import logging import threading from base64 import b64encode +from typing import Union from unittest.mock import Mock, patch import pytest @@ -16,6 +17,7 @@ import httpx_gssapi from httpx_gssapi import REQUIRED, OPTIONAL +from httpx_gssapi.gssapi_ import FlowGen logging.basicConfig() @@ -105,7 +107,7 @@ def test_force_preemptive(patched_ctx): request = null_request() - flow = auth.sync_auth_flow(request) + flow = auth.auth_flow(request) next(flow) # Move to first request yield assert 'Authorization' in request.headers @@ -117,7 +119,7 @@ def test_no_force_preemptive(patched_ctx): request = null_request() - flow = auth.sync_auth_flow(request) + flow = auth.auth_flow(request) next(flow) # Move to first request yield assert 'Authorization' not in request.headers @@ -181,9 +183,9 @@ def test_handle_response_200(patched_ctx): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=REQUIRED) - flow = auth.handle_response(response_ok, gssapi.SecurityContext()) + flow = _init_sync_flow(auth, response_ok, gssapi.SecurityContext()) with pytest.raises(StopIteration): # No other requests required - next(flow) + flow.send(response_ok) fake_resp.assert_called_with(b"servertoken") @@ -192,9 +194,9 @@ def test_handle_response_200_mutual_auth_required_failure(patched_ctx_fail): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=REQUIRED) - flow = auth.handle_response(response_ok, gssapi.SecurityContext()) + flow = _init_sync_flow(auth, response_ok, gssapi.SecurityContext()) with pytest.raises(httpx_gssapi.MutualAuthenticationError): - next(flow) + flow.send(response_ok) assert not fail_resp.called @@ -207,9 +209,9 @@ def test_handle_response_200_mutual_auth_required_failure_2(patched_ctx_fail): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=REQUIRED) - flow = auth.handle_response(response_ok, gssapi.SecurityContext()) + flow = _init_sync_flow(auth, response_ok, gssapi.SecurityContext()) with pytest.raises(httpx_gssapi.MutualAuthenticationError): - next(flow) + flow.send(response_ok) fail_resp.assert_called_with(b"servertoken") @@ -222,9 +224,9 @@ def test_handle_response_200_mutual_auth_optional_hard_fail(patched_ctx_fail): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=OPTIONAL) - flow = auth.handle_response(response_ok, gssapi.SecurityContext()) + flow = _init_sync_flow(auth, response_ok, gssapi.SecurityContext()) with pytest.raises(httpx_gssapi.MutualAuthenticationError): - next(flow) + flow.send(response_ok) fail_resp.assert_called_with(b"servertoken") @@ -235,9 +237,9 @@ def test_handle_response_200_mutual_auth_optional_soft_failure(patched_ctx): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=OPTIONAL) auth.context = {"www.example.org": gssapi.SecurityContext} - flow = auth.handle_response(response_ok) + flow = _init_sync_flow(auth, response_ok) with pytest.raises(StopIteration): # advance flow with no new requests - next(flow) + flow.send(response_ok) assert not fake_resp.called @@ -251,9 +253,9 @@ def test_handle_response_500_mutual_auth_required_failure(patched_ctx_fail): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=REQUIRED) - flow = auth.handle_response(response_500, gssapi.SecurityContext()) + flow = _init_sync_flow(auth, response_500, gssapi.SecurityContext()) with pytest.raises(StopIteration): # advance flow with no new requests - next(flow) + flow.send(response_500) assert 'other' not in response_500.headers assert response_500.headers['date'] == 'DATE' @@ -276,9 +278,9 @@ def test_handle_response_500_mutual_auth_required_fail_no_san(patched_ctx_fail): ) auth.context = {"www.example.org": "CTX"} - flow = auth.handle_response(response_500) + flow = _init_sync_flow(auth, response_500) with pytest.raises(StopIteration): # advance flow with no new requests - next(flow) + flow.send(response_500) assert response_500.headers['other'] == 'x' assert response_500.headers['date'] == 'DATE' @@ -298,9 +300,9 @@ def test_handle_response_500_mutual_auth_optional_failure(patched_ctx_fail): auth = httpx_gssapi.HTTPSPNEGOAuth(mutual_authentication=OPTIONAL) auth.context = {"www.example.org": "CTX"} - flow = auth.handle_response(response_500) + flow = _init_sync_flow(auth, response_500) with pytest.raises(StopIteration): # advance flow with no new requests - next(flow) + flow.send(response_500) assert response_500.headers['other'] == 'x' assert response_500.headers['date'] == 'DATE' @@ -313,8 +315,8 @@ def test_handle_response_500_mutual_auth_optional_failure(patched_ctx_fail): def test_handle_response_401(patched_ctx): auth = httpx_gssapi.HTTPSPNEGOAuth() response_401 = null_response(status=401, headers=neg_token) - flow = auth.handle_response(response_401) - request = next(flow) + flow = _init_sync_flow(auth, response_401) + request = flow.send(response_401) assert isinstance(request, httpx.Request) assert request.headers['Authorization'] == b64_negotiate_response response_ok = null_response(headers=neg_server, request=request) @@ -329,9 +331,9 @@ def test_handle_response_401_rejected(patched_ctx): # Ensure there is no infinite auth loop. auth = httpx_gssapi.HTTPSPNEGOAuth() response_401 = null_response(status=401, headers=neg_token) - flow = auth.handle_response(response_401) + flow = _init_sync_flow(auth, response_401) - request = next(flow) + request = flow.send(response_401) assert isinstance(request, httpx.Request) assert request.headers['Authorization'] == b64_negotiate_response @@ -349,8 +351,8 @@ def test_handle_response_401_rejected(patched_ctx): def test_delegation(patched_ctx): auth = httpx_gssapi.HTTPSPNEGOAuth(delegate=True) response_401 = null_response(status=401, headers=neg_token) - flow = auth.handle_response(response_401) - request = next(flow) + flow = _init_sync_flow(auth, response_401) + request = flow.send(response_401) assert isinstance(request, httpx.Request) assert request.headers['Authorization'] == b64_negotiate_response response_ok = null_response(headers=neg_server, request=request) @@ -365,7 +367,7 @@ def test_opportunistic_auth(patched_ctx): request = null_request() - flow = auth.sync_auth_flow(request) + flow = auth.auth_flow(request) assert next(flow) is request assert 'Authorization' in request.headers @@ -459,5 +461,23 @@ def test_os_default_mech(patched_ctx): fake_resp.assert_called_with(b"token") +def _init_sync_flow( + auth: httpx_gssapi.HTTPSPNEGOAuth, + resp: httpx.Response, + ctx: Union[gssapi.SecurityContext, None] = None, +) -> FlowGen: + if ctx: + auth.set_auth_header = lambda *_: gssapi.SecurityContext() + auth.opportunistic_auth = True + + flow = auth.auth_flow(resp.request) + assert next(flow) is resp.request + + if ctx: + del auth.set_auth_header + + return flow + + if __name__ == '__main__': pytest.main() From 8002a7a729f001ae002442bc772bac02b34ed398 Mon Sep 17 00:00:00 2001 From: Roger Aiudi Date: Thu, 19 Nov 2026 15:36:16 -0500 Subject: [PATCH 3/3] Pinned 3.9 to ubuntu-24.04 --- .github/workflows/ci.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e3608b8..3e2fb52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -51,7 +51,7 @@ jobs: needs: - flake8 - mypy - runs-on: ubuntu-latest + runs-on: ${{ matrix.os || 'ubuntu-latest' }} strategy: fail-fast: false matrix: @@ -63,6 +63,9 @@ jobs: - '3.13' - '3.14' - '3.15' + include: + - python: "3.9" + os: ubuntu-24.04 steps: - uses: actions/checkout@v7.0.1 - name: Setup Build Tools