Skip to content

Commit 0e69c67

Browse files
committed
Update branch
2 parents 5accc27 + 96a2213 commit 0e69c67

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Version 2.0.0
77

88
Not ready yet, it lacks documentation and not officially distributed.
99

10-
Unit test compatible with java6 / java7 (java8 experiences an issue with PowerMock)
11-
Make sure maven & your IDE uses this java version.
10+
Compatible with java6 / java7 / java8.
1211

1312
## TODOs
1413

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,80 @@ public static String convertToPem(X509Certificate certificate) {
551551
LOGGER.debug("Error converting certificate on PEM format: "+ e.getMessage(), e);
552552
}
553553
return pemCert;
554+
}
555+
556+
/**
557+
* Redirect to location url
558+
*
559+
* @param response
560+
* HttpServletResponse object to be used
561+
* @param location
562+
* target location url
563+
* @param parameters
564+
* GET parameters to be added
565+
*
566+
* @throws IOException
567+
*
568+
* @see javax.servlet.http.HttpServletResponse#sendRedirect(String)
569+
*/
570+
public static void sendRedirect(HttpServletResponse response, String location, Map<String, String> parameters) throws IOException {
571+
String target = location;
572+
573+
if (!parameters.isEmpty()) {
574+
boolean first = !location.contains("?");
575+
for (Map.Entry<String, String> parameter : parameters.entrySet())
576+
{
577+
if (first) {
578+
target += "?";
579+
first = false;
580+
} else {
581+
target += "&";
582+
}
583+
target += parameter.getKey();
584+
if (!parameter.getValue().isEmpty()) {
585+
target += "=" + Util.urlEncoder(parameter.getValue());
586+
}
587+
}
588+
}
589+
response.sendRedirect(target);
590+
}
591+
592+
/**
593+
* Redirect to location url
594+
*
595+
* @param response
596+
* HttpServletResponse object to be used
597+
* @param location
598+
* target location url
599+
*
600+
* @throws IOException
601+
*
602+
* @see javax.servlet.http.HttpServletResponse#sendRedirect(String)
603+
*/
604+
public static void sendRedirect(HttpServletResponse response, String location) throws IOException {
605+
Map<String, String> parameters =new HashMap<String, String>();
606+
sendRedirect(response, location, parameters);
607+
}
608+
609+
610+
/**
611+
* Returns the protocol + the current host + the port (if different than
612+
* common ports).
613+
*
614+
* @param request
615+
* HttpServletRequest object to be processed
616+
*
617+
* @return the HOST URL
618+
*/
619+
public static String getSelfURLhost(HttpServletRequest request) {
620+
String hostUrl = StringUtils.EMPTY;
621+
final int serverPort = request.getServerPort();
622+
if ((serverPort == 80) || (serverPort == 443) || serverPort == 0) {
623+
hostUrl = String.format("%s://%s", request.getScheme(), request.getServerName());
624+
} else {
625+
hostUrl = String.format("%s://%s:%s", request.getScheme(), request.getServerName(), serverPort);
626+
}
627+
return hostUrl;
554628
}
555629

556630
/**

core/src/test/java/com/onelogin/saml2/test/util/UtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.PrivateKey;
3333
import java.security.SignatureException;
3434
import java.security.spec.InvalidKeySpecException;
35+
import java.util.Collections;
3536
import java.util.Date;
3637
import java.util.HashMap;
3738
import java.util.Map;
@@ -1887,4 +1888,16 @@ public void testQuery() throws XPathExpressionException, URISyntaxException, IOE
18871888
Node assertion_2 = assertionNodes_2.item(0);
18881889
assertEquals("saml2:Assertion", assertion_2.getNodeName());
18891890
}
1891+
1892+
@Test
1893+
public void sendRedirectToShouldHandleUrlsWithQueryParams() throws Exception {
1894+
// having
1895+
final HttpServletResponse response = mock(HttpServletResponse.class);
1896+
1897+
// when
1898+
Util.sendRedirect(response, "https://sso.connect.pingidentity.com/sso/idp/SSO.saml2?idpid=ffee-aabbb", Collections.singletonMap("SAMLRequest", "data"));
1899+
1900+
// then
1901+
verify(response).sendRedirect("https://sso.connect.pingidentity.com/sso/idp/SSO.saml2?idpid=ffee-aabbb&SAMLRequest=data");
1902+
}
18901903
}

0 commit comments

Comments
 (0)