Search in sources :

Example 1 with StatusAction

use of org.pac4j.core.exception.http.StatusAction in project pac4j by pac4j.

the class JEEHttpActionAdapterTest method testError500.

@Test
public void testError500() throws IOException {
    JEEHttpActionAdapter.INSTANCE.adapt(new StatusAction(500), context);
    verify(response).sendError(500);
}
Also used : StatusAction(org.pac4j.core.exception.http.StatusAction) Test(org.junit.Test)

Example 2 with StatusAction

use of org.pac4j.core.exception.http.StatusAction in project pac4j by pac4j.

the class OidcRedirectTests method testAjaxRequestAfterStandardRequestShouldNotOverrideState.

@Test
public void testAjaxRequestAfterStandardRequestShouldNotOverrideState() throws MalformedURLException, URISyntaxException {
    var client = getClient();
    client.setCallbackUrl(CALLBACK_URL);
    client.setAjaxRequestResolver(new AjaxRequestResolver() {

        boolean first = true;

        @Override
        public boolean isAjax(final WebContext context, final SessionStore sessionStore) {
            /*
                 * Considers that the first request is not ajax, all the subsequent ones are
                 */
            if (first) {
                first = false;
                return false;
            } else {
                return true;
            }
        }

        @Override
        public HttpAction buildAjaxResponse(final WebContext context, final SessionStore sessionStore, final RedirectionActionBuilder redirectionActionBuilder) {
            return new StatusAction(401);
        }
    });
    var context = MockWebContext.create();
    final SessionStore sessionStore = new MockSessionStore();
    final var firstRequestAction = (FoundAction) client.getRedirectionAction(context, sessionStore).orElse(null);
    var state = TestsHelper.splitQuery(new URL(firstRequestAction.getLocation())).get("state");
    try {
        // noinspection ThrowableNotThrown
        client.getRedirectionAction(context, sessionStore);
        fail("Ajax request should throw exception");
    } catch (Exception e) {
        var stateAfterAjax = (State) sessionStore.get(context, client.getStateSessionAttributeName()).orElse(null);
        assertEquals("subsequent ajax request should not override the state in the session store", state, stateAfterAjax.toString());
    }
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockWebContext(org.pac4j.core.context.MockWebContext) RedirectionActionBuilder(org.pac4j.core.redirect.RedirectionActionBuilder) AjaxRequestResolver(org.pac4j.core.http.ajax.AjaxRequestResolver) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) SessionStore(org.pac4j.core.context.session.SessionStore) StatusAction(org.pac4j.core.exception.http.StatusAction) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) FoundAction(org.pac4j.core.exception.http.FoundAction) HttpAction(org.pac4j.core.exception.http.HttpAction) Test(org.junit.Test)

Example 3 with StatusAction

use of org.pac4j.core.exception.http.StatusAction in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testAuthorizerThrowsRequiresHttpAction.

@Test
public void testAuthorizerThrowsRequiresHttpAction() {
    final var profile = new CommonProfile();
    final var profiles = new LinkedHashMap<String, CommonProfile>();
    profiles.put(NAME, profile);
    sessionStore.set(context, Pac4jConstants.USER_PROFILES, profiles);
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, Optional.of(new MockCredentials()), new CommonProfile());
    authorizers = NAME;
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    config.addAuthorizer(NAME, (context, store, prof) -> {
        throw new StatusAction(400);
    });
    call();
    assertEquals(400, action.getCode());
    assertEquals(0, nbCall);
}
Also used : StatusAction(org.pac4j.core.exception.http.StatusAction) CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 StatusAction (org.pac4j.core.exception.http.StatusAction)3 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 LinkedHashMap (java.util.LinkedHashMap)1 MockWebContext (org.pac4j.core.context.MockWebContext)1 WebContext (org.pac4j.core.context.WebContext)1 MockSessionStore (org.pac4j.core.context.session.MockSessionStore)1 SessionStore (org.pac4j.core.context.session.SessionStore)1 MockCredentials (org.pac4j.core.credentials.MockCredentials)1 FoundAction (org.pac4j.core.exception.http.FoundAction)1 HttpAction (org.pac4j.core.exception.http.HttpAction)1 AjaxRequestResolver (org.pac4j.core.http.ajax.AjaxRequestResolver)1 CommonProfile (org.pac4j.core.profile.CommonProfile)1 RedirectionActionBuilder (org.pac4j.core.redirect.RedirectionActionBuilder)1