예제 #1
0
class Population(object):
    def __init__(self, cache=None):
        if cache:
            if isinstance(cache, basestring):
                self.cache = Cache(cache)
            else:
                self.cache = cache
        else:
            self.cache = Cache()

    def add_information_about_person(self, session_info):
        """If there already are information from this source in the cache
        this function will overwrite that information"""

        name_id = session_info["name_id"]
        issuer = session_info["issuer"]
        del session_info["issuer"]
        self.cache.set(name_id, issuer, session_info,
                       session_info["not_on_or_after"])
        return name_id

    def stale_sources_for_person(self, name_id, sources=None):
        """

        :param name_id: Identifier of the subject, a NameID instance
        :param sources: Sources for information about the subject
        :return:
        """
        if not sources:  # assume that all the members has be asked
            # once before, hence they are represented in the cache
            sources = self.cache.entities(name_id)
        sources = [m for m in sources if not self.cache.active(name_id, m)]
        return sources

    def issuers_of_info(self, name_id):
        return self.cache.entities(name_id)

    def get_identity(self, name_id, entities=None, check_not_on_or_after=True):
        return self.cache.get_identity(name_id, entities,
                                       check_not_on_or_after)

    def get_info_from(self, name_id, entity_id):
        return self.cache.get(name_id, entity_id)

    def subjects(self):
        """Returns the name id's for all the persons in the cache"""
        return self.cache.subjects()

    def remove_person(self, name_id):
        self.cache.delete(name_id)

    def get_entityid(self, name_id, source_id, check_not_on_or_after=True):
        try:
            return self.cache.get(name_id, source_id,
                                  check_not_on_or_after)["name_id"]
        except (KeyError, ValueError):
            return ""

    def sources(self, name_id):
        return self.cache.entities(name_id)
예제 #2
0
파일: population.py 프로젝트: Amli/pysaml2
class Population(object):
    def __init__(self, cache=None):
        if cache:
            if isinstance(cache, six.string_types):
                self.cache = Cache(cache)
            else:
                self.cache = cache
        else:
            self.cache = Cache()

    def add_information_about_person(self, session_info):
        """If there already are information from this source in the cache
        this function will overwrite that information"""

        session_info = dict(session_info)
        name_id = session_info["name_id"]
        issuer = session_info.pop("issuer")
        self.cache.set(name_id, issuer, session_info,
                       session_info["not_on_or_after"])
        return name_id

    def stale_sources_for_person(self, name_id, sources=None):
        """

        :param name_id: Identifier of the subject, a NameID instance
        :param sources: Sources for information about the subject
        :return:
        """
        if not sources:  # assume that all the members has be asked
                         # once before, hence they are represented in the cache
            sources = self.cache.entities(name_id)
        sources = [m for m in sources if not self.cache.active(name_id, m)]
        return sources

    def issuers_of_info(self, name_id):
        return self.cache.entities(name_id)

    def get_identity(self, name_id, entities=None, check_not_on_or_after=True):
        return self.cache.get_identity(name_id, entities, check_not_on_or_after)

    def get_info_from(self, name_id, entity_id, check_not_on_or_after=True):
        return self.cache.get(name_id, entity_id, check_not_on_or_after)

    def subjects(self):
        """Returns the name id's for all the persons in the cache"""
        return self.cache.subjects()

    def remove_person(self, name_id):
        self.cache.delete(name_id)

    def get_entityid(self, name_id, source_id, check_not_on_or_after=True):
        try:
            return self.cache.get(name_id, source_id, check_not_on_or_after)[
                "name_id"]
        except (KeyError, ValueError):
            return ""

    def sources(self, name_id):
        return self.cache.entities(name_id)
