99
1010"""
1111
12+ from onelogin .saml2 import compat
1213from onelogin .saml2 .constants import OneLogin_Saml2_Constants
1314from onelogin .saml2 .utils import OneLogin_Saml2_Utils
1415from onelogin .saml2 .xml_templates import OneLogin_Saml2_Templates
@@ -22,7 +23,7 @@ class OneLogin_Saml2_Authn_Request(object):
2223
2324 """
2425
25- def __init__ (self , settings , force_authn = False , is_passive = False , set_nameid_policy = True ):
26+ def __init__ (self , settings , force_authn = False , is_passive = False , set_nameid_policy = True , acs_index = None ):
2627 """
2728 Constructs the AuthnRequest object.
2829
@@ -37,6 +38,9 @@ def __init__(self, settings, force_authn=False, is_passive=False, set_nameid_pol
3738
3839 :param set_nameid_policy: Optional argument. When true the AuthNRequest will set a nameIdPolicy element.
3940 :type set_nameid_policy: bool
41+
42+ :param acs_index: Optional argument. The index of the assertionConsumerService to use, if multiple were specified.
43+ :type acs_index: int
4044 """
4145 self .__settings = settings
4246
@@ -102,6 +106,19 @@ def __init__(self, settings, force_authn=False, is_passive=False, set_nameid_pol
102106 if 'attributeConsumingService' in sp_data and sp_data ['attributeConsumingService' ]:
103107 attr_consuming_service_str = "\n AttributeConsumingServiceIndex=\" 1\" "
104108
109+ assertion_url = ''
110+ if isinstance (sp_data ['assertionConsumerService' ], dict ):
111+ assertion_url = sp_data ['assertionConsumerService' ]['url' ]
112+ else :
113+ for idx , acs in enumerate (sp_data ['assertionConsumerService' ]):
114+ if idx == 0 :
115+ # By default, use the first assertion consumer service if an index is not specified.
116+ assertion_url = acs ['url' ]
117+ index = compat .to_string (acs .get ('index' , idx ))
118+ if index == compat .to_string (acs_index ):
119+ assertion_url = acs ['url' ]
120+ break
121+
105122 request = OneLogin_Saml2_Templates .AUTHN_REQUEST % \
106123 {
107124 'id' : uid ,
@@ -110,7 +127,7 @@ def __init__(self, settings, force_authn=False, is_passive=False, set_nameid_pol
110127 'is_passive_str' : is_passive_str ,
111128 'issue_instant' : issue_instant ,
112129 'destination' : destination ,
113- 'assertion_url' : sp_data [ 'assertionConsumerService' ][ 'url' ] ,
130+ 'assertion_url' : assertion_url ,
114131 'entity_id' : sp_data ['entityId' ],
115132 'nameid_policy_str' : nameid_policy_str ,
116133 'requested_authn_context_str' : requested_authn_context_str ,
0 commit comments