I've been working on integrating Pac4j into my Play 2.5 Java app.
I've got a basic login form and a custom authenticator, which works great. I can login and it will redirect me to the page I need. Trouble comes when I enable the security rules.
pac4j.security {
rules = [
{"/loginForm" = {
authorizers = "_anonymous_"
}
}
{"/callback.*" = {
authorizers = "_anonymous_"
}
}
{"/assets/.*" = {
authorizers = "_anonymous_"
}
}
{"/introPage" = {
clients = "FormClient"
authorizers = "_authenticated_"
}
}
{".*" = {
authorizers = "_authenticated_"
clients = "FormClient"
}
}
]
}
When I enable, for instance, a rule on the /introPage it ALWAYS bounces me back to the login form, as if it's not authenticated. Any ideas where to look or what to share here for further troubleshooting?
Here's the relevant parts of my build.sbt:
"org.pac4j" % "play-pac4j" % "3.0.1",
"org.pac4j" % "pac4j-http" % "2.0.0",
"be.objectify" %% "deadbolt-java" % "2.4.0",
Here's my SecurityModule, whittled down to the bare minimum:
public class SecurityModule extends AbstractModule {
public final static String JWT_SALT = "12345678901234567890123456789012";
private final Configuration configuration;
private static class MyPac4jRoleHandler implements Pac4jRoleHandler { }
public SecurityModule(final Environment environment, final Configuration configuration) {
this.configuration = configuration;
}
@Override
protected void configure() {
System.out.println("configure()");
bind(HandlerCache.class).to(Pac4jHandlerCache.class);
bind(Pac4jRoleHandler.class).to(MyPac4jRoleHandler.class);
final PlayCacheSessionStore playCacheSessionStore = new PlayCacheSessionStore(getProvider(CacheApi.class));
bind(PlaySessionStore.class).toInstance(playCacheSessionStore);
// final String fbId = configuration.getString("fbId");
// final String fbSecret = configuration.getString("fbSecret");
final String baseUrl = "
http://localhost:9000";
System.out.println("baseurl=" + baseUrl);
// final FacebookClient facebookClient = new FacebookClient(fbId, fbSecret);
// final TwitterClient twitterClient = new TwitterClient("HVSQGAw2XmiwcKOTvZFbQ", "FSiO9G9VRR4KCuksky0kgGuo8gAVndYymr4Nl7qc8AA");
final FormClient formClient = new FormClient(baseUrl + "/loginForm", new DiagnosticGUIAuthenticator());
// final IndirectBasicAuthClient indirectBasicAuthClient = new IndirectBasicAuthClient(new DiagnosticGUIAuthenticator());
// final IndirectBasicAuthClient indirectBasicAuthClient = new IndirectBasicAuthClient(new SimpleTestUsernamePasswordAuthenticator());
// final CasConfiguration casConfiguration = new CasConfiguration("
http://localhost:8888/cas/login");
// final CasProxyReceptor casProxyReceptor = new CasProxyReceptor();
// casConfiguration.setProxyReceptor(casProxyReceptor);
// final CasClient casClient = new CasClient(casConfiguration);
// final SAML2ClientConfiguration cfg = new SAML2ClientConfiguration("resource:samlKeystore.jks", "pac4j-demo-passwd", "pac4j-demo-passwd", "resource:openidp-feide.xml");
// cfg.setMaximumAuthenticationLifetime(3600);
// cfg.setServiceProviderEntityId("urn:mace:saml:
pac4j.org");
// cfg.setServiceProviderMetadataPath(new File("target", "sp-metadata.xml").getAbsolutePath());
// final SAML2Client saml2Client = new SAML2Client(cfg);
// final OidcConfiguration oidcConfiguration = new OidcConfiguration();
// oidcConfiguration.setClientId("
343992089165-i1es0qvej18asl33mvlbeq750i3ko32k.apps.googleusercontent.com");
// oidcConfiguration.setSecret("unXK_RSCbCXLTic2JACTiAo9");
// oidcConfiguration.setDiscoveryURI("
https://accounts.google.com/.well-known/openid-configuration");
// oidcConfiguration.addCustomParam("prompt", "consent");
// final OidcClient oidcClient = new OidcClient(oidcConfiguration);
// oidcClient.addAuthorizationGenerator((ctx, profile) -> { profile.addRole("ROLE_ADMIN"); return profile; });
// final ParameterClient parameterClient = new ParameterClient("token",
// new JwtAuthenticator(new SecretSignatureConfiguration(JWT_SALT)));
// parameterClient.setSupportGetRequest(true);
// parameterClient.setSupportPostRequest(false);
// final DirectBasicAuthClient directBasicAuthClient = new DirectBasicAuthClient(new SimpleTestUsernamePasswordAuthenticator());
final Clients clients = new Clients(baseUrl + "/callback", formClient, new AnonymousClient());
final Config config = new Config(clients);
// config.addAuthorizer("admin", new RequireAnyRoleAuthorizer<>("ROLE_ADMIN"));
config.addAuthorizer("custom", new CustomAuthorizer());
// config.setHttpActionAdapter( new DefaultHttpActionAdapter());
config.setHttpActionAdapter(new DiagnosticGuiHttpActionAdapter());
bind(Config.class).toInstance(config);
final CallbackController callbackController = new CallbackController();
callbackController.setDefaultUrl("/");
bind(CallbackController.class).toInstance(callbackController);
final LogoutController logoutController = new LogoutController();
logoutController.setDefaultUrl("/?defaulturlafterlogout");
bind(LogoutController.class).toInstance(logoutController);
}
// @Override
// protected void configure() {
//// bind(HandlerCache.class).to(Pac4jHandlerCache.class);
////
////// bind(Pac4jRoleHandler.class).to(MyPac4jRoleHandler.class);
//// final PlayCacheSessionStore playCacheSessionStore = new PlayCacheSessionStore(getProvider(SyncCacheApi.class));
//// //bind(PlaySessionStore.class).toInstance(playCacheSessionStore);
//// bind(PlaySessionStore.class).to(PlayCacheSessionStore.class);
////
//// // callback
//// final CallbackController callbackController = new CallbackController();
//// callbackController.setDefaultUrl("/");
//// callbackController.setMultiProfile(true);
//// bind(CallbackController.class).toInstance(callbackController);
////
//// // logout
//// final LogoutController logoutController = new LogoutController();
//// logoutController.setDefaultUrl("/?defaulturlafterlogout");
//// logoutController.setDestroySession(true);
//// bind(LogoutController.class).toInstance(logoutController);
//// bind(HandlerCache.class).to(Pac4jHandlerCache.class);
////
////// bind(Pac4jRoleHandler.class).to(MyPac4jRoleHandler.class);
//// final PlayCacheSessionStore playCacheSessionStore = new PlayCacheSessionStore(getProvider(SyncCacheApi.class));
//// //bind(PlaySessionStore.class).toInstance(playCacheSessionStore);
//// bind(PlaySessionStore.class).to(PlayCacheSessionStore.class);
////
//// // callback
//// final CallbackController callbackController = new CallbackController();
//// callbackController.setDefaultUrl("/");
//// callbackController.setMultiProfile(true);
//// bind(CallbackController.class).toInstance(callbackController);
////
//// // logout
//// final LogoutController logoutController = new LogoutController();
//// logoutController.setDefaultUrl("/?defaulturlafterlogout");
//// //logoutController.setDestroySession(true);
//// bind(LogoutController.class).toInstance(logoutController);
// }
//
@Provides
protected FormClient provideFormClient() {
System.out.println("providing form client");
return new FormClient("
http://127.0.0.1" + "/loginForm", new DiagnosticGUIAuthenticator());//new SimpleTestUsernamePasswordAuthenticator());
}
////
//// @Provides Config provideConfig(FormClient formClient) {
//// final Clients clients = new Clients("", formClient);
//// final Config config = new Config(clients);
//// return config;
////
//// }
}