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.")
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))