Skip to content

Commit 602f113

Browse files
committed
Add get_session_index to auth class
1 parent 8445fc6 commit 602f113

2 files changed

Lines changed: 53 additions & 22 deletions

File tree

src/onelogin/saml2/auth.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def __init__(self, request_data, old_settings=None, custom_base_path=None):
4747
self.__settings = OneLogin_Saml2_Settings(old_settings, custom_base_path)
4848
self.__attributes = []
4949
self.__nameid = None
50+
self.__session_index = None
5051
self.__authenticated = False
5152
self.__errors = []
5253

@@ -86,7 +87,9 @@ def process_response(self, request_id=None):
8687
if response.is_valid(self.__request_data, request_id):
8788
self.__attributes = response.get_attributes()
8889
self.__nameid = response.get_nameid()
90+
self.__session_index = response.get_session_index()
8991
self.__authenticated = True
92+
9093
else:
9194
self.__errors.append('invalid_response')
9295

@@ -192,6 +195,14 @@ def get_nameid(self):
192195
"""
193196
return self.__nameid
194197

198+
def get_session_index(self):
199+
"""
200+
Returns the SessionIndex from the AuthnStatement.
201+
:returns: The SessionIndex of the assertion
202+
:rtype: string
203+
"""
204+
return self.__session_index
205+
195206
def get_errors(self):
196207
"""
197208
Returns a list with code errors if something went wrong

tests/src/OneLogin/saml2_tests/auth_test.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_request(self):
4343

4444
def testGetSettings(self):
4545
"""
46-
Tests the getSettings method of the OneLogin_Saml2_Auth class
46+
Tests the get_settings method of the OneLogin_Saml2_Auth class
4747
Build a OneLogin_Saml2_Settings object with a setting array
4848
and compare the value returned from the method of the
4949
auth object
@@ -57,7 +57,7 @@ def testGetSettings(self):
5757

5858
def testGetSSOurl(self):
5959
"""
60-
Tests the getSSOurl method of the OneLogin_Saml2_Auth class
60+
Tests the get_sso_url method of the OneLogin_Saml2_Auth class
6161
"""
6262
settings_info = self.loadSettingsJSON()
6363
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
@@ -67,14 +67,34 @@ def testGetSSOurl(self):
6767

6868
def testGetSLOurl(self):
6969
"""
70-
Tests the getSLOurl method of the OneLogin_Saml2_Auth class
70+
Tests the get_slo_url method of the OneLogin_Saml2_Auth class
7171
"""
7272
settings_info = self.loadSettingsJSON()
7373
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
7474

7575
slo_url = settings_info['idp']['singleLogoutService']['url']
7676
self.assertEqual(auth.get_slo_url(), slo_url)
7777

78+
def testGetSessionIndex(self):
79+
"""
80+
Tests the get_session_index method of the OneLogin_Saml2_Auth class
81+
"""
82+
settings_info = self.loadSettingsJSON()
83+
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
84+
self.assertIsNone(auth.get_session_index())
85+
86+
request_data = self.get_request()
87+
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
88+
del request_data['get_data']
89+
request_data['post_data'] = {
90+
'SAMLResponse': message
91+
}
92+
auth2 = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
93+
self.assertIsNone(auth2.get_session_index())
94+
95+
auth2.process_response()
96+
self.assertEqual('_6273d77b8cde0c333ec79d22a9fa0003b9fe2d75cb', auth2.get_session_index())
97+
7898
def testProcessNoResponse(self):
7999
"""
80100
Tests the processResponse method of the OneLogin_Saml2_Auth class
@@ -92,7 +112,7 @@ def testProcessNoResponse(self):
92112

93113
def testProcessResponseInvalid(self):
94114
"""
95-
Tests the processResponse method of the OneLogin_Saml2_Auth class
115+
Tests the process_response method of the OneLogin_Saml2_Auth class
96116
Case Invalid Response, After processing the response the user
97117
is not authenticated, attributes are notreturned, no nameID and
98118
the error array is not empty, contains 'invalid_response
@@ -114,7 +134,7 @@ def testProcessResponseInvalid(self):
114134

115135
def testProcessResponseInvalidRequestId(self):
116136
"""
117-
Tests the processResponse method of the OneLogin_Saml2_Auth class
137+
Tests the process_response method of the OneLogin_Saml2_Auth class
118138
Case Invalid Response, Invalid requestID
119139
"""
120140
request_data = self.get_request()
@@ -141,7 +161,7 @@ def testProcessResponseInvalidRequestId(self):
141161

142162
def testProcessResponseValid(self):
143163
"""
144-
Tests the processResponse method of the OneLogin_Saml2_Auth class
164+
Tests the process_response method of the OneLogin_Saml2_Auth class
145165
Case Valid Response, After processing the response the user
146166
is authenticated, attributes are returned, also has a nameID and
147167
the error array is empty
@@ -172,7 +192,7 @@ def testProcessResponseValid(self):
172192

173193
def testRedirectTo(self):
174194
"""
175-
Tests the redirectTo method of the OneLogin_Saml2_Auth class
195+
Tests the redirect_to method of the OneLogin_Saml2_Auth class
176196
(phpunit raises an exception when a redirect is executed, the
177197
exception is catched and we check that the targetURL is correct)
178198
Case redirect without url parameter
@@ -186,7 +206,7 @@ def testRedirectTo(self):
186206

187207
def testRedirectTowithUrl(self):
188208
"""
189-
Tests the redirectTo method of the OneLogin_Saml2_Auth class
209+
Tests the redirect_to method of the OneLogin_Saml2_Auth class
190210
(phpunit raises an exception when a redirect is executed, the
191211
exception is catched and we check that the targetURL is correct)
192212
Case redirect with url parameter
@@ -201,7 +221,7 @@ def testRedirectTowithUrl(self):
201221

202222
def testProcessNoSLO(self):
203223
"""
204-
Tests the processSLO method of the OneLogin_Saml2_Auth class
224+
Tests the process_slo method of the OneLogin_Saml2_Auth class
205225
Case No Message, An exception is throw
206226
"""
207227
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=self.loadSettingsJSON())
@@ -213,7 +233,7 @@ def testProcessNoSLO(self):
213233

214234
def testProcessSLOResponseInvalid(self):
215235
"""
216-
Tests the processSLO method of the OneLogin_Saml2_Auth class
236+
Tests the process_slo method of the OneLogin_Saml2_Auth class
217237
Case Invalid Logout Response
218238
"""
219239
request_data = self.get_request()
@@ -235,7 +255,7 @@ def testProcessSLOResponseInvalid(self):
235255

236256
def testProcessSLOResponseNoSucess(self):
237257
"""
238-
Tests the processSLO method of the OneLogin_Saml2_Auth class
258+
Tests the process_slo method of the OneLogin_Saml2_Auth class
239259
Case Logout Response not sucess
240260
"""
241261
request_data = self.get_request()
@@ -254,7 +274,7 @@ def testProcessSLOResponseNoSucess(self):
254274

255275
def testProcessSLOResponseRequestId(self):
256276
"""
257-
Tests the processSLO method of the OneLogin_Saml2_Auth class
277+
Tests the process_slo method of the OneLogin_Saml2_Auth class
258278
Case Logout Response with valid and invalid Request ID
259279
"""
260280
request_data = self.get_request()
@@ -278,7 +298,7 @@ def testProcessSLOResponseRequestId(self):
278298

279299
def testProcessSLOResponseValid(self):
280300
"""
281-
Tests the processSLO method of the OneLogin_Saml2_Auth class
301+
Tests the process_slo method of the OneLogin_Saml2_Auth class
282302
Case Valid Logout Response
283303
"""
284304
request_data = self.get_request()
@@ -308,7 +328,7 @@ def testProcessSLOResponseValid(self):
308328

