C - The Credentials typeU - The UserProfile typepublic final class InjectedPac4jModule<C extends Credentials,U extends UserProfile>
extends ratpack.pac4j.internal.AbstractPac4jModule<C,U>
If you don't need/want to perform dependency injection on either the Client or Authorizer, use Pac4jModule instead.
To use this module, you need to register it as well as a custom module that binds a Client and an Authorizer.
import com.google.inject.*;
import org.pac4j.core.client.Client;
import org.pac4j.openid.client.GoogleOpenIdClient;
import org.pac4j.openid.credentials.OpenIdCredentials;
import org.pac4j.openid.profile.google.GoogleOpenIdProfile;
import ratpack.func.Action;
import ratpack.guice.*;
import ratpack.guice.Guice;
import ratpack.handling.*;
import ratpack.launch.*;
import ratpack.pac4j.*;
import ratpack.session.SessionModule;
import ratpack.session.store.MapSessionsModule;
class AuthenticateAllAuthorizer extends AbstractAuthorizer<GoogleOpenIdProfile> {
public boolean isAuthenticationRequired(Context context) {
return true;
}
}
class MyHandler implements Handler {
public void handle(final Context context) {
context.render("Authenticated as " + context.getRequest().get(GoogleOpenIdProfile.class).getDisplayName());
}
}
class ModuleBootstrap implements Action<ModuleRegistry> {
public void execute(ModuleRegistry modules) {
modules.register(new SessionModule());
modules.register(new MapSessionsModule(10, 5));
modules.register(new InjectedPac4jModule<>(OpenIdCredentials.class, GoogleOpenIdProfile.class));
modules.register(new AbstractModule() {
protected void configure() {
bind(new TypeLiteral<Client<OpenIdCredentials, GoogleOpenIdProfile>>() {}).to(GoogleOpenIdClient.class);
bind(new TypeLiteral<Authorizer<GoogleOpenIdProfile>>() {}).to(AuthenticateAllAuthorizer.class);
}
});
}
}
LaunchConfig launchConfig = LaunchConfigBuilder.baseDir(new File("appRoot"))
.build(new HandlerFactory() {
public Handler create(LaunchConfig launchConfig) throws Exception {
return Guice.handler(launchConfig, new ModuleBootstrap(), new Action<Chain>() {
public void execute(Chain chain) {
chain.handler(new MyHandler());
}
});
}
});
Example usage: (Groovy DSL)
import com.google.inject.*
import org.pac4j.core.client.Client
import org.pac4j.openid.client.GoogleOpenIdClient
import org.pac4j.openid.credentials.OpenIdCredentials
import org.pac4j.openid.profile.google.GoogleOpenIdProfile
import ratpack.handling.Context
import ratpack.pac4j.*
import ratpack.session.SessionModule
import ratpack.session.store.MapSessionsModule
import static ratpack.groovy.Groovy.ratpack
class AuthenticateAllAuthorizer extends AbstractAuthorizer<GoogleOpenIdProfile> {
boolean isAuthenticationRequired(Context context) {
true
}
}
ratpack {
modules {
register new SessionModule()
register new MapSessionsModule(10, 5)
register new InjectedPac4jModule<>(OpenIdCredentials, GoogleOpenIdProfile)
register new AbstractModule() {
protected void configure() {
bind(new TypeLiteral<Client<OpenIdCredentials, GoogleOpenIdProfile>>() {}).to(GoogleOpenIdClient)
bind(new TypeLiteral<Authorizer<GoogleOpenIdProfile>>() {}).to(AuthenticateAllAuthorizer)
}
}
}
handlers {
get {
def userProfile = request.get(GoogleOpenIdProfile)
render "Authenticated as ${userProfile.displayName}"
}
}
}
| Constructor and Description |
|---|
InjectedPac4jModule(Class<C> credentialsType,
Class<U> userProfileType)
Constructs a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
protected Authorizer<U> |
getAuthorizer(Injector injector) |
protected Client<C,U> |
getClient(Injector injector) |
callbackPath, configure, decorateaddError, addError, addError, bind, bind, bind, bindConstant, binder, bindInterceptor, bindListener, bindScope, configure, convertToTypes, currentStage, getMembersInjector, getMembersInjector, getProvider, getProvider, install, requestInjection, requestStaticInjection, requireBinding, requireBindingprotected Client<C,U> getClient(Injector injector)
getClient in class ratpack.pac4j.internal.AbstractPac4jModule<C extends Credentials,U extends UserProfile>protected Authorizer<U> getAuthorizer(Injector injector)
getAuthorizer in class ratpack.pac4j.internal.AbstractPac4jModule<C extends Credentials,U extends UserProfile>