SMTP через Exchange с помощью Интегрированной аутентификации Windows (NTLM) использование Python

Я хочу использовать учетные данные зарегистрированного пользователя Windows для аутентификации соединения SMTP с Exchange Server с помощью NTLM.

Я знаю о модуле python-ntlm и два патчи, которые включают аутентификацию NTLM для SMTP, однако я хочу использовать маркер безопасности текущего пользователя и не иметь для предоставления имени пользователя и пароля.

Очень похожая проблема к аутентификации Windows с Python и urllib2.

12
задан Community 23 May 2017 в 12:32
поделиться

1 ответ

Хотя в приведенном ниже решении используются только расширения Python Win32 (очень полезен был пример кода sspi, включенный в расширения Python Win32), упомянутые в вопросе патчи IMAP и SMTP python-ntlm также служили как полезные руководства.

from smtplib import SMTPException, SMTPAuthenticationError
import string
import base64
import sspi

# NTLM Guide -- http://curl.haxx.se/rfc/ntlm.html

SMTP_EHLO_OKAY = 250
SMTP_AUTH_CHALLENGE = 334
SMTP_AUTH_OKAY = 235

def asbase64(msg):
    return string.replace(base64.encodestring(msg), '\n', '')

def connect_to_exchange_as_current_user(smtp):
    """Example:
    >>> import smtplib
    >>> smtp = smtplib.SMTP("my.smtp.server")
    >>> connect_to_exchange_as_current_user(smtp)
    """

    # Send the SMTP EHLO command
    code, response = smtp.ehlo()
    if code != SMTP_EHLO_OKAY:
        raise SMTPException("Server did not respond as expected to EHLO command")

    sspiclient = sspi.ClientAuth('NTLM')

    # Generate the NTLM Type 1 message
    sec_buffer=None
    err, sec_buffer = sspiclient.authorize(sec_buffer)
    ntlm_message = asbase64(sec_buffer[0].Buffer)

    # Send the NTLM Type 1 message -- Authentication Request
    code, response = smtp.docmd("AUTH", "NTLM " + ntlm_message)

    # Verify the NTLM Type 2 response -- Challenge Message
    if code != SMTP_AUTH_CHALLENGE:
        raise SMTPException("Server did not respond as expected to NTLM negotiate message")

    # Generate the NTLM Type 3 message
    err, sec_buffer = sspiclient.authorize(base64.decodestring(response))
    ntlm_message = asbase64(sec_buffer[0].Buffer)

    # Send the NTLM Type 3 message -- Response Message
    code, response = smtp.docmd("", ntlm_message)
    if code != SMTP_AUTH_OKAY:
        raise SMTPAuthenticationError(code, response)
14
ответ дан 2 December 2019 в 07:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: