Skip to content

Instantly share code, notes, and snippets.

@skurfuerst
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skurfuerst/920da93be0db058a1034 to your computer and use it in GitHub Desktop.
Save skurfuerst/920da93be0db058a1034 to your computer and use it in GitHub Desktop.
Make Pac4j in Ratpack Name-Based-Hosting aware
import com.google.inject.AbstractModule
import com.google.inject.Injector
import org.pac4j.core.client.Client
import ratpack.guice.HandlerDecoratingModule
import ratpack.handling.Handler
import ratpack.handling.Handlers
/**
* Pac4j does not properly support "Name-based Virtual Hosts". After some debugging, I could find the
* root-cause of the issue:
*
* - The {@link ratpack.pac4j.internal.AbstractPac4jModule} configures all the handlers in the correct ordering.
* - The first handler which is configured is the {@link ratpack.pac4j.internal.Pac4jClientsHandler}, which only
* registers a {@link org.pac4j.core.client.Clients} object in the Request context.
* - This "Clients" object is a "proxy" to the actually configured Client objects (which are usually just one);
* configuring them correctly by handing back the right callback URI.
* - This callback URI is *fully-qualified*, i.e. including the hostname.
* - The callback URI is only updated if it is NOT YET SET in the client; see {@link org.pac4j.core.client.Clients internalInit()}
*
* We basically modify the *last* part of the above behavior chain, by *always* resetting the callback URL to NULL
* in this module and the corresponding handler. Then, it is, on every request, set anew, taking the current host
* into account.
*
* USAGE:
* - include this module *BEFORE* the Pac4jModule and pass in the same client *instance* as in the Pac4j Module.
*/
class ResetClientCallbackUriModule extends AbstractModule implements HandlerDecoratingModule {
private Client client
ResetClientCallbackUriModule(Client client) {
this.client = client
}
@Override
protected void configure() {
}
@Override
Handler decorate(Injector injector, Handler handler) {
return Handlers.chain(new ResetClientCallbackUriHandler(client), handler)
}
}
import org.pac4j.core.client.BaseClient
import ratpack.handling.Context
import ratpack.handling.Handler
import org.pac4j.core.client.Client
/**
* Actually reset the callback URI and then delegate to the next handler.
*
* See {@link ResetClientCallbackUriModule} for usage instructions.
*/
class ResetClientCallbackUriHandler implements Handler {
private BaseClient client
ResetClientCallbackUriHandler(Client client) {
this.client = (BaseClient) client
}
@Override
void handle(Context context) throws Exception {
client.setCallbackUrl(null)
context.next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment