def get_sso_cookies(url, cert=None, **kwargs): """ Based on https://github.com/cerndb/cern-sso-python :param url: URL of the CERN website you want to access :param cert: (certificate, key) tuple :return: CERN SSO cookie """ verify = kwargs.pop("verify", None) ca_bundle = certs.where() if verify is None else verify with requests.Session() as session: session.cert = cert if cert else certs.default_user_certificate_paths() login_redirect_response = session.get(url, verify=ca_bundle) login_redirect_response.raise_for_status() redirect_url = login_redirect_response.url authentication_url = _construct_certificate_authentication_url(redirect_url) authentication_response = session.get(authentication_url, verify=ca_bundle) authentication_response.raise_for_status() action, form_data = _extract_login_form(authentication_response.content) session.post(url=action, data=form_data, verify=ca_bundle) return session.cookies
def check_certificates(): cert, key = certs.default_user_certificate_paths() if not os.path.isfile(cert): print("Error: {} does not exist".format(cert)) sys.exit() if not os.path.isfile(key): print("Error: {} does not exist".format(key)) sys.exit()
def _get_cookies(url, **kwargs): """ Gets the cookies required to query RR API :return: the cookies required to query Run Registry API. In particular 'connect.sid' is the one we are interested in """ if os.getenv('ENVIRONMENT') == 'development': return None cert = kwargs.pop('cert', None) # If no certificate provided, cernrequests will look in default paths: cert = cert if cert else certs.default_user_certificate_paths() if cert == ('', ''): raise Exception( 'No certificate passed, pass one in a tuple as cert=(cert,key), to authenticate your request. Or place them in your /private folder on AFS under the names of "usercert.pem" and "userkey.pem", please read authentication on README.md for more details' ) cookies = get_sso_cookies(url, cert) return cookies
def _get_cookies(url, **kwargs): """ Gets the cookies required to query RR API :return: the cookies required to query Run Registry API. In particular 'connect.sid' is the one we are interested in """ if os.getenv("ENVIRONMENT") == "development": return None cert = kwargs.pop("cert", None) # If no certificate provided, cernrequests will look in default paths: if cert == None and os.getenv("ENVIRONMENT") == "staging": cert = (staging_cert, staging_key) cert = cert if cert else certs.default_user_certificate_paths() if cert == ("", ""): raise Exception( 'No certificate passed, pass one in a tuple as cert=(cert,key), to authenticate your request. Or place them in your /private folder on AFS under the names of "usercert.pem" and "userkey.pem", please read authentication on README.md for more details' ) ca_bundle = certs.where() # Skip SSL verification since this must be fixed in the cernrequest package cookies = get_sso_cookies(url, cert, verify=True) return cookies
def test_default_user_certificate_paths(): cert, key = default_user_certificate_paths() assert "usercert.pem" in cert assert "userkey.pem" in key
def get(url, params=None, **kwargs): if "cert" not in kwargs: kwargs["cert"] = default_user_certificate_paths() if "verify" not in kwargs: kwargs["verify"] = where() return requests.get(url, params=params, **kwargs)