Skip to content

Commit 6663bef

Browse files
committed
Improve get_metadata method from Parser, allowing to set timeouts and headers
1 parent ffc4f0b commit 6663bef

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ The method above requires a little extra work to manually specify attributes abo
589589

590590
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.
591591

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

594594
But take in mind that the OneLogin_Saml2_IdPMetadataParser class does not validate in any way the URL that is introduced in order to be parsed.
595595

@@ -598,9 +598,15 @@ Usually the same administrator that handles the Service Provider also sets the U
598598
But there are other scenarios, like a SAAS app where the administrator of the app delegates this functionality to other users. In this case, extra precaution should be taken in order to validate such URL inputs and avoid attacks like SSRF.
599599

600600

601-
``
601+
```
602602
idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote('https://example.com/auth/saml2/idp/metadata')
603-
``
603+
```
604+
605+
You can specify a timeout in seconds for metadata retrieval, if not it is not guaranteed that the request will complete
606+
607+
```
608+
idp_data = OneLogin_Saml2_IdPMetadataParser.parse_remote('https://example.com/auth/saml2/idp/metadata', timeout=5)
609+
```
604610

605611
If the Metadata contains several entities, the relevant ``EntityDescriptor`` can be specified when retrieving the settings from the ``IdpMetadataParser`` by its ``EntityId`` value:
606612
```

src/onelogin/saml2/idp_metadata_parser.py

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

2929
@staticmethod
30-
def get_metadata(url, validate_cert=True):
30+
def get_metadata(url, validate_cert=True, timeout=None, headers=None):
3131
"""
3232
Gets the metadata XML from the provided URL
3333
@@ -37,17 +37,26 @@ def get_metadata(url, validate_cert=True):
3737
:param validate_cert: If the url uses https schema, that flag enables or not the verification of the associated certificate.
3838
:type validate_cert: bool
3939
40+
:param timeout: Timeout in seconds to wait for metadata response
41+
:type timeout: int
42+
43+
:param headers: Extra headers to send in the request
44+
:type headers: dict
45+
4046
:returns: metadata XML
4147
:rtype: string
4248
"""
4349
valid = False
50+
51+
request = urllib2.Request(url, headers=headers or {})
52+
4453
if validate_cert:
45-
response = urllib2.urlopen(url)
54+
response = urllib2.urlopen(request, timeout=timeout)
4655
else:
4756
ctx = ssl.create_default_context()
4857
ctx.check_hostname = False
4958
ctx.verify_mode = ssl.CERT_NONE
50-
response = urllib2.urlopen(url, context=ctx)
59+
response = urllib2.urlopen(request, context=ctx, timeout=timeout)
5160
xml = response.read()
5261

5362
if xml:
@@ -65,7 +74,7 @@ def get_metadata(url, validate_cert=True):
6574
return xml
6675

6776
@staticmethod
68-
def parse_remote(url, validate_cert=True, entity_id=None, **kwargs):
77+
def parse_remote(url, validate_cert=True, entity_id=None, timeout=None, **kwargs):
6978
"""
7079
Gets the metadata XML from the provided URL and parse it, returning a dict with extracted data
7180
@@ -79,10 +88,13 @@ def parse_remote(url, validate_cert=True, entity_id=None, **kwargs):
7988
that contains multiple EntityDescriptor.
8089
:type entity_id: string
8190
91+
:param timeout: Timeout in seconds to wait for metadata response
92+
:type timeout: int
93+
8294
:returns: settings dict with extracted data
8395
:rtype: dict
8496
"""
85-
idp_metadata = OneLogin_Saml2_IdPMetadataParser.get_metadata(url, validate_cert)
97+
idp_metadata = OneLogin_Saml2_IdPMetadataParser.get_metadata(url, validate_cert, timeout, headers=kwargs.pop('headers', None))
8698
return OneLogin_Saml2_IdPMetadataParser.parse(idp_metadata, entity_id=entity_id, **kwargs)
8799

88100
@staticmethod

0 commit comments

Comments
 (0)