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

Is it necessary to monkey patch urllib3? #25

Open
matthewhampton opened this issue Jan 25, 2021 · 4 comments
Open

Is it necessary to monkey patch urllib3? #25

matthewhampton opened this issue Jan 25, 2021 · 4 comments

Comments

@matthewhampton
Copy link

matthewhampton commented Jan 25, 2021

Hi @brandond,

This just a question.

I am wondering why it was necessary to monkey patch urllib3 to access the peercert.

Would it not be possible to just call response._connection.sock.getpeercert(binary_form=True)?

At this point:

Thanks!

@brandond
Copy link
Owner

If that works now, it didn't three years ago when I wrote this. Does it work now?

@matthewhampton
Copy link
Author

I'll give it a try!

@matthewhampton
Copy link
Author

matthewhampton commented Jan 25, 2021

Ah.. I see that the response object there is a requests.models.Response - rather than a urllib3.connection.VerifiedHTTPSConnection...

This is a way that it would be possible to achieve the same effect using the Transport Adapter APIs from requests:

from requests.adapters import HTTPAdapter

class HttpNegotiateAdapter(HTTPAdapter):

    def build_response(self, req, resp):
        peercert = self._get_response_peercert(resp)
        response = super(HttpNegotiateAdapter, self).build_response(req, resp)
        response.peercert = peercert
        return response

    def _get_response_peercert(self, resp):
        try:
            return resp.connection.sock.getpeercert(binary_form=True)
        except AttributeError as e:
            return None

And then, for usage:

import requests
from requests_negotiate_sspi import HttpNegotiateAuth, HttpNegotiateAdapter

s = requests.Session()
s.mount("https://", HttpNegotiateAdapter())
r = s.get('https://iis.contoso.com', auth=HttpNegotiateAuth())

See https://requests.readthedocs.io/en/master/user/advanced/#transport-adapters

@brandond
Copy link
Owner

Looks great, want to put in a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants