diff options
author | Roland Hedberg <roland.hedberg@adm.umu.se> | 2014-10-02 16:04:41 +0200 |
---|---|---|
committer | Roland Hedberg <roland.hedberg@adm.umu.se> | 2014-10-02 16:04:41 +0200 |
commit | 13fdda52ab3367d4ebb2e9c5888bea74ac0f081b (patch) | |
tree | b00bbd5868ce65bc637feef35ecaee7b14c178e0 /src/saml2/mcache.py | |
parent | 095f8dbe1d4de3519a7e244233aa2b38ed420d9f (diff) | |
download | pysaml2-13fdda52ab3367d4ebb2e9c5888bea74ac0f081b.tar.gz |
PEP-8
Diffstat (limited to 'src/saml2/mcache.py')
-rw-r--r-- | src/saml2/mcache.py | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/saml2/mcache.py b/src/saml2/mcache.py index d2c200aa..0ead465f 100644 --- a/src/saml2/mcache.py +++ b/src/saml2/mcache.py @@ -5,7 +5,7 @@ import memcache from saml2 import time_util from saml2.cache import ToOld, CacheError -# The assumption is that any subject may consist of data +# The assumption is that any subject may consist of data # gathered from several different sources, all with their own # timeout time. @@ -13,18 +13,18 @@ logger = logging.getLogger(__name__) def _key(prefix, name): return "%s_%s" % (prefix, name) - + class Cache(object): def __init__(self, servers, debug=0): self._cache = memcache.Client(servers, debug) - + def delete(self, subject_id): entities = self.entities(subject_id) if entities: for entity_id in entities: if not self._cache.delete(_key(subject_id, entity_id)): raise CacheError("Delete failed") - + if not self._cache.delete(subject_id): raise CacheError("Delete failed") @@ -33,33 +33,33 @@ class Cache(object): subjects.remove(subject_id) if not self._cache.set("subjects", subjects): raise CacheError("Set operation failed") - + def get_identity(self, subject_id, entities=None): - """ Get all the identity information that has been received and + """ Get all the identity information that has been received and are still valid about the subject. - + :param subject_id: The identifier of the subject :param entities: The identifiers of the entities whoes assertions are interesting. If the list is empty all entities are interesting. :return: A 2-tuple consisting of the identity information (a - dictionary of attributes and values) and the list of entities + dictionary of attributes and values) and the list of entities whoes information has timed out. """ if not entities: entities = self.entities(subject_id) if not entities: return {}, [] - + res = {} oldees = [] - for (entity_id, item) in self._cache.get_multi(entities, + for (entity_id, item) in self._cache.get_multi(entities, subject_id+'_').items(): try: info = self.get_info(item) except ToOld: oldees.append(entity_id) continue - for key, vals in info["ava"].items(): + for key, vals in info["ava"].items(): try: tmp = set(res[key]).union(set(vals)) res[key] = list(tmp) @@ -78,7 +78,7 @@ class Cache(object): (timestamp, info) = item except ValueError: raise ToOld() - + if check_not_on_or_after and not time_util.not_on_or_after(timestamp): raise ToOld() @@ -90,13 +90,13 @@ class Cache(object): return {} else: return self.get_info(res) - + def set(self, subject_id, entity_id, info, timestamp=0): """ Stores session information in the cache. Assumes that the subject_id is unique within the context of the Service Provider. - + :param subject_id: The subject identifier - :param entity_id: The identifier of the entity_id/receiver of an + :param entity_id: The identifier of the entity_id/receiver of an assertion :param info: The session info, the assertion is part of this :param timestamp: A time after which the assertion is not valid. @@ -111,31 +111,31 @@ class Cache(object): subjects.append(subject_id) if not self._cache.set("subjects", subjects): raise CacheError("set failed") - + if entity_id not in entities: entities.append(entity_id) if not self._cache.set(subject_id, entities): raise CacheError("set failed") - + # Should use memcache's expire if not self._cache.set(_key(subject_id, entity_id), (timestamp, info)): raise CacheError("set failed") - + def reset(self, subject_id, entity_id): """ Scrap the assertions received from a IdP or an AA about a special subject. - + :param subject_id: The subjects identifier :param entity_id: The identifier of the entity_id of the assertion :return: """ if not self._cache.set(_key(subject_id, entity_id), {}, 0): raise CacheError("reset failed") - + def entities(self, subject_id): """ Returns all the entities of assertions for a subject, disregarding whether the assertion still is valid or not. - + :param subject_id: The identifier of the subject :return: A possibly empty list of entity identifiers """ @@ -144,15 +144,15 @@ class Cache(object): raise KeyError("No such subject") else: return res - + def receivers(self, subject_id): - """ Another name for entities() just to make it more logic in the IdP + """ Another name for entities() just to make it more logic in the IdP scenario """ return self.entities(subject_id) - + def active(self, subject_id, entity_id): """ Returns the status of assertions from a specific entity_id. - + :param subject_id: The ID of the subject :param entity_id: The entity ID of the entity_id of the assertion :return: True or False depending on if the assertion is still @@ -164,18 +164,18 @@ class Cache(object): return False except TypeError: return False - + # if not info: # return False - + try: return time_util.not_on_or_after(timestamp) except ToOld: return False - + def subjects(self): """ Return identifiers for all the subjects that are in the cache. - + :return: list of subject identifiers """ return self._cache.get("subjects") @@ -189,7 +189,7 @@ class Cache(object): if info: info.update(ava) self.set(subject_id, entity_id, info, res[0]) - + def valid_to(self, subject_id, entity_id, newtime): try: (timestamp, info) = self._cache.get(_key(subject_id, entity_id)) @@ -197,6 +197,6 @@ class Cache(object): return False except TypeError: info = {} - + if not self._cache.set(_key(subject_id, entity_id), (newtime, info)): raise CacheError("valid_to failed") |