Skip to content

Commit f7a9652

Browse files
committed
SAML-Toolkits#56. Be able to relax SSL Certificate verification when retrieving idp metadata
1 parent 8422212 commit f7a9652

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/onelogin/saml2/idp_metadata_parser.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
except ImportError:
1515
import urllib2
1616

17+
import ssl
18+
1719
from onelogin.saml2.constants import OneLogin_Saml2_Constants
1820
from onelogin.saml2.xml_utils import OneLogin_Saml2_XML
1921
from onelogin.saml2.utils import OneLogin_Saml2_Utils
@@ -25,7 +27,7 @@ class OneLogin_Saml2_IdPMetadataParser(object):
2527
"""
2628

2729
@staticmethod
28-
def get_metadata(url):
30+
def get_metadata(url, validate_cert=True):
2931
"""
3032
Gets the metadata XML from the provided URL
3133
:param url: Url where the XML of the Identity Provider Metadata is published.
@@ -34,7 +36,14 @@ def get_metadata(url):
3436
:rtype: string
3537
"""
3638
valid = False
37-
response = urllib2.urlopen(url)
39+
40+
if validate_cert:
41+
response = urllib2.urlopen(url)
42+
else:
43+
ctx = ssl.create_default_context()
44+
ctx.check_hostname = False
45+
ctx.verify_mode = ssl.CERT_NONE
46+
response = urllib2.urlopen(url, context=ctx)
3847
xml = response.read()
3948

4049
if xml:
@@ -52,15 +61,15 @@ def get_metadata(url):
5261
return xml
5362

5463
@staticmethod
55-
def parse_remote(url, **kwargs):
64+
def parse_remote(url, validate_cert=True, **kwargs):
5665
"""
5766
Gets the metadata XML from the provided URL and parse it, returning a dict with extracted data
5867
:param url: Url where the XML of the Identity Provider Metadata is published.
5968
:type url: string
6069
:returns: settings dict with extracted data
6170
:rtype: dict
6271
"""
63-
idp_metadata = OneLogin_Saml2_IdPMetadataParser.get_metadata(url)
72+
idp_metadata = OneLogin_Saml2_IdPMetadataParser.get_metadata(url, validate_cert)
6473
return OneLogin_Saml2_IdPMetadataParser.parse(idp_metadata, **kwargs)
6574

6675
@staticmethod

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,4 +1411,4 @@ def testStatusCheckBeforeAssertionCheck(self):
14111411
xml = self.file_contents(join(self.data_path, 'responses', 'invalids', 'status_code_responder.xml.base64'))
14121412
response = OneLogin_Saml2_Response(settings, xml)
14131413
with self.assertRaisesRegexp(Exception, 'The status code of the Response was not Success, was Responder'):
1414-
response.is_valid(self.get_request_data(), raise_exceptions=True)
1414+
response.is_valid(self.get_request_data(), raise_exceptions=True)

0 commit comments

Comments
 (0)