@@ -180,21 +180,9 @@ public static AuthnRequest buildAuthnRequestObject(final String authnId, final S
180180
181181 // AuthnContextClass. When this is false, the authentication requirements are defered to the SAML IDP and its default or configured workflow
182182 if (requirePasswordAuthentication ) {
183- AuthnContextClassRefBuilder authnContextClassRefBuilder = new AuthnContextClassRefBuilder ();
184- AuthnContextClassRef authnContextClassRef = authnContextClassRefBuilder .buildObject (
185- SAMLConstants .SAML20_NS ,
186- "AuthnContextClassRef" , "saml" );
187- authnContextClassRef .setAuthnContextClassRef (AuthnContext .PPT_AUTHN_CTX );
188-
189- // AuthnContext
190- RequestedAuthnContextBuilder requestedAuthnContextBuilder = new RequestedAuthnContextBuilder ();
191- RequestedAuthnContext requestedAuthnContext = requestedAuthnContextBuilder .buildObject ();
192- requestedAuthnContext .setComparison (AuthnContextComparisonTypeEnumeration .EXACT );
193- requestedAuthnContext .getAuthnContextClassRefs ().add (authnContextClassRef );
194- authnRequest .setRequestedAuthnContext (requestedAuthnContext );
183+ setRequestedAuthnContext (authnRequest , requirePasswordAuthentication );
195184 }
196185
197-
198186 authnRequest .setID (authnId );
199187 authnRequest .setDestination (idpUrl );
200188 authnRequest .setVersion (SAMLVersion .VERSION_20 );
@@ -209,6 +197,21 @@ public static AuthnRequest buildAuthnRequestObject(final String authnId, final S
209197 return authnRequest ;
210198 }
211199
200+ public static void setRequestedAuthnContext (AuthnRequest authnRequest , boolean requirePasswordAuthentication ) {
201+ AuthnContextClassRefBuilder authnContextClassRefBuilder = new AuthnContextClassRefBuilder ();
202+ AuthnContextClassRef authnContextClassRef = authnContextClassRefBuilder .buildObject (
203+ SAMLConstants .SAML20_NS ,
204+ "AuthnContextClassRef" , "saml" );
205+ authnContextClassRef .setAuthnContextClassRef (AuthnContext .PPT_AUTHN_CTX );
206+
207+ // AuthnContext
208+ RequestedAuthnContextBuilder requestedAuthnContextBuilder = new RequestedAuthnContextBuilder ();
209+ RequestedAuthnContext requestedAuthnContext = requestedAuthnContextBuilder .buildObject ();
210+ requestedAuthnContext .setComparison (AuthnContextComparisonTypeEnumeration .EXACT );
211+ requestedAuthnContext .getAuthnContextClassRefs ().add (authnContextClassRef );
212+ authnRequest .setRequestedAuthnContext (requestedAuthnContext );
213+ }
214+
212215 public static LogoutRequest buildLogoutRequest (String logoutUrl , String spId , String nameIdString ) {
213216 Issuer issuer = new IssuerBuilder ().buildObject ();
214217 issuer .setValue (spId );
@@ -304,13 +307,8 @@ public static void setupSamlUserCookies(final LoginCmdResponse loginResponse, fi
304307 throw new CloudRuntimeException ("Invalid URI: " + redirectUrl );
305308 }
306309
307- resp .addCookie (newCookie (domain , path , "userid" , URLEncoder .encode (loginResponse .getUserId (), HttpUtils .UTF_8 )));
308- resp .addCookie (newCookie (domain , path ,"domainid" , URLEncoder .encode (loginResponse .getDomainId (), HttpUtils .UTF_8 )));
309- resp .addCookie (newCookie (domain , path ,"role" , URLEncoder .encode (loginResponse .getType (), HttpUtils .UTF_8 )));
310- resp .addCookie (newCookie (domain , path ,"username" , URLEncoder .encode (loginResponse .getUsername (), HttpUtils .UTF_8 )));
311- resp .addCookie (newCookie (domain , path ,"account" , URLEncoder .encode (loginResponse .getAccount (), HttpUtils .UTF_8 )));
312- resp .addCookie (newCookie (domain , path ,"isSAML" , URLEncoder .encode ("true" , HttpUtils .UTF_8 )));
313- resp .addCookie (newCookie (domain , path ,"twoFaEnabled" , URLEncoder .encode (loginResponse .is2FAenabled (), HttpUtils .UTF_8 )));
310+ addBaseCookies (loginResponse , resp , domain , path );
311+
314312 String providerFor2FA = loginResponse .getProviderFor2FA ();
315313 if (StringUtils .isNotEmpty (providerFor2FA )) {
316314 resp .addCookie (newCookie (domain , path ,"twoFaProvider" , URLEncoder .encode (loginResponse .getProviderFor2FA (), HttpUtils .UTF_8 )));
@@ -319,7 +317,6 @@ public static void setupSamlUserCookies(final LoginCmdResponse loginResponse, fi
319317 if (timezone != null ) {
320318 resp .addCookie (newCookie (domain , path ,"timezone" , URLEncoder .encode (timezone , HttpUtils .UTF_8 )));
321319 }
322- resp .addCookie (newCookie (domain , path ,"userfullname" , URLEncoder .encode (loginResponse .getFirstName () + " " + loginResponse .getLastName (), HttpUtils .UTF_8 ).replace ("+" , "%20" )));
323320
324321 String sameSite = ApiServlet .getApiSessionKeySameSite ();
325322 String sessionKeyCookie = String .format ("%s=%s;Domain=%s;Path=%s;%s" , ApiConstants .SESSIONKEY , loginResponse .getSessionKey (), domain , path , sameSite );
@@ -328,6 +325,17 @@ public static void setupSamlUserCookies(final LoginCmdResponse loginResponse, fi
328325 resp .addHeader ("SET-COOKIE" , String .format ("%s=%s;HttpOnly;Path=/client/api;%s" , ApiConstants .SESSIONKEY , loginResponse .getSessionKey (), sameSite ));
329326 }
330327
328+ private static void addBaseCookies (final LoginCmdResponse loginResponse , final HttpServletResponse resp , String domain , String path ) throws IOException {
329+ resp .addCookie (newCookie (domain , path , "userid" , URLEncoder .encode (loginResponse .getUserId (), HttpUtils .UTF_8 )));
330+ resp .addCookie (newCookie (domain , path ,"domainid" , URLEncoder .encode (loginResponse .getDomainId (), HttpUtils .UTF_8 )));
331+ resp .addCookie (newCookie (domain , path ,"role" , URLEncoder .encode (loginResponse .getType (), HttpUtils .UTF_8 )));
332+ resp .addCookie (newCookie (domain , path ,"username" , URLEncoder .encode (loginResponse .getUsername (), HttpUtils .UTF_8 )));
333+ resp .addCookie (newCookie (domain , path ,"account" , URLEncoder .encode (loginResponse .getAccount (), HttpUtils .UTF_8 )));
334+ resp .addCookie (newCookie (domain , path ,"isSAML" , URLEncoder .encode ("true" , HttpUtils .UTF_8 )));
335+ resp .addCookie (newCookie (domain , path ,"twoFaEnabled" , URLEncoder .encode (loginResponse .is2FAenabled (), HttpUtils .UTF_8 )));
336+ resp .addCookie (newCookie (domain , path ,"userfullname" , URLEncoder .encode (loginResponse .getFirstName () + " " + loginResponse .getLastName (), HttpUtils .UTF_8 ).replace ("+" , "%20" )));
337+ }
338+
331339 private static Cookie newCookie (final String domain , final String path , final String name , final String value ) {
332340 Cookie cookie = new Cookie (name , value );
333341 cookie .setDomain (domain );
0 commit comments