From 3a83a644c297b0aaf2fb240828e30459f92717ee Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Thu, 12 Feb 2015 14:36:40 +0100
Subject: [PATCH] remove cache on metadata view (#6487)

---
 authentic2/idp/saml/app_settings.py    |  1 -
 authentic2/idp/saml/saml2_endpoints.py |  4 +--
 authentic2/utils.py                    | 61 ----------------------------------
 3 files changed, 1 insertion(+), 65 deletions(-)

diff --git a/authentic2/idp/saml/app_settings.py b/authentic2/idp/saml/app_settings.py
index db9795d..ed28c57 100644
--- a/authentic2/idp/saml/app_settings.py
+++ b/authentic2/idp/saml/app_settings.py
@@ -1,7 +1,6 @@
 class AppSettings(object):
     __DEFAULTS = dict(
             ENABLE=False,
-            LOCAL_METADATA_CACHE_TIMEOUT=600,
             METADATA_OPTIONS={},
             SECONDS_TOLERANCE=60,
             AUTHN_CONTEXT_FROM_SESSION=True,
diff --git a/authentic2/idp/saml/saml2_endpoints.py b/authentic2/idp/saml/saml2_endpoints.py
index 2b9ac97..95c998b 100644
--- a/authentic2/idp/saml/saml2_endpoints.py
+++ b/authentic2/idp/saml/saml2_endpoints.py
@@ -75,8 +75,7 @@ from authentic2.constants import NONCE_FIELD_NAME
 from authentic2.idp import signals as idp_signals
 # from authentic2.idp.models import *
 
-from authentic2.utils import (cache_and_validate, get_backends as
-        get_idp_backends, get_username, login_require)
+from authentic2.utils import (get_backends as get_idp_backends, get_username, login_require)
 from authentic2.decorators import is_transient_user
 from authentic2.attributes_ng.engine import get_attributes
 
@@ -99,7 +98,6 @@ metadata_map = (
             lasso.SAML2_METADATA_BINDING_SOAP, '/artifact')
 )
 
-@cache_and_validate(lambda: app_settings.LOCAL_METADATA_CACHE_TIMEOUT)
 def metadata(request):
     '''Endpoint to retrieve the metadata file'''
     logger.info('return metadata')
diff --git a/authentic2/utils.py b/authentic2/utils.py
index 7b6ccd7..6d59d1e 100644
--- a/authentic2/utils.py
+++ b/authentic2/utils.py
@@ -68,67 +68,6 @@ class MWT(object):
         return func
 
 
-def cache_and_validate(timeout, hashing=hashlib.md5):
-    '''
-       Decorator to add caching, with support for ETag and Last-modified
-       validation.
-
-       Just give it the time for caching.
-    '''
-    def transform(f):
-        f.cache = dict()
-        def get_content(request, *args, **kwargs):
-            '''
-               Content is kept as
-
-                (last_generation_time, last_modified_time, etag, content)
-
-               inside the f.cache dictionnary
-            '''
-            key=args+tuple(sorted(kwargs.items()))
-            if request.method == 'PURGE' and request.environ.get('REMOTE_ADDR') \
-                    in settings.INTERNAL_IPS:
-                # purge the cache place
-                f.cache.pop(key, None)
-            now = dt.datetime.now()
-            if key in f.cache:
-                date, last_modified, etag, mime_type, old_content = f.cache[key]
-                if callable(timeout):
-                    real_timeout = timeout()
-                else:
-                    real_timeout = timeout
-                if now - date < dt.timedelta(seconds=real_timeout):
-                    return date, last_modified, etag, mime_type, old_content
-                else:
-                    content = f(request, *args, **kwargs)
-                    if old_content == content.content:
-                        data = (now, last_modified, etag, mime_type, old_content)
-                        return data
-            else:
-                content = f(request, *args, **kwargs)
-            if content.status_code == 200:
-                content_type = content.get('Content-Type', None)
-                data = now, now, hashing(content.content).hexdigest(), content_type, content.content
-                f.cache[key] = data
-            else:
-                data = None, None, None, None, content
-            return data
-        def get_last_modified(request, *args, **kwargs):
-            _, last_modified, _, _, _ = get_content(request, *args, **kwargs)
-            return last_modified
-        def get_etag(request, *args, **kwargs):
-            _, _, etag, _, _ = get_content(request, *args, **kwargs)
-            return etag
-        @condition(etag_func=get_etag, last_modified_func=get_last_modified)
-        def replacement(request, *args, **kwargs):
-            _, _, _, content_type, content = get_content(request, *args, **kwargs)
-            if isinstance(content, six.string_types):
-                return HttpResponse(content, content_type=content_type)
-            else:
-                return content
-        return replacement
-    return transform
-
 def import_from(module, name):
     module = __import__(module, fromlist=[name])
     return getattr(module, name)
-- 
2.1.4

