Skip to content

Commit

Permalink
more combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Dec 1, 2023
1 parent 98e091e commit 76ee155
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 39 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/casinthecloud/simpletest/cas/CasDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
@Setter
public class CasDelegate extends CasLogin {

private final int nbCallbacks;

private final String clientName;

public CasDelegate(final String clientName, final CasTest casTest) {
public CasDelegate(final int nbCallbacks, final String clientName, final CasTest casTest) {
this.nbCallbacks = nbCallbacks;
this.tests = new CasTest[] { casTest };
this.clientName = clientName;
}
Expand All @@ -38,9 +41,9 @@ public void run(final Context ctx) throws Exception {
this.tests[0].run(ctx);
decrLevel();

callback(ctx, 302);

callback(ctx, 302);
for (int i = 0; i < nbCallbacks; i++) {
callback(ctx, 302);
}

info("< END CasDelegate");

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/casinthecloud/simpletest/cas/CasLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ protected String callLoginPage(final Context ctx) throws Exception {
return loginUrl;
}



protected void postCredentials(final Context ctx, final String loginUrl) throws Exception {
val webflow = substringBetween(ctx.getBody(), "name=\"execution\" value=\"", "\"/>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public void run(final Context ctx) throws Exception {

protected void authorize(final Context ctx) throws Exception {
var authorizeUrl = getLocation(ctx);
if (isNotBlank(authorizeUrl)) {
info("Existing authorizeUrl: " + authorizeUrl);
if (isNotBlank(authorizeUrl) && authorizeUrl.contains("response_type=code")) {
info("! Existing authorizeUrl");
} else {
info("Client app: " + getServiceUrl());
val state = "s" + random(10000);
Expand Down
57 changes: 37 additions & 20 deletions src/main/java/com/casinthecloud/simpletest/cas/CasSAML2Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.time.ZonedDateTime;

import static com.casinthecloud.simpletest.util.Utils.*;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.substringBetween;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
Expand Down Expand Up @@ -47,32 +47,49 @@ public void run(final Context ctx) throws Exception {

callback(ctx, 200);

val pac4jCallbackUrl = htmlDecode(substringBetween(ctx.getBody(), "<form action=\"", "\" met"));
val samlResponse = htmlDecode(substringBetween(ctx.getBody(), "\"SAMLResponse\" value=\"", "\"/>"));
assertEquals(serviceUrl, pac4jCallbackUrl);
val body = ctx.getBody();
val pac4jCallbackUrl = htmlDecode(substringBetween(body, "<form action=\"", "\" method=\"post"));
val samlResponse = htmlDecode(substringBetween(body, "\"SAMLResponse\" value=\"", "\"/>"));
assertNotNull(base64Decode(samlResponse));

info("< END CasSAML2Login");

}

protected void postRequest(final Context ctx) throws Exception {
val samlSsoUrl = getCasPrefixUrl() + "/idp/profile/SAML2/POST/SSO";
val relayState = getRelayState();
val serviceUrl = getServiceUrl();
val samlRequestId = random(1000);
val samlRequest = base64Encode("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<saml2p:AuthnRequest\n" +
" xmlns:saml2p=\"urn:oasis:names:tc:SAML:2.0:protocol\" AssertionConsumerServiceURL=\"" + serviceUrl + "\" Destination=\"" + samlSsoUrl + "\" ForceAuthn=\"false\" ID=\"" + samlRequestId + "\" IsPassive=\"false\" IssueInstant=\"" + ZonedDateTime.now(ZoneOffset.UTC) + "\" ProtocolBinding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" ProviderName=\"client-app\" Version=\"2.0\">\n" +
" <saml2:Issuer\n" +
" xmlns:saml2=\"urn:oasis:names:tc:SAML:2.0:assertion\" Format=\"urn:oasis:names:tc:SAML:2.0:nameid-format:entity\">" + serviceUrl + "\n" +
" </saml2:Issuer>\n" +
"</saml2p:AuthnRequest>");

val fp = ctx.getFormParameters();
fp.put("RelayState", relayState);
fp.put("SAMLRequest", samlRequest);
ctx.setRequest(post(ctx, samlSsoUrl));
val redirectUrl = getLocation(ctx);
val body = ctx.getBody();
val postUrl = htmlDecode(substringBetween(body, "<form action=\"", "\" method=\"post"));
val samlRequest = htmlDecode(substringBetween(body, "\"SAMLRequest\" value=\"", "\"/>"));
if (isNotBlank(redirectUrl) && redirectUrl.contains("SAMLRequest=")) {
info("! Existing redirect binding");
ctx.setRequest(get(ctx, redirectUrl));

} else if (isNotBlank(postUrl) && isNotBlank(samlRequest)) {
info("! Existing POST binding");
ctx.getFormParameters().put("SAMLRequest", samlRequest);
ctx.setRequest(post(ctx, redirectUrl));

} else {

val samlSsoUrl = getCasPrefixUrl() + "/idp/profile/SAML2/POST/SSO";
val relayState = getRelayState();
val serviceUrl = getServiceUrl();
val samlRequestId = random(1000);
val createdSamlRequest = base64Encode("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<saml2p:AuthnRequest\n" +
" xmlns:saml2p=\"urn:oasis:names:tc:SAML:2.0:protocol\" AssertionConsumerServiceURL=\"" + serviceUrl + "\" Destination=\"" + samlSsoUrl + "\" ForceAuthn=\"false\" ID=\"" + samlRequestId + "\" IsPassive=\"false\" IssueInstant=\"" + ZonedDateTime.now(ZoneOffset.UTC) + "\" ProtocolBinding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" ProviderName=\"client-app\" Version=\"2.0\">\n" +
" <saml2:Issuer\n" +
" xmlns:saml2=\"urn:oasis:names:tc:SAML:2.0:assertion\" Format=\"urn:oasis:names:tc:SAML:2.0:nameid-format:entity\">" + serviceUrl + "\n" +
" </saml2:Issuer>\n" +
"</saml2p:AuthnRequest>");

val fp = ctx.getFormParameters();
fp.put("RelayState", relayState);
fp.put("SAMLRequest", createdSamlRequest);
ctx.setRequest(post(ctx, samlSsoUrl));
}

execute(ctx);
assertStatus(ctx, 302);

Expand Down
22 changes: 20 additions & 2 deletions src/main/java/com/casinthecloud/simpletest/cas/CasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import lombok.val;
import org.apache.commons.lang3.tuple.Pair;

import static com.casinthecloud.simpletest.util.Utils.htmlDecode;
import static org.apache.commons.lang3.StringUtils.substringBetween;
import static org.junit.Assert.assertTrue;

/**
* A test for CAS.
*
Expand Down Expand Up @@ -64,12 +68,26 @@ protected void useSsoSession(final Context ctx) {
}

protected void callback(final Context ctx, final int status) throws Exception {
val callbackUrl = getLocation(ctx);

useSsoSession(ctx);
useCasSession(ctx);

ctx.setRequest(get(ctx, callbackUrl));
val previousStatus = ctx.getStatus();
if (previousStatus == 302) {
val callbackUrl = getLocation(ctx);
ctx.setRequest(get(ctx, callbackUrl));
} else if (previousStatus == 200) {
val body = ctx.getBody();
val callbackUrl = htmlDecode(substringBetween(body, "<form action=\"", "\" met"));
val samlResponse = htmlDecode(substringBetween(body, "\"SAMLResponse\" value=\"", "\"/>"));
val relayState = htmlDecode(substringBetween(body, "\"RelayState\" value=\"", "\"/>"));
ctx.getFormParameters().put("SAMLResponse", samlResponse);
ctx.getFormParameters().put("RelayState", relayState);
ctx.setRequest(post(ctx, callbackUrl));
} else {
assertTrue("Status must be 302 or 200", previousStatus == 302 || previousStatus == 200);
}

execute(ctx);
assertStatus(ctx, status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public void run(final Context ctx) throws Exception {
execute(ctx);
assertStatus(ctx, 200);

info("User: " + ctx.getBody());

info("< END CasValidate");

}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/casinthecloud/simpletest/MainAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.casinthecloud.simpletest;

public class MainAll {

public static void main(final String... args) throws Exception {
MainCAS.main(args);
MainOIDC.main(args);
MainSAML.main(args);

MainCASDelegateCAS.main(args);
MainCASDelegateOIDC.main(args);
MainCASDelegateSAML.main(args);

MainOIDCDelegateCAS.main(args);
MainOIDCDelegateOIDC.main(args);
MainOIDCDelegateSAML.main(args);

MainSAMLDelegateCAS.main(args);
MainSAMLDelegateOIDC.main(args);
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/casinthecloud/simpletest/MainCASDelegateCAS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasLogin;
import com.casinthecloud.simpletest.cas.CasValidate;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.AND;

public class MainCASDelegateCAS {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasDelegate(2, "CasClient", new CasLogin());
login.setCasPrefixUrl("http://oidc-server:8080/cas");
val validate = new CasValidate();
validate.setCasPrefixUrl(login.getCasPrefixUrl());
return AND(login, validate);
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasOIDCLogin;
import com.casinthecloud.simpletest.cas.CasValidate;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.AND;

public class MainCASDelegateOIDC {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasDelegate(2, "OidcClient", new CasOIDCLogin());
login.setCasPrefixUrl("http://oidc-server:8080/cas");
val validate = new CasValidate();
validate.setCasPrefixUrl(login.getCasPrefixUrl());
return AND(login, validate);
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.cas.CasValidate;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.AND;

public class MainCASDelegateSAML {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasDelegate(1, "SAML2Client", new CasSAML2Login());
login.setCasPrefixUrl("http://oidc-server:8080/cas");
val validate = new CasValidate();
validate.setCasPrefixUrl(login.getCasPrefixUrl());
return AND(login, validate);
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasLogin;
import com.casinthecloud.simpletest.cas.CasOIDCLogin;
import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.cas.CasOIDCValidateOC;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

public class MainDelegate {
import static com.casinthecloud.simpletest.util.Utils.AND;

public class MainOIDCDelegateCAS {

public static void main(final String... args) throws Exception {
/*new Execution(() -> {
val login = new CasOIDCLogin(new CasDelegate("OidcClient", new CasOIDCLogin()));
new Execution(() -> {
val login = new CasOIDCLogin(new CasDelegate(2, "CasClient", new CasLogin()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
val validate = new CasOIDCValidateOC();
validate.setCasPrefixUrl(login.getCasPrefixUrl());
return AND(login, validate);
}).launch();*/
new Execution(() -> {
val login = new CasOIDCLogin(new CasDelegate("OidcClient", new CasSAML2Login()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
return login;
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasOIDCLogin;
import com.casinthecloud.simpletest.cas.CasOIDCValidateOC;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.AND;

public class MainOIDCDelegateOIDC {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasOIDCLogin(new CasDelegate(2, "OidcClient", new CasOIDCLogin()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
val validate = new CasOIDCValidateOC();
validate.setCasPrefixUrl(login.getCasPrefixUrl());
return AND(login, validate);
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasOIDCLogin;
import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

public class MainOIDCDelegateSAML {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasOIDCLogin(new CasDelegate(1, "SAML2Client", new CasSAML2Login()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
return login;
}).launch();
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/casinthecloud/simpletest/MainSAML.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

public class MainSAML {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasSAML2Login();
login.setCasPrefixUrl("http://oidc-server:8080/cas");
return login;
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasLogin;
import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

public class MainSAMLDelegateCAS {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasSAML2Login(new CasDelegate(2, "CasClient", new CasLogin()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
return login;
}).launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.casinthecloud.simpletest;

import com.casinthecloud.simpletest.cas.CasDelegate;
import com.casinthecloud.simpletest.cas.CasOIDCLogin;
import com.casinthecloud.simpletest.cas.CasSAML2Login;
import com.casinthecloud.simpletest.execution.Execution;
import lombok.val;

public class MainSAMLDelegateOIDC {

public static void main(final String... args) throws Exception {
new Execution(() -> {
val login = new CasSAML2Login(new CasDelegate(2, "OidcClient", new CasOIDCLogin()));
login.setCasPrefixUrl("http://oidc-server:8080/cas");
return login;
}).launch();
}
}
Loading

0 comments on commit 76ee155

Please sign in to comment.