【问题标题】:Zeep: Struggling to add mustunderstand=1 to WSE headerZeep:努力将 mustunderstand=1 添加到 WSE 标头
【发布时间】:2020-10-26 10:22:45
【问题描述】:

当我在 SOAPUI 中查看原始请求时,我在 <soapenv:Header> 部分中得到 = wsse:Security soapenv:mustUnderstand="1"。使用 zeep 和 python 执行此操作时,我在发送到服务器的请求中看不到这一点 - 我在应用程序日志中遇到了安全问题

from zeep import Client
from zeep.transports import Transport
from zeep import xsd
from zeep.wsse.username import UsernameToken
from zeep.wsse.utils import get_security_header
from requests import Session

request_data = {
        'idNumber': 'someID',
        'encryptedPin': 'encPin0101='
}
header_value = {
    "wsse":{
        "mustUnderstand":'1'
    }
}
wsdl = 'http://someURL/AuthenticationWS?WSDL'
# session = Session()
# session.verify = True
# transport = Transport(session=session,
#                       operation_timeout=10)
cl = Client(wsdl=wsdl,
            wsse=UsernameToken('username', 'password', use_digest=True))

def send_request(client, data):
    return client.service.authenticateCustomer(data)

node = cl.create_message(cl.service, 'authenticateCustomer',
                         idNumber='someID',
                         encryptedPin='encPin=')

from lxml import etree

print('###########')
print(etree.tostring(node))
print('###########')
print(send_request(cl, request_data))

第一个打印出来的作品,我看到了我需要的信息除了 mustunderstand=1 第二个打印错误 - 我得到“发生故障”,应用程序日志给出了与安全相关的错误,让我认为这是必须理解的事情,我尝试了不同的事情

我已尝试使用soapheader 执行此操作,如这些位置中所述,但没有成功:

How do I add attributes to header authentication in Zeep?

添加会话\传输的东西没有弹出我需要的标题。我正忙着看呢

https://pydoc.net/zeep/2.5.0/zeep.wsse.signature/

为了理解 `get_security_header` 的事情,但我没有赢得这个:( 我看过的其他资源:

https://stackoverflow.com/questions/62924433/zeep-with-complex-header

https://docs.python-zeep.org/en/master/headers.html

https://stackoverflow.com/questions/44330748/how-to-comply-with-policy-defined-in-wsdl

【问题讨论】:

    标签: python soap zeep


    【解决方案1】:

    我改用https://github.com/suds-community/suds,它有简单的方法来添加这些安全令牌:

    security = Security()
    token = UsernameToken('username', 'password')
    token.setnonce()
    token.setcreated()
    token.setnonceencoding(True)
    token.setpassworddigest('digest')
    security.tokens.append(token)
    client = Client('http://someURL/AuthenticationWS?WSDL')
    client.set_options(wsse=security)
    client.service.logCustomerInNoAuth('id_number', id_number))
    

    简单多了

    【讨论】:

    • suds 非常很慢,没有维护(使用 suds-py3 代替),最重要的是创建/自定义消息的布局非常冗长(zeep 允许你传递一个嵌套的字典)
    • @Pynchia - suds-community 最后一次更新是在今年 1 月,但谢谢 - 我也在寻找 python3 替代品
    【解决方案2】:

    现在很忙,但是这里有一个 sn-p:

    class UsernameToken2(UsernameToken):
    
        def apply(self, envelope, headers):
            from zeep.wsse import utils
            from lxml.etree import QName
            envelope, headers = super().apply(envelope, headers)
            security = utils.get_security_header(envelope)
            security.set(QName('http://schemas.xmlsoap.org/soap/envelope/', 'mustUnderstand'), '1')
            return envelope, headers
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 谢谢。尽管我同意社区机器人,但它比我的代码实现起来要简单一些,我希望我可以测试它。另一方面,我不推荐zeep。根据@Pyncia 的评论,suds-py3 比 zeep 快(至少在那个环境中是这样)
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多