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 all 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
4 changes: 4 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,9 @@ async def start(self, connection: "Connection") -> "ClientResponse":
set_result(self._continue, True)
self._continue = None

if self._connection is not None and self._connection.transport is not None:
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 is
closed, otherwise it is ``None``.

.. method:: close()

Close response and underlying connection.
Expand Down
27 changes: 26 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,28 @@ 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: Any, client_ssl_ctx: Any, aiohttp_client: Any, aiohttp_raw_server: Any
) -> None:
async def handler(request):
return web.Response(text="ok")

server = await aiohttp_raw_server(handler, ssl=ssl_ctx)

async with await aiohttp_client(
server, connector=aiohttp.TCPConnector(ssl=client_ssl_ctx)
) as client:
async with await client.get("/") as resp:
assert frozenset(resp.certificate) >= frozenset(
(
"subject",
"issuer",
"version",
"serialNumber",
"notBefore",
"notAfter",
"subjectAltName",
)
)