diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py new file mode 100644 index 0000000..56561a0 --- /dev/null +++ b/Users/ldap_auth.py @@ -0,0 +1,38 @@ +# TODO: Error checking and showing it if we can't connect to ActiveDirectory + +from ldap3 import Connection, Server, ALL, ObjectDef, Reader, NTLM +from django.conf import settings +from django.auth.backends import BaseBackend + +from models import User + +class LDAPAuthentication(BaseBackend): + + server = Server(settings.LDAP_URL, get_info=ALL) + + def create_from_ldap(ldap_user) -> User: + new_user = User.objects.create_user(id=ldap_user.uid, username=ldap_user.name, first_name=ldap_user.givenName, last_name=ldap_user.sn, email=ldap_user.mail) + new_user.save() + return new_user + + def get_ldap_user(conn, ldap_id): + user_obj = ObjectDef('user', conn) + reader = Reader(conn, user_obj, f'name={ldap_id},{settings.LDAP_BASE_CONTEXT}') + reader.search() + if len(reader) == 1: + return reader[0] + else: + return None + + def authenticate(self, request, username=None, password=None): + with Connection(setup_server(), user=f'{settings.LDAP_DOMAIN}\\{username}', password=password, authentication=NTLM) as conn: + ldap_id = conn.extend.standard.who_am_i() + ldap_user = self.get_ldap_user(conn, ldap_id) + if ldap_user is None: + return None + else: + if User.objects.filter(id=ldap_user.uid).exists(): + return User.objects.get(id=ldap_user.uid) + else: + return self.create_from_ldap(ldap_user) + -- knotmirror 0.0.1 From 63f590e7aae2a6e6c83ec8743daeb47c3309e07b Mon Sep 17 00:00:00 2001 From: Ben C Date: Wed, 27 Oct 2021 13:04:57 -0400 Subject: [PATCH 2/8] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 15ab1f2..407addc 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ review, the instructor will get a final email with the notes the senior has give - DB_NAME: The name for the DB we'll use - DB_PASS: The password for the DB we'll use - LDAP_URL: The URL of the ActiveDirectory Server we want to use for auth -- LDAP_USER: The username of the LDAP admin we want to log in as -- LDAP_PASS: The password of the LDAP admin we want to log in as +- LDAP_DOMAIN: The domain to use when logging users in +- LDAP_BASE_CONTEXT: The base context to use when searching for users ### Services Used -- knotmirror 0.0.1 From 8da6d7c4851c65367caaedda4081db9a65670727 Mon Sep 17 00:00:00 2001 From: Ben C Date: Sat, 30 Oct 2021 11:59:50 -0400 Subject: [PATCH 3/8] Added help text for the users page --- Instructor/templates/user_list.html | 136 ++++++++++++++++------------ 1 file changed, 78 insertions(+), 58 deletions(-) diff --git a/Instructor/templates/user_list.html b/Instructor/templates/user_list.html index b2a2809..66c40e5 100644 --- a/Instructor/templates/user_list.html +++ b/Instructor/templates/user_list.html @@ -15,65 +15,85 @@ {% block mainContent %} {# TODO: Add the ability to sync this user list with ActiveDirectory #} -
- - {% csrf_token %} -
-

AM Session

-

PM Session

+
+
+
-
-
- - - - - - - - - {% for user in AM %} - - - - - {% endfor %} - -
NameReviewer
-
-
-
- - - - - - - - - {% for user in PM %} - - - - - {% endfor %} - -
NameReviewer
{{ user }}
-
+
+
-
-
- - -
+
+
+
+ + + {% csrf_token %} +
+

AM Session

+

PM Session

