Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
112 lines (98 loc) · 3.93 KB
/
Copy pathApp.java
File metadata and controls
112 lines (98 loc) · 3.93 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package starter;
import com.typesafe.config.Config;
import io.jooby.Jooby;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.WebVariables;
import io.jooby.handlebars.HandlebarsModule;
import io.jooby.json.JacksonModule;
import io.jooby.pac4j.Pac4jModule;
import org.pac4j.core.credentials.UsernamePasswordCredentials;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.definition.CommonProfileDefinition;
import org.pac4j.core.util.Pac4jConstants;
import org.pac4j.http.client.direct.ParameterClient;
import org.pac4j.http.client.indirect.FormClient;
import org.pac4j.jwt.config.signature.SecretSignatureConfiguration;
import org.pac4j.jwt.credentials.authenticator.JwtAuthenticator;
import org.pac4j.jwt.profile.JwtGenerator;
import org.pac4j.oauth.client.FacebookClient;
import org.pac4j.oauth.client.TwitterClient;
import org.pac4j.oidc.client.GoogleOidcClient;
import org.pac4j.oidc.config.OidcConfiguration;
import java.nio.file.Paths;
public class App extends Jooby {
{
decorator(new WebVariables());
/** JSON: */
install(new JacksonModule());
/** Template engine: */
install(new HandlebarsModule());
assets("/css/*", Paths.get("views", "css"));
assets("/images/*", Paths.get("views", "images"));
get("/login", ctx -> new ModelAndView("login.hbs"));
install(new Pac4jModule()
/** Login with facebook: */
.client("/facebook", conf ->
new FacebookClient(conf.getString("fb.key"), conf.getString("fb.secret"))
)
/** Login with twitter: */
.client("/twitter", conf ->
new TwitterClient(conf.getString("twitter.key"), conf.getString("twitter.secret"))
)
/** Login with google: */
.client("/google", conf -> {
OidcConfiguration oidc = new OidcConfiguration();
oidc.setClientId(conf.getString("oidc.clientId"));
oidc.setSecret(conf.getString("oidc.secret"));
oidc.addCustomParam("prompt", "consent");
oidc.setUseNonce(true);
return new GoogleOidcClient(oidc);
})
/** Login with JWT: */
.client("/api/*", conf -> {
ParameterClient client = new ParameterClient("token",
new JwtAuthenticator(new SecretSignatureConfiguration(conf.getString("jwt.salt"))));
client.setSupportGetRequest(true);
client.setSupportPostRequest(true);
return client;
})
/** Fallback to form login: */
.client(conf ->
new FormClient("/login", ((credentials, context) -> {
// Create default profile:
String username = ((UsernamePasswordCredentials) credentials).getUsername();
final CommonProfile profile = new CommonProfile();
profile.setId(username);
profile.addAttribute(Pac4jConstants.USERNAME, username);
profile.addAttribute(CommonProfileDefinition.DISPLAY_NAME, username);
credentials.setUserProfile(profile);
}))
));
/** Protected pages: */
get("/", ctx -> {
CommonProfile profile = ctx.getUser();
return new ModelAndView("profile.hbs")
.put("jwtToken", generateToken(getConfig(), profile))
.put("profile", profile);
});
/** Generate Token for user: */
get("/generate-token", ctx -> {
String token = generateToken(getConfig(), ctx.getUser());
return ctx.setResponseType(MediaType.text).send(token);
});
/** API protected via JWT: */
get("/api/profile", ctx -> {
CommonProfile profile = ctx.getUser();
return profile.getAttributes();
});
}
private String generateToken(Config conf, CommonProfile profile) {
JwtGenerator<CommonProfile> jwtGenerator = new JwtGenerator<>(
new SecretSignatureConfiguration(conf.getString("jwt.salt")));
return jwtGenerator.generate(profile);
}
public static void main(String[] args) {
runApp(args, App::new);
}
}