Exemplo n.º 1
0
    def create_attribute_query(self, destination, name_id=None,
                               attribute=None, message_id=0, consent=None,
                               extensions=None, sign=False, sign_prepare=False,
                               **kwargs):
        """ Constructs an AttributeQuery

        :param destination: To whom the query should be sent
        :param name_id: The identifier of the subject
        :param attribute: A dictionary of attributes and values that is
            asked for. The key are one of 4 variants:
            3-tuple of name_format,name and friendly_name,
            2-tuple of name_format and name,
            1-tuple with name or
            just the name as a string.
        :param sp_name_qualifier: The unique identifier of the
            service provider or affiliation of providers for whom the
            identifier was generated.
        :param name_qualifier: The unique identifier of the identity
            provider that generated the identifier.
        :param format: The format of the name ID
        :param message_id: The identifier of the session
        :param consent: Whether the principal have given her consent
        :param extensions: Possible extensions
        :param sign: Whether the query should be signed or not.
        :param sign_prepare: Whether the Signature element should be added.
        :return: Tuple of request ID and an AttributeQuery instance
        """

        if name_id is None:
            if "subject_id" in kwargs:
                name_id = saml.NameID(text=kwargs["subject_id"])
                for key in ["sp_name_qualifier", "name_qualifier",
                            "format"]:
                    try:
                        setattr(name_id, key, kwargs[key])
                    except KeyError:
                        pass
            else:
                raise AttributeError("Missing required parameter")
        elif isinstance(name_id, basestring):
            name_id = saml.NameID(text=name_id)
            for key in ["sp_name_qualifier", "name_qualifier", "format"]:
                try:
                    setattr(name_id, key, kwargs[key])
                except KeyError:
                    pass

        subject = saml.Subject(name_id=name_id)

        if attribute:
            attribute = do_attributes(attribute)

        try:
            nsprefix = kwargs["nsprefix"]
        except KeyError:
            nsprefix = None

        return self._message(AttributeQuery, destination, message_id, consent,
                             extensions, sign, sign_prepare, subject=subject,
                             attribute=attribute, nsprefix=nsprefix)
Exemplo n.º 2
0
    def create_attribute_query(self, destination, name_id=None,
                               attribute=None, message_id=0, consent=None,
                               extensions=None, sign=False, sign_prepare=False,
                               **kwargs):
        """ Constructs an AttributeQuery

        :param destination: To whom the query should be sent
        :param name_id: The identifier of the subject
        :param attribute: A dictionary of attributes and values that is
            asked for. The key are one of 4 variants:
            3-tuple of name_format,name and friendly_name,
            2-tuple of name_format and name,
            1-tuple with name or
            just the name as a string.
        :param sp_name_qualifier: The unique identifier of the
            service provider or affiliation of providers for whom the
            identifier was generated.
        :param name_qualifier: The unique identifier of the identity
            provider that generated the identifier.
        :param format: The format of the name ID
        :param message_id: The identifier of the session
        :param consent: Whether the principal have given her consent
        :param extensions: Possible extensions
        :param sign: Whether the query should be signed or not.
        :param sign_prepare: Whether the Signature element should be added.
        :return: Tuple of request ID and an AttributeQuery instance
        """

        if name_id is None:
            if "subject_id" in kwargs:
                name_id = saml.NameID(text=kwargs["subject_id"])
                for key in ["sp_name_qualifier", "name_qualifier",
                            "format"]:
                    try:
                        setattr(name_id, key, kwargs[key])
                    except KeyError:
                        pass
            else:
                raise AttributeError("Missing required parameter")
        elif isinstance(name_id, six.string_types):
            name_id = saml.NameID(text=name_id)
            for key in ["sp_name_qualifier", "name_qualifier", "format"]:
                try:
                    setattr(name_id, key, kwargs[key])
                except KeyError:
                    pass

        subject = saml.Subject(name_id=name_id)

        if attribute:
            attribute = do_attributes(attribute)

        try:
            nsprefix = kwargs["nsprefix"]
        except KeyError:
            nsprefix = None

        return self._message(AttributeQuery, destination, message_id, consent,
                             extensions, sign, sign_prepare, subject=subject,
                             attribute=attribute, nsprefix=nsprefix)
