Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _parse_response(bytes_content):
# We could return lots more interesting things here
if not is_xml(bytes_content):
raise AutoDiscoverFailed('Unknown autodiscover response: %s' % bytes_content)
autodiscover = to_xml(bytes_content)
resp = autodiscover.find('{%s}Response' % RESPONSE_NS)
if resp is None:
_raise_response_errors(autodiscover)
account = resp.find('{%s}Account' % RESPONSE_NS)
action = get_xml_attr(account, '{%s}Action' % RESPONSE_NS)
redirect_email = get_xml_attr(account, '{%s}RedirectAddr' % RESPONSE_NS)
if action == 'redirectAddr' and redirect_email:
# This is redirection to e.g. Office365
raise AutoDiscoverRedirect(redirect_email)
# The AccountType element is only available if we are not redirecting
account_type = get_xml_attr(account, '{%s}AccountType' % RESPONSE_NS)
if account_type != 'email':
raise ValueError("'Unexpected AccountType '%s'" % account_type)
user = resp.find('{%s}User' % RESPONSE_NS)
# AutoDiscoverSMTPAddress might not be present in the XML, so primary_smtp_address might be None. In this
# case, the original email address IS the primary address
primary_smtp_address = get_xml_attr(user, '{%s}AutoDiscoverSMTPAddress' % RESPONSE_NS)
protocols = {get_xml_attr(p, '{%s}Type' % RESPONSE_NS): p for p in account.findall('{%s}Protocol' % RESPONSE_NS)}
# There are three possible protocol types: EXCH, EXPR and WEB.
# EXPR is meant for EWS. See
# https://techcommunity.microsoft.com/t5/Exchange-Team-Blog/The-Autodiscover-Service-and-Outlook-Providers-how-does-this/ba-p/584403
if resp is None:
_raise_response_errors(autodiscover)
account = resp.find('{%s}Account' % RESPONSE_NS)
action = get_xml_attr(account, '{%s}Action' % RESPONSE_NS)
redirect_email = get_xml_attr(account, '{%s}RedirectAddr' % RESPONSE_NS)
if action == 'redirectAddr' and redirect_email:
# This is redirection to e.g. Office365
raise AutoDiscoverRedirect(redirect_email)
# The AccountType element is only available if we are not redirecting
account_type = get_xml_attr(account, '{%s}AccountType' % RESPONSE_NS)
if account_type != 'email':
raise ValueError("'Unexpected AccountType '%s'" % account_type)
user = resp.find('{%s}User' % RESPONSE_NS)
# AutoDiscoverSMTPAddress might not be present in the XML, so primary_smtp_address might be None. In this
# case, the original email address IS the primary address
primary_smtp_address = get_xml_attr(user, '{%s}AutoDiscoverSMTPAddress' % RESPONSE_NS)
protocols = {get_xml_attr(p, '{%s}Type' % RESPONSE_NS): p for p in account.findall('{%s}Protocol' % RESPONSE_NS)}
# There are three possible protocol types: EXCH, EXPR and WEB.
# EXPR is meant for EWS. See
# https://techcommunity.microsoft.com/t5/Exchange-Team-Blog/The-Autodiscover-Service-and-Outlook-Providers-how-does-this/ba-p/584403
# We allow fallback to EXCH if EXPR is not available to support installations where EXPR is not available.
try:
protocol = protocols['EXPR']
except KeyError:
try:
protocol = protocols['EXCH']
except KeyError:
# Neither type was found. Give up
raise_from(AutoDiscoverFailed('Invalid AutoDiscover response: %s' % xml_to_str(autodiscover)), None)
ews_url = get_xml_attr(protocol, '{%s}EwsUrl' % RESPONSE_NS)
if not ews_url:
def from_xml(cls, elem, account):
# Gets value of this specific ExtendedProperty from a list of 'ExtendedProperty' XML elements
python_type = cls.python_type()
if cls.is_array_type():
values = elem.find('{%s}Values' % TNS)
if cls.is_binary_type():
return [safe_b64decode(val) for val in get_xml_attrs(values, '{%s}Value' % TNS)]
return [
xml_text_to_value(value=val, value_type=python_type)
for val in get_xml_attrs(values, '{%s}Value' % TNS)
]
if cls.is_binary_type():
return safe_b64decode(get_xml_attr(elem, '{%s}Value' % TNS))
extended_field_value = xml_text_to_value(value=get_xml_attr(elem, '{%s}Value' % TNS), value_type=python_type)
if python_type == string_types[0] and not extended_field_value:
# For string types, we want to return the empty string instead of None if the element was
# actually found, but there was no XML value. For other types, it would be more problematic
# to make that distinction, e.g. return False for bool, 0 for int, etc.
return ''
return extended_field_value
def from_xml(cls, elem, account):
# Gets value of this specific ExtendedProperty from a list of 'ExtendedProperty' XML elements
python_type = cls.python_type()
if cls.is_array_type():
values = elem.find('{%s}Values' % TNS)
if cls.is_binary_type():
return [safe_b64decode(val) for val in get_xml_attrs(values, '{%s}Value' % TNS)]
return [
xml_text_to_value(value=val, value_type=python_type)
for val in get_xml_attrs(values, '{%s}Value' % TNS)
]
if cls.is_binary_type():
return safe_b64decode(get_xml_attr(elem, '{%s}Value' % TNS))
extended_field_value = xml_text_to_value(value=get_xml_attr(elem, '{%s}Value' % TNS), value_type=python_type)
if python_type == string_types[0] and not extended_field_value:
# For string types, we want to return the empty string instead of None if the element was
# actually found, but there was no XML value. For other types, it would be more problematic
# to make that distinction, e.g. return False for bool, 0 for int, etc.
return ''
return extended_field_value
primary_smtp_address = get_xml_attr(user, '{%s}AutoDiscoverSMTPAddress' % RESPONSE_NS)
protocols = {get_xml_attr(p, '{%s}Type' % RESPONSE_NS): p for p in account.findall('{%s}Protocol' % RESPONSE_NS)}
# There are three possible protocol types: EXCH, EXPR and WEB.
# EXPR is meant for EWS. See
# https://techcommunity.microsoft.com/t5/Exchange-Team-Blog/The-Autodiscover-Service-and-Outlook-Providers-how-does-this/ba-p/584403
# We allow fallback to EXCH if EXPR is not available to support installations where EXPR is not available.
try:
protocol = protocols['EXPR']
except KeyError:
try:
protocol = protocols['EXCH']
except KeyError:
# Neither type was found. Give up
raise_from(AutoDiscoverFailed('Invalid AutoDiscover response: %s' % xml_to_str(autodiscover)), None)
ews_url = get_xml_attr(protocol, '{%s}EwsUrl' % RESPONSE_NS)
if not ews_url:
raise ValueError("Required element 'EwsUrl' not found in response")
log.debug('Primary SMTP: %s, EWS endpoint: %s', primary_smtp_address, ews_url)
return ews_url, primary_smtp_address
def _get_element_container(self, message, response_message=None, name=None):
if response_message is None:
response_message = message
# ResponseClass: See
# https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/finditemresponsemessage
response_class = response_message.get('ResponseClass')
# ResponseCode, MessageText: See
# https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/responsecode
response_code = get_xml_attr(response_message, '{%s}ResponseCode' % MNS)
msg_text = get_xml_attr(response_message, '{%s}MessageText' % MNS)
msg_xml = response_message.find('{%s}MessageXml' % MNS)
if response_class == 'Success' and response_code == 'NoError':
if not name:
return True
container = message.find(name)
if container is None:
raise MalformedResponseError('No %s elements in ResponseMessage (%s)' % (name, xml_to_str(message)))
return container
if response_code == 'NoError':
return True
# Raise any non-acceptable errors in the container, or return the container or the acceptable exception instance
if response_class == 'Warning':
try:
raise self._get_exception(code=response_code, text=msg_text, msg_xml=msg_xml)
except self.WARNINGS_TO_CATCH_IN_RESPONSE as e:
def _raise_soap_errors(cls, fault):
# Fault: See http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383507
faultcode = get_xml_attr(fault, 'faultcode')
faultstring = get_xml_attr(fault, 'faultstring')
faultactor = get_xml_attr(fault, 'faultactor')
detail = fault.find('detail')
if detail is not None:
code, msg = None, ''
if detail.find('{%s}ResponseCode' % ENS) is not None:
code = get_xml_attr(detail, '{%s}ResponseCode' % ENS)
if detail.find('{%s}Message' % ENS) is not None:
msg = get_xml_attr(detail, '{%s}Message' % ENS)
msg_xml = detail.find('{%s}MessageXml' % TNS) # Crazy. Here, it's in the TNS namespace
if code == 'ErrorServerBusy':
back_off = None
try:
value = msg_xml.find('{%s}Value' % TNS)
if value.get('Name') == 'BackOffMilliseconds':
back_off = int(value.text) / 1000.0 # Convert to seconds
except (TypeError, AttributeError):
def _parse_response(bytes_content):
# We could return lots more interesting things here
if not is_xml(bytes_content):
raise AutoDiscoverFailed('Unknown autodiscover response: %s' % bytes_content)
autodiscover = to_xml(bytes_content)
resp = autodiscover.find('{%s}Response' % RESPONSE_NS)
if resp is None:
_raise_response_errors(autodiscover)
account = resp.find('{%s}Account' % RESPONSE_NS)
action = get_xml_attr(account, '{%s}Action' % RESPONSE_NS)
redirect_email = get_xml_attr(account, '{%s}RedirectAddr' % RESPONSE_NS)
if action == 'redirectAddr' and redirect_email:
# This is redirection to e.g. Office365
raise AutoDiscoverRedirect(redirect_email)
# The AccountType element is only available if we are not redirecting
account_type = get_xml_attr(account, '{%s}AccountType' % RESPONSE_NS)
if account_type != 'email':
raise ValueError("'Unexpected AccountType '%s'" % account_type)
user = resp.find('{%s}User' % RESPONSE_NS)
# AutoDiscoverSMTPAddress might not be present in the XML, so primary_smtp_address might be None. In this
# case, the original email address IS the primary address
primary_smtp_address = get_xml_attr(user, '{%s}AutoDiscoverSMTPAddress' % RESPONSE_NS)
protocols = {get_xml_attr(p, '{%s}Type' % RESPONSE_NS): p for p in account.findall('{%s}Protocol' % RESPONSE_NS)}
# There are three possible protocol types: EXCH, EXPR and WEB.
# EXPR is meant for EWS. See
# https://techcommunity.microsoft.com/t5/Exchange-Team-Blog/The-Autodiscover-Service-and-Outlook-Providers-how-does-this/ba-p/584403
# We allow fallback to EXCH if EXPR is not available to support installations where EXPR is not available.