def test_auth(): # the Cadc() will cause a remote data call to TAP service capabilities # To avoid this, use an anonymous session and replace it with an # auth session later cadc = Cadc(auth_session=requests.Session()) cadc.cadctap._session = authsession.AuthSession() user = '******' password = '******' cert = 'cert' with pytest.raises(AttributeError): cadc.login(None, None, None) with pytest.raises(AttributeError): cadc.login(user=user) with pytest.raises(AttributeError): cadc.login(password=password) cadc.login(certificate_file=cert) assert cadc.cadctap._session.credentials.get( 'ivo://ivoa.net/sso#tls-with-certificate').cert == cert # reset and try with user password/cookies cadc.cadctap._session = authsession.AuthSession() post_mock = Mock() cookie = 'ABC' mock_resp = Mock() mock_resp.text = cookie post_mock.return_value.cookies = requests.cookies.RequestsCookieJar() post_mock.return_value = mock_resp cadc._request = post_mock cadc.login(user=user, password=password) assert cadc.cadctap._session.credentials.get( 'ivo://ivoa.net/sso#cookie').cookies[cadc_core.CADC_COOKIE_PREFIX] == \ '"{}"'.format(cookie)
def __init__(self, url=None, auth_session=None): """ Initialize Cadc object Parameters ---------- url : str, optional, default 'None; a url to use instead of the default auth_session: `requests.Session` or `pyvo.auth.authsession.AuthSession` A existing authenticated session containing the appropriate credentials to be used by the client to communicate with the server. This is an alternative to using login/logout methods that allows clients to reuse existing session with multiple services. Returns ------- Cadc object """ super().__init__() self.baseurl = url # _auth_session contains the credentials that are used by both # the cadc tap and cadc datalink services if auth_session: self._auth_session = auth_session else: self._auth_session = authsession.AuthSession()
def CEFCA_authenticate(login, password, login_url='https://archive.cefca.es/catalogues/login'): ''' Create an authentication session to pass to ``TAPService``. Example ------- auth = CEFCA_authenticate('*****@*****.**', 'p4$$w0rd') service = TAPService('https://archive.cefca.es/catalogues/vo/tap/minijpas-idr201910', auth) result = service.search('select * from minijpas.filter') print(result) ''' postdata = { 'login': login, 'password': password, 'submit': 'Sign+in', } headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain', } session = requests.Session() resp = session.post(login_url, data=postdata, headers=headers) resp.raise_for_status() auth = authsession.AuthSession() auth.credentials.set(securitymethods.ANONYMOUS, session) return auth
def test_login_with_user_password(self): for auth_session in [ None, authsession.AuthSession(), requests.Session() ]: cadc = Cadc(auth_session=auth_session) now = datetime.utcnow() query = \ "select top 1 * from caom2.Plane where metaRelease>'{}'".\ format(now.strftime('%Y-%m-%dT%H:%M:%S.%f')) result = cadc.exec_sync(query) assert len(result) == 0 cadc.login(os.environ['CADC_USER'], os.environ['CADC_PASSWD']) query = "select top 1 * from caom2.Plane where metaRelease>'{}'".\ format(now.strftime('%Y-%m-%dT%H:%M:%S.%f')) result = cadc.exec_sync(query) assert len(result) == 1 # repeat after logout cadc.logout() query = \ "select top 1 * from caom2.Plane where metaRelease>'{}'".\ format(now.strftime('%Y-%m-%dT%H:%M:%S.%f')) result = cadc.exec_sync(query) assert len(result) == 0 # login in again cadc.login(os.environ['CADC_USER'], os.environ['CADC_PASSWD']) query = "select top 1 * from caom2.Plane where metaRelease>'{}'". \ format(now.strftime('%Y-%m-%dT%H:%M:%S.%f')) result = cadc.exec_sync(query) assert len(result) == 1
def cadctap(self): if not self._auth_session: self._auth_session = authsession.AuthSession() if not hasattr(self, '_cadctap'): if self.baseurl is None: self.baseurl = get_access_url(self.CADCTAP_SERVICE_URI) # remove capabilities endpoint to get to the service url self.baseurl = self.baseurl.rstrip('capabilities') self._cadctap = pyvo.dal.TAPService(self.baseurl, session=self._auth_session) else: self._cadctap = pyvo.dal.TAPService(self.baseurl, session=self._auth_session) return self._cadctap
def logout(self, verbose=None): """ Logout. Anonymous access with all the subsequent use of the object. Note that the original session is not affected (in case it was passed when the object was first instantiated) Parameters ---------- verbose : deprecated """ if verbose is not None: warnings.warn('verbose deprecated since 0.4.0') # the only way to ensure complete logout is to start with a new # session. This is mainly because of certificates. Adding cert # argument to a session already in use does not force it to # re-do the HTTPS hand shake self.cadctap._session = authsession.AuthSession() self.cadctap._session.update_from_capabilities( self.cadctap.capabilities)
def __get_schema(self, service): try: self.joinable_dictionary = {} self.on_condition_dictionary = {} # if there is a cookie assigned, create a pyvo service with auth. if self.cookie != '': auth = authsession.AuthSession() auth.credentials.set_cookie('CADC_SSO', self.cookie) self.service = pyvo.dal.TAPService(service, auth) else: # else create an anonymous pyvo service. self.service = pyvo.dal.TAPService(service) table_query1 = "SELECT schema_name FROM tap_schema.schemas" table_query2 = """SELECT schema_name, table_name FROM tap_schema.tables""" table_query3 = """SELECT from_table, target_table, from_column, target_column FROM tap_schema.keys JOIN tap_schema.key_columns ON tap_schema.keys.key_id=tap_schema.key_columns.key_id""" schemas = self.service.search(table_query1) tables = self.service.search(table_query2) joinables = self.service.search(table_query3) tmp = schemas['schema_name'] schema_list = [x.decode() for x in list(tmp)] tmp = tables['schema_name'] table_schema_list = [x.decode() for x in list(tmp)] tmp = tables['table_name'] table_list = [x.decode() for x in list(tmp)] tmp = joinables['from_table'] from_table_list = [x.decode() for x in list(tmp)] tmp = joinables['target_table'] target_table_list = [x.decode() for x in list(tmp)] tmp = joinables['from_column'] from_column_list = [x.decode() for x in list(tmp)] tmp = joinables['target_column'] target_column_list = [x.decode() for x in list(tmp)] for idx in range(0, len(table_schema_list)): tmp = table_schema_list[idx] # populate schema_table_dictionary, # key:table name, value: schema name self.schema_table_dictionary[table_list[idx]] = tmp # build the on_condition_dictionary, # Example: # {'table_A to table_B': 'table_A.column_1=table_B.column_1', # 'table_B to table_A': 'table_B.column_1=table_A.column_1'} for idx in range(0, len(from_table_list)): f_t = from_table_list[idx] t_t = target_table_list[idx] f_c = from_column_list[idx] t_c = target_column_list[idx] r1 = f"{f_t} to {t_t}" r2 = f"{t_t} to {f_t}" on_condition1 = f"{f_t}.{f_c}={t_t}.{t_c}" on_condition2 = f"{t_t}.{t_c}={f_t}.{f_c}" if r1 not in self.on_condition_dictionary: self.on_condition_dictionary[r1] = on_condition1 if r2 not in self.on_condition_dictionary: self.on_condition_dictionary[r2] = on_condition2 # joinable_dictionary is the graph which be used in the BFS for table in table_list: self.joinable_dictionary[table] = [] for idx in range(0, len(from_table_list)): f_t = from_table_list[idx] t_t = target_table_list[idx] if t_t not in self.joinable_dictionary[f_t]: self.joinable_dictionary[f_t].append(t_t) self.joinable_dictionary[t_t].append(f_t) for key, value in self.joinable_dictionary.items(): for value_item in value: self.graph.add_edge(key, value_item) except Exception: print("Service not found") return # creating a schema dropdown widget. self.schema_dropdown = widgets.Dropdown(options=schema_list, description='SCHEMA', continuous_update=False, layout=widgets.Layout( left='-20px', width='780px')) # interactively calling __get_table. output_tables = widgets.interactive_output( self.__get_table, {'schema': self.schema_dropdown}) # display the widget and the interactive output display(self.schema_dropdown) display(output_tables)
import getpass import requests import pyvo from pyvo.auth import securitymethods, authsession # Gather login information data = { 'username': input('Username:'******'password': getpass.getpass('Password:'******'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain' } # Create a session and do the login. # The cookie will end up in the cookie jar of the session. login_url = 'http://gea.esac.esa.int/tap-server/login' session = requests.Session() response = session.post(login_url, data=data, headers=headers) response.raise_for_status() # Use this session with the auth cookie for all requests to Gaia. auth = authsession.AuthSession() auth.credentials.set(securitymethods.ANONYMOUS, session) service = pyvo.dal.TAPService('http://gea.esac.esa.int/tap-server/tap', auth) job = service.search('SELECT * from TAP_SCHEMA.tables') print(job)