Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit a1d4ac6

Browse files
committed
Fix jooby-project#115 pac4j module
+ some utility and new methods around Request & Mutant
1 parent 0c3adf3 commit a1d4ac6

File tree

60 files changed

+3510
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3510
-57
lines changed

coverage-report/pom.xml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<source>${project.parent.basedir}/jooby-hbv/src/main/java</source>
5454
<source>${project.parent.basedir}/jooby-gson/src/main/java</source>
5555
<source>${project.parent.basedir}/jooby-ehcache/src/main/java</source>
56+
<source>${project.parent.basedir}/jooby-pac4j/src/main/java</source>
5657
</sources>
5758
</configuration>
5859
</execution>
@@ -84,6 +85,7 @@
8485
<source>${project.parent.basedir}/jooby-hbv/src/test/java</source>
8586
<source>${project.parent.basedir}/jooby-gson/src/test/java</source>
8687
<source>${project.parent.basedir}/jooby-ehcache/src/test/java</source>
88+
<source>${project.parent.basedir}/jooby-pac4j/src/test/java</source>
8789
</sources>
8890
</configuration>
8991
</execution>
@@ -229,7 +231,7 @@
229231
<artifactId>jooby-elasticsearch</artifactId>
230232
<version>${project.version}</version>
231233
</dependency>
232-
234+
233235
<dependency>
234236
<groupId>org.jooby</groupId>
235237
<artifactId>jooby-ehcache</artifactId>
@@ -292,6 +294,27 @@
292294
<version>${project.version}</version>
293295
</dependency>
294296

297+
<dependency>
298+
<groupId>org.jooby</groupId>
299+
<artifactId>jooby-pac4j</artifactId>
300+
<version>${project.version}</version>
301+
</dependency>
302+
303+
<dependency>
304+
<groupId>org.pac4j</groupId>
305+
<artifactId>pac4j-http</artifactId>
306+
</dependency>
307+
308+
<dependency>
309+
<groupId>org.pac4j</groupId>
310+
<artifactId>pac4j-oauth</artifactId>
311+
</dependency>
312+
313+
<dependency>
314+
<groupId>org.pac4j</groupId>
315+
<artifactId>pac4j-saml</artifactId>
316+
</dependency>
317+
295318
<dependency>
296319
<groupId>org.webjars</groupId>
297320
<artifactId>jquery</artifactId>
@@ -389,6 +412,13 @@
389412
<scope>test</scope>
390413
</dependency>
391414

415+
<dependency>
416+
<groupId>org.jsoup</groupId>
417+
<artifactId>jsoup</artifactId>
418+
<version>1.8.2</version>
419+
<scope>test</scope>
420+
</dependency>
421+
392422
</dependencies>
393423

