Skip to content

Commit e594bb8

Browse files
authored
Merge pull request #259 from doticatto/add-get-metadata-timeout
Add get metadata timeout
2 parents a866e22 + 11cc4f5 commit e594bb8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,18 @@ The method above requires a little extra work to manually specify attributes abo
520520

521521
There's an easier method -- use a metadata exchange. Metadata is just an XML file that defines the capabilities of both the IdP and the SP application. It also contains the X.509 public key certificates which add to the trusted relationship. The IdP administrator can also configure custom settings for an SP based on the metadata.
522522

523-
Using ````parse_remote```` IdP metadata can be obtained and added to the settings withouth further ado.
523+
Using ````parse_remote```` IdP metadata can be obtained and added to the settings without further ado.
524524

525525
``
526526
idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote('https://example.com/auth/saml2/idp/metadata')
527527
``
528528

529+
You can specify a timeout in seconds for metadata retrieval, without it is not guaranteed that the request will complete
530+
531+
``
532+
idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote('https://example.com/auth/saml2/idp/metadata', timeout=5)
533+
``
534+
529535
If the Metadata contains several entities, the relevant ``EntityDescriptor`` can be specified when retrieving the settings from the ``IdpMetadataParser`` by its ``entityId`` value:
530536

531537
``idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote(https://example.com/metadatas, entity_id='idp_entity_id')``

src/onelogin/saml2/idp_metadata_parser.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class OneLogin_Saml2_IdPMetadataParser(object):
2626
"""
2727

2828
@classmethod
29-
def get_metadata(cls, url, validate_cert=True):
29+
def get_metadata(cls, url, validate_cert=True, timeout=None):
3030
"""
3131
Gets the metadata XML from the provided URL
3232
:param url: Url where the XML of the Identity Provider Metadata is published.
@@ -35,18 +35,21 @@ def get_metadata(cls, url, validate_cert=True):
3535
:param validate_cert: If the url uses https schema, that flag enables or not the verification of the associated certificate.
3636
:type validate_cert: bool
3737
38+
:param timeout: Timeout in seconds to wait for metadata response
39+
:type timeout: int
40+
3841
:returns: metadata XML
3942
:rtype: string
4043
"""
4144
valid = False
4245

4346
if validate_cert:
44-
response = urllib2.urlopen(url)
47+
response = urllib2.urlopen(url, timeout=timeout)
4548
else:
4649
ctx = ssl.create_default_context()
4750
ctx.check_hostname = False
4851
ctx.verify_mode = ssl.CERT_NONE
49-
response = urllib2.urlopen(url, context=ctx)
52+
response = urllib2.urlopen(url, context=ctx, timeout=timeout)
5053
xml = response.read()
5154

5255
if xml:
@@ -64,7 +67,7 @@ def get_metadata(cls, url, validate_cert=True):
6467
return xml
6568

6669
@classmethod
67-
def parse_remote(cls, url, validate_cert=True, entity_id=None, **kwargs):
70+
def parse_remote(cls, url, validate_cert=True, entity_id=None, timeout=None, **kwargs):
6871
"""
6972
Gets the metadata XML from the provided URL and parse it, returning a dict with extracted data
7073
:param url: Url where the XML of the Identity Provider Metadata is published.
@@ -77,10 +80,13 @@ def parse_remote(cls, url, validate_cert=True, entity_id=None, **kwargs):
7780
that contains multiple EntityDescriptor.
7881
:type entity_id: string
7982
83+
:param timeout: Timeout in seconds to wait for metadata response
84+
:type timeout: int
85+
8086
:returns: settings dict with extracted data
8187
:rtype: dict
8288
"""
83-
idp_metadata = cls.get_metadata(url, validate_cert)
89+
idp_metadata = cls.get_metadata(url, validate_cert, timeout)
8490
return cls.parse(idp_metadata, entity_id=entity_id, **kwargs)
8591

8692
@classmethod

0 commit comments

Comments
 (0)