Skip to content

Commit

Permalink
sources/ldap: check if we need to connect to ldap before connecting
Browse files Browse the repository at this point in the history
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
  • Loading branch information
BeryJu committed Oct 16, 2023
1 parent 2664a1d commit d37732d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions authentik/sources/ldap/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def __init__(self, source: LDAPSource) -> None:
self._source = source
self._connection = source.connection()

@staticmethod
def should_check_user(user: User) -> bool:
"""Check if the user has LDAP parameters and needs to be checked"""
return LDAP_DISTINGUISHED_NAME in user.attributes

def get_domain_root_dn(self) -> str:
"""Attempt to get root DN via MS specific fields or generic LDAP fields"""
info = self._connection.server.info
Expand Down
9 changes: 6 additions & 3 deletions authentik/sources/ldap/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ def ldap_password_validate(sender, password: str, plan_context: dict[str, Any],
if not sources.exists():
return
source = sources.first()
user = plan_context.get(PLAN_CONTEXT_PENDING_USER, None)
if user and not LDAPPasswordChanger.should_check_user(user):
return

Check warning on line 46 in authentik/sources/ldap/signals.py

View check run for this annotation

Codecov / codecov/patch

authentik/sources/ldap/signals.py#L44-L46

Added lines #L44 - L46 were not covered by tests
changer = LDAPPasswordChanger(source)
if changer.check_ad_password_complexity_enabled():
passing = changer.ad_password_complexity(
password, plan_context.get(PLAN_CONTEXT_PENDING_USER, None)
)
passing = changer.ad_password_complexity(password, user)

Check warning on line 49 in authentik/sources/ldap/signals.py

View check run for this annotation

Codecov / codecov/patch

authentik/sources/ldap/signals.py#L49

Added line #L49 was not covered by tests
if not passing:
raise ValidationError(_("Password does not match Active Directory Complexity."))

Expand All @@ -57,6 +58,8 @@ def ldap_sync_password(sender, user: User, password: str, **_):
if not sources.exists():
return
source = sources.first()
if not LDAPPasswordChanger.should_check_user(user):
return
try:
changer = LDAPPasswordChanger(source)
changer.change_password(user, password)
Expand Down

0 comments on commit d37732d

Please sign in to comment.