Search in sources :

Example 1 with DefaultAjaxRequestResolver

use of org.pac4j.core.http.ajax.DefaultAjaxRequestResolver in project pac4j by pac4j.

the class ClientsTests method testValuesSet.

@Test
public void testValuesSet() {
    MockIndirectClient facebookClient = newFacebookClient();
    final Clients clients = new Clients(facebookClient);
    final AjaxRequestResolver ajaxRequestResolver = new DefaultAjaxRequestResolver();
    final UrlResolver urlResolver = new DefaultUrlResolver();
    final CallbackUrlResolver callbackUrlResolver = new QueryParameterCallbackUrlResolver();
    final AuthorizationGenerator authorizationGenerator = (context, profile) -> profile;
    clients.setCallbackUrl(CALLBACK_URL);
    clients.setAjaxRequestResolver(ajaxRequestResolver);
    clients.setUrlResolver(urlResolver);
    clients.setCallbackUrlResolver(callbackUrlResolver);
    clients.addAuthorizationGenerator(authorizationGenerator);
    clients.init();
    assertEquals(CALLBACK_URL, facebookClient.getCallbackUrl());
    assertEquals(urlResolver, facebookClient.getUrlResolver());
    assertEquals(callbackUrlResolver, facebookClient.getCallbackUrlResolver());
    assertEquals(ajaxRequestResolver, facebookClient.getAjaxRequestResolver());
    assertEquals(authorizationGenerator, facebookClient.getAuthorizationGenerators().get(0));
}
Also used : TestsConstants(org.pac4j.core.util.TestsConstants) CommonProfile(org.pac4j.core.profile.CommonProfile) TestsHelper(org.pac4j.core.util.TestsHelper) Test(org.junit.Test) AjaxRequestResolver(org.pac4j.core.http.ajax.AjaxRequestResolver) DefaultAjaxRequestResolver(org.pac4j.core.http.ajax.DefaultAjaxRequestResolver) ArrayList(java.util.ArrayList) RedirectAction(org.pac4j.core.redirect.RedirectAction) List(java.util.List) UrlResolver(org.pac4j.core.http.url.UrlResolver) DefaultUrlResolver(org.pac4j.core.http.url.DefaultUrlResolver) AuthorizationGenerator(org.pac4j.core.authorization.generator.AuthorizationGenerator) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver) Credentials(org.pac4j.core.credentials.Credentials) Assert(org.junit.Assert) CallbackUrlResolver(org.pac4j.core.http.callback.CallbackUrlResolver) UrlResolver(org.pac4j.core.http.url.UrlResolver) DefaultUrlResolver(org.pac4j.core.http.url.DefaultUrlResolver) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver) CallbackUrlResolver(org.pac4j.core.http.callback.CallbackUrlResolver) DefaultAjaxRequestResolver(org.pac4j.core.http.ajax.DefaultAjaxRequestResolver) AjaxRequestResolver(org.pac4j.core.http.ajax.AjaxRequestResolver) DefaultAjaxRequestResolver(org.pac4j.core.http.ajax.DefaultAjaxRequestResolver) AuthorizationGenerator(org.pac4j.core.authorization.generator.AuthorizationGenerator) DefaultUrlResolver(org.pac4j.core.http.url.DefaultUrlResolver) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver) CallbackUrlResolver(org.pac4j.core.http.callback.CallbackUrlResolver) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver) Test(org.junit.Test)

Example 2 with DefaultAjaxRequestResolver

use of org.pac4j.core.http.ajax.DefaultAjaxRequestResolver in project pac4j by pac4j.

the class IndirectClient method internalInit.

@Override
protected final void internalInit() {
    // check configuration
    CommonHelper.assertNotBlank("callbackUrl", this.callbackUrl, "set it up either on this IndirectClient or on the global Config");
    if (this.urlResolver == null) {
        this.urlResolver = new DefaultUrlResolver();
    }
    if (this.callbackUrlResolver == null) {
        this.callbackUrlResolver = new QueryParameterCallbackUrlResolver();
    }
    if (this.ajaxRequestResolver == null) {
        ajaxRequestResolver = new DefaultAjaxRequestResolver();
    }
    clientInit();
    // ensures components have been properly initialized
    CommonHelper.assertNotNull("redirectActionBuilder", this.redirectActionBuilder);
    CommonHelper.assertNotNull("credentialsExtractor", getCredentialsExtractor());
    CommonHelper.assertNotNull("authenticator", getAuthenticator());
    CommonHelper.assertNotNull("profileCreator", getProfileCreator());
    CommonHelper.assertNotNull("logoutActionBuilder", this.logoutActionBuilder);
}
Also used : DefaultAjaxRequestResolver(org.pac4j.core.http.ajax.DefaultAjaxRequestResolver) DefaultUrlResolver(org.pac4j.core.http.url.DefaultUrlResolver) QueryParameterCallbackUrlResolver(org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver)

Example 3 with DefaultAjaxRequestResolver

