cache.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. objs = {}
  30. objs.update(self)
  31. self._set_objects(objs)
  32. class OutstandingQueriesCache(object):
  33. """Handles the queries that have been sent to the IdP and have not
  34. been replied yet.
  35. """
  36. def __init__(self, django_session):
  37. self._db = DjangoSessionCacheAdapter(django_session,
  38. '_outstanding_queries')
  39. def outstanding_queries(self):
  40. return self._db._get_objects()
  41. def set(self, saml2_session_id, came_from):
  42. self._db[saml2_session_id] = came_from
  43. self._db.sync()
  44. def delete(self, saml2_session_id):
  45. if saml2_session_id in self._db:
  46. del self._db[saml2_session_id]
  47. self._db.sync()
  48. class IdentityCache(Cache):
  49. """Handles information about the users that have been succesfully
  50. logged in.
  51. This information is useful because when the user logs out we must
  52. know where does he come from in order to notify such IdP/AA.
  53. The current implementation stores this information in the Django session.
  54. """
  55. def __init__(self, django_session):
  56. self._db = DjangoSessionCacheAdapter(django_session, '_identities')
  57. self._sync = True
  58. def get(self, name_id, entity_id, *args, **kwargs):
  59. info = super(IdentityCache, self).get(name_id, entity_id, *args, **kwargs)
  60. try:
  61. name_id = info['name_id']
  62. except KeyError:
  63. pass
  64. else:
  65. info = dict(info)
  66. info['name_id'] = decode(name_id)
  67. return info
  68. def set(self, name_id, entity_id, info, *args, **kwargs):
  69. try:
  70. name_id = info['name_id']
  71. except KeyError:
  72. pass
  73. else:
  74. info = dict(info)
  75. info['name_id'] = code(name_id)
  76. return super(IdentityCache, self).set(name_id, entity_id, info, *args, **kwargs)
  77. class StateCache(DjangoSessionCacheAdapter):
  78. """Store state information that is needed to associate a logout
  79. request with its response.
  80. """
  81. def __init__(self, django_session):
  82. super(StateCache, self).__init__(django_session, '_state')