Skip to content

Commit

Permalink
pac4j: don't wrap exception as runtime exception fix #3623
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Feb 18, 2025
1 parent 4cd96a9 commit 269de30
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Context;
import io.jooby.Route;
import io.jooby.SneakyThrows;
import io.jooby.pac4j.Pac4jFrameworkParameters;
import io.jooby.pac4j.Pac4jOptions;

Expand All @@ -26,16 +27,23 @@ public CallbackFilterImpl(Config config, Pac4jOptions options) {

@NonNull @Override
public Object apply(@NonNull Context ctx) throws Exception {
var result =
config
.getCallbackLogic()
.perform(
config,
options.getDefaultUrl(),
options.getRenewSession(),
options.getDefaultClient(),
Pac4jFrameworkParameters.create(ctx));

return result == null ? ctx : result;
try {
var result =
config
.getCallbackLogic()
.perform(
config,
options.getDefaultUrl(),
options.getRenewSession(),
options.getDefaultClient(),
Pac4jFrameworkParameters.create(ctx));

return result == null ? ctx : result;
} catch (RuntimeException re) {
if (re.getCause() != null) {
throw SneakyThrows.propagate(re.getCause());
}
throw re;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Context;
import io.jooby.Route;
import io.jooby.SneakyThrows;
import io.jooby.pac4j.Pac4jFrameworkParameters;
import io.jooby.pac4j.Pac4jOptions;

Expand All @@ -26,20 +27,27 @@ public LogoutImpl(Config config, Pac4jOptions options) {

@NonNull @Override
public Object apply(@NonNull Context ctx) throws Exception {
var redirectTo = (String) ctx.getAttributes().get("pac4j.logout.redirectTo");
if (redirectTo == null || redirectTo.isEmpty()) {
redirectTo = options.getDefaultUrl();
try {
var redirectTo = (String) ctx.getAttributes().get("pac4j.logout.redirectTo");
if (redirectTo == null || redirectTo.isEmpty()) {
redirectTo = options.getDefaultUrl();
}
redirectTo = ctx.getRequestURL(redirectTo);
return config
.getLogoutLogic()
.perform(
config,
redirectTo,
options.getLogoutUrlPattern(),
options.isLocalLogout(),
options.isDestroySession(),
options.isCentralLogout(),
Pac4jFrameworkParameters.create(ctx));
} catch (RuntimeException re) {
if (re.getCause() != null) {
throw SneakyThrows.propagate(re.getCause());
}
throw re;
}
redirectTo = ctx.getRequestURL(redirectTo);
return config
.getLogoutLogic()
.perform(
config,
redirectTo,
options.getLogoutUrlPattern(),
options.isLocalLogout(),
options.isDestroySession(),
options.isCentralLogout(),
Pac4jFrameworkParameters.create(ctx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jooby.Context;
import io.jooby.Route;
import io.jooby.SneakyThrows;
import io.jooby.pac4j.Pac4jFrameworkParameters;
import io.jooby.pac4j.Pac4jOptions;

Expand Down Expand Up @@ -76,12 +77,19 @@ public Object apply(@NonNull Context ctx) throws Exception {
return perform(ctx, new GrantAccessAdapterImpl(ctx, options));
}

private Object perform(Context ctx, GrantAccessAdapterImpl accessAdapter) throws Exception {
var securityLogic = config.getSecurityLogic();
var clients = ctx.lookup(clientName(securityLogic)).value(this.clients.get());
var authorizers = ofNullable(this.authorizers).orElse(NoopAuthorizer.NAME);
return securityLogic.perform(
config, accessAdapter, clients, authorizers, null, Pac4jFrameworkParameters.create(ctx));
private Object perform(Context ctx, GrantAccessAdapterImpl accessAdapter) {
try {
var securityLogic = config.getSecurityLogic();
var clients = ctx.lookup(clientName(securityLogic)).value(this.clients.get());
var authorizers = ofNullable(this.authorizers).orElse(NoopAuthorizer.NAME);
return securityLogic.perform(
config, accessAdapter, clients, authorizers, null, Pac4jFrameworkParameters.create(ctx));
} catch (RuntimeException re) {
if (re.getCause() != null) {
throw SneakyThrows.propagate(re.getCause());
}
throw re;
}
}

private String clientName(SecurityLogic securityLogic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ private static class ProtectedPath {

private final List<Object> clients = new ArrayList<>();

public ProtectedPath add(String authorizer, Object client) {
public void add(String authorizer, Object client) {
this.authorizers.add(authorizer);
this.clients.add(client);
return this;
}
}

Expand Down

0 comments on commit 269de30

Please sign in to comment.