flask_ldap3_login/__init__.py
def authenticate_direct_credentials(self, username, password):
        """
        Performs a direct bind, however using direct credentials. Can be used
        if interfacing with an Active Directory domain controller which
        authenticates using username@domain.com directly.

        Performing this kind of lookup limits the information we can get from
        ldap. Instead we can only deduce whether or not their bind was
        successful. Do not use this method if you require more user info.

        Args:
            username (str): Username for the user to bind with.
                            LDAP_BIND_DIRECT_PREFIX will be prepended and
                            LDAP_BIND_DIRECT_SUFFIX will be appended.
            password (str): User's password to bind with.

        Returns:
            AuthenticationResponse
        """

        bind_user = '{}{}{}'.format(
            self.config.get('LDAP_BIND_DIRECT_PREFIX'),
            username,
            self.config.get('LDAP_BIND_DIRECT_SUFFIX')
        )
        connection = self._make_connection(
            bind_user=bind_user,
            bind_password=password,
        )

        response = AuthenticationResponse()
        try:
            connection.bind()
            response.status = AuthenticationResponseStatus.success
            response.user_id = username
            log.debug(
                "Authentication was successful for user '{0}'".format(username))

            if self.config.get('LDAP_BIND_DIRECT_GET_USER_INFO'):
                # User wants extra info about the bind
                user_filter = '({search_attr}={username})'.format(
                    search_attr=self.config.get('LDAP_USER_LOGIN_ATTR'),
                    username=username
                )
                search_filter = '(&{0}{1})'.format(
                    self.config.get('LDAP_USER_OBJECT_FILTER'),
                    user_filter,
                )

                connection.search(
                    search_base=self.full_user_search_dn,
                    search_filter=search_filter,
                    search_scope=getattr(
                        ldap3, self.config.get('LDAP_USER_SEARCH_SCOPE')),
                    attributes=self.config.get('LDAP_GET_USER_ATTRIBUTES'),
                )

                if len(connection.response) == 0 or \
                        (self.config.get('LDAP_FAIL_AUTH_ON_MULTIPLE_FOUND') and
                         len(connection.response) > 1):
                    # Don't allow them to log in.
                    log.error(
                        "Could not gather extra info for user '{0}'".format(username))
                else:

                    user = connection.response[0]
                    user['attributes']['dn'] = user['dn']
                    response.user_info = user['attributes']
                    response.user_dn = user['dn']

        except ldap3.core.exceptions.LDAPInvalidCredentialsResult:
            log.debug(
                "Authentication was not successful for user '{0}'".format(username))
            response.status = AuthenticationResponseStatus.fail
        except Exception as e:
            log.error(e)
            response.status = AuthenticationResponseStatus.fail

        self.destroy_connection(connection)
        return response
def authenticate_direct_credentials(self, username, password):
        """
        Performs a direct bind, however using direct credentials. Can be used
        if interfacing with an Active Directory domain controller which
        authenticates using username@domain.com directly.

        Performing this kind of lookup limits the information we can get from
        ldap. Instead we can only deduce whether or not their bind was
        successful. Do not use this method if you require more user info.

        Args:
            username (str): Username for the user to bind with.
                            LDAP_BIND_DIRECT_PREFIX will be prepended and
                            LDAP_BIND_DIRECT_SUFFIX will be appended.
            password (str): User's password to bind with.

        Returns:
            AuthenticationResponse
        """

        bind_user = '{}{}{}'.format(
            self.config.get('LDAP_BIND_DIRECT_PREFIX'),
            username,
            self.config.get('LDAP_BIND_DIRECT_SUFFIX')
        )
        connection = self._make_connection(
            bind_user=bind_user,
            bind_password=password,
        )

        response = AuthenticationResponse()
        try:
            connection.bind()
            response.status = AuthenticationResponseStatus.success
            response.user_id = username
            log.debug(
                "Authentication was successful for user '{0}'".format(username))

            if self.config.get('LDAP_BIND_DIRECT_GET_USER_INFO'):
                # User wants extra info about the bind
                user_filter = '({search_attr}={username})'.format(
                    search_attr=self.config.get('LDAP_USER_LOGIN_ATTR'),
                    username=username
                )
                search_filter = '(&{0}{1})'.format(
                    self.config.get('LDAP_USER_OBJECT_FILTER'),
                    user_filter,
                )

                connection.search(
                    search_base=self.full_user_search_dn,
                    search_filter=search_filter,
                    search_scope=getattr(
                        ldap3, self.config.get('LDAP_USER_SEARCH_SCOPE')),
                    attributes=self.config.get('LDAP_GET_USER_ATTRIBUTES'),
                )

                if len(connection.response) == 0 or \
                        (self.config.get('LDAP_FAIL_AUTH_ON_MULTIPLE_FOUND') and
                         len(connection.response) > 1):
                    # Don't allow them to log in.
                    log.error(
                        "Could not gather extra info for user '{0}'".format(username))
                else:

                    user = connection.response[0]
                    user['attributes']['dn'] = user['dn']
                    response.user_info = user['attributes']
                    response.user_dn = user['dn']

        except ldap3.core.exceptions.LDAPInvalidCredentialsResult:
            log.debug(
                "Authentication was not successful for user '{0}'".format(username))
            response.status = AuthenticationResponseStatus.fail
        except Exception as e:
            log.error(e)
            response.status = AuthenticationResponseStatus.fail

        self.destroy_connection(connection)
        return response
Performs a direct bind, however using direct credentials. Can be used
        if interfacing with an Active Directory domain controller which
        authenticates using username@domain.com directly.

        Performing this kind of lookup limits the information we can get from
        ldap. Instead we can only deduce whether or not their bind was
        successful. Do not use this method if you require more user info.

        Args:
            username (str): Username for the user to bind with.
                            LDAP_BIND_DIRECT_PREFIX will be prepended and
                            LDAP_BIND_DIRECT_SUFFIX will be appended.
            password (str): User's password to bind with.

        Returns:
            AuthenticationResponse