2121 *
2222 * A class that implements SAML 2 Authentication Request
2323 */
24- public class AuthnRequest {
24+ public class AuthnRequest extends AuthnRequestParams {
2525 /**
2626 * Private property to construct a logger for this class.
2727 */
@@ -42,26 +42,6 @@ public class AuthnRequest {
4242 */
4343 private final Saml2Settings settings ;
4444
45- /**
46- * When true the AuthNRequest will set the ForceAuthn='true'
47- */
48- private final boolean forceAuthn ;
49-
50- /**
51- * When true the AuthNRequest will set the IsPassive='true'
52- */
53- private final boolean isPassive ;
54-
55- /**
56- * When true the AuthNReuqest will set a nameIdPolicy
57- */
58- private final boolean setNameIdPolicy ;
59-
60- /**
61- * Indicates to the IdP the subject that should be authenticated
62- */
63- private final String nameIdValueReq ;
64-
6545 /**
6646 * Time stamp that indicates when the AuthNRequest was created
6747 */
@@ -81,46 +61,64 @@ public AuthnRequest(Saml2Settings settings) {
8161 * Constructs the AuthnRequest object.
8262 *
8363 * @param settings
84- * OneLogin_Saml2_Settings
64+ * OneLogin_Saml2_Settings
8565 * @param forceAuthn
86- * When true the AuthNReuqest will set the ForceAuthn='true'
66+ * When true the AuthNReuqest will set the ForceAuthn='true'
8767 * @param isPassive
88- * When true the AuthNReuqest will set the IsPassive='true'
68+ * When true the AuthNReuqest will set the IsPassive='true'
8969 * @param setNameIdPolicy
90- * When true the AuthNReuqest will set a nameIdPolicy
70+ * When true the AuthNReuqest will set a nameIdPolicy
9171 * @param nameIdValueReq
92- * Indicates to the IdP the subject that should be authenticated
72+ * Indicates to the IdP the subject that should be authenticated
73+ * @deprecated use {@link #AuthnRequest(Saml2Settings, AuthnRequestParams)} with
74+ * {@link AuthnRequestParams#AuthnRequestParams(boolean, boolean, boolean, String)}
75+ * instead
9376 */
77+ @ Deprecated
9478 public AuthnRequest (Saml2Settings settings , boolean forceAuthn , boolean isPassive , boolean setNameIdPolicy , String nameIdValueReq ) {
95- this .id = Util .generateUniqueID (settings .getUniqueIDPrefix ());
96- issueInstant = Calendar .getInstance ();
97- this .isPassive = isPassive ;
98- this .settings = settings ;
99- this .forceAuthn = forceAuthn ;
100- this .setNameIdPolicy = setNameIdPolicy ;
101- this .nameIdValueReq = nameIdValueReq ;
102-
103- StrSubstitutor substitutor = generateSubstitutor (settings );
104- authnRequestString = substitutor .replace (getAuthnRequestTemplate ());
105- LOGGER .debug ("AuthNRequest --> " + authnRequestString );
79+ this (settings , new AuthnRequestParams (forceAuthn , isPassive , setNameIdPolicy , nameIdValueReq ));
10680 }
107-
81+
10882 /**
10983 * Constructs the AuthnRequest object.
11084 *
11185 * @param settings
112- * OneLogin_Saml2_Settings
86+ * OneLogin_Saml2_Settings
11387 * @param forceAuthn
114- * When true the AuthNReuqest will set the ForceAuthn='true'
88+ * When true the AuthNReuqest will set the ForceAuthn='true'
11589 * @param isPassive
116- * When true the AuthNReuqest will set the IsPassive='true'
90+ * When true the AuthNReuqest will set the IsPassive='true'
11791 * @param setNameIdPolicy
118- * When true the AuthNReuqest will set a nameIdPolicy
92+ * When true the AuthNReuqest will set a nameIdPolicy
93+ * @deprecated use {@link #AuthnRequest(Saml2Settings, AuthnRequestParams)} with
94+ * {@link AuthnRequestParams#AuthnRequestParams(boolean, boolean, boolean)}
95+ * instead
11996 */
97+ @ Deprecated
12098 public AuthnRequest (Saml2Settings settings , boolean forceAuthn , boolean isPassive , boolean setNameIdPolicy ) {
12199 this (settings , forceAuthn , isPassive , setNameIdPolicy , null );
122100 }
123101
102+ /**
103+ * Constructs the AuthnRequest object.
104+ *
105+ * @param settings
106+ * OneLogin_Saml2_Settings
107+ * @param params
108+ * a set of authentication request input parameters that shape the
109+ * request to create
110+ */
111+ public AuthnRequest (Saml2Settings settings , AuthnRequestParams params ) {
112+ super (params );
113+ this .id = Util .generateUniqueID (settings .getUniqueIDPrefix ());
114+ issueInstant = Calendar .getInstance ();
115+ this .settings = settings ;
116+
117+ StrSubstitutor substitutor = generateSubstitutor (settings );
118+ authnRequestString = substitutor .replace (getAuthnRequestTemplate ());
119+ LOGGER .debug ("AuthNRequest --> " + authnRequestString );
120+ }
121+
124122 /**
125123 * @return the base64 encoded unsigned AuthnRequest (deflated or not)
126124 *
@@ -171,12 +169,12 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) {
171169 Map <String , String > valueMap = new HashMap <String , String >();
172170
173171 String forceAuthnStr = "" ;
174- if (forceAuthn ) {
172+ if (isForceAuthn () ) {
175173 forceAuthnStr = " ForceAuthn=\" true\" " ;
176174 }
177175
178176 String isPassiveStr = "" ;
179- if (isPassive ) {
177+ if (isPassive () ) {
180178 isPassiveStr = " IsPassive=\" true\" " ;
181179 }
182180
@@ -191,6 +189,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) {
191189 valueMap .put ("destinationStr" , destinationStr );
192190
193191 String subjectStr = "" ;
192+ String nameIdValueReq = getNameIdValueReq ();
194193 if (nameIdValueReq != null && !nameIdValueReq .isEmpty ()) {
195194 String nameIDFormat = settings .getSpNameIDFormat ();
196195 subjectStr = "<saml:Subject>" ;
@@ -201,7 +200,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) {
201200 valueMap .put ("subjectStr" , subjectStr );
202201
203202 String nameIDPolicyStr = "" ;
204- if (setNameIdPolicy ) {
203+ if (isSetNameIdPolicy () ) {
205204 String nameIDPolicyFormat = settings .getSpNameIDFormat ();
206205 if (settings .getWantNameIdEncrypted ()) {
207206 nameIDPolicyFormat = Constants .NAMEID_ENCRYPTED ;
@@ -239,6 +238,12 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) {
239238 }
240239
241240 valueMap .put ("requestedAuthnContextStr" , requestedAuthnContextStr );
241+
242+ String attributeConsumingServiceIndexStr = "" ;
243+ final Integer acsIndex = getAttributeConsumingServiceSelector ().getAttributeConsumingServiceIndex ();
244+ if (acsIndex != null )
245+ attributeConsumingServiceIndexStr = " AttributeConsumingServiceIndex=\" " + acsIndex + "\" " ;
246+ valueMap .put ("attributeConsumingServiceIndexStr" , attributeConsumingServiceIndexStr );
242247
243248 return new StrSubstitutor (valueMap );
244249 }
@@ -248,7 +253,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) {
248253 */
249254 private static StringBuilder getAuthnRequestTemplate () {
250255 StringBuilder template = new StringBuilder ();
251- template .append ("<samlp:AuthnRequest xmlns:samlp=\" urn:oasis:names:tc:SAML:2.0:protocol\" xmlns:saml=\" urn:oasis:names:tc:SAML:2.0:assertion\" ID=\" ${id}\" Version=\" 2.0\" IssueInstant=\" ${issueInstant}\" ${providerStr}${forceAuthnStr}${isPassiveStr}${destinationStr} ProtocolBinding=\" ${protocolBinding}\" AssertionConsumerServiceURL=\" ${assertionConsumerServiceURL}\" >" );
256+ template .append ("<samlp:AuthnRequest xmlns:samlp=\" urn:oasis:names:tc:SAML:2.0:protocol\" xmlns:saml=\" urn:oasis:names:tc:SAML:2.0:assertion\" ID=\" ${id}\" Version=\" 2.0\" IssueInstant=\" ${issueInstant}\" ${providerStr}${forceAuthnStr}${isPassiveStr}${destinationStr} ProtocolBinding=\" ${protocolBinding}\" AssertionConsumerServiceURL=\" ${assertionConsumerServiceURL}${attributeConsumingServiceIndexStr} \" >" );
252257 template .append ("<saml:Issuer>${spEntityid}</saml:Issuer>" );
253258 template .append ("${subjectStr}${nameIDPolicyStr}${requestedAuthnContextStr}</samlp:AuthnRequest>" );
254259 return template ;
0 commit comments