309329
def testProcessSLOResponseValidDeletingSession(self):
310330
"""
311-
Tests the processSLO method of the OneLogin_Saml2_Auth class
331+
Tests the process_slo method of the OneLogin_Saml2_Auth class
312332
Case Valid Logout Response, validating deleting the local session
313333
"""
314334
request_data = self.get_request()
@@ -338,7 +358,7 @@ def testProcessSLOResponseValidDeletingSession(self):
338358

339359
def testProcessSLORequestInvalidValid(self):
340360
"""
341-
Tests the processSLO method of the OneLogin_Saml2_Auth class
361+
Tests the process_slo method of the OneLogin_Saml2_Auth class
342362
Case Invalid Logout Request
343363
"""
344364
settings_info = self.loadSettingsJSON()
@@ -370,7 +390,7 @@ def testProcessSLORequestInvalidValid(self):
370390

371391
def testProcessSLORequestNotOnOrAfterFailed(self):
372392
"""
373-
Tests the processSLO method of the OneLogin_Saml2_Auth class
393+
Tests the process_slo method of the OneLogin_Saml2_Auth class
374394
Case Logout Request NotOnOrAfter failed
375395
"""
376396
request_data = self.get_request()
@@ -389,7 +409,7 @@ def testProcessSLORequestNotOnOrAfterFailed(self):
389409

390410
def testProcessSLORequestDeletingSession(self):
391411
"""
392-
Tests the processSLO method of the OneLogin_Saml2_Auth class
412+
Tests the process_slo method of the OneLogin_Saml2_Auth class
393413
Case Valid Logout Request, validating that the local session is deleted,
394414
a LogoutResponse is created and a redirection executed
395415
"""
@@ -437,7 +457,7 @@ def testProcessSLORequestDeletingSession(self):
437457

438458
def testProcessSLORequestRelayState(self):
439459
"""
440-
Tests the processSLO method of the OneLogin_Saml2_Auth class
460+
Tests the process_slo method of the OneLogin_Saml2_Auth class
441461
Case Valid Logout Request, validating the relayState,
442462
a LogoutResponse is created and a redirection executed
443463
"""
@@ -464,7 +484,7 @@ def testProcessSLORequestRelayState(self):
464484

465485
def testProcessSLORequestSignedResponse(self):
466486
"""
467-
Tests the processSLO method of the OneLogin_Saml2_Auth class
487+
Tests the process_slo method of the OneLogin_Saml2_Auth class
468488
Case Valid Logout Request, validating the relayState,
469489
a signed LogoutResponse is created and a redirection executed
470490
"""
@@ -627,7 +647,7 @@ def testLogoutNoSLO(self):
627647

628648
def testSetStrict(self):
629649
"""
630-
Tests the setStrict method of the OneLogin_Saml2_Auth
650+
Tests the set_strict method of the OneLogin_Saml2_Auth
631651
"""
632652
settings_info = self.loadSettingsJSON()
633653
settings_info['strict'] = False
@@ -652,7 +672,7 @@ def testSetStrict(self):
652672

653673
def testBuildRequestSignature(self):
654674
"""
655-
Tests the buildRequestSignature method of the OneLogin_Saml2_Auth
675+
Tests the build_request_signature method of the OneLogin_Saml2_Auth
656676
"""
657677
settings = self.loadSettingsJSON()
658678
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
@@ -673,7 +693,7 @@ def testBuildRequestSignature(self):
673693

674694
def testBuildResponseSignature(self):
675695
"""
676-
Tests the buildResponseSignature method of the OneLogin_Saml2_Auth
696+
Tests the build_response_signature method of the OneLogin_Saml2_Auth
677697
"""
678698
settings = self.loadSettingsJSON()
679699
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))

0 commit comments

Comments
 (0)