Skip to content

Commit ec3af78

Browse files
committed
See #291 Support sending extra GET parameters on login and logout
1 parent e756d62 commit ec3af78

File tree

2 files changed

+140
-9
lines changed

2 files changed

+140
-9
lines changed

toolkit/src/main/java/com/onelogin/saml2/Auth.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,41 @@ public void setStrict(Boolean value) {
332332
public String login(String returnTo, Boolean forceAuthn, Boolean isPassive, Boolean setNameIdPolicy, Boolean stay,
333333
String nameIdValueReq) throws IOException, SettingsException {
334334
Map<String, String> parameters = new HashMap<String, String>();
335+
return login(returnTo, forceAuthn, isPassive, setNameIdPolicy, stay,
336+
nameIdValueReq, parameters);
337+
}
335338

339+
/**
340+
* Initiates the SSO process.
341+
*
342+
* @param returnTo The target URL the user should be returned to after
343+
* login (relayState). Will be a self-routed URL when
344+
* null, or not be appended at all when an empty string
345+
* is provided
346+
* @param forceAuthn When true the AuthNRequest will set the
347+
* ForceAuthn='true'
348+
* @param isPassive When true the AuthNRequest will set the
349+
* IsPassive='true'
350+
* @param setNameIdPolicy When true the AuthNRequest will set a nameIdPolicy
351+
* @param stay True if we want to stay (returns the url string) False
352+
* to execute redirection
353+
* @param nameIdValueReq Indicates to the IdP the subject that should be
354+
* authenticated
355+
* @param parameters Use it to send extra parameters in addition to the AuthNRequest
356+
*
357+
* @return the SSO URL with the AuthNRequest if stay = True
358+
*
359+
* @throws IOException
360+
* @throws SettingsException
361+
*/
362+
public String login(String returnTo, Boolean forceAuthn, Boolean isPassive, Boolean setNameIdPolicy, Boolean stay,
363+
String nameIdValueReq, Map<String, String> parameters) throws IOException, SettingsException {
336364
AuthnRequest authnRequest = new AuthnRequest(settings, forceAuthn, isPassive, setNameIdPolicy, nameIdValueReq);
337365

366+
if (parameters == null) {
367+
parameters = new HashMap<String, String>();
368+
}
369+
338370
String samlRequest = authnRequest.getEncodedAuthnRequest();
339371

340372
parameters.put("SAMLRequest", samlRequest);
@@ -468,6 +500,44 @@ public String logout(String returnTo, String nameId, String sessionIndex, Boolea
468500
String nameIdNameQualifier, String nameIdSPNameQualifier)
469501
throws IOException, XMLEntityException, SettingsException {
470502
Map<String, String> parameters = new HashMap<String, String>();
503+
return logout(returnTo, nameId, sessionIndex, stay, nameidFormat,
504+
nameIdNameQualifier, nameIdSPNameQualifier, parameters);
505+
}
506+
507+
/**
508+
* Initiates the SLO process.
509+
*
510+
* @param returnTo The target URL the user should be returned to
511+
* after logout (relayState). Will be a self-routed
512+
* URL when null, or not be appended at all when an
513+
* empty string is provided
514+
* @param nameId The NameID that will be set in the
515+
* LogoutRequest.
516+
* @param sessionIndex The SessionIndex (taken from the SAML Response
517+
* in the SSO process).
518+
* @param stay True if we want to stay (returns the url string)
519+
* False to execute redirection
520+
* @param nameidFormat The NameID Format that will be set in the
521+
* LogoutRequest.
522+
* @param nameIdNameQualifier The NameID NameQualifier that will be set in the
523+
* LogoutRequest.
524+
* @param nameIdSPNameQualifier The NameID SP Name Qualifier that will be set in
525+
* the LogoutRequest.
526+
* @param parameters Use it to send extra parameters in addition to the LogoutRequest
527+
*
528+
* @return the SLO URL with the LogoutRequest if stay = True
529+
*
530+
* @throws IOException
531+
* @throws XMLEntityException
532+
* @throws SettingsException
533+
*/
534+
public String logout(String returnTo, String nameId, String sessionIndex, Boolean stay, String nameidFormat,
535+
String nameIdNameQualifier, String nameIdSPNameQualifier, Map<String, String> parameters)
536+
throws IOException, XMLEntityException, SettingsException {
537+
538+
if (parameters == null) {
539+
parameters = new HashMap<String, String>();
540+
}
471541

472542
LogoutRequest logoutRequest = new LogoutRequest(settings, null, nameId, sessionIndex, nameidFormat,
473543
nameIdNameQualifier, nameIdSPNameQualifier);

toolkit/src/test/java/com/onelogin/saml2/test/AuthTest.java

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ public void testLogin() throws IOException, SettingsException, URISyntaxExceptio
12831283
HttpServletResponse response = mock(HttpServletResponse.class);
12841284
when(request.getScheme()).thenReturn("http");
12851285
when(request.getServerPort()).thenReturn(8080);
1286-
when(request.getServerName()).thenReturn("localhost");
1286+
when(request.getServerName()).thenReturn("localhost");
12871287
when(request.getRequestURI()).thenReturn("/initial.jsp");
12881288

12891289
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1311,7 +1311,7 @@ public void testLoginWithRelayState() throws IOException, SettingsException, URI
13111311
HttpServletResponse response = mock(HttpServletResponse.class);
13121312
when(request.getScheme()).thenReturn("http");
13131313
when(request.getServerPort()).thenReturn(8080);
1314-
when(request.getServerName()).thenReturn("localhost");
1314+
when(request.getServerName()).thenReturn("localhost");
13151315
when(request.getRequestURI()).thenReturn("/initial.jsp");
13161316

13171317
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1354,6 +1354,37 @@ public void testLoginWithoutRelayState() throws IOException, SettingsException,
13541354
assertThat(urlCaptor.getValue(), not(containsString("&RelayState=")));
13551355
}
13561356

1357+
/**
1358+
* Tests the login method of Auth
1359+
* Case: Login with extra parameters
1360+
*
1361+
* @throws SettingsException
1362+
* @throws IOException
1363+
* @throws URISyntaxException
1364+
* @throws Error
1365+
*
1366+
* @see com.onelogin.saml2.Auth#login
1367+
*/
1368+
@Test
1369+
public void testLoginWithExtraParameters() throws IOException, SettingsException, URISyntaxException, Error {
1370+
HttpServletRequest request = mock(HttpServletRequest.class);
1371+
HttpServletResponse response = mock(HttpServletResponse.class);
1372+
when(request.getScheme()).thenReturn("http");
1373+
when(request.getServerPort()).thenReturn(8080);
1374+
when(request.getServerName()).thenReturn("localhost");
1375+
when(request.getRequestURI()).thenReturn("/initial.jsp");
1376+
1377+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
1378+
settings.setAuthnRequestsSigned(false);
1379+
1380+
Auth auth = new Auth(settings, request, response);
1381+
Map<String, String> extraParameters = new HashMap<String, String>();
1382+
extraParameters.put("parameter1", "xxx");
1383+
String target = auth.login("", false, false, false, true, null, extraParameters);
1384+
assertThat(target, startsWith("https://pitbulk.no-ip.org/simplesaml/saml2/idp/SSOService.php?SAMLRequest="));
1385+
assertThat(target, containsString("&parameter1=xxx"));
1386+
}
1387+
13571388
/**
13581389
* Tests the login method of Auth
13591390
* Case: Login with stay enabled
@@ -1454,7 +1485,7 @@ public void testLoginSignedFail() throws IOException, SettingsException, URISynt
14541485
HttpServletResponse response = mock(HttpServletResponse.class);
14551486
when(request.getScheme()).thenReturn("http");
14561487
when(request.getServerPort()).thenReturn(8080);
1457-
when(request.getServerName()).thenReturn("localhost");
1488+
when(request.getServerName()).thenReturn("localhost");
14581489
when(request.getRequestURI()).thenReturn("/initial.jsp");
14591490

14601491
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
@@ -1483,7 +1514,7 @@ public void testLoginSigned() throws IOException, SettingsException, URISyntaxEx
14831514
HttpServletResponse response = mock(HttpServletResponse.class);
14841515
when(request.getScheme()).thenReturn("http");
14851516
when(request.getServerPort()).thenReturn(8080);
1486-
when(request.getServerName()).thenReturn("localhost");
1517+
when(request.getServerName()).thenReturn("localhost");
14871518
when(request.getRequestURI()).thenReturn("/initial.jsp");
14881519

14891520
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1517,7 +1548,7 @@ public void testLogout() throws IOException, SettingsException, XMLEntityExcepti
15171548
HttpServletResponse response = mock(HttpServletResponse.class);
15181549
when(request.getScheme()).thenReturn("http");
15191550
when(request.getServerPort()).thenReturn(8080);
1520-
when(request.getServerName()).thenReturn("localhost");
1551+
when(request.getServerName()).thenReturn("localhost");
15211552
when(request.getRequestURI()).thenReturn("/initial.jsp");
15221553

15231554
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1529,6 +1560,36 @@ public void testLogout() throws IOException, SettingsException, XMLEntityExcepti
15291560
assertThat(auth.getLastRequestId(), startsWith(Util.UNIQUE_ID_PREFIX));
15301561
}
15311562

1563+
/**
1564+
* Tests the logout method of Auth
1565+
* Case: Logout with no parameters
1566+
*
1567+
* @throws IOException
1568+
* @throws SettingsException
1569+
* @throws XMLEntityException
1570+
* @throws Error
1571+
*
1572+
* @see com.onelogin.saml2.Auth#logout
1573+
*/
1574+
@Test
1575+
public void testLogoutWithExtraParameters() throws IOException, SettingsException, XMLEntityException, Error {
1576+
HttpServletRequest request = mock(HttpServletRequest.class);
1577+
HttpServletResponse response = mock(HttpServletResponse.class);
1578+
when(request.getScheme()).thenReturn("http");
1579+
when(request.getServerPort()).thenReturn(8080);
1580+
when(request.getServerName()).thenReturn("localhost");
1581+
when(request.getRequestURI()).thenReturn("/initial.jsp");
1582+
1583+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
1584+
settings.setLogoutRequestSigned(false);
1585+
Auth auth = new Auth(settings, request, response);
1586+
Map<String, String> extraParameters = new HashMap<String, String>();
1587+
extraParameters.put("parameter1", "xxx");
1588+
String target = auth.logout("", null, null, true, null, null, null, extraParameters);
1589+
assertThat(target, startsWith("https://pitbulk.no-ip.org/simplesaml/saml2/idp/SingleLogoutService.php?SAMLRequest="));
1590+
assertThat(target, containsString("&parameter1=xxx"));
1591+
}
1592+
15321593
/**
15331594
* Tests the logout method of Auth
15341595
* Case: Logout with RelayState
@@ -1546,7 +1607,7 @@ public void testLogoutWithRelayState() throws IOException, SettingsException, XM
15461607
HttpServletResponse response = mock(HttpServletResponse.class);
15471608
when(request.getScheme()).thenReturn("http");
15481609
when(request.getServerPort()).thenReturn(8080);
1549-
when(request.getServerName()).thenReturn("localhost");
1610+
when(request.getServerName()).thenReturn("localhost");
15501611
when(request.getRequestURI()).thenReturn("/initial.jsp");
15511612

15521613
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1608,7 +1669,7 @@ public void testLogoutStay() throws IOException, SettingsException, XMLEntityExc
16081669
HttpServletResponse response = mock(HttpServletResponse.class);
16091670
when(request.getScheme()).thenReturn("http");
16101671
when(request.getServerPort()).thenReturn(8080);
1611-
when(request.getServerName()).thenReturn("localhost");
1672+
when(request.getServerName()).thenReturn("localhost");
16121673
when(request.getRequestURI()).thenReturn("/initial.jsp");
16131674

16141675
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();
@@ -1642,7 +1703,7 @@ public void testLogoutSignedFail() throws IOException, SettingsException, XMLEnt
16421703
HttpServletResponse response = mock(HttpServletResponse.class);
16431704
when(request.getScheme()).thenReturn("http");
16441705
when(request.getServerPort()).thenReturn(8080);
1645-
when(request.getServerName()).thenReturn("localhost");
1706+
when(request.getServerName()).thenReturn("localhost");
16461707
when(request.getRequestURI()).thenReturn("/initial.jsp");
16471708

16481709
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
@@ -1671,7 +1732,7 @@ public void testLogoutSigned() throws IOException, SettingsException, XMLEntityE
16711732
HttpServletResponse response = mock(HttpServletResponse.class);
16721733
when(request.getScheme()).thenReturn("http");
16731734
when(request.getServerPort()).thenReturn(8080);
1674-
when(request.getServerName()).thenReturn("localhost");
1735+
when(request.getServerName()).thenReturn("localhost");
16751736
when(request.getRequestURI()).thenReturn("/initial.jsp");
16761737

16771738
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.my.properties").build();

0 commit comments

Comments
 (0)