@@ -268,6 +268,128 @@ def resolve(request: AuthRequest) -> Any:
268268 assert not errors
269269
270270
271+ @pytest .mark .asyncio
272+ @pytest .mark .parametrize ("leg" , ["mcp" , "a2a" ])
273+ @pytest .mark .parametrize ("status" , [401 , 403 ])
274+ @pytest .mark .parametrize ("code" , ["identity_org_mismatch" , "network_policy_denied" , "x" ])
275+ async def test_resolver_rejection_carries_a_stable_code (
276+ leg : str , status : Any , code : str , caplog : pytest .LogCaptureFixture
277+ ) -> None :
278+ def resolve (request : AuthRequest ) -> Principal | None :
279+ error = PrincipalResolverError (status , code = code )
280+ error .args = ("private-token" ,)
281+ raise error
282+
283+ config = BearerTokenAuth (validate_token = lambda token : None , resolve_principal = resolve )
284+ with caplog .at_level (logging .DEBUG , logger = "adcp.server.auth" ):
285+ async with httpx .AsyncClient (
286+ transport = httpx .ASGITransport (app = resolver_app (leg , config )), base_url = "http://test"
287+ ) as client :
288+ response = await client .post ("/" , json = CALL )
289+ assert response .status_code == status
290+ assert response .json () == {"error" : code }
291+ assert ("www-authenticate" in response .headers ) == (status == 401 )
292+ assert "private-token" not in caplog .text + response .text
293+
294+
295+ @pytest .mark .asyncio
296+ @pytest .mark .parametrize ("leg" , ["mcp" , "a2a" ])
297+ @pytest .mark .parametrize ("status" , [401 , 403 ])
298+ @pytest .mark .parametrize (
299+ "code" ,
300+ [
301+ "" ,
302+ "Identity_org_mismatch" ,
303+ "1dentity" ,
304+ "_identity" ,
305+ "identity-org-mismatch" ,
306+ "identity org mismatch" ,
307+ "identity_org_mismatch\n " ,
308+ "a" * 65 ,
309+ "token=private-token" ,
310+ 42 ,
311+ ],
312+ )
313+ async def test_resolver_rejection_falls_back_on_an_unsafe_code (
314+ leg : str , status : Any , code : Any , caplog : pytest .LogCaptureFixture
315+ ) -> None :
316+ def resolve (request : AuthRequest ) -> Principal | None :
317+ raise PrincipalResolverError (status , code = code )
318+
319+ config = BearerTokenAuth (validate_token = lambda token : None , resolve_principal = resolve )
320+ with caplog .at_level (logging .DEBUG , logger = "adcp.server.auth" ):
321+ async with httpx .AsyncClient (
322+ transport = httpx .ASGITransport (app = resolver_app (leg , config )), base_url = "http://test"
323+ ) as client :
324+ response = await client .post ("/" , json = CALL )
325+ assert response .status_code == status
326+ assert response .json () == {"error" : "forbidden" if status == 403 else "unauthenticated" }
327+ assert ("www-authenticate" in response .headers ) == (status == 401 )
328+ rejected = [r for r in caplog .records if getattr (r , "reason" , None ) == "invalid_code" ]
329+ assert len (rejected ) == 1
330+ assert rejected [0 ].args == ()
331+ if code not in ("" , "a" * 65 ):
332+ assert str (code ) not in caplog .text + response .text
333+
334+
335+ @pytest .mark .asyncio
336+ async def test_combined_server_resolver_code_on_both_transports () -> None :
337+ class Seller (ADCPHandler ):
338+ advertised_tools = {"get_products" }
339+
340+ async def get_products (self , params : Any , context : Any = None ) -> dict [str , Any ]:
341+ raise AssertionError ("a rejected caller must not reach the handler" )
342+
343+ async def resolve (request : AuthRequest ) -> Principal | None :
344+ raise PrincipalResolverError (403 , code = "identity_org_mismatch" )
345+
346+ app = _build_mcp_and_a2a_app (
347+ Seller (),
348+ name = "seller" ,
349+ port = 3001 ,
350+ host = "127.0.0.1" ,
351+ instructions = None ,
352+ test_controller = None ,
353+ validation = None ,
354+ stateless_http = True ,
355+ enable_dns_rebinding_protection = False ,
356+ context_factory = auth_context_factory ,
357+ auth = BearerTokenAuth (validate_token = lambda token : None , resolve_principal = resolve ),
358+ )
359+ params = {"buying_mode" : "brief" , "brief" : "video" }
360+ mcp = {
361+ "jsonrpc" : "2.0" ,
362+ "id" : 1 ,
363+ "method" : "tools/call" ,
364+ "params" : {"name" : "get_products" , "arguments" : params },
365+ }
366+ a2a = {
367+ "jsonrpc" : "2.0" ,
368+ "id" : 1 ,
369+ "method" : "SendMessage" ,
370+ "params" : {
371+ "message" : {
372+ "messageId" : "m1" ,
373+ "role" : "ROLE_USER" ,
374+ "parts" : [{"data" : {"skill" : "get_products" , "parameters" : params }}],
375+ }
376+ },
377+ }
378+ async with LifespanManager (app ):
379+ async with httpx .AsyncClient (
380+ transport = httpx .ASGITransport (app = app ), base_url = "http://test" , follow_redirects = True
381+ ) as client :
382+ for path , body in [("/mcp" , mcp ), ("/" , a2a )]:
383+ response = await client .post (
384+ path ,
385+ json = body ,
386+ headers = {"accept" : "application/json, text/event-stream" , "A2A-Version" : "1.0" },
387+ )
388+ assert response .status_code == 403 , response .text
389+ assert response .json () == {"error" : "identity_org_mismatch" }
390+ assert "www-authenticate" not in response .headers
391+
392+
271393@pytest .mark .asyncio
272394@pytest .mark .parametrize ("leg" , ["mcp" , "a2a" ])
273395@pytest .mark .parametrize ("allow" , [False , True ])
0 commit comments