Skip to content

Commit 185ebbd

Browse files
committed
Fix #106. Make Request ids accesible
1 parent 9f45619 commit 185ebbd

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,12 @@ The login method can recieve 2 more optional parameters:
496496
* force_authn When true the AuthNReuqest will set the ForceAuthn='true'
497497
* is_passive When true the AuthNReuqest will set the Ispassive='true'
498498

499+
If a match on the future SAMLResponse ID and the AuthNRequest ID to be sent is required, that AuthNRequest ID must to be extracted and stored for future validation, we can get that ID by
500+
501+
```python
502+
auth.get_last_request_id()
503+
```
504+
499505
#### The SP Endpoints ####
500506

501507
Related to the SP there are 3 important endpoints: The metadata view, the ACS view and the SLS view.
@@ -679,6 +685,12 @@ Also there are 2 optional parameters that can be set:
679685
SAML Response with a NameId, then this NameId will be used.
680686
* session_index. SessionIndex that identifies the session of the user.
681687

688+
If a match on the LogoutResponse ID and the LogoutRequest ID to be sent is required, that LogoutRequest ID must to be extracted and stored for future validation, we can get that ID by
689+
690+
```python
691+
auth.get_last_request_id()
692+
```
693+
682694
####Example of a view that initiates the SSO request and handles the response (is the acs target)####
683695

684696
We can code a unique file that initiates the SSO process, handle the response, get the attributes, initiate the slo and processes the logout response.
@@ -754,6 +766,7 @@ Main class of OneLogin Python Toolkit
754766
* ***get_last_error_reason*** Returns the reason of the last error
755767
* ***get_sso_url*** Gets the SSO url.
756768
* ***get_slo_url*** Gets the SLO url.
769+
* ***get_last_request_id*** The ID of the last Request SAML message generated.
757770
* ***build_request_signature*** Builds the Signature of the SAML Request.
758771
* ***build_response_signature*** Builds the Signature of the SAML Response.
759772
* ***get_settings*** Returns the settings info.

demo-django/demo/views.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def index(request):
3939

4040
if 'sso' in req['get_data']:
4141
return HttpResponseRedirect(auth.login())
42+
# If AuthNRequest ID need to be stored in order to later validate it, do instead
43+
# sso_built_url = auth.login()
44+
# request.session['AuthNRequestID'] = auth.get_last_request_id()
45+
# return HttpResponseRedirect(sso_built_url)
4246
elif 'sso2' in req['get_data']:
4347
return_to = OneLogin_Saml2_Utils.get_self_url(req) + reverse('attrs')
4448
return HttpResponseRedirect(auth.login(return_to))
@@ -51,19 +55,33 @@ def index(request):
5155
session_index = request.session['samlSessionIndex']
5256

5357
return HttpResponseRedirect(auth.logout(name_id=name_id, session_index=session_index))
58+
59+
# If LogoutRequest ID need to be stored in order to later validate it, do instead
60+
# slo_built_url = auth.logout(name_id=name_id, session_index=session_index)
61+
# request.session['LogoutRequestID'] = auth.get_last_request_id()
62+
#return HttpResponseRedirect(slo_built_url)
5463
elif 'acs' in req['get_data']:
55-
auth.process_response()
64+
request_id = None
65+
if 'AuthNRequestID' in request.session:
66+
request_id = request.session['AuthNRequestID']
67+
68+
auth.process_response(request_id=request_id)
5669
errors = auth.get_errors()
5770
not_auth_warn = not auth.is_authenticated()
5871
if not errors:
72+
if 'AuthNRequestID' in request.session:
73+
del request.session['AuthNRequestID']
5974
request.session['samlUserdata'] = auth.get_attributes()
6075
request.session['samlNameId'] = auth.get_nameid()
6176
request.session['samlSessionIndex'] = auth.get_session_index()
6277
if 'RelayState' in req['post_data'] and OneLogin_Saml2_Utils.get_self_url(req) != req['post_data']['RelayState']:
6378
return HttpResponseRedirect(auth.redirect_to(req['post_data']['RelayState']))
6479
elif 'sls' in req['get_data']:
80+
request_id = None
81+
if 'LogoutRequestID' in request.session:
82+
request_id = request.session['LogoutRequestID']
6583
dscb = lambda: request.session.flush()
66-
url = auth.process_slo(delete_session_cb=dscb)
84+
url = auth.process_slo(request_id=request_id, delete_session_cb=dscb)
6785
errors = auth.get_errors()
6886
if len(errors) == 0:
6987
if url is not None:

src/onelogin/saml2/auth.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(self, request_data, old_settings=None, custom_base_path=None):
5858
self.__authenticated = False
5959
self.__errors = []
6060
self.__error_reason = None
61+
self.__last_request_id = None
6162

6263
def get_settings(self):
6364
"""
@@ -151,6 +152,8 @@ def process_slo(self, keep_local_session=False, request_id=None, delete_session_
151152
parameters = {'SAMLResponse': logout_response}
152153
if 'RelayState' in self.__request_data['get_data']:
153154
parameters['RelayState'] = self.__request_data['get_data']['RelayState']
155+
else:
156+
parameters['RelayState'] = OneLogin_Saml2_Utils.get_self_url_no_query(self.__request_data)
154157

155158
security = self.__settings.get_security_data()
156159
if 'logoutResponseSigned' in security and security['logoutResponseSigned']:
@@ -257,6 +260,13 @@ def get_attribute(self, name):
257260
value = self.__attributes[name]
258261
return value
259262

263+
def get_last_request_id(self):
264+
"""
265+
:returns: The ID of the last Request SAML message generated.
266+
:rtype: string
267+
"""
268+
return self.__last_request_id
269+
260270
def login(self, return_to=None, force_authn=False, is_passive=False):
261271
"""
262272
Initiates the SSO process.
@@ -274,6 +284,8 @@ def login(self, return_to=None, force_authn=False, is_passive=False):
274284
"""
275285
authn_request = OneLogin_Saml2_Authn_Request(self.__settings, force_authn, is_passive)
276286

287+
self.__last_request_id = authn_request.get_id()
288+
277289
saml_request = authn_request.get_request()
278290
parameters = {'SAMLRequest': saml_request}
279291

@@ -315,6 +327,8 @@ def logout(self, return_to=None, name_id=None, session_index=None):
315327

316328
logout_request = OneLogin_Saml2_Logout_Request(self.__settings, name_id=name_id, session_index=session_index)
317329

330+
self.__last_request_id = logout_request.id
331+
318332
saml_request = logout_request.get_request()
319333

320334
parameters = {'SAMLRequest': logout_request.get_request()}

src/onelogin/saml2/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class OneLogin_Saml2_Constants(object):
8080
NSMAP = {
8181
'samlp': NS_SAMLP,
8282
'saml': NS_SAML,
83+
'md': NS_MD,
8384
'ds': NS_DS,
8485
'xenc': NS_XENC
8586
}

0 commit comments

Comments
 (0)