如何用xmlsec(或其他更合适的包)签署XML

2024-04-28 21:56:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我从这样一个XML开始:

myXML="""<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mes="http://www.ercot.com/schema/2007-06/nodal/ews/message">
  <soapenv:Header> </soapenv:Header>
  <soapenv:Body>
  <RequestMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.ercot.com/schema/2007-06/nodal/ews/message">
    <Header>
      <Verb>get</Verb>
      <Noun>BidSet</Noun>
      <ReplayDetection>
        <Nonce>177766768</Nonce>
        <Created>2018-10-22T09:03:33.169-05:00</Created>
      </ReplayDetection>
      <Revision>1</Revision>
      <Source>QSAMP</Source>
      <UserID>USER1</UserID>
      <MessageID>test</MessageID>
      <Comment>test</Comment>
    </Header>
    <Request>
      <ID>QSAMP.20181020.EB.AB_C.BID123</ID>
    </Request>
  </RequestMessage>
 </soapenv:Body>
</soapenv:Envelope>"""

我需要签个字,这样看起来像这样

^{pr2}$

另外,这里的证书信息不是真实的,所以不用担心。在

我查看了文档中的示例页面,但不知道它们的源xml是什么样子的,因此对我的用例进行建模非常困难。在


Tags: orgcomhttpmessageschemawwwbodyheader
2条回答

它在zeepxmlsec的源代码上做了一些摸索,甚至尝试了lxml构造函数,但这里是:

from zeep.wsse.signature import sign_envelope
from lxml import etree

raw_xml = open('unsigned-soapenv.xml').read()
xml_root_element = etree.fromstring(raw_xml)

signed = sign_envelope(
    xml_root_element,
    'rsakey.pem',
    'rsacert.pem'
)

tree = etree.ElementTree(xml_root_element)
tree.write('signed-soapenv.xml')

unsigned-soapenv.xml是你的信封。在

如果需要示例签名和证书,它们来自^{}'s test data。在

无价的是ipdb,它是一个具有自动完成功能的调试器。在你的终端里,像这样试试: import ipdb; ipdb.set_trace()

python-zeep包对WSSE-check out the documentation具有初步支持。在

相关问题 更多 >