Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix authentication #375

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aiosmtpd/docs/auth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Activating Authentication
but attempts to authenticate will always be rejected
unless the :attr:`authenticator` parameter of :class:`~aiosmtpd.smtp.SMTP`
is set to a valid & working :ref:`authcallback`.
The :attr:`auth_required` parameter can be set to ``True`` to reject clients that don't authenticate.


AUTH API
Expand Down Expand Up @@ -146,6 +147,11 @@ Authenticator Callback

.. versionadded:: 1.3

.. important::

Setting an authenticator on your server or controller is not sufficient to require authentication.
In addition to rejecting incorrect credentials in your authenticator, you should set :attr:`auth_required` to ``True`` to reject clients that don't authenticate at all.

AuthResult API
--------------

Expand Down
19 changes: 10 additions & 9 deletions examples/authenticated_relayer/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ def __call__(self, server, session, envelope, mechanism, auth_data):
return fail_nothandled
if not isinstance(auth_data, LoginPassword):
return fail_nothandled
username = auth_data.login
try:
username = auth_data.login.decode('utf-8')
except UnicodeDecodeError:
return fail_nothandled
password = auth_data.password
hashpass = self.ph.hash(password)
conn = sqlite3.connect(self.auth_db)
curs = conn.execute(
"SELECT hashpass FROM userauth WHERE username=?", (username,)
Expand All @@ -41,7 +43,7 @@ def __call__(self, server, session, envelope, mechanism, auth_data):
conn.close()
if not hash_db:
return fail_nothandled
if hashpass != hash_db[0]:
if not self.ph.verify(hash_db[0], password):
return fail_nothandled
return AuthResult(success=True)

Expand Down Expand Up @@ -81,12 +83,11 @@ async def amain():
handler,
hostname='',
port=8025,
authenticator=Authenticator(DB_AUTH)
authenticator=Authenticator(DB_AUTH),
auth_required=True,
auth_require_tls=False,
)
try:
cont.start()
finally:
cont.stop()
cont.start()


if __name__ == '__main__':
Expand All @@ -96,7 +97,7 @@ async def amain():
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(amain())
loop.run_until_complete(loop.create_task(amain()))
try:
loop.run_forever()
except KeyboardInterrupt:
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def amain(loop):
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(amain(loop=loop))
loop.run_until_complete(loop.create_task(amain(loop=loop)))
try:
loop.run_forever()
except KeyboardInterrupt:
Expand Down