Search in sources :

Example 71 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class HeaderClientTests method testMissingProfileCreator.

@Test
public void testMissingProfileCreator() {
    final var client = new HeaderClient(NAME, new SimpleTestTokenAuthenticator());
    client.setProfileCreator(null);
    TestsHelper.expectException(() -> client.getUserProfile(new TokenCredentials(TOKEN), MockWebContext.create(), new MockSessionStore()), TechnicalException.class, "profileCreator cannot be null");
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) SimpleTestTokenAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 72 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class OAuth20ClientTests method testGetRedirectionGithub.

@Test
public void testGetRedirectionGithub() {
    final var action = (FoundAction) getClient().getRedirectionAction(MockWebContext.create(), new MockSessionStore()).get();
    final var url = action.getLocation();
    assertTrue(url != null && !url.isEmpty());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) FoundAction(org.pac4j.core.exception.http.FoundAction) Test(org.junit.Test)

Example 73 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class OAuth20ClientTests method testSetState.

@Test
public void testSetState() throws MalformedURLException {
    var client = new FacebookClient(KEY, SECRET);
    client.setCallbackUrl(CALLBACK_URL);
    client.getConfiguration().setStateGenerator(new StaticValueGenerator("oldstate"));
    final var mockWebContext = MockWebContext.create();
    var action = (FoundAction) client.getRedirectionAction(mockWebContext, new MockSessionStore()).get();
    var url = new URL(action.getLocation());
    final var stringMap = TestsHelper.splitQuery(url);
    assertEquals(stringMap.get("state"), "oldstate");
    action = (FoundAction) client.getRedirectionAction(mockWebContext, new MockSessionStore()).get();
    var url2 = new URL(action.getLocation());
    final var stringMap2 = TestsHelper.splitQuery(url2);
    assertEquals(stringMap2.get("state"), "oldstate");
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) FoundAction(org.pac4j.core.exception.http.FoundAction) URL(java.net.URL) StaticValueGenerator(org.pac4j.core.util.generator.StaticValueGenerator) Test(org.junit.Test)

Example 74 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore 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 75 with MockSessionStore

use of org.pac4j.core.context.session.MockSessionStore in project pac4j by pac4j.

the class SAML2AuthnRequestBuilderTests method testHttpSessionStoreGetterAndSetter.

@Test
public void testHttpSessionStoreGetterAndSetter() {
    final WebContext webContext = MockWebContext.create();
    final var messageStoreFactory = configuration.getSamlMessageStoreFactory();
    final var store = messageStoreFactory.getMessageStore(webContext, new MockSessionStore());
    final var builder = new SAML2AuthnRequestBuilder();
    final var context = buildContext();
    final var authnRequest = builder.build(context);
    authnRequest.setAssertionConsumerServiceURL("https://pac4j.org");
    store.set(authnRequest.getID(), authnRequest);
    assertNotNull(store.get(authnRequest.getID()));
    assertEquals("https://pac4j.org", authnRequest.getAssertionConsumerServiceURL());
}
Also used : WebContext(org.pac4j.core.context.WebContext) MockWebContext(org.pac4j.core.context.MockWebContext) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) Test(org.junit.Test)

Aggregations

MockSessionStore (org.pac4j.core.context.session.MockSessionStore)164 Test (org.junit.Test)151 FoundAction (org.pac4j.core.exception.http.FoundAction)29 SessionStore (org.pac4j.core.context.session.SessionStore)22 CommonProfile (org.pac4j.core.profile.CommonProfile)20 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)19 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)17 MockWebContext (org.pac4j.core.context.MockWebContext)15 WebContext (org.pac4j.core.context.WebContext)15 CasConfiguration (org.pac4j.cas.config.CasConfiguration)14 HttpAction (org.pac4j.core.exception.http.HttpAction)12 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)11 AnonymousProfile (org.pac4j.core.profile.AnonymousProfile)9 SimpleTestUsernamePasswordAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator)7 CasProfile (org.pac4j.cas.profile.CasProfile)6 HashMap (java.util.HashMap)5 Authorizer (org.pac4j.core.authorization.authorizer.Authorizer)5 RequireAnyRoleAuthorizer (org.pac4j.core.authorization.authorizer.RequireAnyRoleAuthorizer)5 OkAction (org.pac4j.core.exception.http.OkAction)5 URL (java.net.URL)4