예제 #3
0
class TestClass:
    def setup_class(self):
        self.cache = Cache()

    def test_set(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Derek"]}
        self.cache.set("1234", "abcd", session_info, not_on_or_after)

        (ava, inactive) = self.cache.get_identity("1234")
        assert inactive == []
        assert ava.keys() == ["givenName"]
        assert ava["givenName"] == ["Derek"]

    def test_add_ava_info(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"surName": ["Jeter"]}
        self.cache.set("1234", "bcde", session_info, not_on_or_after)

        (ava, inactive) = self.cache.get_identity("1234")
        assert inactive == []
        assert _eq(ava.keys(), ["givenName", "surName"])
        assert ava["givenName"] == ["Derek"]
        assert ava["surName"] == ["Jeter"]

    def test_from_one_target_source(self):
        session_info = self.cache.get("1234", "bcde")
        ava = session_info["ava"]
        assert _eq(ava.keys(), ["surName"])
        assert ava["surName"] == ["Jeter"]
        session_info = self.cache.get("1234", "abcd")
        ava = session_info["ava"]
        assert _eq(ava.keys(), ["givenName"])
        assert ava["givenName"] == ["Derek"]

    def test_entities(self):
        assert _eq(self.cache.entities("1234"), ["abcd", "bcde"])
        py.test.raises(Exception, "self.cache.entities('6666')")

    def test_remove_info(self):
        self.cache.reset("1234", "bcde")
        assert self.cache.active("1234", "bcde") == False
        assert self.cache.active("1234", "abcd")

        (ava, inactive) = self.cache.get_identity("1234")
        assert inactive == ['bcde']
        assert _eq(ava.keys(), ["givenName"])
        assert ava["givenName"] == ["Derek"]

    def test_active(self):
        assert self.cache.active("1234", "bcde") == False
        assert self.cache.active("1234", "abcd")

    def test_subjects(self):
        assert self.cache.subjects() == ["1234"]

    def test_second_subject(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Ichiro"], "surName": ["Suzuki"]}
        self.cache.set("9876", "abcd", session_info, not_on_or_after)

        (ava, inactive) = self.cache.get_identity("9876")
        assert inactive == []
        assert _eq(ava.keys(), ["givenName", "surName"])
        assert ava["givenName"] == ["Ichiro"]
        assert ava["surName"] == ["Suzuki"]
        assert _eq(self.cache.subjects(), ["1234", "9876"])

    def test_receivers(self):
        assert _eq(self.cache.receivers("9876"), ["abcd"])

        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Ichiro"], "surName": ["Suzuki"]}
        self.cache.set("9876", "bcde", session_info, not_on_or_after)

        assert _eq(self.cache.receivers("9876"), ["abcd", "bcde"])
        assert _eq(self.cache.subjects(), ["1234", "9876"])

    def test_timeout(self):
        not_on_or_after = str_to_time(in_a_while(seconds=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Alex"], "surName": ["Rodriguez"]}
        self.cache.set("1000", "bcde", session_info, not_on_or_after)

        time.sleep(2)
        (ava, inactive) = self.cache.get_identity("1000")
        assert inactive == ["bcde"]
        assert ava == {}
예제 #4
0
class TestClass:
    def setup_class(self):
        self.cache = Cache()

    def test_set(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Derek"]}
        self.cache.set(nid[0], "abcd", session_info, not_on_or_after)

        (ava, inactive) = self.cache.get_identity(nid[0])
        assert inactive == []
        assert list(ava.keys()) == ["givenName"]
        assert ava["givenName"] == ["Derek"]

    def test_add_ava_info(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"surName": ["Jeter"]}
        self.cache.set(nid[0], "bcde", session_info, not_on_or_after)

        (ava, inactive) = self.cache.get_identity(nid[0])
        assert inactive == []
        assert _eq(ava.keys(), ["givenName", "surName"])
        assert ava["givenName"] == ["Derek"]
        assert ava["surName"] == ["Jeter"]

    def test_from_one_target_source(self):
        session_info = self.cache.get(nid[0], "bcde")
        ava = session_info["ava"]
        assert _eq(ava.keys(), ["surName"])
        assert ava["surName"] == ["Jeter"]
        session_info = self.cache.get(nid[0], "abcd")
        ava = session_info["ava"]
        assert _eq(ava.keys(), ["givenName"])
        assert ava["givenName"] == ["Derek"]

    def test_entities(self):
        assert _eq(self.cache.entities(nid[0]), ["abcd", "bcde"])
        py.test.raises(Exception, "self.cache.entities('6666')")

    def test_remove_info(self):
        self.cache.reset(nid[0], "bcde")
        assert self.cache.active(nid[0], "bcde") == False
        assert self.cache.active(nid[0], "abcd")

        (ava, inactive) = self.cache.get_identity(nid[0])
        assert inactive == ['bcde']
        assert _eq(ava.keys(), ["givenName"])
        assert ava["givenName"] == ["Derek"]

    def test_active(self):
        assert self.cache.active(nid[0], "bcde") == False
        assert self.cache.active(nid[0], "abcd")

    def test_subjects(self):
        assert nid_eq(self.cache.subjects(), [nid[0]])

    def test_second_subject(self):
        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Ichiro"],
                               "surName": ["Suzuki"]}
        self.cache.set(nid[1], "abcd", session_info,
                       not_on_or_after)

        (ava, inactive) = self.cache.get_identity(nid[1])
        assert inactive == []
        assert _eq(ava.keys(), ["givenName", "surName"])
        assert ava["givenName"] == ["Ichiro"]
        assert ava["surName"] == ["Suzuki"]
        assert nid_eq(self.cache.subjects(), [nid[0], nid[1]])

    def test_receivers(self):
        assert _eq(self.cache.receivers(nid[1]), ["abcd"])

        not_on_or_after = str_to_time(in_a_while(days=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Ichiro"],
                               "surName": ["Suzuki"]}
        self.cache.set(nid[1], "bcde", session_info,
                       not_on_or_after)

        assert _eq(self.cache.receivers(nid[1]), ["abcd", "bcde"])
        assert nid_eq(self.cache.subjects(), nid[0:2])

    def test_timeout(self):
        not_on_or_after = str_to_time(in_a_while(seconds=1))
        session_info = SESSION_INFO_PATTERN.copy()
        session_info["ava"] = {"givenName": ["Alex"],
                               "surName": ["Rodriguez"]}
        self.cache.set(nid[2], "bcde", session_info,
                       not_on_or_after)

        time.sleep(2)
        (ava, inactive) = self.cache.get_identity(nid[2])
        assert inactive == ["bcde"]
        assert ava == {}