From 9f2f38f009b2ee437b7dbba980e021a6b45a91b3 Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Mon, 6 Feb 2012 12:53:00 -0500 Subject: [PATCH] Add caching back into verify_id_token with a memory only cache. Reviewed in http://codereview.appspot.com/5585047/. --- oauth2client/client.py | 21 ++++++++++++++++++++- tests/test_oauth2client_jwt.py | 1 - 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/oauth2client/client.py b/oauth2client/client.py index cd03e38..2d60f5a 100644 --- a/oauth2client/client.py +++ b/oauth2client/client.py @@ -89,6 +89,22 @@ def _abstract(): raise NotImplementedError('You need to override this function') +class MemoryCache(object): + """httplib2 Cache implementation which only caches locally.""" + + def __init__(self): + self.cache = {} + + def get(self, key): + return self.cache.get(key) + + def set(self, key, value): + self.cache[key] = value + + def delete(self, key): + self.cache.pop(key, None) + + class Credentials(object): """Base class for all Credentials objects. @@ -705,6 +721,9 @@ if HAS_OPENSSL: Signer.from_string(self.private_key, self.private_key_password), payload) + # Only used in verify_id_token(), which is always calling to the same URI + # for the certs. + _cached_http = httplib2.Http(MemoryCache()) def verify_id_token(id_token, audience, http=None, cert_uri=ID_TOKEN_VERIFICATON_CERTS): @@ -725,7 +744,7 @@ if HAS_OPENSSL: oauth2client.crypt.AppIdentityError if the JWT fails to verify. """ if http is None: - http = httplib2.Http() + http = _cached_http resp, content = http.request(cert_uri) diff --git a/tests/test_oauth2client_jwt.py b/tests/test_oauth2client_jwt.py index 50dab5a..83551c6 100644 --- a/tests/test_oauth2client_jwt.py +++ b/tests/test_oauth2client_jwt.py @@ -115,7 +115,6 @@ class CryptTests(unittest.TestCase): self.assertEquals('billy bob', contents['user']) self.assertEquals('data', contents['metadata']['meta']) - def test_verify_id_token_with_certs_uri_fails(self): jwt = self._create_signed_jwt()