def _test_before_login_check(self, username, password): ok, msg = self.test_config() if not ok: raise LDAPConfigurationError(msg) backend = LDAPAuthorizationBackend() ok, msg = backend.pre_check(username, password) if not ok: raise self.LDAPBeforeLoginCheckError(msg)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): ldap_username = serializer.validated_data["AUTH_LDAP_USER_NAME"] ldap_password = serializer.validated_data[ "AUTH_LDAP_USERNAME_PASSWORD"] local_user = User.objects.filter(username=ldap_username, source='local') if local_user: return Response( { "error": _('The current user [{}] is a local user and can not perform LDAP authentication login test!' ).format(ldap_username) }, status=401) users = LDAPAuthorizationBackend().authenticate( username=ldap_username, password=ldap_password) if users != None: return Response({ "msg": _("Match users %(name)s(%(username)s),Groups [%(groups)s]." ) % ({ 'name': users.name, 'username': users.username, 'groups': users.groups_display }) }) else: return Response( { "error": _("LDAP User {} Authentication Failed, Make sure the username or password is correct, or there are no find users" ).format(ldap_username) }, status=401) else: return Response({"error": serializer.errors}, status=401)
def _test_login_auth(username, password): backend = LDAPAuthorizationBackend() ldap_user = LDAPUser(backend, username=username.strip()) ldap_user._authenticate_user_dn(password)