Skip to content

Commit

Permalink
service now saves Token not string in header to allow later retrieving
Browse files Browse the repository at this point in the history
(ie. by a batch callback function)
  • Loading branch information
czervenka committed Jul 1, 2013
1 parent b244676 commit 02545dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
15 changes: 9 additions & 6 deletions gapi/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

"""
Uses PyCrypto
OAuth 2.0 for Server to Server Applications
see https://developers.google.com/accounts/docs/OAuth2ServiceAccount
"""
from types import StringTypes
from time import time
Expand Down Expand Up @@ -45,7 +48,7 @@ def __init__(self, service_email, key, scope, impersonate_as=None, life_time=360
if isinstance(scope, (list, tuple)):
scope = ' '.join(scope)
self.scope = scope
self.impersonate_as = impersonate_as
self.email = impersonate_as
self.life_time = life_time

def get_claims(self):
Expand All @@ -57,13 +60,13 @@ def get_claims(self):
"iat": request_time,
"exp": request_time + self.life_time,
}
if self.impersonate_as is not None:
claims['prn'] = self.impersonate_as
if self.email is not None:
claims['prn'] = self.email
return claims

def _cache_key(self):
"""Key used to identify in cache"""
return 'token:%s:%s:%s' % (self.service_email, self.scope, self.impersonate_as)
return 'token:%s:%s:%s' % (self.service_email, self.scope, self.email)

def sign(self, message):
return '.'.join((message, google_base64_url_encode(sing_RS256(message, self.key))))
Expand Down Expand Up @@ -93,7 +96,7 @@ def get_token(self):
"""
token = self._cache_get()
if not token:
logging.debug('Fetching new token for %s/%s.' % (self.service_email, self.impersonate_as))
logging.debug('Fetching new token for %s/%s.' % (self.service_email, self.email))
result = fetch(
'https://accounts.google.com/o/oauth2/token',
method='POST',
Expand All @@ -107,7 +110,7 @@ def get_token(self):
except Exception, e:
pass
if error == 'invalid_grant':
raise InvalidGrantException(result, "Error getting token for %r (service: %r)" % (self.service_email, self.impersonate_as))
raise InvalidGrantException(result, "Error getting token for %r (service: %r)" % (self.service_email, self.email))
raise Exception(result.status_code, result.content) # TODO: custom exception
token = loads(result.content)
self._cache_set(token)
Expand Down
7 changes: 4 additions & 3 deletions gapi/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,21 @@ def _add_batch(self, callback, **kwargs):
self._batch_items[uuid] = kwargs

def fetch_batch(self):
if not self._batch_items:
return
from .multipart import build_multipart, parse_multipart
url = 'https://www.googleapis.com/batch'
method = 'POST'
headers, payload = build_multipart(self._batch_items)

responses = parse_multipart(fetch(url=url, method=method, headers=headers, payload=payload))
for uuid, response in responses.items():
item = self._batch_items[uuid]
item = self._batch_items.pop(uuid)
try:
response = self._parse_response(response, item['kwargs'])
except Exception, e:
response = e
item['__callback__'](response=response, request=item)
self._batch_items = {}

def fetch(self, url, method='GET', headers={}, payload=None, params={}):
headers = deepcopy(headers)
Expand All @@ -107,7 +108,7 @@ def fetch(self, url, method='GET', headers={}, payload=None, params={}):
url += '?' + urlencode(params)
if 'content-type' not in headers:
headers['content-type'] = 'application/json'
headers['Authorization'] = str(self._get_token())
headers['authorization'] = self._get_token()
if payload:
payload = dumps(payload)
if callback:
Expand Down

0 comments on commit 02545dd

Please sign in to comment.