Exemplo n.º 3
0
    def create_attribute_query(self,
                               session_id,
                               subject_id,
                               destination,
                               issuer_id=None,
                               attribute=None,
                               sp_name_qualifier=None,
                               name_qualifier=None,
                               nameid_format=None,
                               sign=False):
        """ Constructs an AttributeQuery
        
        :param session_id: The identifier of the session
        :param subject_id: The identifier of the subject
        :param destination: To whom the query should be sent
        :param issuer_id: Identifier of the issuer
        :param attribute: A dictionary of attributes and values that is
            asked for. The key are one of 4 variants:
            3-tuple of name_format,name and friendly_name,
            2-tuple of name_format and name,
            1-tuple with name or
            just the name as a string.
        :param sp_name_qualifier: The unique identifier of the
            service provider or affiliation of providers for whom the
            identifier was generated.
        :param name_qualifier: The unique identifier of the identity
            provider that generated the identifier.
        :param nameid_format: The format of the name ID
        :param sign: Whether the query should be signed or not.
        :return: An AttributeQuery instance
        """

        subject = saml.Subject(name_id=saml.NameID(
            text=subject_id,
            format=nameid_format,
            sp_name_qualifier=sp_name_qualifier,
            name_qualifier=name_qualifier), )

        query = samlp.AttributeQuery(
            id=session_id,
            version=VERSION,
            issue_instant=instant(),
            destination=destination,
            issuer=self._issuer(issuer_id),
            subject=subject,
        )

        if sign:
            query.signature = pre_signature_part(query.id, self.sec.my_cert, 1)

        if attribute:
            query.attribute = do_attributes(attribute)

        if sign:
            signed_query = self.sec.sign_attribute_query_using_xmlsec("%s" %
                                                                      query)
            return samlp.attribute_query_from_string(signed_query)
        else:
            return query
Exemplo n.º 4
0
def test_attribute_sn():
    attr = utils.do_attributes({"surName": ("Jeter", "")})
    assert len(attr) == 1
    print attr
    inst = attr[0]
    assert inst.name == "surName"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "Jeter"
Exemplo n.º 5
0
def test_attribute_sn():
    attr = utils.do_attributes({"surName": ("Jeter", "")})
    assert len(attr) == 1
    print attr
    inst = attr[0]
    assert inst.name == "surName"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "Jeter"
Exemplo n.º 6
0
 def create_attribute_query(self, session_id, subject_id, destination,
         issuer_id=None, attribute=None, sp_name_qualifier=None,
         name_qualifier=None, nameid_format=None, sign=False):
     """ Constructs an AttributeQuery
     
     :param session_id: The identifier of the session
     :param subject_id: The identifier of the subject
     :param destination: To whom the query should be sent
     :param issuer_id: Identifier of the issuer
     :param attribute: A dictionary of attributes and values that is
         asked for. The key are one of 4 variants:
         3-tuple of name_format,name and friendly_name,
         2-tuple of name_format and name,
         1-tuple with name or
         just the name as a string.
     :param sp_name_qualifier: The unique identifier of the
         service provider or affiliation of providers for whom the
         identifier was generated.
     :param name_qualifier: The unique identifier of the identity
         provider that generated the identifier.
     :param nameid_format: The format of the name ID
     :param sign: Whether the query should be signed or not.
     :return: An AttributeQuery instance
     """
 
     
     subject = saml.Subject(
                 name_id = saml.NameID(
                             text=subject_id, 
                             format=nameid_format,
                             sp_name_qualifier=sp_name_qualifier,
                             name_qualifier=name_qualifier),
                 )
                 
     query = samlp.AttributeQuery(
         id=session_id,
         version=VERSION,
         issue_instant=instant(),
         destination=destination,
         issuer=self._issuer(issuer_id),
         subject=subject,
     )
     
     if sign:
         query.signature = pre_signature_part(query.id, self.sec.my_cert, 1)
     
     if attribute:
         query.attribute = do_attributes(attribute)
     
     if sign:
         signed_query = self.sec.sign_attribute_query_using_xmlsec(
                                                             "%s" % query)
         return samlp.attribute_query_from_string(signed_query)
     else:
         return query
