diff --git a/core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java b/core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java index da64ce46..b5f3811f 100644 --- a/core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java +++ b/core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java @@ -229,7 +229,11 @@ private StrSubstitutor generateSubstitutor(AuthnRequestParams params, Saml2Setti if (settings.getWantNameIdEncrypted()) { nameIDPolicyFormat = Constants.NAMEID_ENCRYPTED; } - nameIDPolicyStr = ""; + String allowCreateStr = ""; + if (params.isAllowCreate()) { + allowCreateStr = " AllowCreate=\"true\""; + } + nameIDPolicyStr = ""; } valueMap.put("nameIDPolicyStr", nameIDPolicyStr); diff --git a/core/src/main/java/com/onelogin/saml2/authn/AuthnRequestParams.java b/core/src/main/java/com/onelogin/saml2/authn/AuthnRequestParams.java index 6aec5a54..0a7efa48 100644 --- a/core/src/main/java/com/onelogin/saml2/authn/AuthnRequestParams.java +++ b/core/src/main/java/com/onelogin/saml2/authn/AuthnRequestParams.java @@ -14,9 +14,14 @@ public class AuthnRequestParams { */ private final boolean isPassive; /** - * When true the AuthNReuqest will set a nameIdPolicy + * When true the AuthNRequest will set a nameIdPolicy */ private final boolean setNameIdPolicy; + /** + * When true and {@link #setNameIdPolicy} is also true, then the + * AllowCreate='true' will be set on the NameIDPolicy element + */ + private final boolean allowCreate; /** * Indicates to the IdP the subject that should be authenticated */ @@ -29,13 +34,34 @@ public class AuthnRequestParams { * whether the ForceAuthn attribute should be set to * true * @param isPassive - * whether the isPassive attribute should be set to + * whether the IsPassive attribute should be set to * true * @param setNameIdPolicy * whether a NameIDPolicy should be set */ public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setNameIdPolicy) { - this(forceAuthn, isPassive, setNameIdPolicy, null); + this(forceAuthn, isPassive, setNameIdPolicy, true); + } + + /** + * Create a set of authentication request input parameters. + * + * @param forceAuthn + * whether the ForceAuthn attribute should be set to + * true + * @param isPassive + * whether the IsPassive attribute should be set to + * true + * @param setNameIdPolicy + * whether a NameIDPolicy should be set + * @param allowCreate + * whether the AllowCreate attribute should be set to + * true on the NameIDPolicy element; only + * meaningful if setNameIdPolicy is also + * true + */ + public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setNameIdPolicy, boolean allowCreate) { + this(forceAuthn, isPassive, setNameIdPolicy, allowCreate, null); } /** @@ -45,7 +71,7 @@ public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setName * whether the ForceAuthn attribute should be set to * true * @param isPassive - * whether the isPassive attribute should be set to + * whether the IsPassive attribute should be set to * true * @param setNameIdPolicy * whether a NameIDPolicy should be set @@ -53,9 +79,34 @@ public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setName * the subject that should be authenticated */ public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setNameIdPolicy, String nameIdValueReq) { + this(forceAuthn, isPassive, setNameIdPolicy, true, nameIdValueReq); + } + + /** + * Create a set of authentication request input parameters. + * + * @param forceAuthn + * whether the ForceAuthn attribute should be set to + * true + * @param isPassive + * whether the IsPassive attribute should be set to + * true + * @param setNameIdPolicy + * whether a NameIDPolicy should be set + * @param allowCreate + * the value to set for the allowCreate attribute of + * NameIDPolicy element; null means it's + * not set at all; only meaningful when + * setNameIdPolicy is true + * @param nameIdValueReq + * the subject that should be authenticated + */ + public AuthnRequestParams(boolean forceAuthn, boolean isPassive, boolean setNameIdPolicy, boolean allowCreate, + String nameIdValueReq) { this.forceAuthn = forceAuthn; this.isPassive = isPassive; this.setNameIdPolicy = setNameIdPolicy; + this.allowCreate = allowCreate; this.nameIdValueReq = nameIdValueReq; } @@ -70,6 +121,7 @@ protected AuthnRequestParams(AuthnRequestParams source) { this.forceAuthn = source.isForceAuthn(); this.isPassive = source.isPassive(); this.setNameIdPolicy = source.isSetNameIdPolicy(); + this.allowCreate = source.isAllowCreate(); this.nameIdValueReq = source.getNameIdValueReq(); } @@ -77,25 +129,34 @@ protected AuthnRequestParams(AuthnRequestParams source) { * @return whether the ForceAuthn attribute should be set to * true */ - protected boolean isForceAuthn() { + public boolean isForceAuthn() { return forceAuthn; } /** - * @return whether the isPassive attribute should be set to + * @return whether the IsPassive attribute should be set to * true */ - protected boolean isPassive() { + public boolean isPassive() { return isPassive; } /** * @return whether a NameIDPolicy should be set */ - protected boolean isSetNameIdPolicy() { + public boolean isSetNameIdPolicy() { return setNameIdPolicy; } + /** + * @return whether the AllowCreate attribute should be set to + * true on the NameIDPolicy element (only + * meaningful if {@link #isSetNameIdPolicy()} is also true) + */ + public boolean isAllowCreate() { + return allowCreate; + } + /** * @return the subject that should be authenticated */ diff --git a/core/src/test/java/com/onelogin/saml2/test/authn/AuthnRequestTest.java b/core/src/test/java/com/onelogin/saml2/test/authn/AuthnRequestTest.java index 3212fb4c..20886fb8 100644 --- a/core/src/test/java/com/onelogin/saml2/test/authn/AuthnRequestTest.java +++ b/core/src/test/java/com/onelogin/saml2/test/authn/AuthnRequestTest.java @@ -242,6 +242,75 @@ public void testNameIDPolicy() throws Exception { assertThat(authnRequestStr, containsString("Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\"")); } + /** + * Tests the AuthnRequest Constructor + * The creation of a deflated SAML Request with NameIDPolicy with and without AllowCreate + * + * @throws Exception + * + * @see com.onelogin.saml2.authn.AuthnRequest + */ + @Test + public void testAllowCreate() throws Exception { + Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build(); + + // by default setNameIdPolicy=true, allowCreate=true + AuthnRequest authnRequest = new AuthnRequest(settings); + String authnRequestStringBase64 = authnRequest.getEncodedAuthnRequest(); + String authnRequestStr = Util.base64decodedInflated(authnRequestStringBase64); + assertThat(authnRequestStr, containsString("