Skip to content

Commit d4cb5d9

Browse files
committed
Sign elements required by WSDL
1 parent 4575965 commit d4cb5d9

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/zeep/wsdl/bindings/soap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _create(self, operation, args, kwargs, client=None, options=None):
8686

8787
# Apply WSSE
8888
if client.wsse:
89-
envelope, http_headers = client.wsse.apply(envelope, http_headers)
89+
envelope, http_headers = client.wsse.apply(envelope, http_headers, operation_obj)
9090
return envelope, http_headers
9191

9292
def send(self, client, options, operation, args, kwargs):

src/zeep/wsse/signature.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from zeep import ns
1515
from zeep.exceptions import SignatureVerificationFailed
1616
from zeep.utils import detect_soap_env
17+
from zeep.wsdl.utils import get_or_create_header
1718
from zeep.wsse.utils import ensure_id, get_security_header
1819

1920
try:
@@ -52,9 +53,9 @@ def __init__(self, key_data, cert_data, password=None):
5253
self.cert_data = cert_data
5354
self.password = password
5455

55-
def apply(self, envelope, headers):
56+
def apply(self, envelope, headers, operation_obj):
5657
key = _make_sign_key(self.key_data, self.cert_data, self.password)
57-
_sign_envelope_with_key(envelope, key)
58+
_sign_envelope_with_key(envelope, key, operation_obj)
5859
return envelope, headers
5960

6061
def verify(self, envelope):
@@ -173,7 +174,7 @@ def sign_envelope(envelope, keyfile, certfile, password=None):
173174
return _sign_envelope_with_key(envelope, key)
174175

175176

176-
def _sign_envelope_with_key(envelope, key):
177+
def _sign_envelope_with_key(envelope, key, operation_obj):
177178
soap_env = detect_soap_env(envelope)
178179

179180
# Create the Signature node.
@@ -198,8 +199,18 @@ def _sign_envelope_with_key(envelope, key):
198199
# Perform the actual signing.
199200
ctx = xmlsec.SignatureContext()
200201
ctx.key = key
201-
_sign_node(ctx, signature, envelope.find(QName(soap_env, 'Body')))
202+
# Sign default elements
202203
_sign_node(ctx, signature, security.find(QName(ns.WSU, 'Timestamp')))
204+
if operation_obj.binding.signatures['body'] or operation_obj.binding.signatures['everything']:
205+
_sign_node(ctx, signature, envelope.find(QName(soap_env, 'Body')))
206+
# Sign extra elements defined in WSDL
207+
header = get_or_create_header(envelope)
208+
if operation_obj.binding.signatures['everything']:
209+
for node in header.iterchildren():
210+
_sign_node(ctx, signature, node)
211+
else:
212+
for node in operation_obj.binding.signatures['header']:
213+
_sign_node(ctx, signature, header.find(QName(node['Namespace'], node['Name'])))
203214
ctx.sign(signature)
204215

205216
# Place the X509 data inside a WSSE SecurityTokenReference within

0 commit comments

Comments
 (0)