Beispiel #1
0
 def get_connection(self, server_id, request):
     """
     Prepares a Blitz connection wrapper (from L{omero.gateway}) for
     use with a view function.
     """
     connection = self.get_authenticated_connection(server_id, request)
     is_valid_public_url = self.is_valid_public_url(server_id, request)
     logger.debug('Is valid public URL? %s' % is_valid_public_url)
     if connection is None and is_valid_public_url:
         # If OMERO.webpublic is enabled, pick up a username and
         # password from configuration and use those credentials to
         # create a connection.
         logger.debug('OMERO.webpublic enabled, attempting to login '
                      'with configuration supplied credentials.')
         if server_id is None:
             server_id = settings.PUBLIC_SERVER_ID
         username = settings.PUBLIC_USER
         password = settings.PUBLIC_PASSWORD
         is_secure = settings.SECURE
         logger.debug('Is SSL? %s' % is_secure)
         # Try and use a cached OMERO.webpublic user session key.
         public_user_connector = self.get_public_user_connector()
         if public_user_connector is not None:
             logger.debug('Attempting to use cached OMERO.webpublic '
                          'connector: %r' % public_user_connector)
             connection = public_user_connector.join_connection(
                 self.useragent)
             if connection is not None:
                 request.session['connector'] = public_user_connector
                 logger.debug('Attempt to use cached OMERO.web public '
                              'session key successful!')
                 return connection
             logger.debug('Attempt to use cached OMERO.web public '
                          'session key failed.')
         # We don't have a cached OMERO.webpublic user session key,
         # create a new connection based on the credentials we've been
         # given.
         connector = Connector(server_id, is_secure)
         connection = connector.create_connection(
             self.useragent,
             username,
             password,
             is_public=True,
             userip=get_client_ip(request))
         request.session['connector'] = connector
         # Clear any previous context so we don't try to access this
         # NB: we also do this in WebclientLoginView.handle_logged_in()
         if 'active_group' in request.session:
             del request.session['active_group']
         if 'user_id' in request.session:
             del request.session['user_id']
         request.session.modified = True
         self.set_public_user_connector(connector)
     elif connection is not None:
         is_anonymous = connection.isAnonymous()
         logger.debug('Is anonymous? %s' % is_anonymous)
         if is_anonymous and not is_valid_public_url:
             return None
     return connection
Beispiel #2
0
 def get_connection(self, server_id, request):
     """
     Prepares a Blitz connection wrapper (from L{omero.gateway}) for
     use with a view function.
     """
     connection = self.get_authenticated_connection(server_id, request)
     is_valid_public_url = self.is_valid_public_url(server_id, request)
     logger.debug('Is valid public URL? %s' % is_valid_public_url)
     if connection is None and is_valid_public_url:
         # If OMERO.webpublic is enabled, pick up a username and
         # password from configuration and use those credentials to
         # create a connection.
         logger.debug('OMERO.webpublic enabled, attempting to login ' \
                      'with configuration supplied credentials.')
         if server_id is None:
             server_id = settings.PUBLIC_SERVER_ID
         username = settings.PUBLIC_USER
         password = settings.PUBLIC_PASSWORD
         is_secure = request.REQUEST.get('ssl', False)
         logger.debug('Is SSL? %s' % is_secure)
         # Try and use a cached OMERO.webpublic user session key.
         public_user_connector = self.get_public_user_connector()
         if public_user_connector is not None:
             logger.debug('Attempting to use cached OMERO.webpublic ' \
                          'connector: %r' % public_user_connector)
             connection = public_user_connector.join_connection(
                 self.useragent)
             if connection is not None:
                 request.session['connector'] = public_user_connector
                 logger.debug('Attempt to use cached OMERO.web public ' \
                              'session key successful!')
                 return connection
             logger.debug('Attempt to use cached OMERO.web public ' \
                          'session key failed.')
         # We don't have a cached OMERO.webpublic user session key,
         # create a new connection based on the credentials we've been
         # given.
         connector = Connector(server_id, is_secure)
         connection = connector.create_connection(self.useragent,
                                                  username,
                                                  password,
                                                  is_public=True)
         request.session['connector'] = connector
         self.set_public_user_connector(connector)
     elif connection is not None:
         is_anonymous = connection.isAnonymous()
         logger.debug('Is anonymous? %s' % is_anonymous)
         if is_anonymous and not is_valid_public_url:
             return None
     return connection
Beispiel #3
0
    def login_with_session(self, request, session):
        # Based on
        # https://github.com/openmicroscopy/openmicroscopy/blob/v5.4.10/components/tools/OmeroWeb/omeroweb/webgateway/views.py#L2943
        username = session
        password = session
        server_id = 1
        is_secure = settings.SECURE
        connector = Connector(server_id, is_secure)

        compatible = True
        if settings.CHECK_VERSION:
            compatible = connector.check_version(USERAGENT)
        if compatible:
            conn = connector.create_connection(USERAGENT,
                                               username,
                                               password,
                                               userip=get_client_ip(request))
            if conn is not None:
                try:
                    request.session['connector'] = connector
                    # UpgradeCheck URL should be loaded from the server or
                    # loaded omero.web.upgrades.url allows to customize web
                    # only
                    try:
                        upgrades_url = settings.UPGRADES_URL
                    except AttributeError:
                        upgrades_url = conn.getUpgradesUrl()
                    upgradeCheck(url=upgrades_url)
                    # super.handle_logged_in does some connection preparation
                    # as wel as redirecting to the main page. We just want
                    # the preparation, so discard the response
                    self.handle_logged_in(request, conn, connector)
                    return HttpResponseRedirect(reverse('oauth_confirm'))
                finally:
                    conn.close(hard=False)

            raise Exception('Failed to login with session %s', session)
        raise Exception('Incompatible server')
Beispiel #4
0
 def getGuestConnection(server_id):
     return Connector(server_id, True).create_guest_connection("OMERO.web")
Beispiel #5
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