public class RatpackPac4j extends Object
import org.pac4j.oauth.client.GitHubClient;
import org.pac4j.oauth.profile.github.GitHubProfile;
import ratpack.guice.Guice;
import ratpack.handling.Context;
import ratpack.http.client.ReceivedResponse;
import ratpack.pac4j.RatpackPac4j;
import ratpack.session.SessionModule;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.of(s -> s
.registry(
Guice.registry(b -> b
.module(SessionModule.class) // session support is required
)
)
.handlers(c -> c
.handler(RatpackPac4j.callback(new GitHubClient("key", "secret"))) // callback handler must be upstream from auth handlers
.prefix("private", p -> p
.handler(RatpackPac4j.auth(GitHubClient.class)) // authenticate all requests flowing through here
.handler(ctx -> {
String displayName = ctx.maybeGet(GitHubProfile.class) // auth handler puts profile in context registry
.map(GitHubProfile::getDisplayName)
.orElse("noone");
ctx.render("Authenticated as " + displayName);
})
)
)
).test(httpClient -> {
ReceivedResponse response = httpClient.get("private/test");
String location = response.getHeaders().get("Location");
assertEquals(301, response.getStatusCode());
assertTrue(location != null && location.startsWith("https://github.com/login/oauth"));
});
}
}
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_CALLBACK_PATH
The default auth callback path, "auth-callback", used by
callback(Client[]) . |
Modifier and Type | Method and Description |
---|---|
static Handler |
auth(Class<? extends Client<?,?>> clientType)
An authentication “filter”, that initiates authentication if necessary.
|
static Handler |
callback(Client<?,?>... clients)
Calls
callback(String, Client[]) with DEFAULT_CALLBACK_PATH . |
static Handler |
callback(String path,
Client<?,?>... clients)
Returns the callback handler, which handles authentication requests.
|
public static final String DEFAULT_CALLBACK_PATH
callback(Client[])
.public static Handler callback(String path, Client<?,?>... clients)
This handler MUST be placed BEFORE the auth handler
in the handler pipeline.
It should be added to the handler chain via the Chain.handler(Handler)
method or similar.
That is, it should not be added with Chain.get(Handler)
or any method that filters based on request method.
It is common for this handler to be one of the first handlers in the pipeline.
This handler performs two different functions, based on whether the given path matches the PathBinding.getPastBinding()
component of the current path binding.
If the path matches, the handler will attempt authentication, which may involve redirecting to an external auth provider, which may then redirect back to this handler.
If authentication is successful, the UserProfile
of the authenticated user will be placed into the session.
The user will then be redirected back to the URL that initiated the authentication.
If the path does not match, the handler will push an instance of Clients
into the context registry and pass control downstream.
The Clients
instance will be retrieved downstream by any auth(Class)
handlers and used to discover the callback URL for authentication.
path
- the auth callback path (not typically seen by users)clients
- the supported authentication clientspublic static Handler callback(Client<?,?>... clients)
callback(String, Client[])
with DEFAULT_CALLBACK_PATH
.clients
- the supported auth clientspublic static Handler auth(Class<? extends Client<?,?>> clientType)
This handler requires a Clients
instance available in the context registry.
This can be provided by the callback(Client[])
handler.
As such, this handler should be downstream of the callback handler.
If there is a UserProfile
present in the context registry, this handler will simply delegate downstream.
If there is a UserProfile
present in the session, this handler will push the user profile into the context registry
before delegating downstream.
If there is no user profile present in the session, authentication will be initiated based on the given client type.
This involves a redirect to the callback(Client[])
handler.
clientType
- the client type to use to authenticate with if required