394424
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class ParamMapFeature extends ServerFeature {
7+
8+
{
9+
post("/params/:p1", req -> req.params().toMap());
10+
11+
post("/sparams/:p1", req -> req.param("p1").toMap());
12+
13+
post("/bparams/", req -> req.body().toMap().entrySet().iterator().next().getValue().value());
14+
}
15+
16+
@Test
17+
public void params() throws Exception {
18+
request()
19+
.post("/params/0")
20+
.expect("{p1=[0]}");
21+
22+
request()
23+
.post("/sparams/0")
24+
.expect("{p1=[0]}");
25+
26+
request()
27+
.post("/bparams")
28+
.body("0", "text/plain")
29+
.expect("0");
30+
31+
}
32+
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jooby.pac4j;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.jooby.test.ServerFeature;
6+
import org.jsoup.Jsoup;
7+
import org.jsoup.nodes.Document;
8+
import org.junit.Test;
9+
10+
public class AuthDefaultClientFeature extends ServerFeature {
11+
12+
{
13+
14+
use(new Auth());
15+
16+
get("/", req -> req.path());
17+
}
18+
19+
@Test
20+
public void auth() throws Exception {
21+
request()
22+
.get("/auth?username=test&password=test")
23+
.expect("/");
24+
}
25+
26+
@Test
27+
public void redirectToLoginPage() throws Exception {
28+
request()
29+
.dontFollowRedirect()
30+
.get("/auth/form")
31+
.expect(302)
32+
.header("Location", "/login");
33+
}
34+
35+
@Test
36+
public void loginPage() throws Exception {
37+
request()
38+
.get("/auth/form")
39+
.expect(rsp -> {
40+
Document html = Jsoup.parse(rsp);
41+
assertEquals("Login Page", html.getElementsByTag("title").iterator().next().text());
42+
});
43+
}
44+
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jooby.pac4j;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
import org.pac4j.http.profile.HttpProfile;
6+
7+
public class AuthWithPathPatternFeature extends ServerFeature {
8+
9+
{
10+
11+
use(new Auth().basic("/private/**"));
12+
13+
get("/hello", () -> "hi");
14+
15+
get("/private", req -> req.require(HttpProfile.class).getId());
16+
}
17+
18+
@Test
19+
public void noauth() throws Exception {
20+
request()
21+
.get("/hello")
22+
.expect("hi");
23+
}
24+
25+
@Test
26+
public void auth() throws Exception {
27+
request()
28+
.basic("test", "test")
29+
.get("/private")
30+
.expect("test");
31+
}
32+
33+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.jooby.pac4j;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
import org.pac4j.http.profile.HttpProfile;
10+
11+
public class AuthWithStoreFeature extends ServerFeature {
12+
13+
public static class InMemory implements AuthStore<HttpProfile> {
14+
15+
private Map<String, HttpProfile> profiles = new HashMap<>();
16+
17+
@Override
18+
public Optional<HttpProfile> get(final String id) throws Exception {
19+
return Optional.of(profiles.get(id));
20+
}
21+
22+
@Override
23+
public void set(final HttpProfile profile) throws Exception {
24+
profiles.put(profile.getId(), profile);
25+
26+
}
27+
28+
@Override
29+
public Optional<HttpProfile> unset(final String id) throws Exception {
30+
return Optional.ofNullable(profiles.remove(id));
31+
}
32+
33+
}
34+
35+
{
36+
37+
use(new Auth().form().store(InMemory.class));
38+
39+
get("/", req -> req.require(HttpProfile.class).getId());
40+
}
41+
42+
@Test
43+
public void auth() throws Exception {
44+
request()
45+
.get("/auth?username=test&password=test")
46+
.expect("test");
47+
}
48+
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.jooby.pac4j;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
import org.pac4j.http.client.BasicAuthClient;
6+
import org.pac4j.http.credentials.SimpleTestUsernamePasswordAuthenticator;
7+
import org.pac4j.http.profile.UsernameProfileCreator;
8+
9+
public class BasicAuthAsClient2Feature extends ServerFeature {
10+
11+
{
12+
13+
use(new Auth()
14+
.client(config ->
15+
new BasicAuthClient(
16+
new SimpleTestUsernamePasswordAuthenticator(),
17+
new UsernameProfileCreator())
18+
));
19+
20+
get("/auth/basic", req -> req.path());
21+
}
22+
23+
@Test
24+
public void auth() throws Exception {
25+
request()
26+
.basic("test", "test")
27+
.get("/auth/basic")
28+
.expect("/auth/basic");
29+
}
30+
31+
@Test
32+
public void unauthorizedAjax() throws Exception {
33+
request()
34+
.get("/auth/basic")
35+
.header("X-Requested-With", "XMLHttpRequest")
36+
.expect(401);
37+
}
38+
39+
@Test
40+
public void unauthorized() throws Exception {
41+
request()
42+
.get("/auth/basic")
43+
.expect(401);
44+
45+
}
46+
47+
@Test
48+
public void badCredentials() throws Exception {
49+
request()
50+
.basic("test", "")
51+
.get("/auth/basic")
52+
.expect(401);
53+
}
54+
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.jooby.pac4j;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
import org.pac4j.http.client.BasicAuthClient;
6+
import org.pac4j.http.credentials.SimpleTestUsernamePasswordAuthenticator;
7+
import org.pac4j.http.profile.UsernameProfileCreator;
8+
9+
public class BasicAuthAsClientFeature extends ServerFeature {
10+
11+
{
12+
13+
use(new Auth()
14+
.client(
15+
new BasicAuthClient(
16+
new SimpleTestUsernamePasswordAuthenticator(),
17+
new UsernameProfileCreator())
18+
));
19+
20+
get("/auth/basic", req -> req.path());
21+
}
22+
23+
@Test
24+
public void auth() throws Exception {
25+
request()
26+
.basic("test", "test")
27+
.get("/auth/basic")
28+
.expect("/auth/basic");
29+
}
30+
31+
@Test
32+
public void unauthorizedAjax() throws Exception {
33+
request()
34+
.get("/auth/basic")
35+
.header("X-Requested-With", "XMLHttpRequest")
36+
.expect(401);
37+
}
38+
39+
@Test
40+
public void unauthorized() throws Exception {
41+
request()
42+
.get("/auth/basic")
43+
.expect(401);
44+
45+
}
46+
47+
@Test
48+
public void badCredentials() throws Exception {
49+
request()
50+
.basic("test", "")
51+
.get("/auth/basic")
52+
.expect(401);
53+
}
54+
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.jooby.pac4j;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class BasicAuthFeature extends ServerFeature {
7+
8+
{
9+
10+
use(new Auth().basic());
11+
12+
get("/auth/basic", req -> req.path());
13+
}
14+
15+
@Test
16+
public void auth() throws Exception {
17+
request()
18+
.basic("test", "test")
19+
.get("/auth/basic")
20+
.expect("/auth/basic");
21+
}
22+
23+
@Test
24+
public void unauthorizedAjax() throws Exception {
25+
request()
26+
.get("/auth/basic")
27+
.header("X-Requested-With", "XMLHttpRequest")
28+
.expect(401);
29+
}
30+
31+
@Test
32+
public void unauthorized() throws Exception {
33+
request()
34+
.get("/auth/basic")
35+
.expect(401);
36+
37+
}
38+
39+
@Test
40+
public void badCredentials() throws Exception {
41+
request()
42+
.basic("test", "")
43+
.get("/auth/basic")
44+
.expect(401);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)