Search in sources :

Example 1 with RequiresHttpAction

use of org.pac4j.core.exception.RequiresHttpAction in project ratpack by ratpack.

the class RatpackPac4j method initiateAuthentication.

private static void initiateAuthentication(Context ctx, Class<? extends Client<?, ?>> clientType) {
    Request request = ctx.getRequest();
    Clients clients = ctx.get(Clients.class);
    Client<?, ?> client = clients.findClient(clientType);
    RatpackWebContext.from(ctx, false).then(webContext -> {
        webContext.getSession().set(Pac4jSessionKeys.REQUESTED_URL, request.getUri());
        try {
            client.redirect(webContext, true);
        } catch (Exception e) {
            if (e instanceof RequiresHttpAction) {
                webContext.sendResponse((RequiresHttpAction) e);
                return;
            } else {
                ctx.error(new TechnicalException("Failed to redirect", e));
            }
        }
        webContext.sendResponse();
    });
}
Also used : RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) TechnicalException(org.pac4j.core.exception.TechnicalException) Request(ratpack.http.Request) Clients(org.pac4j.core.client.Clients) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 2 with RequiresHttpAction

use of org.pac4j.core.exception.RequiresHttpAction in project ratpack by ratpack.

the class Pac4jAuthenticator method handle.

@Override
public void handle(Context ctx) throws Exception {
    PathBinding pathBinding = ctx.getPathBinding();
    String pastBinding = pathBinding.getPastBinding();
    if (pastBinding.equals(path)) {
        RatpackWebContext.from(ctx, true).flatMap(webContext -> {
            SessionData sessionData = webContext.getSession();
            return createClients(ctx, pathBinding).map(clients -> clients.findClient(webContext)).map(Types::<Client<Credentials, UserProfile>>cast).flatMap(client -> getProfile(webContext, client)).map(profile -> {
                if (profile != null) {
                    sessionData.set(Pac4jSessionKeys.USER_PROFILE, profile);
                }
                Optional<String> originalUrl = sessionData.get(Pac4jSessionKeys.REQUESTED_URL);
                sessionData.remove(Pac4jSessionKeys.REQUESTED_URL);
                return originalUrl;
            }).onError(t -> {
                if (t instanceof RequiresHttpAction) {
                    webContext.sendResponse((RequiresHttpAction) t);
                } else {
                    ctx.error(new TechnicalException("Failed to get user profile", t));
                }
            });
        }).then(originalUrlOption -> {
            ctx.redirect(originalUrlOption.orElse("/"));
        });
    } else {
        createClients(ctx, pathBinding).then(clients -> {
            Registry registry = Registry.singleLazy(Clients.class, () -> uncheck(() -> clients));
            ctx.next(registry);
        });
    }
}
Also used : Types(ratpack.util.Types) Context(ratpack.handling.Context) RatpackPac4j(ratpack.pac4j.RatpackPac4j) Exceptions.uncheck(ratpack.util.Exceptions.uncheck) Promise(ratpack.exec.Promise) PublicAddress(ratpack.server.PublicAddress) Blocking(ratpack.exec.Blocking) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) WebContext(org.pac4j.core.context.WebContext) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Clients(org.pac4j.core.client.Clients) Client(org.pac4j.core.client.Client) Handler(ratpack.handling.Handler) Registry(ratpack.registry.Registry) Optional(java.util.Optional) PathBinding(ratpack.path.PathBinding) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Credentials(org.pac4j.core.credentials.Credentials) Types(ratpack.util.Types) RequiresHttpAction(org.pac4j.core.exception.RequiresHttpAction) TechnicalException(org.pac4j.core.exception.TechnicalException) UserProfile(org.pac4j.core.profile.UserProfile) SessionData(ratpack.session.SessionData) Registry(ratpack.registry.Registry) PathBinding(ratpack.path.PathBinding) Credentials(org.pac4j.core.credentials.Credentials)

Aggregations

Clients (org.pac4j.core.client.Clients)2 RequiresHttpAction (org.pac4j.core.exception.RequiresHttpAction)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Optional (java.util.Optional)1 Client (org.pac4j.core.client.Client)1 WebContext (org.pac4j.core.context.WebContext)1 Credentials (org.pac4j.core.credentials.Credentials)1 UserProfile (org.pac4j.core.profile.UserProfile)1 Blocking (ratpack.exec.Blocking)1 Promise (ratpack.exec.Promise)1 Context (ratpack.handling.Context)1 Handler (ratpack.handling.Handler)1 Request (ratpack.http.Request)1 RatpackPac4j (ratpack.pac4j.RatpackPac4j)1 PathBinding (ratpack.path.PathBinding)1 Registry (ratpack.registry.Registry)1 PublicAddress (ratpack.server.PublicAddress)1 SessionData (ratpack.session.SessionData)1