Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce71b87

Browse filesBrowse files
committed
Implement BinarySignature
This method works similarly to Signature but places the key as a BinarySecurityToken
1 parent 29976d9 commit ce71b87
Copy full SHA for ce71b87

4 files changed

+61-2Lines changed: 61 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/wsse.rst‎

Copy file name to clipboardExpand all lines: docs/wsse.rst
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ To use the wsse.Signature() plugin you will need to install the `xmlsec`_
2424
module. See the `README`_ for xmlsec for the required dependencies on your
2525
platform.
2626

27+
To append the security token as `BinarySecurityToken`, you can use wsse.BinarySignature() plugin.
28+
2729
Example usage::
2830

2931
>>> from zeep import Client
Collapse file

‎src/zeep/wsse/__init__.py‎

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .compose import Compose # noqa
2-
from .signature import Signature # noqa
2+
from .signature import BinarySignature, Signature # noqa
33
from .username import UsernameToken # noqa
Collapse file

‎src/zeep/wsse/signature.py‎

Copy file name to clipboardExpand all lines: src/zeep/wsse/signature.py
+35-1Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ def __init__(self, key_file, certfile, password=None):
7171
_read_file(key_file), _read_file(certfile), password)
7272

7373

74+
class BinarySignature(Signature):
75+
"""Sign given SOAP envelope with WSSE sig usign given key file and cert file.
76+
77+
Place the key information into BinarySecurityElement."""
78+
79+
def apply(self, envelope, headers):
80+
key = _make_sign_key(self.key_data, self.cert_data, self.password)
81+
_sign_envelope_with_key_binary(envelope, key)
82+
return envelope, headers
83+
84+
7485
def check_xmlsec_import():
7586
if xmlsec is None:
7687
raise ImportError(
@@ -173,7 +184,8 @@ def sign_envelope(envelope, keyfile, certfile, password=None):
173184
return _sign_envelope_with_key(envelope, key)
174185

175186

176-
def _sign_envelope_with_key(envelope, key):
187+
def _signature_prepare(envelope, key):
188+
"""Prepare envelope and sign."""
177189
soap_env = detect_soap_env(envelope)
178190

179191
# Create the Signature node.
@@ -208,9 +220,31 @@ def _sign_envelope_with_key(envelope, key):
208220
# the X509 data (because it doesn't understand WSSE).
209221
sec_token_ref = etree.SubElement(
210222
key_info, QName(ns.WSSE, 'SecurityTokenReference'))
223+
return security, sec_token_ref, x509_data
224+
225+
226+
def _sign_envelope_with_key(envelope, key):
227+
_, sec_token_ref, x509_data = _signature_prepare(envelope, key)
211228
sec_token_ref.append(x509_data)
212229

213230

231+
def _sign_envelope_with_key_binary(envelope, key):
232+
security, sec_token_ref, x509_data = _signature_prepare(envelope, key)
233+
ref = etree.SubElement(sec_token_ref, QName(ns.WSSE, 'Reference'),
234+
{'ValueType': 'http://docs.oasis-open.org/wss/2004/01/'
235+
'oasis-200401-wss-x509-token-profile-1.0#X509v3'})
236+
ref_id = ensure_id(ref)
237+
bintok = etree.Element(QName(ns.WSSE, 'BinarySecurityToken'), {
238+
QName(ns.WSU, 'Id'): ref_id,
239+
'ValueType': 'http://docs.oasis-open.org/wss/2004/01/'
240+
'oasis-200401-wss-x509-token-profile-1.0#X509v3',
241+
'EncodingType': 'http://docs.oasis-open.org/wss/2004/01/'
242+
'oasis-200401-wss-soap-message-security-1.0#Base64Binary'})
243+
bintok.text = x509_data.find(QName(ns.DS, 'X509Certificate')).text
244+
security.insert(1, bintok)
245+
x509_data.getparent().remove(x509_data)
246+
247+
214248
def verify_envelope(envelope, certfile):
215249
"""Verify WS-Security signature on given SOAP envelope with given cert.
216250
Collapse file

‎tests/test_wsse_signature.py‎

Copy file name to clipboardExpand all lines: tests/test_wsse_signature.py
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,26 @@ def test_signature():
110110
plugin = wsse.Signature(KEY_FILE_PW, KEY_FILE_PW, 'geheim')
111111
envelope, headers = plugin.apply(envelope, {})
112112
plugin.verify(envelope)
113+
114+
115+
@pytest.mark.skipif(sys.platform == 'win32',
116+
reason="does not run on windows")
117+
def test_signature_binary():
118+
envelope = load_xml("""
119+
<soapenv:Envelope
120+
xmlns:tns="http://tests.python-zeep.org/"
121+
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
122+
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
123+
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
124+
<soapenv:Header></soapenv:Header>
125+
<soapenv:Body>
126+
<tns:Function>
127+
<tns:Argument>OK</tns:Argument>
128+
</tns:Function>
129+
</soapenv:Body>
130+
</soapenv:Envelope>
131+
""")
132+
133+
plugin = wsse.BinarySignature(KEY_FILE_PW, KEY_FILE_PW, 'geheim')
134+
envelope, headers = plugin.apply(envelope, {})
135+
plugin.verify(envelope)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.