Exemple #1
0
def logout(request):
    """
    :param request:
    :return:
    """

    from lib.middleware.element import ElementError, ElementXMLRPC
    auth = backend(request)

    try:
        element_session_id = request.get_cookie(settings.ELEMENT_SESSION_ID)
        if element_session_id:
            connection = ElementXMLRPC(url=settings.ELEMENT_URL, session_id=element_session_id)
            connection.logout()
        gen.Return(True)
    except KeyError:
        raise AuthenticationError("Session Timed Out")
    except ElementError as e:
        msg = "Error logging out Element session: {0}".format(str(e))
        raise AuthenticationError(msg)
    finally:
        auth.logout(request)

        request.clear_cookie(settings.ELEMENT_SESSION_ID)
        request.clear_cookie(settings.CONTEXT_COOKIE)
Exemple #2
0
def is_authenticated(request, user):
    """
    :param user:
    :return:
    """

    if user is None:
        return False

    from lib.middleware.element import ElementXMLRPC

    try:
        connection = ElementXMLRPC(url=settings.ELEMENT_URL)
        if connection.is_active(request.get_cookie(settings.ELEMENT_SESSION_ID)):
            if not backend.is_authenticated(request, user):
                logger.error("User '{0}' is not authenticated".format(user))
                return False
        else:
            logger.error("The element session for user '{0}'' is no longer active or valid".format(user))
            return False
    except Exception as e:
        logger.error("An error occured while authenticating user '{0}': {1}".format(user, e))
        return False

    return True
Exemple #3
0
def login(request, username, password):
    """
    :param request:
    :param username:
    :param password:
    :return:
    """

    auth = backend(request)

    if password is None or username is None:
        raise AuthenticationError("Username and password not provided")

    user = auth.login(request, username, password)
    if user is not None:

        # Log into Element
        try:
            connection = ElementXMLRPC(url=settings.ELEMENT_URL)
            element_session_id = connection.authenticate(username, password)
        except ElementError as e:
            msg = "Failed to authenticate against Element: {0}".format(str(e))
            raise AuthenticationError(msg)

        request.set_cookie(settings.ELEMENT_SESSION_ID, element_session_id)
        request.set_cookie(settings.CONTEXT_COOKIE, settings.DEFAULT_CONTEXT)

        auth.add_to_session(settings.ELEMENT_SESSION_ID, element_session_id)

        gen.Return(True)
    else:
        raise AuthenticationError("Login Failed. No such user exists.")
Exemple #4
0
    def get_authenticated_user(self, username, password):
        """
        :param username:
        :param password:
        :return:
        """

        import pam
        from models import User

        if self._current_user.is_authenticated:
            return self._current_user
        else:
            auth_engine = pam.pam()
            if auth_engine.authenticate(username, password):

                try:
                    connection = ElementXMLRPC(url=settings.ELEMENT_URL)
                    self._element_session_id = connection.authenticate(username, password)
                except ElementError as e:
                    raise AuthenticationError(str(e))

                # Until we have a backend, we might have to pickle and de-pickle this
                # When we do, store the object and return the username
                user = User(username, self._element_session_id)
                return user
            else:
                raise AuthenticationError("Login failed: [{0}] {1}".format(auth_engine.code, auth_engine.reason))