Skip to content

Commit

Permalink
login: bubble up LdbError to auth view
Browse files Browse the repository at this point in the history
Step two bubble it all the way upto the login view in views/auth.py and handle the message returned from the LdbError exception.
  • Loading branch information
robvdl committed Mar 9, 2024
1 parent cf65ecf commit 2153184
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
26 changes: 13 additions & 13 deletions src/sambal/security.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional

from ldb import LdbError
from pyramid.authentication import AuthTktCookieHelper
from pyramid.interfaces import ISecurityPolicy
from pyramid.security import forget, remember
Expand Down Expand Up @@ -40,18 +39,19 @@ def forget(self, request, **kwargs):
return self.authtkt.forget(request, **kwargs)


def login(request, username, password, host, realm):
"""Log into server and put credentials in session on success only."""
try:
samdb = connect_samdb(host, username, password, realm)
if user_sid := samdb.connecting_user_sid:
request.session["samba.username"] = username
request.session["samba.password"] = password
request.session["samba.host"] = host
request.session["samba.realm"] = realm
return remember(request, user_sid)
except LdbError:
pass
def login(request, host, username, password, realm):
"""Log into server and put credentials in session on success only.
:raises LdbError: If the login failed or host is incorrect
"""
samdb = connect_samdb(host, username, password, realm)

if user_sid := samdb.connecting_user_sid:
request.session["samba.username"] = username
request.session["samba.password"] = password
request.session["samba.host"] = host
request.session["samba.realm"] = realm
return remember(request, user_sid)


def logout(request):
Expand Down
14 changes: 10 additions & 4 deletions src/sambal/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ldb import LdbError
from pyramid.httpexceptions import HTTPFound
from pyramid.view import forbidden_view_config, view_config

Expand All @@ -21,10 +22,15 @@ def login(request):
host = form.host.data
realm = form.realm.data

if headers := request.login(username, password, host, realm):
return HTTPFound(location=return_url, headers=headers)
else:
request.session.flash("Login to host failed", queue="error")
try:
if headers := request.login(host, username, password, realm):
return HTTPFound(location=return_url, headers=headers)
else:
request.session.flash("Login failed", queue="error")

except LdbError as e:
msg = e.args[1]
request.session.flash(f"Login failed: {msg}", queue="error")
else:
form = LoginForm()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_login_invalid_credentials(testapp, settings):
}

response = testapp.post("/login/", login_form, status=200)
assert "Login to host failed" in response.text
assert "Login failed" in response.text


def test_login_required(testapp):
Expand Down

0 comments on commit 2153184

Please sign in to comment.