使用Python Zeep

时间:2018-12-31 12:21:33

标签: python zeep

我需要创建一条包含以下内容的肥皂消息:

<saml:Subject>     
    <saml:SubjectConfirmation>      
      <saml:ConfirmationMethod>
               urn:oasis:names:tc:SAML:1.0:cm:holder-of-key
      </saml:ConfirmationMethod>
      <saml:SubjectConfirmationData>
         <saml:Assertion AssertionID="123" 
                         IssueInstant="2018-12-27T17:59:36.284Z" 
                         Issuer="issuer" 
                         MajorVersion="1" 
                         MinorVersion="1">
            ...
         </saml:Assertion>
      </saml:SubjectConfirmationData>
   </saml:SubjectConfirmation>
</saml:Subject>

SubjectConfirmationData的类型为AnyType
我几乎可以看到这段代码了:

assertion_type = client.get_type("saml:AssertionType")
assertion = assertion_type(
    AssertionID = "123",
    MajorVersion = 1,
    MinorVersion = 1,
    Issuer = "issuer",
    IssueInstant = datetime.now())

subject_confirm = {
    'ConfirmationMethod': 'urn:oasis:names:tc:SAML:1.0:cm:holder-of-key',
    'SubjectConfirmationData': assertion
}

result = {
    'Subject': {
        'SubjectConfirmation': subject_confirm
    }
}

这是结果:

<saml:Subject>
  <saml:SubjectConfirmation>
    <saml:ConfirmationMethod>
          urn:oasis:names:tc:SAML:1.0:cm:holder-of-key
    </saml:ConfirmationMethod>
    <saml:SubjectConfirmationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        MajorVersion="1" 
        MinorVersion="1" 
        AssertionID="123" 
        Issuer="issuer" 
        IssueInstant="2018-12-31T13:10:00.471365"
        xsi:type="saml:AssertionType"/>
  </saml:SubjectConfirmation>
</saml:Subject>

问题是AssertionType需要包装在单独的Assertion元素中,而不是放在SubjectConfirmationData内。 如何添加此额外元素?

更新1 :我也尝试过类似的操作,但出现错误:

assertion_elem = client.get_element("saml:Assertion")
assertion = assertion_elem(
    AssertionID = "",
    MajorVersion = 1,
    MinorVersion = 1,
    Issuer = cert_subject,
    IssueInstant = datetime.now())

subj_conf_data_type = client.get_element("saml:SubjectConfirmationData")
subj_conf_data = xsd.AnyObject(assertion_elem, assertion)

subject_confirm = {
    'ConfirmationMethod': 'urn:oasis:names:tc:SAML:1.0:cm:holder-of-key',
    'SubjectConfirmationData': subj_conf_data,
    "KeyInfo": key_info
}

这是错误:

  File "...lib\site-packages\zeep\xsd\types\any.py", line 26, in render
    value.xsd_type.render(parent, value.value, None, render_path)
TypeError: render() takes from 3 to 4 positional arguments but 5 were given

更新2

我也尝试过这种方法,但结果却很奇怪:

assertion_elem = client.get_element("saml:Assertion")
assertion = assertion_elem(
    AssertionID = "",
    MajorVersion = 1,
    MinorVersion = 1,
    Issuer = cert_subject,
    IssueInstant = datetime.now())

subject_confirm = {
    'ConfirmationMethod': 'urn:oasis:names:tc:SAML:1.0:cm:holder-of-key',
    'SubjectConfirmationData': {
        'Assertion': assertion
    },
    "KeyInfo": key_info
}

这是结果:

<saml:Subject>
          <saml:SubjectConfirmation>
            <saml:ConfirmationMethod>
                urn:oasis:names:tc:SAML:1.0:cm:holder-of-key
            </saml:ConfirmationMethod>
            <saml:SubjectConfirmationData>{'Assertion': {
    'Conditions': None,
    'Advice': None,
    '_value_1': {
},
    'Signature': None,
    'MajorVersion': 1,
    'MinorVersion': 1,
    'AssertionID': '123',
    'Issuer': 'issuer',
    'IssueInstant': datetime.datetime(2018, 12, 31, 15, 2, 8, 518253)
}}</saml:SubjectConfirmationData>
          </saml:SubjectConfirmation>
        </saml:Subject>

更新3:

我通过在自定义zeep插件中创建完整的标头解决了这一问题。
标头由普通的lxml制成,并由xmlsec签名

0 个答案:

没有答案