Commit 88e5dc50 authored by Alejandro Alvarez Ayllon's avatar Alejandro Alvarez Ayllon
Browse files

Added access_token to the REST clients

parent 533b09e9
......@@ -98,6 +98,9 @@ class Base(object):
help='the user certificate.', default=opt_ucert)
self.opt_parser.add_option('--insecure', dest='verify', default=True, action='store_false',
help='do not validate the server certificate')
self.opt_parser.add_option('--access-token', dest='access_token',
help='OAuth2 access token (supported only by some endpoints, takes precedence)',
default=None)
def __call__(self, argv=sys.argv[1:]):
(self.options, self.args) = self.opt_parser.parse_args(argv)
......@@ -123,5 +126,6 @@ class Base(object):
def _create_context(self):
return Context(
self.options.endpoint, ukey=self.options.ukey, ucert=self.options.ucert, verify=self.options.verify
self.options.endpoint, ukey=self.options.ukey, ucert=self.options.ucert, verify=self.options.verify,
access_token=self.options.access_token
)
......@@ -23,6 +23,7 @@ import json
import logging
import os
import sys
import urllib
from exceptions import *
from request import RequestFactory
......@@ -114,12 +115,22 @@ class Context(object):
raise BadEndpoint("%s (%s)" % (self.endpoint, str(e))), None, sys.exc_info()[2]
return endpoint_info
def __init__(self, endpoint, ucert=None, ukey=None, verify=True):
def __init__(self, endpoint, ucert=None, ukey=None, verify=True, access_token=None, no_creds=False):
self.passwd = None
self._set_endpoint(endpoint)
self._set_x509(ucert, ukey)
self._requester = RequestFactory(self.ucert, self.ukey, passwd=self.passwd, verify=verify)
if no_creds:
self.ucert = self.ukey = self.access_token = None
else:
self.access_token = access_token
if self.access_token:
self.ucert = None
self.ukey = None
else:
self._set_x509(ucert, ukey)
self._requester = RequestFactory(
self.ucert, self.ukey, passwd=self.passwd, verify=verify, access_token=self.access_token
)
self.endpoint_info = self._validate_endpoint()
# Log obtained information
log.debug("Using endpoint: %s" % self.endpoint_info['url'])
......@@ -130,7 +141,10 @@ class Context(object):
def get_endpoint_info(self):
return self.endpoint_info
def get(self, path):
def get(self, path, args=None):
if args:
query = '&'.join(map(lambda (k, v): "%s=%s" % (k, urllib.quote(v)), args.iteritems()))
path += '?' + query
return self._requester.method('GET',
"%s/%s" % (self.endpoint, path))
......
......@@ -23,10 +23,11 @@ from StringIO import StringIO
class RequestFactory(object):
def __init__(self, ucert, ukey, cafile=None, capath=None, passwd=None, verify=True):
def __init__(self, ucert, ukey, cafile=None, capath=None, passwd=None, verify=True, access_token=None):
self.ucert = ucert
self.ukey = ukey
self.passwd = passwd
self.access_token = access_token
self.verify = verify
......@@ -51,15 +52,15 @@ class RequestFactory(object):
if code == 400:
raise ClientError('Bad request: ' + message)
elif code >= 401 and code <= 403:
elif 401 <= code <= 403:
raise Unauthorized()
elif code == 404:
raise NotFound(url)
elif code == 419:
raise NeedDelegation()
raise NeedDelegation('Need delegation')
elif code == 424:
raise FailedDependency()
elif code > 404 and code < 500:
raise FailedDependency('Failed dependency')
elif 404 < code < 500:
raise ClientError(str(code))
elif code == 503:
raise TryAgain(str(code))
......@@ -70,8 +71,8 @@ class RequestFactory(object):
self._response += data
return len(data)
def _send(self, len):
return self._input.read(len)
def _send(self, length):
return self._input.read(length)
def _ioctl(self, cmd):
if cmd == pycurl.IOCMD_RESTARTREAD:
......@@ -88,8 +89,8 @@ class RequestFactory(object):
handle.setopt(pycurl.SSLKEY, self.ukey)
if self.capath:
handle.setopt(pycurl.CAPATH, self.capath)
if self.cafile:
handle.setopt(pycurl.CAINFO, self.cafile)
#if self.cafile:
#handle.setopt(pycurl.CAINFO, self.cafile)
if self.passwd:
handle.setopt(pycurl.SSLKEYPASSWD, self.passwd)
......@@ -103,10 +104,17 @@ class RequestFactory(object):
handle.setopt(pycurl.UPLOAD, True)
else:
handle.setopt(pycurl.CUSTOMREQUEST, method)
_headers = {}
if headers:
handle.setopt(pycurl.HTTPHEADER, map(lambda (k, v): "%s: %s" % (k, v), headers.iteritems()))
_headers.update(headers)
if self.access_token:
_headers['Authorization'] = 'Bearer ' + self.access_token
if len(_headers) > 0:
handle.setopt(pycurl.HTTPHEADER, map(lambda (k, v): "%s: %s" % (k, v), _headers.iteritems()))
handle.setopt(pycurl.URL, str(url))
handle.setopt(pycurl.VERBOSE, 1)
self._response = ''
handle.setopt(pycurl.WRITEFUNCTION, self._receive)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment