Warm tip: This article is reproduced from serverfault.com, please click

python-Zeep:努力向WSE标头添加mustunderstand = 1

(python - Zeep: Struggling to add mustunderstand=1 to WSE header)

发布于 2020-10-26 10:22:45

当我在SOAPUI中查看原始请求时,wsse:Security soapenv:mustUnderstand="1"在该<soapenv:Header>部分中获取了= 使用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来执行此操作,如以下位置所述,但未成功:

如何在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

Questioner
MistaWizard
Viewed
11
MistaWizard 2020-12-10 22:36:24

我改用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))

这么容易