def authorize(self, action, consumer_key, resource_id): """Send an authorization request to the authz service.""" auth_url = "%s/authorize/%s/%s/%s/" % ( self.endpoint_url, consumer_key, self.service, resource_id) request = create_request( auth_url, method=action.upper(), user_agent=self.user_agent) try: response = urllib2.urlopen(request) except urllib2.HTTPError: return False else: return response.code == 202
def authenticate(self, method, url, body=None, headers=None): """Send an authentication request to the authz service. Returns the authenticated consumer as a dict or None if the authentication failed. """ auth_url = "%s/authenticate/%s" % (self.endpoint_url, quote_plus(url, safe='/')) request = create_request( auth_url, method=method.upper(), body=body, user_agent=self.user_agent, headers=headers) try: response = urllib2.urlopen(request) except urllib2.HTTPError: return None else: return json.loads(response.read())
def get_consumer_list(self): """ Query the Authz API to return a list of Consumer objects """ target_url = "%s/consumers/" % (self.base_url) request = create_request(target_url) try: response = urllib2.urlopen(request) except urllib2.HTTPError: return None raw_data = json.loads(response.read()) consumers = [] for rawc in raw_data['objects']: newc = Consumer(rawc['name'], rawc['key'], rawc['resource_uri'], rawc['secret'], rawc['policies']) consumers.append(newc) return consumers
def add_consumer(self, new_consumer): """ Add a Authz consumer. The only required argument is consumer name """ target_url = "%s/consumers/" % (self.base_url) post_body = json.dumps({ "name" : new_consumer.name }) request = create_request(target_url, method="POST", body=post_body, content_type='application/json') try: response = urllib2.urlopen(request) except urllib2.HTTPError: return None rawc = json.loads(response.read()) newc = Consumer(rawc['name'], rawc['key'], rawc['resource_uri'], rawc['secret'], rawc['policies']) return newc