forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssue1737.java
More file actions
70 lines (60 loc) · 2.46 KB
/
Copy pathIssue1737.java
File metadata and controls
70 lines (60 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package io.jooby;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import io.jooby.pac4j.Pac4jModule;
import okhttp3.FormBody;
import okhttp3.Response;
import org.pac4j.http.client.indirect.FormClient;
import org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator;
import java.net.URLEncoder;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Issue1737 {
private static final String WELCOME = "<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>\n"
+ " <title>Welcome Page</title>\n"
+ "</head>\n"
+ "<body>\n"
+ "<h3>Welcome: {0}</h3>\n"
+ "<h4><a href=\"/logout\">Logout</a></h4>\n"
+ "</body>\n"
+ "</html>\n";
@ServerTest
public void pac4jShouldWorkWithSignedSession(ServerTestRunner runner) {
runner.define(app -> {
app.setSessionStore(
SessionStore.signed("123456789", new Cookie("Test").setMaxAge(Duration.ofDays(7))));
app.install(new Pac4jModule()
.client(conf -> new FormClient("/login", new SimpleTestUsernamePasswordAuthenticator()))
);
app.get("/",
ctx -> ctx.setResponseType(MediaType.html).send(String.format(WELCOME, ctx.getUser())));
app.get("/some/page",
ctx -> ctx.setResponseType(MediaType.html).send(String.format(WELCOME, ctx.getUser())));
}).dontFollowRedirects().ready(http -> {
// Save URL
String requestedPath = "http://localhost:9999/some/page";
String cookie =
"Test=ftnFEEumoZJTty9t2TI649TA285kfDDntIVFIaDLANw|pac4jCsrfToken=f780c42c-f750-4b35-bb3d-96660acec005&pac4jRequestedUrl=p4j%7E302%3A"
+ URLEncoder.encode(requestedPath, "UTF-8");
http.header("Cookie", cookie);
http.post("/callback?client_name=FormClient", new FormBody.Builder()
.add("username", "test")
.add("password", "test")
.build(), rsp -> {
String updatedCookie = cleanCookie(rsp);
assertTrue(updatedCookie.contains("pac4jUserProfiles="), updatedCookie);
assertEquals(requestedPath, rsp.header("Location"));
});
});
}
private String cleanCookie(Response response) {
String value = response.headers("Set-Cookie").stream().filter(it -> it.startsWith("Test="))
.findFirst()
.get();
int i = value.indexOf(";Path");
return i > 0 ? value.substring(0, i) : value;
}
}