def connect(self): """ Create an HTTPS connection to the server. Run automatically by request methods. """ kwargs = dict(ca_certs=self.server_ca_file, strict=False, timeout=self.timeout) if self.cert_file: kwargs["cert_file"] = self.cert_file kwargs["key_file"] = self.key_file self.c = VerifiedHTTPSConnection(self.host, self.port, **kwargs) self.c.set_debuglevel(self.httplib_debuglevel)
def get_access_token(username=None, password=None, ca_certs=None): """Get a goauth access token from nexus. Uses basic auth with the user's username and password to authenticate to nexus. If the username or password are not passed, they are prompted for on stdin. @param username: Globus Online username to authenticate as, or None to prompt on stdin. @param password: Globus Online password to authenticate with, or None to prompt on stdin (with echo disabled for security). @param ca_certs: Path to a ca certificate to verify nexus, or None to use the default CA included in the package. @return: GOAuthResult object. Most applications will only care about the token field, but username/password may be useful for caching authentication information when using the prompt. """ if ca_certs is None: from globusonline.transfer.api_client import get_ca ca_certs = get_ca(HOST) if username is None: print "Globus Online Username: "******"Globus Online Password: "******"%s:%s" % (username, password)) headers = { "Content-type": "application/json; charset=UTF-8", "Hostname": HOST, "Accept": "application/json; charset=UTF-8", "Authorization": "Basic %s" % basic_auth } c = VerifiedHTTPSConnection(HOST, PORT, ca_certs=ca_certs) c.request("GET", GOAUTH_PATH, headers=headers) response = c.getresponse() if response.status == 403: raise GOCredentialsError() elif response.status > 299 or response.status < 200: raise GOAuthError("error response: %d %s" % (response.status, response.reason)) data = json.loads(response.read()) token = data.get("access_token") if token is None: raise GOAuthError("no token in response") return GOAuthResult(username, password, token)
def get_go_auth(ca_certs, username=None, password=None): """ POST the login form to www.globusonline.org to get the cookie, prompting for username and password on stdin if they were not passed as parameters. @return: a GOAuthResult instance. The cookie is what most clients will be interested in, but if the username is not passed as a parameter the caller may need that as well, and may want to cache the password. """ if username is None: print "GO Username: "******"GO Password: "******"Content-type": "application/x-www-form-urlencoded", "Hostname": HOST } c = VerifiedHTTPSConnection(HOST, PORT, ca_certs=ca_certs) body = urllib.urlencode(dict(username=username, password=password)) c.request("POST", PATH, body=body, headers=headers) response = c.getresponse() set_cookie_header = response.getheader("set-cookie") if not set_cookie_header: # TODO: more appropriate exc type raise ValueError("No cookies received") cookies = BaseCookie(set_cookie_header) morsel = cookies.get("saml") if not morsel: raise ValueError("No saml cookie received") return GOAuthResult(username, password, morsel.coded_value)