Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch IDP metadata using requests to support custom server certificates root CAs #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/onelogin/saml2/idp_metadata_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@


from copy import deepcopy

try:
import urllib.request as urllib2
except ImportError:
import urllib2
from urllib.request import Request, urlopen

import ssl

Expand All @@ -27,7 +23,15 @@ class OneLogin_Saml2_IdPMetadataParser(object):
"""

@classmethod
def get_metadata(cls, url, validate_cert=True, timeout=None, headers=None):
def get_metadata(
cls,
url,
validate_cert=True,
cafile=None,
capath=None,
timeout=None,
headers=None,
):
"""
Gets the metadata XML from the provided URL
:param url: Url where the XML of the Identity Provider Metadata is published.
Expand All @@ -46,15 +50,20 @@ 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:
# Respect the no-TLS-certificate validation option
ctx = None
if not validate_cert:
if cafile or capath:
raise ValueError(
"Specifying 'cafile' or 'capath' while disabling certificate "
"validation is contradictory."
)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urllib2.urlopen(request, context=ctx, timeout=timeout)

request = Request(url, headers=headers or {})
response = urlopen(request, timeout=timeout, cafile=cafile, capath=capath, context=ctx)
xml = response.read()

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


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

from copy import deepcopy
import json
Expand Down
Loading