Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compatible with pac4j 6.0.0-RC7 #209

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
</developers>

<properties>
<javalin.version>5.5.0</javalin.version>
<pac4j.version>6.0.0-RC1</pac4j.version>
<javalin.version>5.6.0</javalin.version>
<pac4j.version>6.0.0-RC7</pac4j.version>
<java.version>17</java.version>
</properties>

Expand Down
27 changes: 12 additions & 15 deletions src/main/java/org/pac4j/javalin/CallbackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
import io.javalin.http.Handler;
import org.jetbrains.annotations.NotNull;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.context.session.SessionStoreFactory;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.util.FindBest;
import org.pac4j.jee.context.session.JEESessionStoreFactory;
import org.pac4j.jee.context.JEEFrameworkParameters;

import static org.pac4j.core.util.CommonHelper.assertNotNull;

Expand All @@ -36,19 +32,20 @@ public CallbackHandler(Config config, String defaultUrl, Boolean renewSession) {

@Override
public void handle(@NotNull Context javalinCtx) {
final SessionStoreFactory sessionStoreFactory = FindBest.sessionStoreFactory(null, config, JEESessionStoreFactory.INSTANCE);
final SessionStore sessionStore = sessionStoreFactory.newSessionStore(javalinCtx);
final HttpActionAdapter bestAdapter = FindBest.httpActionAdapter(null, config, JavalinHttpActionAdapter.INSTANCE);
final CallbackLogic bestCallbackLogic = FindBest.callbackLogic(null, config, DefaultCallbackLogic.INSTANCE);

JavalinWebContext context = new JavalinWebContext(javalinCtx);
bestCallbackLogic.perform(context,
sessionStore,
final CallbackLogic callbackLogic;
spinscale marked this conversation as resolved.
Show resolved Hide resolved
if (config.getCallbackLogic() != null) {
callbackLogic = config.getCallbackLogic();
} else {
callbackLogic = DefaultCallbackLogic.INSTANCE;
}

callbackLogic.perform(
this.config,
bestAdapter,
this.defaultUrl,
this.renewSession,
config.getClients().getClients().get(0).getName()
config.getClients().getClients().get(0).getName(),
new JEEFrameworkParameters(javalinCtx.req(), javalinCtx.res())
);

}
}
27 changes: 19 additions & 8 deletions src/main/java/org/pac4j/javalin/JavalinHttpActionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.javalin.http.BadRequestResponse;
import io.javalin.http.ForbiddenResponse;
import io.javalin.http.HttpStatus;
import io.javalin.http.RedirectResponse;
import io.javalin.http.UnauthorizedResponse;
import org.pac4j.core.context.HttpConstants;
Expand All @@ -12,6 +11,10 @@
import org.pac4j.core.exception.http.WithLocationAction;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.jee.context.JEEContext;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
* @author Maximilian Hippler
Expand All @@ -24,10 +27,10 @@ public class JavalinHttpActionAdapter implements HttpActionAdapter {
public Void adapt(HttpAction action, WebContext webContext) {
CommonHelper.assertNotNull("action", action);
CommonHelper.assertNotNull("context", webContext);
if (webContext instanceof JavalinWebContext == false) {
throw new RuntimeException("not a Javalin web context, but " + webContext.getClass().getName());
if (webContext instanceof JEEContext == false) {
throw new RuntimeException("not a JEEContext, but " + webContext.getClass().getName());
}
JavalinWebContext context = (JavalinWebContext) webContext;
JEEContext context = (JEEContext) webContext;

final int code = action.getCode();
if (code == HttpConstants.UNAUTHORIZED) {
Expand All @@ -37,14 +40,22 @@ public Void adapt(HttpAction action, WebContext webContext) {
} else if (code == HttpConstants.BAD_REQUEST) {
throw new BadRequestResponse();
} else if (action instanceof WithContentAction){
context.getJavalinCtx().status(action.getCode());
context.getJavalinCtx().result(((WithContentAction) action).getContent());
context.getNativeResponse().setStatus(action.getCode());
String responseData = ((WithContentAction) action).getContent();
context.getNativeResponse().setContentLength(responseData.length());
try {
context.getNativeResponse().getOutputStream().write(responseData.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
} else if (action instanceof WithLocationAction) {
context.getJavalinCtx().redirect(((WithLocationAction) action).getLocation(), HttpStatus.forStatus(action.getCode()));
context.getNativeResponse().setStatus(action.getCode());
String location = ((WithLocationAction) action).getLocation();
context.getNativeResponse().setHeader("Location", location);
throw new RedirectResponse();
} else {
context.getJavalinCtx().status(action.getCode());
context.getNativeResponse().setStatus(action.getCode());
return null;
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/org/pac4j/javalin/JavalinWebContext.java

This file was deleted.

26 changes: 11 additions & 15 deletions src/main/java/org/pac4j/javalin/LogoutHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
import io.javalin.http.Handler;
import org.jetbrains.annotations.NotNull;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.context.session.SessionStoreFactory;
import org.pac4j.core.engine.DefaultLogoutLogic;
import org.pac4j.core.engine.LogoutLogic;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.util.FindBest;
import org.pac4j.jee.context.session.JEESessionStoreFactory;
import org.pac4j.jee.context.JEEFrameworkParameters;

import static org.pac4j.core.util.CommonHelper.assertNotNull;

Expand Down Expand Up @@ -39,21 +35,21 @@ public LogoutHandler(Config config, String defaultUrl, String logoutUrlPattern)

@Override
public void handle(@NotNull Context javalinCtx) {
final SessionStoreFactory sessionStoreFactory = FindBest.sessionStoreFactory(null, config, JEESessionStoreFactory.INSTANCE);
final SessionStore sessionStore = sessionStoreFactory.newSessionStore(javalinCtx);
final HttpActionAdapter bestAdapter = FindBest.httpActionAdapter(null, config, JavalinHttpActionAdapter.INSTANCE);
final LogoutLogic bestLogic = FindBest.logoutLogic(null, config, DefaultLogoutLogic.INSTANCE);

bestLogic.perform(
new JavalinWebContext(javalinCtx),
sessionStore,
final LogoutLogic logoutLogic;
spinscale marked this conversation as resolved.
Show resolved Hide resolved
if (config.getLogoutLogic() == null) {
logoutLogic = DefaultLogoutLogic.INSTANCE;
} else {
logoutLogic = config.getLogoutLogic();
}

logoutLogic.perform(
this.config,
bestAdapter,
this.defaultUrl,
this.logoutUrlPattern,
this.localLogout,
this.destroySession,
this.centralLogout
this.centralLogout,
new JEEFrameworkParameters(javalinCtx.req(), javalinCtx.res())
);
}
}
27 changes: 11 additions & 16 deletions src/main/java/org/pac4j/javalin/SecurityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
import io.javalin.http.servlet.JavalinServletContext;
import org.jetbrains.annotations.NotNull;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.context.session.SessionStoreFactory;
import org.pac4j.core.engine.DefaultSecurityLogic;
import org.pac4j.core.engine.SecurityLogic;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.util.FindBest;
import org.pac4j.jee.context.session.JEESessionStoreFactory;
import org.pac4j.jee.context.JEEFrameworkParameters;

import static org.pac4j.core.util.CommonHelper.assertNotNull;

Expand Down Expand Up @@ -41,21 +37,20 @@ public SecurityHandler(Config config, String clients, String authorizers, String

@Override
public void handle(@NotNull Context javalinCtx) {
final SessionStoreFactory sessionStoreFactory = FindBest.sessionStoreFactory(null, config, JEESessionStoreFactory.INSTANCE);
final SessionStore sessionStore = sessionStoreFactory.newSessionStore(javalinCtx);
final HttpActionAdapter bestAdapter = FindBest.httpActionAdapter(null, config, JavalinHttpActionAdapter.INSTANCE);
final SecurityLogic bestLogic = FindBest.securityLogic(null, config, DefaultSecurityLogic.INSTANCE);
final SecurityLogic securityLogic;
spinscale marked this conversation as resolved.
Show resolved Hide resolved
if (config.getSecurityLogic() == null) {
securityLogic = DefaultSecurityLogic.INSTANCE;
} else {
securityLogic = config.getSecurityLogic();
}

JavalinWebContext context = new JavalinWebContext(javalinCtx);
Object result = bestLogic.perform(
context,
sessionStore,
Object result = securityLogic.perform(
this.config,
(ctx, store, profiles, parameters) -> AUTH_GRANTED,
bestAdapter,
(ctx, store, profiles) -> AUTH_GRANTED,
this.clients,
this.authorizers,
this.matchers
this.matchers,
new JEEFrameworkParameters(javalinCtx.req(), javalinCtx.res())
);
if (result != AUTH_GRANTED) {
((JavalinServletContext) javalinCtx).getTasks().clear(); // Used to throw UnauthorizedResponse
Expand Down
115 changes: 16 additions & 99 deletions src/test/java/org/pac4j/javalin/CallbackHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,143 +6,60 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.http.client.indirect.FormClient;
import org.pac4j.jee.context.session.JEESessionStore;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

public class CallbackHandlerTest {

private final TestCallbackLogic testCallbackLogic = new TestCallbackLogic();
private final CallbackLogic callbackLogic = mock(CallbackLogic.class);
private final HttpServletRequest req = mock(HttpServletRequest.class);
private final HttpServletResponse res = mock(HttpServletResponse.class);
private final Context ctx = mock(Context.class);
private final FormClient formClient = new FormClient();
private final Config config = new Config(formClient);
private final CallbackHandler handler = new CallbackHandler(config, "DefaultClient");

@BeforeEach
public void setCallbackLogic() {
config.setCallbackLogic(testCallbackLogic);
config.setCallbackLogic(callbackLogic);
formClient.setCallbackUrl("http://example.org/callbackUrl");
when(ctx.res()).thenReturn(res);
when(ctx.req()).thenReturn(req);
}

@Test
public void testDefaultSessionStore() {
public void testDefaultUrlIsNull() {
CallbackHandler handler = new CallbackHandler(config);
handler.handle(ctx);

assertThat(testCallbackLogic.sessionStore).isEqualTo(JEESessionStore.INSTANCE);
assertThat(testCallbackLogic.webContext).isExactlyInstanceOf(JavalinWebContext.class);
assertThat(testCallbackLogic.config).isSameAs(config);
verify(callbackLogic).perform(eq(config), isNull(), any(), eq("FormClient"), any());
}

@Test
public void testCustomSessionStore() {
final SessionStore mockSessionStore = mock(SessionStore.class);
config.setSessionStoreFactory(parameters -> mockSessionStore);

public void testDefaultUrl() {
CallbackHandler handler = new CallbackHandler(config, "/my-url");
handler.handle(ctx);

assertThat(testCallbackLogic.sessionStore).isNotEqualTo(JEESessionStore.INSTANCE);
assertThat(testCallbackLogic.sessionStore).isEqualTo(mockSessionStore);
verify(callbackLogic).perform(eq(config), eq("/my-url"), any(), any(), any());
}

@Test
public void testDefaultAdapter() {
handler.handle(ctx);

assertThat(testCallbackLogic.httpActionAdapter).isEqualTo(JavalinHttpActionAdapter.INSTANCE);
}

@Test
public void testCustomAdapter() {
HttpActionAdapter actionAdapter = new JavalinHttpActionAdapter();
config.setHttpActionAdapter(actionAdapter);
public void testRenewSession() {
CallbackHandler handler = new CallbackHandler(config, "/my-url", true);

handler.handle(ctx);

assertThat(testCallbackLogic.httpActionAdapter).isNotEqualTo(JavalinHttpActionAdapter.INSTANCE);
assertThat(testCallbackLogic.httpActionAdapter).isEqualTo(actionAdapter);
verify(callbackLogic).perform(eq(config), any(), eq(true), any(), any());
}

@Test
public void testCustomClientName() {
formClient.setName("my-name");

handler.handle(ctx);
public void testRenewSessionFalse() {
CallbackHandler handler = new CallbackHandler(config, "/my-url", false);

assertThat(testCallbackLogic.defaultClient).isEqualTo("my-name");
}

@Test
public void testCustomDefaultUrl() {
final Config config = new Config(formClient);
config.setCallbackLogic(testCallbackLogic);
final CallbackHandler handler = new CallbackHandler(config, "http://example.org", true);

handler.handle(ctx);

assertThat(testCallbackLogic.defaultUrl).isEqualTo("http://example.org");
}

@Test
public void testDefaultRenewSession() {
handler.handle(ctx);

assertThat(testCallbackLogic.renewSession).isNull();
}

@Test
public void testCustomRenewSessionTrue() {
final Config config = new Config(formClient);
config.setCallbackLogic(testCallbackLogic);
final CallbackHandler handler = new CallbackHandler(config, "http://example.org", true);

handler.handle(ctx);

assertThat(testCallbackLogic.renewSession).isTrue();
}

@Test
public void testCustomRenewSessionFalse() {
final Config config = new Config(formClient);
config.setCallbackLogic(testCallbackLogic);
final CallbackHandler handler = new CallbackHandler(config, "http://example.org", false);

handler.handle(ctx);

assertThat(testCallbackLogic.renewSession).isFalse();
}

public static class TestCallbackLogic implements CallbackLogic {

private WebContext webContext;
private SessionStore sessionStore;
private Config config;
private HttpActionAdapter httpActionAdapter;
private String defaultUrl;
private Boolean renewSession;
private String defaultClient;

@Override
public Object perform(WebContext webContext, SessionStore sessionStore, Config config,
HttpActionAdapter httpActionAdapter, String defaultUrl, Boolean renewSession, String defaultClient) {
this.webContext = webContext;
this.sessionStore = sessionStore;
this.config = config;
this.httpActionAdapter = httpActionAdapter;
this.defaultUrl = defaultUrl;
this.renewSession = renewSession;
this.defaultClient = defaultClient;
return null;
}
verify(callbackLogic).perform(eq(config), any(), eq(false), any(), any());
}
}
Loading