From cccf50acae95c7d4bc06edf7753b67d2c5fa30b9 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 9 Nov 2021 09:38:14 -0500 Subject: [PATCH] Added method to ldap_auth.py --- Users/ldap_auth.py | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index ed2c116..50e1b73 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -4,11 +4,15 @@ from uuid import UUID from django.conf import settings from django.contrib import messages from django.contrib.auth.backends import BaseBackend +from django.db.models import Q from ldap3 import Connection, Server, ALL, ObjectDef, Reader, NTLM, Entry from ldap3.core.exceptions import LDAPBindError, LDAPSocketOpenError from .models import User +class LDAPAuthException(Exception): + pass + class LDAPAuthentication(BaseBackend): """ @@ -49,6 +53,11 @@ class LDAPAuthentication(BaseBackend): except IndexError: return "AM" + @staticmethod + def check_user_is_admin(ldap_user: Entry) -> bool: + # TODO: Implement + return False + def update_from_ldap(self, ldap_user: Entry, django_user: User) -> User: """ This function takes an ldap user and a django user and syncs their data @@ -90,6 +99,12 @@ class LDAPAuthentication(BaseBackend): new_user.save() return new_user + def get_all_users(self, conn: Connection) -> Reader: + user_obj = ObjectDef('user', conn) + reader = Reader(conn, user_obj, settings.LDAP_BASE_CONTEXT) + reader.search() + return reader + def get_ldap_user(self, conn: Connection, username: str) -> Optional[Entry]: """ This function gets an ldap user from the ldap server @@ -102,9 +117,7 @@ class LDAPAuthentication(BaseBackend): :rtype: User """ - user_obj = ObjectDef('user', conn) - reader = Reader(conn, user_obj, settings.LDAP_BASE_CONTEXT) - reader.search() + reader = self.get_all_users(conn) results = reader.match("msDS-PrincipalName", username) if len(results) == 1: return results[0] @@ -161,3 +174,24 @@ class LDAPAuthentication(BaseBackend): return User.objects.get(id=user_id) except User.DoesNotExist: return None + + def delete_old_users(self, username, password): + conn = Connection(self.server, user=f"{settings.LDAP_DOMAIN}\\{username}", + password=password, authentication=NTLM) + try: + if conn.bind(): + ldap_user = self.get_ldap_user(conn, conn.extend.standard.who_am_i()) + if self.check_user_is_admin(ldap_user): + to_check = User.objects.filter(is_superuser=False).filter(Q(password__startswith='!') | Q(password__isnull=True)) + ldap_users = self.get_all_users(conn) + for user in to_check: + if len(ldap_users.match("msDs-principalName", user.username)) == 0: + user.delete() + else: + raise LDAPAuthException("User is not admin") + else: + raise LDAPAuthException("Invalid Credentials") + except LDAPBindError: + raise LDAPAuthException("Can't connect to ActiveDirectory") + except LDAPSocketOpenError: + raise LDAPAuthException("Can't connect to ActiveDirectory") -- knotmirror 0.0.1