Skip to content

Commit be73188

Browse files
committed
Fix auth_login logic (bpo-27820)
1 parent b4f9089 commit be73188

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Lib/smtplib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def __init__(self, host='', port=0, local_hostname=None,
248248
self.esmtp_features = {}
249249
self.command_encoding = 'ascii'
250250
self.source_address = source_address
251+
self._auth_challenge_count = 0
251252

252253
if host:
253254
(code, msg) = self.connect(host, port)
@@ -633,10 +634,13 @@ def auth(self, mechanism, authobject, *, initial_response_ok=True):
633634
if initial_response is not None:
634635
response = encode_base64(initial_response.encode('ascii'), eol='')
635636
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
637+
self._auth_challenge_count = 1
636638
else:
637639
(code, resp) = self.docmd("AUTH", mechanism)
640+
self._auth_challenge_count = 0
638641
# If server responds with a challenge, send the response.
639-
if code == 334:
642+
while code == 334:
643+
self._auth_challenge_count += 1
640644
challenge = base64.decodebytes(resp)
641645
response = encode_base64(
642646
authobject(challenge).encode('ascii'), eol='')
@@ -662,7 +666,7 @@ def auth_plain(self, challenge=None):
662666
def auth_login(self, challenge=None):
663667
""" Authobject to use with LOGIN authentication. Requires self.user and
664668
self.password to be set."""
665-
if challenge is None:
669+
if challenge is None or self._auth_challenge_count < 2:
666670
return self.user
667671
else:
668672
return self.password

0 commit comments

Comments
 (0)