|
| 1 | +/** |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package examples; |
| 7 | + |
| 8 | +import io.jooby.Context; |
| 9 | +import io.jooby.Jooby; |
| 10 | +import io.jooby.ModelAndView; |
| 11 | +import io.jooby.Session; |
| 12 | +import io.jooby.handlebars.HandlebarsModule; |
| 13 | +import io.jooby.pac4j.Pac4jContext; |
| 14 | +import io.jooby.json.JacksonModule; |
| 15 | +import io.jooby.pac4j.Pac4jModule; |
| 16 | +import org.pac4j.core.context.Pac4jConstants; |
| 17 | +import org.pac4j.core.exception.http.WithLocationAction; |
| 18 | +import org.pac4j.http.client.indirect.FormClient; |
| 19 | +import org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator; |
| 20 | +import org.pac4j.oauth.client.FacebookClient; |
| 21 | +import org.pac4j.oauth.client.TwitterClient; |
| 22 | +import org.pac4j.oidc.client.OidcClient; |
| 23 | +import org.pac4j.oidc.config.OidcConfiguration; |
| 24 | + |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +public class Pac4jApp extends Jooby { |
| 28 | + |
| 29 | + { |
| 30 | + final FacebookClient facebookClient = new FacebookClient("145278422258960", |
| 31 | + "be21409ba8f39b5dae2a7de525484da8"); |
| 32 | + TwitterClient twitterClient = new TwitterClient("CoxUiYwQOSFDReZYdjigBA", |
| 33 | + "2kAzunH5Btc4gRSaMr7D7MkyoJ5u1VzbOOzE8rBofs"); |
| 34 | + final OidcConfiguration oidcConfiguration = new OidcConfiguration(); |
| 35 | + oidcConfiguration |
| 36 | + .setClientId("343992089165-sp0l1km383i8cbm2j5nn20kbk5dk8hor.apps.googleusercontent.com"); |
| 37 | + oidcConfiguration.setSecret("uR3D8ej1kIRPbqAFaxIE3HWh"); |
| 38 | + oidcConfiguration |
| 39 | + .setDiscoveryURI("https://accounts.google.com/.well-known/openid-configuration"); |
| 40 | + oidcConfiguration.setUseNonce(true); |
| 41 | + //oidcClient.setPreferredJwsAlgorithm(JWSAlgorithm.RS256); |
| 42 | + oidcConfiguration.addCustomParam("prompt", "consent"); |
| 43 | + final OidcClient oidcClient = new OidcClient(oidcConfiguration); |
| 44 | + |
| 45 | + decorator(next -> ctx -> { |
| 46 | + try { |
| 47 | + getLog().info("{} {} sid: {} {}", ctx.getMethod(), ctx.getRequestURL(), |
| 48 | + ctx.cookie("jooby.sid").toOptional().orElse(""), requestedUrl(ctx)); |
| 49 | + return next.apply(ctx); |
| 50 | + } finally { |
| 51 | + getLog().info(" {} {} {} sid: {} {}", ctx.getMethod(), ctx.getRequestURL(), |
| 52 | + ctx.getResponseCode(), |
| 53 | + Optional.ofNullable(ctx.sessionOrNull()).map(Session::getId).orElse(""), requestedUrl(ctx)); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | +// setContextPath("/myapp"); |
| 58 | + |
| 59 | + install(new HandlebarsModule()); |
| 60 | + |
| 61 | + get("/login", ctx -> new ModelAndView("login.hbs")); |
| 62 | + |
| 63 | + install(new Pac4jModule() |
| 64 | +// .client("/google", conf -> oidcClient) |
| 65 | +// .client("/twitter", conf -> twitterClient) |
| 66 | +// .client(conf -> new FormClient("/login", new SimpleTestUsernamePasswordAuthenticator())) |
| 67 | + ); |
| 68 | + |
| 69 | + install(new JacksonModule()); |
| 70 | + |
| 71 | + get("/", ctx -> new ModelAndView("pac4j.hbs").put("user", ctx.getUser())); |
| 72 | + |
| 73 | + get("/api", ctx -> ctx.getUser()); |
| 74 | + |
| 75 | + get("/api/v1", ctx -> ctx.getUser()); |
| 76 | + } |
| 77 | + |
| 78 | + private Object requestedUrl(Context ctx) { |
| 79 | + Pac4jContext pac4jContext = Pac4jContext.create(ctx); |
| 80 | + return pac4jContext.getSessionStore().get(pac4jContext, Pac4jConstants.REQUESTED_URL).map(it-> { |
| 81 | + if (it instanceof WithLocationAction) { |
| 82 | + return ((WithLocationAction) it).getLocation(); |
| 83 | + } else { |
| 84 | + return it.toString(); |
| 85 | + } |
| 86 | + }).orElse("null"); |
| 87 | + } |
| 88 | + |
| 89 | + public static void main(String[] args) { |
| 90 | + runApp(args, Pac4jApp::new); |
| 91 | + } |
| 92 | +} |
0 commit comments