authentik.sources.ldap.sync.vendor.freeipa
FreeIPA specific
1"""FreeIPA specific""" 2 3from collections.abc import Generator 4from datetime import UTC, datetime 5from typing import Any 6 7from authentik.core.models import User 8from authentik.sources.ldap.models import flatten 9from authentik.sources.ldap.sync.base import BaseLDAPSynchronizer 10from authentik.stages.password.models import PasswordDevice 11 12 13class FreeIPA(BaseLDAPSynchronizer): 14 """FreeIPA-specific LDAP""" 15 16 @staticmethod 17 def name() -> str: 18 return "freeipa" 19 20 def get_objects(self, **kwargs) -> Generator: 21 yield None 22 23 def sync(self, attributes: dict[str, Any], user: User, created: bool): 24 self.check_pwd_last_set(attributes, user, created) 25 26 def check_pwd_last_set(self, attributes: dict[str, Any], user: User, created: bool): 27 """Check krbLastPwdChange""" 28 if "krbLastPwdChange" not in attributes: 29 return 30 pwd_last_set: datetime = flatten(attributes.get("krbLastPwdChange", datetime.now())) 31 pwd_last_set = pwd_last_set.replace(tzinfo=UTC) 32 if created or pwd_last_set > user.password_change_date: 33 self._task.info(f"'{user.username}': Reset user's password") 34 self._logger.debug( 35 "Reset user's password", 36 user=user.username, 37 created=created, 38 pwd_last_set=pwd_last_set, 39 ) 40 # Record the directory's change date, or every later sync resets the password 41 # again, and each new unusable hash ends the user's sessions. 42 PasswordDevice.set_unusable_password(user, password_change_date=pwd_last_set) 43 44 def get_account_active(self, attributes: dict[str, Any]) -> bool | None: 45 """https://www.port389.org/docs/389ds/howto/howto-account-inactivation.html""" 46 # This is more of a 389-ds quirk rather than FreeIPA, but FreeIPA uses 47 # 389-ds and this will trigger regardless 48 if "nsaccountlock" not in attributes: 49 return 50 # For some reason, nsaccountlock is not defined properly in the schema as bool 51 # hence we get it as a list of strings 52 _is_locked = str(flatten(attributes.get("nsaccountlock", ["FALSE"]))) 53 # So we have to attempt to convert it to a bool 54 is_locked = _is_locked.lower() == "true" 55 # And then invert it since freeipa saves locked and we save active 56 return not is_locked
14class FreeIPA(BaseLDAPSynchronizer): 15 """FreeIPA-specific LDAP""" 16 17 @staticmethod 18 def name() -> str: 19 return "freeipa" 20 21 def get_objects(self, **kwargs) -> Generator: 22 yield None 23 24 def sync(self, attributes: dict[str, Any], user: User, created: bool): 25 self.check_pwd_last_set(attributes, user, created) 26 27 def check_pwd_last_set(self, attributes: dict[str, Any], user: User, created: bool): 28 """Check krbLastPwdChange""" 29 if "krbLastPwdChange" not in attributes: 30 return 31 pwd_last_set: datetime = flatten(attributes.get("krbLastPwdChange", datetime.now())) 32 pwd_last_set = pwd_last_set.replace(tzinfo=UTC) 33 if created or pwd_last_set > user.password_change_date: 34 self._task.info(f"'{user.username}': Reset user's password") 35 self._logger.debug( 36 "Reset user's password", 37 user=user.username, 38 created=created, 39 pwd_last_set=pwd_last_set, 40 ) 41 # Record the directory's change date, or every later sync resets the password 42 # again, and each new unusable hash ends the user's sessions. 43 PasswordDevice.set_unusable_password(user, password_change_date=pwd_last_set) 44 45 def get_account_active(self, attributes: dict[str, Any]) -> bool | None: 46 """https://www.port389.org/docs/389ds/howto/howto-account-inactivation.html""" 47 # This is more of a 389-ds quirk rather than FreeIPA, but FreeIPA uses 48 # 389-ds and this will trigger regardless 49 if "nsaccountlock" not in attributes: 50 return 51 # For some reason, nsaccountlock is not defined properly in the schema as bool 52 # hence we get it as a list of strings 53 _is_locked = str(flatten(attributes.get("nsaccountlock", ["FALSE"]))) 54 # So we have to attempt to convert it to a bool 55 is_locked = _is_locked.lower() == "true" 56 # And then invert it since freeipa saves locked and we save active 57 return not is_locked
FreeIPA-specific LDAP
def
sync( self, attributes: dict[str, typing.Any], user: authentik.core.models.User, created: bool):
24 def sync(self, attributes: dict[str, Any], user: User, created: bool): 25 self.check_pwd_last_set(attributes, user, created)
Sync function, implemented in subclass
def
check_pwd_last_set( self, attributes: dict[str, typing.Any], user: authentik.core.models.User, created: bool):
27 def check_pwd_last_set(self, attributes: dict[str, Any], user: User, created: bool): 28 """Check krbLastPwdChange""" 29 if "krbLastPwdChange" not in attributes: 30 return 31 pwd_last_set: datetime = flatten(attributes.get("krbLastPwdChange", datetime.now())) 32 pwd_last_set = pwd_last_set.replace(tzinfo=UTC) 33 if created or pwd_last_set > user.password_change_date: 34 self._task.info(f"'{user.username}': Reset user's password") 35 self._logger.debug( 36 "Reset user's password", 37 user=user.username, 38 created=created, 39 pwd_last_set=pwd_last_set, 40 ) 41 # Record the directory's change date, or every later sync resets the password 42 # again, and each new unusable hash ends the user's sessions. 43 PasswordDevice.set_unusable_password(user, password_change_date=pwd_last_set)
Check krbLastPwdChange
def
get_account_active(self, attributes: dict[str, typing.Any]) -> bool | None:
45 def get_account_active(self, attributes: dict[str, Any]) -> bool | None: 46 """https://www.port389.org/docs/389ds/howto/howto-account-inactivation.html""" 47 # This is more of a 389-ds quirk rather than FreeIPA, but FreeIPA uses 48 # 389-ds and this will trigger regardless 49 if "nsaccountlock" not in attributes: 50 return 51 # For some reason, nsaccountlock is not defined properly in the schema as bool 52 # hence we get it as a list of strings 53 _is_locked = str(flatten(attributes.get("nsaccountlock", ["FALSE"]))) 54 # So we have to attempt to convert it to a bool 55 is_locked = _is_locked.lower() == "true" 56 # And then invert it since freeipa saves locked and we save active 57 return not is_locked