+
+
+
+ + + + + + + + + {% for user in AM %} + + + + + {% endfor %} + +
NameReviewer
+
+
+
+ + + + + + + + + {% for user in PM %} + + + + + {% endfor %} + +
NameReviewer
{{ user }}
+
+
+
+
+
+ Drag users in order to change their session. + Click the toggle switch next to a user to make them a reviewer. +
+
+
+
+ +
+
+
- +
{% endblock %} -- knotmirror 0.0.1 From 753b4adf62daa36c8a16700716f9cab5eb11e6c6 Mon Sep 17 00:00:00 2001 From: Ben C Date: Sat, 30 Oct 2021 12:03:50 -0400 Subject: [PATCH 4/8] Revert "Added help text for the users page" This reverts commit 8da6d7c4851c65367caaedda4081db9a65670727. --- Instructor/templates/user_list.html | 136 ++++++++++++---------------- 1 file changed, 58 insertions(+), 78 deletions(-) diff --git a/Instructor/templates/user_list.html b/Instructor/templates/user_list.html index 66c40e5..b2a2809 100644 --- a/Instructor/templates/user_list.html +++ b/Instructor/templates/user_list.html @@ -15,85 +15,65 @@ {% block mainContent %} {# TODO: Add the ability to sync this user list with ActiveDirectory #} -
-
- +
+ + {% csrf_token %} +
+

AM Session

+

PM Session

-
- +
+
+ + + + + + + + + {% for user in AM %} + + + + + {% endfor %} + +
NameReviewer
+
+
+
+ + + + + + + + + {% for user in PM %} + + + + + {% endfor %} + +
NameReviewer
{{ user }}
+
-
-
-
- - - {% csrf_token %} -
-

AM Session

-

PM Session

-
-
-
- - - - - - - - - {% for user in AM %} - - - - - {% endfor %} - -
NameReviewer
-
-
-
- - - - - - - - - {% for user in PM %} - - - - - {% endfor %} - -
NameReviewer
{{ user }}
-
-
-
-
-
- Drag users in order to change their session. - Click the toggle switch next to a user to make them a reviewer. -
-
-
-
- -
-
- +
+
+ + +
-
+ {% endblock %} -- knotmirror 0.0.1 From d857b7a48d775cf7d6833f8b6868acaee6a0449f Mon Sep 17 00:00:00 2001 From: Ben C Date: Sun, 31 Oct 2021 12:40:54 -0400 Subject: [PATCH 5/8] Made LDAP Auth Backend --- CodeReview/settings.py | 11 +++++ Users/ldap_auth.py | 101 ++++++++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 32 deletions(-) diff --git a/CodeReview/settings.py b/CodeReview/settings.py index 92523b8..11a3748 100644 --- a/CodeReview/settings.py +++ b/CodeReview/settings.py @@ -92,12 +92,23 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +AUTHENTICATION_BACKENDS = [ + 'Users.ldap_auth.LDAPAuthentication', + 'django.contrib.auth.backends.ModelBackend', +] + AUTH_USER_MODEL = "Users.User" LOGIN_URL = "/users/login" LOGIN_REDIRECT_URL = "/" LOGOUT_REDIRECT_URL = "/users/logout-done" +# LDAP AUTH + +LDAP_URL = os.getenv('LDAP_URL', "localhost") +LDAP_DOMAIN = os.getenv('LDAP_DOMAIN', None) +LDAP_BASE_CONTEXT = os.getenv('LDAP_BASE_CONTEXT', None) + if DEBUG: EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend' EMAIL_FILE_PATH = 'debug-emails' diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index 56561a0..df23496 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -1,38 +1,75 @@ -# TODO: Error checking and showing it if we can't connect to ActiveDirectory +from uuid import UUID -from ldap3 import Connection, Server, ALL, ObjectDef, Reader, NTLM from django.conf import settings -from django.auth.backends import BaseBackend +from django.contrib import messages +from django.contrib.auth.backends import BaseBackend +from ldap3 import Connection, Server, ALL, ObjectDef, Reader, NTLM, Entry +from ldap3.core.exceptions import LDAPBindError, LDAPSocketOpenError + +from .models import User -from models import User class LDAPAuthentication(BaseBackend): - - server = Server(settings.LDAP_URL, get_info=ALL) - - def create_from_ldap(ldap_user) -> User: - new_user = User.objects.create_user(id=ldap_user.uid, username=ldap_user.name, first_name=ldap_user.givenName, last_name=ldap_user.sn, email=ldap_user.mail) - new_user.save() - return new_user - - def get_ldap_user(conn, ldap_id): - user_obj = ObjectDef('user', conn) - reader = Reader(conn, user_obj, f'name={ldap_id},{settings.LDAP_BASE_CONTEXT}') - reader.search() - if len(reader) == 1: - return reader[0] - else: - return None - - def authenticate(self, request, username=None, password=None): - with Connection(setup_server(), user=f'{settings.LDAP_DOMAIN}\\{username}', password=password, authentication=NTLM) as conn: - ldap_id = conn.extend.standard.who_am_i() - ldap_user = self.get_ldap_user(conn, ldap_id) - if ldap_user is None: - return None - else: - if User.objects.filter(id=ldap_user.uid).exists(): - return User.objects.get(id=ldap_user.uid) + server = Server(settings.LDAP_URL, get_info=ALL) + + @staticmethod + def ldap_empty(value) -> str: + return value if str(value) != "[]" else "" + + def update_from_ldap(self, ldap_user: Entry, django_user: User) -> User: + django_user.username = ldap_user["msDS-PrincipalName"] + django_user.first_name = self.ldap_empty(ldap_user.givenName) + django_user.last_name = self.ldap_empty(ldap_user.sn) + django_user.email = self.ldap_empty(ldap_user.mail) + django_user.save() + return django_user + + def create_from_ldap(self, ldap_user: Entry, guid: str) -> User: + new_user = User.objects.create_user(id=UUID(guid), username=ldap_user["msDS-PrincipalName"], + first_name=self.ldap_empty(ldap_user.givenName), + last_name=self.ldap_empty(ldap_user.sn), + email=self.ldap_empty(ldap_user.mail)) + new_user.save() + return new_user + + def get_ldap_user(self, conn: Connection, username: str) -> Entry: + user_obj = ObjectDef('user', conn) + reader = Reader(conn, user_obj, settings.LDAP_BASE_CONTEXT) + reader.search() + results = reader.match("msDS-PrincipalName", username) + if len(results) == 1: + return results[0] else: - return self.create_from_ldap(ldap_user) - + return None + + def authenticate(self, request, username: str = None, password: str = None) -> User: + try: + conn = Connection(self.server, user=f'{settings.LDAP_DOMAIN}\\{username}', password=password, + authentication=NTLM) + success = conn.bind() + if success: + ldap_id = conn.extend.standard.who_am_i()[2:] + ldap_user = self.get_ldap_user(conn, ldap_id) + guid = str(ldap_user["objectGUID"]) + if ldap_user is None: + return None + else: + if User.objects.filter(id=UUID(guid)).exists(): + return self.update_from_ldap(ldap_user, User.objects.get(id=UUID(guid))) + else: + return self.create_from_ldap(ldap_user, guid) + else: + return None + except LDAPBindError: + return None + except LDAPSocketOpenError: + if not settings.DEBUG: + messages.add_message(request, messages.ERROR, + "There was an error contacting the auth server, please try again later.") + return None + + def get_user(self, user_id: str) -> User: + try: + return User.objects.get(id=user_id) + except User.DoesNotExist: + return None -- knotmirror 0.0.1 From 342456b271999604831506a13d7ea02858ccf0b3 Mon Sep 17 00:00:00 2001 From: bwc9876 Date: Mon, 1 Nov 2021 14:17:33 -0400 Subject: [PATCH 6/8] Fixed typing notation for ldap_auth.py --- Users/ldap_auth.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index df23496..14574aa 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -1,3 +1,4 @@ +from typing import Optional from uuid import UUID from django.conf import settings @@ -32,7 +33,7 @@ class LDAPAuthentication(BaseBackend): new_user.save() return new_user - def get_ldap_user(self, conn: Connection, username: str) -> Entry: + def get_ldap_user(self, conn: Connection, username: str) -> Optional[Entry]: user_obj = ObjectDef('user', conn) reader = Reader(conn, user_obj, settings.LDAP_BASE_CONTEXT) reader.search() @@ -42,7 +43,7 @@ class LDAPAuthentication(BaseBackend): else: return None - def authenticate(self, request, username: str = None, password: str = None) -> User: + def authenticate(self, request, username: str = None, password: str = None) -> Optional[User]: try: conn = Connection(self.server, user=f'{settings.LDAP_DOMAIN}\\{username}', password=password, authentication=NTLM) @@ -68,7 +69,7 @@ class LDAPAuthentication(BaseBackend): "There was an error contacting the auth server, please try again later.") return None - def get_user(self, user_id: str) -> User: + def get_user(self, user_id: str) -> Optional[User]: try: return User.objects.get(id=user_id) except User.DoesNotExist: -- knotmirror 0.0.1 From c0e562358ee05fc2ad339cc902156c065fd57026 Mon Sep 17 00:00:00 2001 From: Ben C Date: Mon, 1 Nov 2021 18:56:15 -0400 Subject: [PATCH 7/8] Added docstrings to backend --- CodeReview/settings.py | 3 + .../templates/widgets/rubric_create.html | 66 ++++++++++++++++++ Users/ldap_auth.py | 67 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 Instructor/templates/widgets/rubric_create.html diff --git a/CodeReview/settings.py b/CodeReview/settings.py index 11a3748..82f1e9a 100644 --- a/CodeReview/settings.py +++ b/CodeReview/settings.py @@ -105,8 +105,11 @@ LOGOUT_REDIRECT_URL = "/users/logout-done" # LDAP AUTH +# This setting denotes what url to use to access the LDAP server LDAP_URL = os.getenv('LDAP_URL', "localhost") +# This setting tells ldap what NetBIOS domain name to use when logging in a user LDAP_DOMAIN = os.getenv('LDAP_DOMAIN', None) +# This setting tells ldap what base search context to use when searching for a user LDAP_BASE_CONTEXT = os.getenv('LDAP_BASE_CONTEXT', None) if DEBUG: diff --git a/Instructor/templates/widgets/rubric_create.html b/Instructor/templates/widgets/rubric_create.html new file mode 100644 index 0000000..189538f --- /dev/null +++ b/Instructor/templates/widgets/rubric_create.html @@ -0,0 +1,66 @@ +{% comment %} + This field widget is used when creating or editting a Rubric +{% endcomment %} + + +
+ + + + + + + + {# This will be filled in through JS #} + + + + + +
CriteriaScore
+ +
+
+ +{# This div is used in JS as a template for creating rows and cells in the table #} +
+ + + + + + + + +
+
+ +
+ +
+
+ +
+
+
+ +
+
+ +
+ +
+
+ +
+
+
+
diff --git a/Users/ldap_auth.py b/Users/ldap_auth.py index 14574aa..bb064ca 100644 --- a/Users/ldap_auth.py +++ b/Users/ldap_auth.py @@ -11,13 +11,39 @@ from .models import User class LDAPAuthentication(BaseBackend): + """ + This authentication backend will login the user to ldap and then create a User in the database + with the same info. + + :cvar server: The server we're connecting to with LDAP (Must be an ActiveDirectory Server) + """ + server = Server(settings.LDAP_URL, get_info=ALL) @staticmethod def ldap_empty(value) -> str: + """ + This function will return an empty string if the given Entry's attribute is empty + + :param value: The value to check + :returns: The attribute as either an empty string or the attribute + :rtype: str + """ + return value if str(value) != "[]" else "" 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 + + :param ldap_user: The ldap user to sync from + :type ldap_user: Entry + :param django_user: The django user to sync to + :type django_user: User + :returns: The updated django user + :rtype: User + """ + django_user.username = ldap_user["msDS-PrincipalName"] django_user.first_name = self.ldap_empty(ldap_user.givenName) django_user.last_name = self.ldap_empty(ldap_user.sn) @@ -26,6 +52,17 @@ class LDAPAuthentication(BaseBackend): return django_user def create_from_ldap(self, ldap_user: Entry, guid: str) -> User: + """ + This function creates a django user from an ldap user + + :param ldap_user: The user to read from + :type ldap_user: Entry + :param guid: The objectGUID of the user + :type guid: str + :returns: The new django user + :rtype: User + """ + new_user = User.objects.create_user(id=UUID(guid), username=ldap_user["msDS-PrincipalName"], first_name=self.ldap_empty(ldap_user.givenName), last_name=self.ldap_empty(ldap_user.sn), @@ -34,6 +71,17 @@ class LDAPAuthentication(BaseBackend): return new_user def get_ldap_user(self, conn: Connection, username: str) -> Optional[Entry]: + """ + This function gets an ldap user from the ldap server + + :param conn: The connection for the server + :type conn: Connection + :param username: The username (MsDs-principalName) to check for + :type username: str + :returns: The user that matches the username, if any + :rtype: User + """ + user_obj = ObjectDef('user', conn) reader = Reader(conn, user_obj, settings.LDAP_BASE_CONTEXT) reader.search() @@ -44,6 +92,16 @@ class LDAPAuthentication(BaseBackend): return None def authenticate(self, request, username: str = None, password: str = None) -> Optional[User]: + """ + This is the function that django uses to authenticate a user + + :param request: The request that we're trying to authenticate + :param username: The username we're authenticating with + :param password: The password we're authenticating with + :returns: the user we wish to authenticate with the request, if any + :rtype: User + """ + try: conn = Connection(self.server, user=f'{settings.LDAP_DOMAIN}\\{username}', password=password, authentication=NTLM) @@ -70,6 +128,15 @@ class LDAPAuthentication(BaseBackend): return None def get_user(self, user_id: str) -> Optional[User]: + """ + This function gets a user given their id + + :param user_id: The id of the user + :type user_id: str + :returns: The User that has the id, if any + :rtype: User + """ + try: return User.objects.get(id=user_id) except User.DoesNotExist: -- knotmirror 0.0.1 From a542ca4e8e18680980fa5aa2886fea7084e8d530 Mon Sep 17 00:00:00 2001 From: Ben C Date: Mon, 1 Nov 2021 18:58:57 -0400 Subject: [PATCH 8/8] Added AUTHENTICATION_BACKENDS docstring --- CodeReview/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/CodeReview/settings.py b/CodeReview/settings.py index 12006ee..a2b7a1f 100644 --- a/CodeReview/settings.py +++ b/CodeReview/settings.py @@ -116,6 +116,7 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] +# This option defines what backends we use for authentication AUTHENTICATION_BACKENDS = [ 'Users.ldap_auth.LDAPAuthentication', 'django.contrib.auth.backends.ModelBackend',