Beispiel #1
0
    def get_authenticated_connection(self, server_id, request):
        """
        Prepares an authenticated Blitz connection wrapper (from
        L{omero.gateway}) for use with a view function.
        """
        # TODO: Handle previous try_super logic; is it still needed?

        userip = get_client_ip(request)
        session = request.session
        request = request.GET
        is_secure = settings.SECURE
        logger.debug("Is SSL? %s" % is_secure)
        connector = session.get("connector", None)
        logger.debug("Connector: %s" % connector)

        if server_id is None:
            # If no server id is passed, the db entry will not be used and
            # instead we'll depend on the request.session and request.GET
            # values
            if connector is not None:
                server_id = connector.server_id
            else:
                try:
                    server_id = request["server"]
                except Exception:
                    logger.debug("No Server ID available.")
                    return None

        # If we have an OMERO session key in our request variables attempt
        # to make a connection based on those credentials.
        try:
            omero_session_key = request["bsession"]
            connector = Connector(server_id, is_secure)
        except KeyError:
            # We do not have an OMERO session key in the current request.
            pass
        else:
            # We have an OMERO session key in the current request use it
            # to try join an existing connection / OMERO session.
            logger.debug("Have OMERO session key %s, attempting to join..." %
                         omero_session_key)
            connector.user_id = None
            connector.omero_session_key = omero_session_key
            connection = connector.join_connection(self.useragent, userip)
            session["connector"] = connector
            return connection

        # An OMERO session is not available, we're either trying to service
        # a request to a login page or an anonymous request.
        username = None
        password = None
        try:
            username = request["username"]
            password = request["password"]
        except KeyError:
            if connector is None:
                logger.debug("No username or password in request, exiting.")
                # We do not have an OMERO session or a username and password
                # in the current request and we do not have a valid connector.
                # Raise an error (return None).
                return None

        if username is not None and password is not None:
            # We have a username and password in the current request, or
            # OMERO.webpublic is enabled and has provided us with a username
            # and password via configureation. Use them to try and create a
            # new connection / OMERO session.
            logger.debug("Creating connection with username and password...")
            connector = Connector(server_id, is_secure)
            connection = connector.create_connection(self.useragent,
                                                     username,
                                                     password,
                                                     userip=userip)
            session["connector"] = connector
            return connection

        logger.debug("Django session connector: %r" % connector)
        if connector is not None:
            # We have a connector, attempt to use it to join an existing
            # connection / OMERO session.
            connection = connector.join_connection(self.useragent, userip)
            if connection is not None:
                logger.debug("Connector valid, session successfully joined.")
                return connection
            # Fall through, we the session we've been asked to join may
            # be invalid and we may have other credentials as request
            # variables.
            logger.debug("Connector is no longer valid, destroying...")
            del session["connector"]
            return None

        session["connector"] = connector
        return None
Beispiel #2
0
    def get_authenticated_connection(self, server_id, request):
        """
        Prepares an authenticated Blitz connection wrapper (from
        L{omero.gateway}) for use with a view function.
        """
        # TODO: Handle previous try_super logic; is it still needed?

        session = request.session
        request = request.REQUEST
        is_secure = request.get('ssl', False)
        logger.debug('Is SSL? %s' % is_secure)
        connector = session.get('connector', None)
        logger.debug('Connector: %s' % connector)

        if server_id is None:
            # If no server id is passed, the db entry will not be used and
            # instead we'll depend on the request.session and request.REQUEST
            # values
            if connector is not None:
                server_id = connector.server_id
            else:
                try:
                    server_id = request['server']
                except:
                    logger.debug('No Server ID available.')
                    return None

        # If we have an OMERO session key in our request variables attempt
        # to make a connection based on those credentials.
        try:
            omero_session_key = request['bsession']
            connector = Connector(server_id, is_secure)
        except KeyError:
            # We do not have an OMERO session key in the current request.
            pass
        else:
            # We have an OMERO session key in the current request use it
            # to try join an existing connection / OMERO session.
            logger.debug('Have OMERO session key %s, attempting to join...' % \
                    omero_session_key)
            connector.user_id = None
            connector.omero_session_key = omero_session_key
            connection = connector.join_connection(self.useragent)
            session['connector'] = connector
            return connection

        # An OMERO session is not available, we're either trying to service
        # a request to a login page or an anonymous request.
        username = None
        password = None
        try:
            username = request['username']
            password = request['password']
        except KeyError:
            if connector is None:
                logger.debug('No username or password in request, exiting.')
                # We do not have an OMERO session or a username and password
                # in the current request and we do not have a valid connector.
                # Raise an error (return None).
                return None

        if username is not None and password is not None:
            # We have a username and password in the current request, or
            # OMERO.webpublic is enabled and has provided us with a username
            # and password via configureation. Use them to try and create a
            # new connection / OMERO session.
            logger.debug('Creating connection with username and password...')
            connector = Connector(server_id, is_secure)
            connection = connector.create_connection(
                    self.useragent, username, password)
            session['connector'] = connector
            return connection

        logger.debug('Django session connector: %r' % connector)
        if connector is not None:
            # We have a connector, attempt to use it to join an existing
            # connection / OMERO session.
            connection = connector.join_connection(self.useragent)
            if connection is not None:
                logger.debug('Connector valid, session successfully joined.')
                return connection
            # Fall through, we the session we've been asked to join may
            # be invalid and we may have other credentials as request
            # variables.
            logger.debug('Connector is no longer valid, destroying...')
            del session['connector']
            return None

        session['connector'] = connector
        return connection