Exemplo n.º 7
0
def test_attribute_age():
    attr = utils.do_attributes({"age": (37, "")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "age"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "37"
    assert av.get_type() == "xs:integer"
Exemplo n.º 8
0
def test_attribute_onoff():
    attr = utils.do_attributes({"onoff": (False, "")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "onoff"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "false"
    assert av.get_type() == "xs:boolean"
Exemplo n.º 9
0
def test_attribute_age():
    attr = utils.do_attributes({"age": (37, "")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "age"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "37"
    assert av.get_type() == "xs:integer"
Exemplo n.º 10
0
def test_attribute_onoff():
    attr = utils.do_attributes({"onoff": (False, "")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "onoff"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.text == "false"
    assert av.get_type() == "xs:boolean"
Exemplo n.º 11
0
def test_attribute_base64():
    b64sl = base64.b64encode("Selma Lagerlöf")
    attr = utils.do_attributes({"name": (b64sl, "xs:base64Binary")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "name"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.get_type() == "xs:base64Binary"
    assert av.text.strip() == b64sl
Exemplo n.º 12
0
def test_attribute_base64():
    b64sl = base64.b64encode("Selma Lagerlöf")
    attr = utils.do_attributes({"name": (b64sl, "xs:base64Binary")})

    assert len(attr) == 1
    inst = attr[0]
    print inst
    assert inst.name == "name"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.get_type() == "xs:base64Binary"
    assert av.text.strip() == b64sl
Exemplo n.º 13
0
    def create_attribute_query(self,
                               destination,
                               subject_id,
                               attribute=None,
                               sp_name_qualifier=None,
                               name_qualifier=None,
                               nameid_format=None,
                               id=0,
                               consent=None,
                               extensions=None,
                               sign=False,
                               **kwargs):
        """ Constructs an AttributeQuery
        
        :param destination: To whom the query should be sent
        :param subject_id: The identifier of the subject
        :param attribute: A dictionary of attributes and values that is
            asked for. The key are one of 4 variants:
            3-tuple of name_format,name and friendly_name,
            2-tuple of name_format and name,
            1-tuple with name or
            just the name as a string.
        :param sp_name_qualifier: The unique identifier of the
            service provider or affiliation of providers for whom the
            identifier was generated.
        :param name_qualifier: The unique identifier of the identity
            provider that generated the identifier.
        :param nameid_format: The format of the name ID
        :param id: The identifier of the session
        :param consent: Whether the principal have given her consent
        :param extensions: Possible extensions
        :param sign: Whether the query should be signed or not.
        :return: An AttributeQuery instance
        """

        subject = saml.Subject(
            name_id=saml.NameID(text=subject_id,
                                format=nameid_format,
                                sp_name_qualifier=sp_name_qualifier,
                                name_qualifier=name_qualifier))

        if attribute:
            attribute = do_attributes(attribute)

        return self._message(AttributeQuery,
                             destination,
                             id,
                             consent,
                             extensions,
                             sign,
                             subject=subject,
                             attribute=attribute)
Exemplo n.º 14
0
def test_attribute_base64():
    txt = "Selma Lagerlöf"
    if not isinstance(txt, six.binary_type):
        txt = txt.encode("utf-8")
    b64sl = base64.b64encode(txt).decode('ascii')
    attr = utils.do_attributes({"name": (b64sl, "xs:base64Binary")})

    assert len(attr) == 1
    inst = attr[0]
    print(inst)
    assert inst.name == "name"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.get_type() == "xs:base64Binary"
    assert av.text.strip() == b64sl
Exemplo n.º 15
0
def test_attribute_base64():
    txt = "Selma Lagerlöf"
    if not isinstance(txt, six.binary_type):
        txt = txt.encode("utf-8")
    b64sl = base64.b64encode(txt).decode('ascii')
    attr = utils.do_attributes({"name": (b64sl, "xs:base64Binary")})

    assert len(attr) == 1
    inst = attr[0]
    print(inst)
    assert inst.name == "name"
    assert len(inst.attribute_value) == 1
    av = inst.attribute_value[0]
    assert av.get_type() == "xs:base64Binary"
    assert av.text.strip() == b64sl
Exemplo n.º 16
0
    def create_attribute_query(self, destination, subject_id,
                               attribute=None, sp_name_qualifier=None,
                               name_qualifier=None, nameid_format=None,
                               id=0, consent=None, extensions=None, sign=False,
                               **kwargs):
        """ Constructs an AttributeQuery
        
        :param destination: To whom the query should be sent
        :param subject_id: The identifier of the subject
        :param attribute: A dictionary of attributes and values that is
            asked for. The key are one of 4 variants:
            3-tuple of name_format,name and friendly_name,
            2-tuple of name_format and name,
            1-tuple with name or
            just the name as a string.
        :param sp_name_qualifier: The unique identifier of the
            service provider or affiliation of providers for whom the
            identifier was generated.
        :param name_qualifier: The unique identifier of the identity
            provider that generated the identifier.
        :param nameid_format: The format of the name ID
        :param id: The identifier of the session
        :param consent: Whether the principal have given her consent
        :param extensions: Possible extensions
        :param sign: Whether the query should be signed or not.
        :return: An AttributeQuery instance
        """


        subject = saml.Subject(
            name_id = saml.NameID(text=subject_id,
                                  format=nameid_format,
                                  sp_name_qualifier=sp_name_qualifier,
                                  name_qualifier=name_qualifier))

        if attribute:
            attribute = do_attributes(attribute)

        return self._message(AttributeQuery, destination, id, consent,
                             extensions, sign, subject=subject,
                             attribute=attribute)