Skip to content

Commit 2fc2f57

Browse files
committed
Fix Context.matches and Pac4j issues
- router: Context.matches(pattern) broken since 2.8.7 fix jooby-project#1831 - pac4j: nullpointerexception on default setup fix jooby-project#1830 - Since 2.8.7, jooby-project#1737 issue is back, Once logged out, "ERR_TOO_MANY_REDIRECTS" is returned when trying any page. fix jooby-project#1822
1 parent 988bd4a commit 2fc2f57

File tree

7 files changed

+62
-35
lines changed

7 files changed

+62
-35
lines changed

jooby/src/main/java/io/jooby/internal/Chi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ public void destroy() {
753753
}
754754

755755
public boolean exists(String method, String path) {
756-
return find(method, path) != null;
756+
return find(method, path).matches();
757757
}
758758

759759
@Override public Router.Match find(String method, String path) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.jooby.internal;
2+
3+
import io.jooby.MessageEncoder;
4+
import io.jooby.Route;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class Issue1822 {
11+
12+
@Test
13+
public void routerExistsShouldNotReturnsFalsePositives() {
14+
String pattern = "/api/*";
15+
String path = "/";
16+
Chi router = new Chi();
17+
router.insert(route("GET", pattern, stringHandler("api")));
18+
assertFalse(router.exists("GET", path));
19+
assertTrue(router.exists("GET", "/api/v1"));
20+
}
21+
22+
private Route.Handler stringHandler(String foo) {
23+
return ctx -> foo;
24+
}
25+
26+
private Route route(String method, String pattern, Route.Handler handler) {
27+
return new Route(method, pattern, handler)
28+
.setEncoder(MessageEncoder.TO_STRING);
29+
}
30+
}

modules/jooby-pac4j/src/main/java/io/jooby/pac4j/Pac4jModule.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -271,32 +271,30 @@ public Pac4jModule client(Class<? extends Authorizer> authorizer,
271271
boolean devLogin = false;
272272
if (clientMap.isEmpty()) {
273273
devLogin = true;
274-
allClients.computeIfAbsent("*", k -> new ArrayList<>())
275-
.add(new FormClient(contextPath + "/login",
276-
new SimpleTestUsernamePasswordAuthenticator()));
277-
} else {
278-
com.typesafe.config.Config conf = application.getConfig();
279-
/** Initialize clients from DSL: */
280-
for (Map.Entry<String, ProtectedPath> routing : clientMap.entrySet()) {
281-
List<Client> localClients = allClients
282-
.computeIfAbsent(routing.getKey(), k -> new ArrayList<>());
283-
ProtectedPath path = routing.getValue();
284-
for (Object candidate : path.clients) {
285-
if (candidate instanceof Client) {
286-
localClients.add((Client) candidate);
287-
} else {
288-
Function<com.typesafe.config.Config, Client> clientProvider = (Function<com.typesafe.config.Config, Client>) candidate;
289-
localClients.add(clientProvider.apply(conf));
290-
}
274+
client(conf -> new FormClient(contextPath + "/login",
275+
new SimpleTestUsernamePasswordAuthenticator()));
276+
}
277+
com.typesafe.config.Config conf = application.getConfig();
278+
/** Initialize clients from DSL: */
279+
for (Map.Entry<String, ProtectedPath> routing : clientMap.entrySet()) {
280+
List<Client> localClients = allClients
281+
.computeIfAbsent(routing.getKey(), k -> new ArrayList<>());
282+
ProtectedPath path = routing.getValue();
283+
for (Object candidate : path.clients) {
284+
if (candidate instanceof Client) {
285+
localClients.add((Client) candidate);
286+
} else {
287+
Function<com.typesafe.config.Config, Client> clientProvider = (Function<com.typesafe.config.Config, Client>) candidate;
288+
localClients.add(clientProvider.apply(conf));
291289
}
292-
allClients.put(routing.getKey(), localClients);
290+
}
291+
allClients.put(routing.getKey(), localClients);
293292

294-
// check for forwarding authorizers
295-
for (String authorizerName : path.authorizers) {
296-
Authorizer authorizer = pac4j.getAuthorizers().get(authorizerName);
297-
if (authorizer instanceof ForwardingAuthorizer) {
298-
((ForwardingAuthorizer) authorizer).setRegistry(application);
299-
}
293+
// check for forwarding authorizers
294+
for (String authorizerName : path.authorizers) {
295+
Authorizer authorizer = pac4j.getAuthorizers().get(authorizerName);
296+
if (authorizer instanceof ForwardingAuthorizer) {
297+
((ForwardingAuthorizer) authorizer).setRegistry(application);
300298
}
301299
}
302300
}

starters/pac4j-form-starter/src/main/java/starter/App.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.jooby.jdbi.JdbiModule;
99
import io.jooby.jdbi.TransactionalRequest;
1010
import io.jooby.pac4j.Pac4jModule;
11-
import io.jooby.pac4j.Pac4jOptions;
1211
import org.pac4j.core.credentials.UsernamePasswordCredentials;
1312
import org.pac4j.core.credentials.authenticator.Authenticator;
1413
import org.pac4j.http.client.indirect.FormClient;
@@ -17,9 +16,6 @@
1716

1817
public class App extends Jooby {
1918
{
20-
System.out.println("cccc");
21-
setContextPath("/app");
22-
2319
/** DataSource module: */
2420
install(new HikariModule());
2521

@@ -37,15 +33,12 @@ public class App extends Jooby {
3733

3834
get("/login", ctx -> new ModelAndView("login.hbs"));
3935

40-
Pac4jOptions options = new Pac4jOptions();
41-
options.setDefaultUrl("/dashboard");
42-
install(new Pac4jModule(options)
36+
install(new Pac4jModule()
4337
.client(conf -> new FormClient("/login", authenticator()))
4438
);
4539

4640
get("/", ctx -> new ModelAndView("welcome.hbs")
4741
.put("user", ctx.getUser())
48-
.put("defurl", ctx.getRequestURL(options.getDefaultUrl()))
4942
);
5043
}
5144

starters/pac4j-form-starter/views/login.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</head>
2626
<body onload="document.form.username.focus();">
2727
<h3>Login</h3>
28-
<form name="form" action="/app/callback?client_name=FormClient" method="POST">
28+
<form name="form" action="/callback?client_name=FormClient" method="POST">
2929
<input name="username" onkeypress="onKeyPressEvent(event)" />
3030
<p></p>
3131
<input type="password" name="password" onkeypress="onKeyPressEvent(event)" />

starters/pac4j-form-starter/views/welcome.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
</head>
66
<body>
77
<h3>Welcome: {{user.username}} {{defurl}}</h3>
8-
<h4><a href="/app/logout">Logout</a></h4>
8+
<h4><a href="/logout">Logout</a></h4>
99
</body>
1010
</html>

starters/pac4j-starter/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262
<artifactId>pac4j-jwt</artifactId>
6363
</dependency>
6464

65+
<!-- required by pac4j oidc/oauth-->
66+
<dependency>
67+
<groupId>com.google.guava</groupId>
68+
<artifactId>guava</artifactId>
69+
</dependency>
70+
6571
<!-- Tests -->
6672
<dependency>
6773
<groupId>org.junit.jupiter</groupId>

0 commit comments

Comments
 (0)