def test_loginFromForm(self): params = { 'username': '******', 'password': self.root_password, 'server':self.server_id, 'ssl':'on' } request = fakeRequest(method="post", params=params) server_id = request.REQUEST.get('server') form = LoginForm(data=request.REQUEST.copy()) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] server_id = form.cleaned_data['server'] is_secure = toBoolean(form.cleaned_data['ssl']) connector = Connector(server_id, is_secure) conn = connector.create_connection('OMERO.web', username, password) if conn is None: self.fail('Cannot connect') conn.seppuku() if conn.isConnected() and conn.keepAlive(): self.fail('Connection was not closed') else: errors = form.errors.as_text() self.fail(errors)
def setUp (self): c = omero.client(pmap=['--Ice.Config='+(os.environ.get("ICE_CONFIG"))]) try: self.root_password = c.ic.getProperties().getProperty('omero.rootpass') omero_host = c.ic.getProperties().getProperty('omero.host') finally: c.__del__() blitz = Server.find(host=omero_host) if blitz is None: Server.reset() for s in settings.SERVER_LIST: server = (len(s) > 2) and unicode(s[2]) or None Server(host=unicode(s[0]), port=int(s[1]), server=server) Server.freeze() blitz = Server.find(server=omero_host) if blitz is not None: self.server_id = blitz.id connector = Connector(self.server_id, True) self.rootconn = connector.create_connection('TEST.webadmin', 'root', self.root_password) if self.rootconn is None or not self.rootconn.isConnected() or not self.rootconn.keepAlive(): raise exceptions.Exception("Cannot connect") else: raise exceptions.Exception("'%s' is not on omero.web.server_list" % omero_host)
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
def loginAsUser(self, username, password): blitz = Server.get(pk=self.server_id) if blitz is not None: connector = Connector(self.server_id, True) conn = connector.create_connection('TEST.webadmin', username, password) if conn is None or not conn.isConnected() or not conn.keepAlive(): raise exceptions.Exception("Cannot connect") return conn else: raise exceptions.Exception("'%s' is not on omero.web.server_list" % omero_host)
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
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, userip=get_client_ip(request)) 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
def test_loginFromRequest(self): params = { 'username': '******', 'password': self.root_password, 'server':self.server_id, 'ssl':'on' } request = fakeRequest(method="post", path="/webadmin/login", params=params) server_id = request.REQUEST.get('server') username = request.REQUEST.get('username') password = request.REQUEST.get('password') is_secure = toBoolean(request.REQUEST.get('ssl')) connector = Connector(server_id, is_secure) conn = connector.create_connection('TEST.webadmin', username, password) if conn is None: self.fail('Cannot connect') conn.seppuku() if conn.isConnected() and conn.keepAlive(): self.fail('Connection was not closed')
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')
def test_loginFailure(self): params = { 'username': '******', 'password': '******', 'server':self.server_id } request = fakeRequest(method="post", params=params) server_id = request.REQUEST.get('server') form = LoginForm(data=request.REQUEST.copy()) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] server_id = form.cleaned_data['server'] is_secure = toBoolean(form.cleaned_data['ssl']) connector = Connector(server_id, is_secure) conn = connector.create_connection('OMERO.web', username, password) if conn is not None: self.fail('This user does not exist. Login failure error!') else: errors = form.errors.as_text() self.fail(errors)
def getGuestConnection(server_id): return Connector(server_id, True).create_guest_connection("OMERO.web")
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
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
def test_version_compatible(self, versions): server = versions[0] client = versions[1] compatible = versions[2] assert Connector.is_compatible(server, client) == compatible