Skip to content
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

Add ClientResponse.certificate and populate it, if appropriate #7181

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2816.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``ClientResponse.certificate``, which is populated, if appropriate, with the certificate of the server.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Pavol Vargovčík
Pawel Kowalski
Pawel Miech
Pepe Osca
Petros Blanis
Philipp A.
Pieter van Beek
Qiao Han
Expand Down
8 changes: 8 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ def __init__(

self.method = method
self.cookies: SimpleCookie[str] = SimpleCookie()
self.certificate: Optional[Dict[str, Union[Tuple[Any, ...], int, str]]] = None

self._real_url = url
self._url = url.with_fragment(None)
Expand Down Expand Up @@ -860,6 +861,13 @@ async def start(self, connection: "Connection") -> "ClientResponse":
set_result(self._continue, True)
self._continue = None

# certificate
auxsvr marked this conversation as resolved.
Show resolved Hide resolved
is_tls_transport = (
self._connection is not None and self._connection.transport is not None
)
auxsvr marked this conversation as resolved.
Show resolved Hide resolved
if is_tls_transport:
self.certificate = self._connection.transport.get_extra_info("peercert")

# payload eof handler
payload.on_eof(self._response_eof)

Expand Down
6 changes: 6 additions & 0 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,12 @@ Response object
objects of preceding requests (earliest request first) if there were
redirects, an empty sequence otherwise.

.. attribute:: certificate

The server certificate. This is available with TLS connections,
if the certificate is retrieved before the connection or the TLS session are
auxsvr marked this conversation as resolved.
Show resolved Hide resolved
closed, otherwise it is ``None``.

.. method:: close()

Close response and underlying connection.
Expand Down
30 changes: 29 additions & 1 deletion tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from yarl import URL

import aiohttp
from aiohttp import BaseConnector, hdrs, payload
from aiohttp import BaseConnector, hdrs, payload, web
from aiohttp.client_reqrep import (
ClientRequest,
ClientResponse,
Expand Down Expand Up @@ -1230,3 +1230,31 @@ def test_loose_cookies_types(loop: Any) -> None:
def test_gen_default_accept_encoding(has_brotli: Any, expected: Any) -> None:
with mock.patch("aiohttp.client_reqrep.HAS_BROTLI", has_brotli):
assert _gen_default_accept_encoding() == expected


async def test_certificate_exists(ssl_ctx, client_ssl_ctx) -> None:
async def handler(request):
return web.Response(text="ok")

server = web.Server(handler)
runner = web.ServerRunner(server)
await runner.setup()
site = web.TCPSite(runner, ssl_context=ssl_ctx)
await site.start()

async with aiohttp.ClientSession() as session:
auxsvr marked this conversation as resolved.
Show resolved Hide resolved
resp = await session.get("https://127.0.0.1:8443", ssl=client_ssl_ctx)
assert frozenset(resp.certificate) >= frozenset(
(
"subject",
"issuer",
"version",
"serialNumber",
"notBefore",
"notAfter",
"subjectAltName",
)
)
resp.close()
auxsvr marked this conversation as resolved.
Show resolved Hide resolved
await site.stop()
await server.shutdown()