Skip to content

Commit bb76693

Browse files
committed
Create the signer along with the sigalg allowance check
Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent ec3f598 commit bb76693

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

src/saml2/entity.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from saml2.sigver import pre_encrypt_assertion
7272
from saml2.sigver import signed_instance_factory
7373
from saml2.virtual_org import VirtualOrg
74+
from saml2.pack import http_redirect_message
7475

7576
import saml2.xmldsig as ds
7677

@@ -251,19 +252,14 @@ def apply_binding(
251252
info["method"] = "POST"
252253
elif binding == BINDING_HTTP_REDIRECT:
253254
logger.info("HTTP REDIRECT")
254-
signer = (
255-
self.sec.sec_backend.get_signer(sigalg)
256-
if sign and sigalg
257-
else None
258-
)
259-
info = self.use_http_get(
260-
msg_str,
261-
destination,
262-
relay_state,
263-
typ,
264-
signer=signer,
255+
info = http_redirect_message(
256+
message=msg_str,
257+
location=destination,
258+
relay_state=relay_state,
259+
typ=typ,
260+
sign=sign,
265261
sigalg=sigalg,
266-
**kwargs,
262+
backend=self.sec.sec_backend,
267263
)
268264
info["url"] = str(destination)
269265
info["method"] = "GET"

src/saml2/httpbase.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -388,25 +388,3 @@ def send_using_soap(self, request, destination, headers=None, sign=False):
388388
def add_credentials(self, user, passwd):
389389
self.user = user
390390
self.passwd = passwd
391-
392-
@staticmethod
393-
def use_http_get(message, destination, relay_state,
394-
typ="SAMLRequest", sigalg="", signer=None, **kwargs):
395-
"""
396-
Send a message using GET, this is the HTTP-Redirect case so
397-
no direct response is expected to this request.
398-
399-
:param message:
400-
:param destination:
401-
:param relay_state:
402-
:param typ: Whether a Request, Response or Artifact
403-
:param sigalg: Which algorithm the signature function will use to sign
404-
the message
405-
:param signer: A signing function that can be used to sign the message
406-
:return: dictionary
407-
"""
408-
if not isinstance(message, six.string_types):
409-
message = "%s" % (message,)
410-
411-
return http_redirect_message(message, destination, relay_state, typ,
412-
sigalg, signer)

src/saml2/pack.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,15 @@ def http_post_message(message, relay_state="", typ="SAMLRequest", **kwargs):
141141
"status": 200}
142142

143143

144-
def http_redirect_message(message, location, relay_state="", typ="SAMLRequest",
145-
sigalg='', signer=None, **kwargs):
144+
def http_redirect_message(
145+
message,
146+
location,
147+
relay_state="",
148+
typ="SAMLRequest",
149+
sigalg=None,
150+
sign=None,
151+
backend=None,
152+
):
146153
"""The HTTP Redirect binding defines a mechanism by which SAML protocol
147154
messages can be transmitted within URL parameters.
148155
Messages are encoded for use with this binding using a URL encoding
@@ -156,7 +163,7 @@ def http_redirect_message(message, location, relay_state="", typ="SAMLRequest",
156163
:param typ: What type of message it is SAMLRequest/SAMLResponse/SAMLart
157164
:param sigalg: Which algorithm the signature function will use to sign
158165
the message
159-
:param signer: A signature function that can be used to sign the message
166+
:param sign: Whether the message should be signed
160167
:return: A tuple containing header information and a HTML message.
161168
"""
162169

@@ -178,19 +185,18 @@ def http_redirect_message(message, location, relay_state="", typ="SAMLRequest",
178185
if relay_state:
179186
args["RelayState"] = relay_state
180187

181-
# XXX !should not depend on signer, but on sign
182-
# XXX if both signalg and signer are here they have to match
183-
# XXX now we allow them to differ
184-
# XXX signer should be created here; not passed in
185-
if signer:
188+
if sign:
186189
# XXX check for allowed algo -- should do the same for POST binding
187190
# sigalgs, should be one defined in xmldsig
188191
if sigalg not in [long_name for short_name, long_name in SIG_ALLOWED_ALG]:
189192
raise Exception(
190193
"Signature algo not in allowed list: {algo}".format(algo=sigalg)
191194
)
192-
args["SigAlg"] = sigalg
195+
signer = backend.get_signer(sigalg) if sign and sigalg else None
196+
if not signer:
197+
raise Exception("Could not init signer fro algo {algo}".format(algo=sigalg))
193198

199+
args["SigAlg"] = sigalg
194200
string = "&".join(urlencode({k: args[k]}) for k in _order if k in args)
195201
string_enc = string.encode('ascii')
196202
args["Signature"] = base64.b64encode(signer.sign(string_enc))

tests/test_70_redirect_signing.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ def test():
3030
destination = srvs[0]["location"]
3131
req_id, req = sp.create_authn_request(destination, id="id1")
3232

33-
signer = sp.sec.sec_backend.get_signer(SIG_RSA_SHA1)
34-
35-
info = http_redirect_message(req, destination, relay_state="RS",
36-
typ="SAMLRequest", sigalg=SIG_RSA_SHA1,
37-
signer=signer)
33+
info = http_redirect_message(
34+
req,
35+
destination,
36+
relay_state="RS",
37+
typ="SAMLRequest",
38+
sigalg=SIG_RSA_SHA1,
39+
sign=True,
40+
backend=sp.sec.sec_backend,
41+
)
3842

3943
verified_ok = False
4044

0 commit comments

Comments
 (0)