Skip to content

Commit

Permalink
Merge pull request #18 from maykinmedia/issue/support-self-signed-cer…
Browse files Browse the repository at this point in the history
…tificates

Support self signed certificates
  • Loading branch information
sergei-maertens authored Apr 2, 2024
2 parents e82e68d + a5a90e8 commit aa4f25e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
23 changes: 7 additions & 16 deletions src/onelogin/saml2/idp_metadata_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

from copy import deepcopy

try:
import urllib.request as urllib2
except ImportError:
import urllib2

import ssl
import requests

from onelogin.saml2.constants import OneLogin_Saml2_Constants
from onelogin.saml2.xml_utils import OneLogin_Saml2_XML
Expand Down Expand Up @@ -46,16 +41,12 @@ def get_metadata(cls, url, validate_cert=True, timeout=None, headers=None):
"""
valid = False

request = urllib2.Request(url, headers=headers or {})

if validate_cert:
response = urllib2.urlopen(request, timeout=timeout)
else:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib2.urlopen(request, context=ctx, timeout=timeout)
xml = response.read()
# MAYKIN: use requests to retrieve the metadata, so the CA bundle configured for
# requests can be used and self-signed/private root certificates still continue
# to work.
response = requests.get(url, headers=headers, verify=validate_cert, timeout=timeout)
response.raise_for_status()
xml = response.content

if xml:
try:
Expand Down
15 changes: 5 additions & 10 deletions tests/src/OneLogin/saml2_tests/idp_metadata_parser_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# -*- coding: utf-8 -*-


try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError

from copy import deepcopy
import json
from os.path import dirname, join, exists
from lxml.etree import XMLSyntaxError
from requests import RequestException
import unittest

from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser
Expand Down Expand Up @@ -51,7 +46,7 @@ def testGetMetadata(self):
try:
data = OneLogin_Saml2_IdPMetadataParser.get_metadata('https://idp.testshib.org/idp/shibboleth')
self.assertTrue(data is not None and data is not {})
except URLError:
except RequestException:
pass

def testGetMetadataWithHeaders(self):
Expand All @@ -69,7 +64,7 @@ def testParseRemote(self):

try:
data = OneLogin_Saml2_IdPMetadataParser.parse_remote('https://idp.testshib.org/idp/shibboleth')
except URLError:
except RequestException:
xml = self.file_contents(join(self.data_path, 'metadata', 'testshib-providers.xml'))
data = OneLogin_Saml2_IdPMetadataParser.parse(xml)

Expand Down Expand Up @@ -172,7 +167,7 @@ def test_parse_testshib_required_binding_sso_redirect(self):
try:
xmldoc = OneLogin_Saml2_IdPMetadataParser.get_metadata(
'https://idp.testshib.org/idp/shibboleth')
except URLError:
except RequestException:
xmldoc = self.file_contents(join(self.data_path, 'metadata', 'testshib-providers.xml'))

# Parse, require SSO REDIRECT binding, implicitly.
Expand Down Expand Up @@ -215,7 +210,7 @@ def test_parse_testshib_required_binding_sso_post(self):
try:
xmldoc = OneLogin_Saml2_IdPMetadataParser.get_metadata(
'https://idp.testshib.org/idp/shibboleth')
except URLError:
except RequestException:
xmldoc = self.file_contents(join(self.data_path, 'metadata', 'testshib-providers.xml'))

# Parse, require POST binding.
Expand Down

0 comments on commit aa4f25e

Please sign in to comment.