Skip to content

Commit 1e28891

Browse files
committed
Add support for inline username with SMTP LOGIN (#32)
1 parent ef6b0fd commit 1e28891

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

emailproxy.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__author__ = 'Simon Robinson'
55
__copyright__ = 'Copyright (c) 2022 Simon Robinson'
66
__license__ = 'Apache 2.0'
7-
__version__ = '2022-06-09' # ISO 8601 (YYYY-MM-DD)
7+
__version__ = '2022-06-13' # ISO 8601 (YYYY-MM-DD)
88

99
import argparse
1010
import asyncore
@@ -772,17 +772,14 @@ def process_data(self, byte_data, censor_server_log=False):
772772
self.send_authentication_request()
773773

774774
elif self.authentication_state is self.AUTH.PENDING and str_data_lower.startswith('auth login'):
775-
self.authentication_state = self.AUTH.LOGIN_AWAITING_USERNAME
776-
self.send(b'334 %s\r\n' % base64.b64encode(b'Username:'))
775+
if len(str_data) > 11: # 11 = len('AUTH LOGIN ') - this method can have the username either inline...
776+
self.decode_username_and_request_password(str_data[11:])
777+
else: # ...or requested separately
778+
self.authentication_state = self.AUTH.LOGIN_AWAITING_USERNAME
779+
self.send(b'334 %s\r\n' % base64.b64encode(b'Username:'))
777780

778781
elif self.authentication_state is self.AUTH.LOGIN_AWAITING_USERNAME:
779-
try:
780-
self.server_connection.username = base64.b64decode(str_data).decode('utf-8')
781-
except binascii.Error:
782-
self.server_connection.username = ''
783-
self.authentication_state = self.AUTH.LOGIN_AWAITING_PASSWORD
784-
self.censor_next_log = True
785-
self.send(b'334 %s\r\n' % base64.b64encode(b'Password:'))
782+
self.decode_username_and_request_password(str_data)
786783

787784
elif self.authentication_state is self.AUTH.LOGIN_AWAITING_PASSWORD:
788785
try:
@@ -795,6 +792,15 @@ def process_data(self, byte_data, censor_server_log=False):
795792
else:
796793
super().process_data(byte_data)
797794

795+
def decode_username_and_request_password(self, encoded_username):
796+
try:
797+
self.server_connection.username = base64.b64decode(encoded_username).decode('utf-8')
798+
except binascii.Error:
799+
self.server_connection.username = ''
800+
self.authentication_state = self.AUTH.LOGIN_AWAITING_PASSWORD
801+
self.censor_next_log = True
802+
self.send(b'334 %s\r\n' % base64.b64encode(b'Password:'))
803+
798804
def send_authentication_request(self):
799805
self.authentication_state = self.AUTH.PENDING
800806
self.server_connection.authentication_state = SMTPOAuth2ServerConnection.AUTH.STARTED

0 commit comments

Comments
 (0)