Skip to content

Commit

Permalink
reuse previous login on OIDC
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Feb 5, 2024
1 parent f184e11 commit a6244fa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
23 changes: 16 additions & 7 deletions src/main/java/com/casinthecloud/simpletest/cas/CasOIDCLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,28 @@ public void run(final Context ctx) throws Exception {

info("> BEGIN CasOIDCLogin");

authorize(ctx);
val alreadyAuthenticated = authorize(ctx);

incrLevel();
this.tests[0].run(ctx);
decrLevel();
if (!alreadyAuthenticated) {
info("> Not authenticated = performing login");
incrLevel();
this.tests[0].run(ctx);
decrLevel();

callback(ctx, 302);
callback(ctx, 302);

callback(ctx, 302);
callback(ctx, 302);
} else {
info("> Already authenticated = skipping login");
}

info("< END CasOIDCLogin");

}

protected void authorize(final Context ctx) throws Exception {
protected boolean authorize(final Context ctx) throws Exception {
val alreadyAuthenticated = useCasSession(ctx);

var authorizeUrl = getLocation(ctx);
if (isNotBlank(authorizeUrl) && authorizeUrl.contains("response_type=code")) {
info("! Existing authorizeUrl");
Expand All @@ -69,5 +76,7 @@ protected void authorize(final Context ctx) throws Exception {
assertStatus(ctx, 302);

saveCasSession(ctx);

return alreadyAuthenticated;
}
}
11 changes: 8 additions & 3 deletions src/main/java/com/casinthecloud/simpletest/cas/CasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ protected void saveCasSession(final Context ctx) {
if (casSession == null) {
casSession = getCookie(ctx, JSESSIONID);
}
ctx.put(testId + CAS_SESSION, casSession);
info("Found CAS session: " + casSession.getLeft() + "=" + casSession.getRight());
if (casSession != null) {
ctx.put(testId + CAS_SESSION, casSession);
info("Found CAS session: " + casSession.getLeft() + "=" + casSession.getRight());
}
}

protected void useCasSession(final Context ctx) {
protected boolean useCasSession(final Context ctx) {
val casSession = (Pair<String, String>) ctx.get(testId + CAS_SESSION);
if (casSession != null) {
info("Re-use: " + casSession.getLeft() + "=" + casSession.getRight());
ctx.getCookies().put(casSession.getLeft(), casSession.getRight());
return true;
} else {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void run() {
test.setDisplayInfos(displayInfos);

val maxErrors = test.getMaxErrors();
val smallInterval = test.getSmallInterval();
val bigInterval = test.getBigInterval();
val interval = test.getInterval();

val ctx = new Context();

Expand All @@ -58,15 +57,18 @@ public void run() {
if (displayInfos) {
println();
}
if (smallInterval != -1 && i % smallInterval == 0) {
print((char) (97 + id));
}
if (bigInterval != -1 && i % bigInterval == 0) {
print((char) (65 + id));
if (interval != -1 && i % interval == 0) {
char c = '%';
if (id > 1 && id <= 10) {
c = (char) (37 + id);
} else if (id >= 11) {
c = (char) (58 + id - 11);
}
print((i *100 ) / nbIterations + "" + c + " ");
}
} catch (final Exception e) {
} catch (final Throwable t) {
if (displayErrors) {
e.printStackTrace();
t.printStackTrace();
} else {
print("!");
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/com/casinthecloud/simpletest/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ public abstract class BaseTest {

@Getter
@Setter
private int smallInterval = 40;

@Getter
@Setter
private int bigInterval = 250;
private int interval = 100;

@Getter
@Setter
Expand All @@ -68,11 +64,13 @@ protected String getLocation(final Context ctx) {

protected Pair<String, String> getCookie(final Context ctx, final String name) {
val listHeaders = ctx.getHeaders().get("set-cookie");
for (val header : listHeaders) {
if (header.startsWith(name)) {
val key = substringBefore(header, "=");
val value = substringBetween(header, "=", ";");
return new ImmutablePair<String, String>(key, value);
if (listHeaders != null) {
for (val header : listHeaders) {
if (header.startsWith(name)) {
val key = substringBefore(header, "=");
val value = substringBetween(header, "=", ";");
return new ImmutablePair<String, String>(key, value);
}
}
}
return null;
Expand Down

0 comments on commit a6244fa

Please sign in to comment.