use of org.pac4j.core.http.ajax.DefaultAjaxRequestResolver in project ddf by codice.

the class OidcLogoutActionProvider method getAction.

/**
 * *
 *
 * @param <T> is a Map<String, Subject>
 * @param subjectMap containing the corresponding subject
 * @return OidcLogoutActionProvider containing the logout url
 */
@Override
public <T> Action getAction(T subjectMap) {
    if (!canHandle(subjectMap)) {
        return null;
    }
    String logoutUrlString = "";
    URL logoutUrl = null;
    try {
        HttpServletRequest request = (HttpServletRequest) ((Map) subjectMap).get("http_request");
        HttpServletResponse response = (HttpServletResponse) ((Map) subjectMap).get("http_response");
        JEESessionStore sessionStore = new JEESessionStore();
        JEEContext jeeContext = new JEEContext(request, response, sessionStore);
        HttpSession session = request.getSession(false);
        PrincipalHolder principalHolder = null;
        if (session != null) {
            principalHolder = (PrincipalHolder) session.getAttribute(SecurityConstants.SECURITY_TOKEN_KEY);
        }
        OidcProfile oidcProfile = null;
        if (principalHolder != null && principalHolder.getPrincipals() != null) {
            Collection<SecurityAssertion> securityAssertions = principalHolder.getPrincipals().byType(SecurityAssertion.class);
            for (SecurityAssertion securityAssertion : securityAssertions) {
                if (SecurityAssertionJwt.JWT_TOKEN_TYPE.equals(securityAssertion.getTokenType())) {
                    oidcProfile = (OidcProfile) securityAssertion.getToken();
                    break;
                }
            }
        }
        if (oidcProfile == null) {
            throw new IllegalStateException("Unable to determine OIDC profile for logout");
        }
        OidcLogoutActionBuilder logoutActionBuilder = handlerConfiguration.getOidcLogoutActionBuilder();
        logoutActionBuilder.setAjaxRequestResolver(new DefaultAjaxRequestResolver() {

            @Override
            public boolean isAjax(final WebContext context) {
                return false;
            }
        });
        URIBuilder urlBuilder = new URIBuilder(SystemBaseUrl.EXTERNAL.constructUrl("/oidc/logout", true));
        String prevUrl = getPreviousUrl(request);
        if (prevUrl != null) {
            urlBuilder.addParameter(PREV_URL, prevUrl);
        }
        RedirectionAction logoutAction = logoutActionBuilder.getLogoutAction(jeeContext, oidcProfile, urlBuilder.build().toString()).orElse(null);
        if (logoutAction instanceof WithLocationAction) {
            logoutUrlString = ((WithLocationAction) logoutAction).getLocation();
        }
        logoutUrl = new URL(logoutUrlString);
    } catch (MalformedURLException | URISyntaxException e) {
        LOGGER.info("Unable to resolve logout URL: {}", logoutUrlString);
    } catch (ClassCastException e) {
        LOGGER.debug("Unable to cast parameter to Map<String, Object>, {}", subjectMap, e);
    }
    return new ActionImpl(ID, TITLE, DESCRIPTION, logoutUrl);
}
Also used : RedirectionAction(org.pac4j.core.exception.http.RedirectionAction) MalformedURLException(java.net.MalformedURLException) WebContext(org.pac4j.core.context.WebContext) HttpSession(javax.servlet.http.HttpSession) JEEContext(org.pac4j.core.context.JEEContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) JEESessionStore(org.pac4j.core.context.session.JEESessionStore) WithLocationAction(org.pac4j.core.exception.http.WithLocationAction) URISyntaxException(java.net.URISyntaxException) SecurityAssertion(ddf.security.assertion.SecurityAssertion) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpServletRequest(javax.servlet.http.HttpServletRequest) DefaultAjaxRequestResolver(org.pac4j.core.http.ajax.DefaultAjaxRequestResolver) OidcLogoutActionBuilder(org.pac4j.oidc.logout.OidcLogoutActionBuilder) ActionImpl(ddf.action.impl.ActionImpl) OidcProfile(org.pac4j.oidc.profile.OidcProfile) PrincipalHolder(ddf.security.common.PrincipalHolder)

Aggregations

DefaultAjaxRequestResolver (org.pac4j.core.http.ajax.DefaultAjaxRequestResolver)3 QueryParameterCallbackUrlResolver (org.pac4j.core.http.callback.QueryParameterCallbackUrlResolver)2 DefaultUrlResolver (org.pac4j.core.http.url.DefaultUrlResolver)2 ActionImpl (ddf.action.impl.ActionImpl)1 SecurityAssertion (ddf.security.assertion.SecurityAssertion)1 PrincipalHolder (ddf.security.common.PrincipalHolder)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1 AuthorizationGenerator (org.pac4j.core.authorization.generator.AuthorizationGenerator)1 JEEContext (org.pac4j.core.context.JEEContext)1 WebContext (org.pac4j.core.context.WebContext)1