cache.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. from saml2.ident import code, decode
  17. class DjangoSessionCacheAdapter(dict):
  18. """A cache of things that are stored in the Django Session"""
  19. key_prefix = '_saml2'
  20. def __init__(self, django_session, key_suffix):
  21. self.session = django_session
  22. self.key = self.key_prefix + key_suffix
  23. super(DjangoSessionCacheAdapter, self).__init__(self._get_objects())
  24. def _get_objects(self):
  25. return self.session.get(self.key, {})
  26. def _set_objects(self, objects):
  27. self.session[self.key] = objects
  28. def sync(self):
  29. # Changes in inner objects do not cause session invalidation
  30. # https://docs.djangoproject.com/en/1.9/topics/http/sessions/#when-sessions-are-saved
  31. #add objects to session
  32. self._set_objects(dict(self))
  33. #invalidate session
  34. self.session.modified = True
  35. class OutstandingQueriesCache(object):
  36. """Handles the queries that have been sent to the IdP and have not
  37. been replied yet.
  38. """
  39. def __init__(self, django_session):
  40. self._db = DjangoSessionCacheAdapter(django_session,
  41. '_outstanding_queries')
  42. def outstanding_queries(self):
  43. return self._db._get_objects()
  44. def set(self, saml2_session_id, came_from):
  45. self._db[saml2_session_id] = came_from
  46. self._db.sync()
  47. def delete(self, saml2_session_id):
  48. if saml2_session_id in self._db:
  49. del self._db[saml2_session_id]
  50. self._db.sync()
  51. class IdentityCache(Cache):
  52. """Handles information about the users that have been succesfully
  53. logged in.
  54. This information is useful because when the user logs out we must
  55. know where does he come from in order to notify such IdP/AA.
  56. The current implementation stores this information in the Django session.
  57. """
  58. def __init__(self, django_session):
  59. self._db = DjangoSessionCacheAdapter(django_session, '_identities')
  60. self._sync = True
  61. class StateCache(DjangoSessionCacheAdapter):
  62. """Store state information that is needed to associate a logout
  63. request with its response.
  64. """
  65. def __init__(self, django_session):
  66. super(StateCache, self).__init__(django_session, '_state')