Enable unsafe legacy renegotiation in exchangelib

216 views Asked by At

I have issues enabling unsafe legacy renegotiations in exchangelib. I'm behind a corpo network and that's the underlying problem as far as I know. The same script when I'm connected to a personal network works fine but there's nothing I can do about the networking of my company. I have found a similar question and I'm trying to implement their solution but I still get the error:

exchangelib.errors.TransportError: HTTPSConnectionPool(host='outlook.office365.com', port=443): Max retries exceeded with url: /EWS/Exchange.asmx (Caused by SSLError(SSLError(1, '[SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1006)')))

This is the simplest script I can manage to recreate the error:

import ssl
from exchangelib import Account, BaseProtocol, Configuration, Credentials, NoVerifyHTTPAdapter
import requests
import urllib3

class CustomHttpAdapter (requests.adapters.HTTPAdapter):
    # "Transport adapter" that allows us to use custom ssl_context.

    def __init__(self, ssl_context=None, **kwargs):
        self.ssl_context = ssl_context
        super().__init__(**kwargs)

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = urllib3.poolmanager.PoolManager(
            num_pools=connections, maxsize=maxsize,
            block=block, ssl_context=self.ssl_context)

def get_legacy_session():
    ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
    ctx.options |= ssl.Options.OP_NO_RENEGOTIATION
    session = requests.session()
    session.mount('https://', CustomHttpAdapter(ctx))
    return session

BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter
requests.Session = get_legacy_session

credentials = Credentials(username="[email protected]", password="my_password")
config = Configuration(server="outlook.office365.com", credentials=credentials)

account = Account(
    primary_smtp_address=credentials.username,
    config=config,
    autodiscover=False
)

EDIT: python 3.12 - exchangelib 5.1.0

0

There are 0 answers