Skip to content

Commit cf577a4

Browse files
author
Roland Hedberg
committed
Fixed making redirect signature work, cleaned up.
1 parent 3d2fe23 commit cf577a4

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

src/saml2/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ def _parse_request(self, enc_request, request_cls, service, binding):
641641
only_valid_cert = False
642642
if only_valid_cert:
643643
must = True
644-
_request = _request.loads(xmlstr, binding, origdoc=enc_request, must=must,
645-
only_valid_cert=only_valid_cert)
644+
_request = _request.loads(xmlstr, binding, origdoc=enc_request,
645+
must=must, only_valid_cert=only_valid_cert)
646646

647647
_log_debug("Loaded request")
648648

src/saml2/request.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22

33
from attribute_converter import to_local
4-
from saml2 import time_util
4+
from saml2 import time_util, BINDING_HTTP_REDIRECT
55
from saml2.s_utils import OtherError
66

77
from saml2.validate import valid_instance
@@ -38,6 +38,9 @@ def _clear(self):
3838

3939
def _loads(self, xmldata, binding=None, origdoc=None, must=None,
4040
only_valid_cert=False):
41+
if binding == BINDING_HTTP_REDIRECT:
42+
pass
43+
4144
# own copy
4245
self.xmlstr = xmldata[:]
4346
logger.info("xmlstr: %s" % (self.xmlstr,))
@@ -87,8 +90,10 @@ def _verify(self):
8790
assert self.issue_instant_ok()
8891
return self
8992

90-
def loads(self, xmldata, binding, origdoc=None, must=None, only_valid_cert=False):
91-
return self._loads(xmldata, binding, origdoc, must, only_valid_cert=only_valid_cert)
93+
def loads(self, xmldata, binding, origdoc=None, must=None,
94+
only_valid_cert=False):
95+
return self._loads(xmldata, binding, origdoc, must,
96+
only_valid_cert=only_valid_cert)
9297

9398
def verify(self):
9499
try:

src/saml2/server.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
import shelve
1313
import threading
1414

15-
from saml2.eptid import EptidShelve, Eptid
16-
from saml2.saml import EncryptedAssertion
17-
from saml2.sdb import SessionStorage
18-
from saml2.schema import soapenv
19-
20-
from saml2.samlp import NameIDMappingResponse
21-
from saml2.entity import Entity
22-
23-
from saml2 import saml, element_to_extension_element
15+
from saml2 import saml
16+
from saml2 import element_to_extension_element
2417
from saml2 import class_name
2518
from saml2 import BINDING_HTTP_REDIRECT
2619

20+
from saml2.entity import Entity
21+
from saml2.eptid import Eptid
22+
from saml2.eptid import EptidShelve
23+
from saml2.samlp import NameIDMappingResponse
24+
from saml2.sdb import SessionStorage
25+
from saml2.schema import soapenv
26+
2727
from saml2.request import AuthnRequest
2828
from saml2.request import AssertionIDRequest
2929
from saml2.request import AttributeQuery
@@ -33,7 +33,9 @@
3333

3434
from saml2.s_utils import MissingValue, Unknown, rndstr
3535

36-
from saml2.sigver import pre_signature_part, signed_instance_factory, CertificateError, CryptoBackendXmlSec1
36+
from saml2.sigver import pre_signature_part
37+
from saml2.sigver import signed_instance_factory
38+
from saml2.sigver import CertificateError
3739

3840
from saml2.assertion import Assertion
3941
from saml2.assertion import Policy

src/saml2/sigver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def verify(self, msg, sig, key):
608608
RESP_ORDER = ["SAMLResponse", "RelayState", "SigAlg"]
609609

610610

611-
def verify_redirect_signature(saml_msg, cert):
611+
def verify_redirect_signature(saml_msg, cert=None, sigkey=None):
612612
"""
613613
614614
:param saml_msg: A dictionary as produced by parse_qs, means all values are
@@ -622,7 +622,7 @@ def verify_redirect_signature(saml_msg, cert):
622622
except KeyError:
623623
raise Unsupported("Signature algorithm: %s" % saml_msg["SigAlg"])
624624
else:
625-
if saml_msg["SigAlg"][0] == SIG_RSA_SHA1:
625+
if saml_msg["SigAlg"][0] in SIGNER_ALGS:
626626
if "SAMLRequest" in saml_msg:
627627
_order = REQ_ORDER
628628
elif "SAMLResponse" in saml_msg:
@@ -635,7 +635,10 @@ def verify_redirect_signature(saml_msg, cert):
635635
del args["Signature"] # everything but the signature
636636
string = "&".join(
637637
[urllib.urlencode({k: args[k][0]}) for k in _order if k in args])
638-
_key = extract_rsa_key_from_x509_cert(pem_format(cert))
638+
if cert:
639+
_key = extract_rsa_key_from_x509_cert(pem_format(cert))
640+
else:
641+
_key = sigkey
639642
_sign = base64.b64decode(saml_msg["Signature"][0])
640643

641644
return bool(signer.verify(string, _sign, _key))

tests/test_51_client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import base64
55
import urllib
66
import urlparse
7-
from Crypto.PublicKey import RSA
87
from xmldsig import SIG_RSA_SHA256
98
from saml2 import BINDING_HTTP_POST
109
from saml2 import BINDING_HTTP_REDIRECT
@@ -25,7 +24,8 @@
2524
from saml2.saml import NAMEID_FORMAT_TRANSIENT
2625
from saml2.saml import NameID
2726
from saml2.server import Server
28-
from saml2.sigver import pre_encryption_part, rm_xmltag
27+
from saml2.sigver import pre_encryption_part, rm_xmltag, \
28+
verify_redirect_signature
2929
from saml2.s_utils import do_attribute_statement
3030
from saml2.s_utils import factory
3131
from saml2.time_util import in_a_while
@@ -497,7 +497,7 @@ def test_sign_then_encrypt_assertion2(self):
497497
def test_signed_redirect(self):
498498

499499
msg_str = "%s" % self.client.create_authn_request(
500-
"http://www.example.com/sso", message_id="id1")[1]
500+
"http://localhost:8088/sso", message_id="id1")[1]
501501

502502
key = self.client.signkey
503503

@@ -508,7 +508,13 @@ def test_signed_redirect(self):
508508
loc = info["headers"][0][1]
509509
qs = urlparse.parse_qs(loc[1:])
510510
assert _leq(qs.keys(),
511-
['SigAlg', 'SAMLRequest', 'RelayState', 'Signature'])
511+
['SigAlg', 'SAMLRequest', 'RelayState', 'Signature'])
512+
513+
assert verify_redirect_signature(qs, sigkey=key)
514+
515+
res = self.server.parse_authn_request(qs["SAMLRequest"][0],
516+
BINDING_HTTP_REDIRECT)
517+
print res
512518

513519
# Below can only be done with dummy Server
514520
IDP = "urn:mace:example.com:saml:roland:idp"

0 commit comments

Comments
 (0)