cache.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2011-2012 Yaco Sistemas (http://www.yaco.es)
  2. # Copyright (C) 2010 Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from saml2.cache import Cache
  16. class DjangoSessionCacheAdapter(dict):
  17. """A cache of things that are stored in the Django Session"""
  18. key_prefix = '_saml2'
  19. def __init__(self, django_session, key_suffix):
  20. self.session = django_session
  21. self.key = self.key_prefix + key_suffix
  22. super(DjangoSessionCacheAdapter, self).__init__(self._get_objects())
  23. def _get_objects(self):
  24. return self.session.get(self.key, {})
  25. def _set_objects(self, objects):
  26. self.session[self.key] = objects
  27. def sync(self):
  28. # Changes in inner objects do not cause session invalidation
  29. # https://docs.djangoproject.com/en/1.9/topics/http/sessions/#when-sessions-are-saved
  30. #add objects to session
  31. self._set_objects(dict(self))
  32. #invalidate session
  33. self.session.modified = True
  34. class OutstandingQueriesCache(object):
  35. """Handles the queries that have been sent to the IdP and have not
  36. been replied yet.
  37. """
  38. def __init__(self, django_session):
  39. self._db = DjangoSessionCacheAdapter(django_session,
  40. '_outstanding_queries')
  41. def outstanding_queries(self):
  42. return self._db._get_objects()
  43. def set(self, saml2_session_id, came_from):
  44. self._db[saml2_session_id] = came_from
  45. self._db.sync()
  46. def delete(self, saml2_session_id):
  47. if saml2_session_id in self._db:
  48. del self._db[saml2_session_id]
  49. self._db.sync()
  50. class IdentityCache(Cache):
  51. """Handles information about the users that have been succesfully
  52. logged in.
  53. This information is useful because when the user logs out we must
  54. know where does he come from in order to notify such IdP/AA.
  55. The current implementation stores this information in the Django session.
  56. """
  57. def __init__(self, django_session):
  58. self._db = DjangoSessionCacheAdapter(django_session, '_identities')
  59. self._sync = True
  60. class StateCache(DjangoSessionCacheAdapter):
  61. """Store state information that is needed to associate a logout
  62. request with its response.
  63. """
  64. def __init__(self, django_session):
  65. super(StateCache, self).__init__(django_session, '_state')