Example #1
0
 def test_parse_auth_login_with_username(self):
     self.parser = self.init_command_parser(
         authenticator=DummyAuthenticator())
     self.send('EHLO foo\r\n')
     self.send('AUTH LOGIN %s\r\n' % b64encode('foo'))
     self.send('%s\r\n' % b64encode('secret'))
     self.send('MAIL FROM: [email protected]\r\n')
Example #2
0
    def test_send_email_with_auth_login(self):
        """Check that we can send an email with using AUTH LOGIN."""
        self.connection.ehlo()
        login_response = self.connection.docmd('AUTH', 'LOGIN %s' % b64encode('foo'))
        assert_equals((334, b(b64encode('Password:'******'foo'))
        assert_equals((235, b('Authentication successful')), password_response)

        recipient = '*****@*****.**'
        self.connection.sendmail('*****@*****.**', recipient, rfc822_msg)
        self.connection.quit()
        msg = self._check_received_mail([recipient])
        assert_equals('foo', msg.username)
Example #3
0
    def test_auth_plain_with_bad_credentials_is_rejected(self):
        self.session._authenticator = DummyAuthenticator()

        self.send('EHLO', 'foo.example.com')
        base64_credentials = b64encode('\x00foo\x00bar')
        self.send('AUTH PLAIN', base64_credentials, expected_first_digit=5)
        self._check_last_code(535)
Example #4
0
    def test_auth_plain_with_bad_format_is_rejected(self):
        self.session._authenticator = DummyAuthenticator()

        self.send('EHLO', 'foo.example.com')
        base64_credentials = b64encode('\x00foo')
        self.send('AUTH PLAIN', base64_credentials, expected_first_digit=5)
        assert_length(3, self.command_parser.replies)
        self._check_last_code(501)
Example #5
0
    def test_auth_plain_with_username_and_password_is_accepted(self):
        self.session._authenticator = DummyAuthenticator()

        self.send('EHLO', 'foo.example.com')
        self.send('AUTH PLAIN', b64encode('\x00foo\x00foo'))
        assert_length(3, self.command_parser.replies)
        self._check_last_code(235)
        code, reply_text = self.last_reply()
        assert_equals('Authentication successful', reply_text)
Example #6
0
    def test_auth_plain_can_be_rejected(self):
        class FalsePolicy(IMTAPolicy):
            def accept_auth_plain(self, username, password, message):
                return False

        self.init(policy=FalsePolicy(), authenticator=DummyAuthenticator())
        self.send('EHLO', 'foo.example.com')
        base64_credentials = b64encode('\x00foo\x00foo')
        self.send('AUTH PLAIN', base64_credentials, expected_first_digit=5)
Example #7
0
 def send_auth_login(self,
                     username=None,
                     username_b64=None,
                     password=None,
                     password_b64=None,
                     **kwargs):
     assert (username is not None) ^ (username_b64 is not None)
     if username_b64 is None:
         username_b64 = b64encode(username)
         expect_username_error = False
     else:
         expect_username_error = True
     assert (password is not None) ^ (password_b64 is not None)
     if password_b64 is None:
         password_b64 = b64encode(password)
     return super(BasicMessageSendingTest, self).send_auth_login(
         username_b64=username_b64,
         password_b64=password_b64,
         expect_username_error=expect_username_error,
         **kwargs)
Example #8
0
    def test_auth_plain_with_authzid_username_and_password_is_accepted(self):
        self.session._authenticator = DummyAuthenticator()

        self.send('EHLO', 'foo.example.com')
        # RFC 4616 defines SASL PLAIN in the form
        # [authzid] \x00 authcid \x00 passwd
        # smtplib in Python 2.3 will send an additional authzid (which is equal
        # to authcid)
        self.send('AUTH PLAIN', b64encode('ignored\x00foo\x00foo'))
        assert_length(3, self.command_parser.replies)
        self._check_last_code(235)
        code, reply_text = self.last_reply()
        assert_equals('Authentication successful', reply_text)
Example #9
0
    def handle_auth_credentials(self, input_):
        # necessary so "self.validate()" works, usually done via ".handle_input()"
        self._command_arguments = input_
        validated_parameters = self.validate(AuthLoginSchema)
        decoded_input = validated_parameters['username']

        username = self._message.unvalidated_input.get('username')
        if username is None:
            self._message.unvalidated_input['username'] = decoded_input
            next_ = 'Password:'******'username']
            self._command_parser.switch_to_command_mode()
            self._check_password(username, password)
Example #10
0
 def smtp_auth_login(self):
     validated_data = self.validate(
         AuthLoginSchema) if self.arguments() else {}
     username = validated_data.get('username')
     decision, response_sent = self.is_allowed('accept_auth_login',
                                               username, self._message)
     if not decision:
         raise PolicyDenial(response_sent)
     elif not response_sent:
         if not username:
             next_ = 'Username:'******'username'] = username
             next_ = 'Password:'
         self._command_parser.switch_to_auth_login_mode()
         self.reply(334, b64encode(next_))
Example #11
0
 def test_rejects_invalid_format(self):
     e = self.assert_bad_input(b64encode('foobar'))
     assert_equals('Garbled data sent', e.msg())
Example #12
0
 def base64(self, value):
     return b64encode(value).strip()