Skip to content

Commit 9fdb11d

Browse files
committed
Adding get_idp_sso_url, get_idp_slo_url and get_idp_slo_response_url methods to the Settings class and use it in the toolkit
1 parent ae5cae2 commit 9fdb11d

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

src/onelogin/saml2/auth.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ def get_sso_url(self):
454454
:returns: An URL, the SSO endpoint of the IdP
455455
:rtype: string
456456
"""
457-
idp_data = self.__settings.get_idp_data()
458-
return idp_data['singleSignOnService']['url']
457+
return self.__settings.get_idp_sso_url()
459458

460459
def get_slo_url(self):
461460
"""
@@ -464,9 +463,7 @@ def get_slo_url(self):
464463
:returns: An URL, the SLO endpoint of the IdP
465464
:rtype: string
466465
"""
467-
idp_data = self.__settings.get_idp_data()
468-
if 'url' in idp_data['singleLogoutService']:
469-
return idp_data['singleLogoutService']['url']
466+
return self.__settings.get_idp_slo_url()
470467

471468
def get_slo_response_url(self):
472469
"""
@@ -475,8 +472,7 @@ def get_slo_response_url(self):
475472
:returns: an URL, the SLO return endpoint of the IdP
476473
:rtype: string
477474
"""
478-
slo_data = self.__settings.get_idp_data()['singleLogoutService']
479-
return slo_data.get('responseUrl', self.get_slo_url())
475+
return self.__settings.get_idp_slo_response_url()
480476

481477
def add_request_signature(self, request_data, sign_algorithm=OneLogin_Saml2_Constants.RSA_SHA1):
482478
"""

src/onelogin/saml2/logout_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(self, settings, request=None, name_id=None, session_index=None, nq=
110110
{
111111
'id': uid,
112112
'issue_instant': issue_instant,
113-
'single_logout_url': idp_data['singleLogoutService']['url'],
113+
'single_logout_url': self.__settings.get_idp_slo_response_url(),
114114
'entity_id': sp_data['entityId'],
115115
'name_id': name_id_obj,
116116
'session_index': session_index_str,

src/onelogin/saml2/logout_response.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,12 @@ def build(self, in_response_to):
162162

163163
uid = OneLogin_Saml2_Utils.generate_unique_id()
164164
issue_instant = OneLogin_Saml2_Utils.parse_time_to_SAML(OneLogin_Saml2_Utils.now())
165-
destination = idp_data['singleLogoutService'].get('responseUrl', idp_data['singleLogoutService']['url'])
166165

167166
logout_response = OneLogin_Saml2_Templates.LOGOUT_RESPONSE % \
168167
{
169168
'id': uid,
170169
'issue_instant': issue_instant,
171-
'destination': destination,
170+
'destination': self.__settings.get_idp_slo_response_url(),
172171
'in_response_to': in_response_to,
173172
'entity_id': sp_data['entityId'],
174173
'status': "urn:oasis:names:tc:SAML:2.0:status:Success"

src/onelogin/saml2/settings.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,37 @@ def check_sp_certs(self):
506506
cert = self.get_sp_cert()
507507
return key is not None and cert is not None
508508

509+
def get_idp_sso_url(self):
510+
"""
511+
Gets the IdP SSO URL.
512+
513+
:returns: An URL, the SSO endpoint of the IdP
514+
:rtype: string
515+
"""
516+
idp_data = self.get_idp_data()
517+
return idp_data['singleSignOnService']['url']
518+
519+
def get_idp_slo_url(self):
520+
"""
521+
Gets the IdP SLO URL.
522+
523+
:returns: An URL, the SLO endpoint of the IdP
524+
:rtype: string
525+
"""
526+
idp_data = self.get_idp_data()
527+
if 'url' in idp_data['singleLogoutService']:
528+
return idp_data['singleLogoutService']['url']
529+
530+
def get_idp_slo_response_url(self):
531+
"""
532+
Gets the IdP SLO return URL for IdP-initiated logout.
533+
534+
:returns: an URL, the SLO return endpoint of the IdP
535+
:rtype: string
536+
"""
537+
slo_data = self.get_idp_data()['singleLogoutService']
538+
return slo_data.get('responseUrl', self.get_idp_slo_url())
539+
509540
def get_sp_key(self):
510541
"""
511542
Returns the x509 private key of the SP.

tests/src/OneLogin/saml2_tests/settings_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,41 @@ def testGetSchemasPath(self):
165165
base = settings.get_base_path()
166166
self.assertEqual(join(base, 'lib', 'schemas') + sep, settings.get_schemas_path())
167167

168+
def testGetIdPSSOurl(self):
169+
"""
170+
Tests the get_idp_sso_url method of the OneLogin_Saml2_Settings class
171+
"""
172+
settings_info = self.loadSettingsJSON()
173+
settings = OneLogin_Saml2_Settings(settings_info)
174+
175+
sso_url = settings_info['idp']['singleSignOnService']['url']
176+
self.assertEqual(settings.get_idp_sso_url(), sso_url)
177+
178+
def testGetIdPSLOurl(self):
179+
"""
180+
Tests the get_idp_slo_url method of the OneLogin_Saml2_Settings class
181+
"""
182+
settings_info = self.loadSettingsJSON()
183+
settings = OneLogin_Saml2_Settings(settings_info)
184+
185+
slo_url = settings_info['idp']['singleLogoutService']['url']
186+
self.assertEqual(settings.get_idp_slo_url(), slo_url)
187+
188+
def testGetIdPSLOresponseUrl(self):
189+
"""
190+
Tests the get_idp_slo_response_url method of the OneLogin_Saml2_Settings class
191+
"""
192+
settings_info = self.loadSettingsJSON()
193+
settings_info['idp']['singleLogoutService']['responseUrl'] = "http://idp.example.com/SingleLogoutReturn.php"
194+
settings = OneLogin_Saml2_Settings(settings_info)
195+
slo_url = settings_info['idp']['singleLogoutService']['responseUrl']
196+
self.assertEqual(settings.get_idp_slo_response_url(), slo_url)
197+
# test that the function falls back to the url setting if responseUrl is not set
198+
settings_info['idp']['singleLogoutService'].pop('responseUrl')
199+
settings = OneLogin_Saml2_Settings(settings_info)
200+
slo_url = settings_info['idp']['singleLogoutService']['url']
201+
self.assertEqual(settings.get_idp_slo_response_url(), slo_url)
202+
168203
def testGetSPCert(self):
169204
"""
170205
Tests the get_sp_cert method of the OneLogin_Saml2_Settings

0 commit comments

Comments
 (0)