Search in sources :

Example 66 with MockSessionStore

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

the class DirectFormClientTests method testGetUserProfile.

@Test
public void testGetUserProfile() {
    final var formClient = getFormClient();
    formClient.setProfileCreator((credentials, context, session) -> {
        var username = ((UsernamePasswordCredentials) credentials).getUsername();
        final var profile = new CommonProfile();
        profile.setId(username);
        profile.addAttribute(Pac4jConstants.USERNAME, username);
        return Optional.of(profile);
    });
    final var context = MockWebContext.create();
    final var profile = (CommonProfile) formClient.getUserProfile(new UsernamePasswordCredentials(USERNAME, USERNAME), context, new MockSessionStore()).get();
    assertEquals(USERNAME, profile.getId());
    assertEquals(CommonProfile.class.getName() + Pac4jConstants.TYPED_ID_SEPARATOR + USERNAME, profile.getTypedId());
    assertTrue(ProfileHelper.isTypedIdOf(profile.getTypedId(), CommonProfile.class));
    assertEquals(USERNAME, profile.getUsername());
    assertEquals(1, profile.getAttributes().size());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CommonProfile(org.pac4j.core.profile.CommonProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 67 with MockSessionStore

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

the class DirectFormClientTests method testGetBadCredentials.

@Test
public void testGetBadCredentials() {
    final var formClient = getFormClient();
    final var context = MockWebContext.create();
    assertFalse(formClient.getCredentials(context.addRequestParameter(formClient.getUsernameParameter(), USERNAME).addRequestParameter(formClient.getPasswordParameter(), PASSWORD), new MockSessionStore()).isPresent());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) Test(org.junit.Test)

Example 68 with MockSessionStore

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

the class DirectFormClientTests method testGetCredentialsMissingPassword.

@Test
public void testGetCredentialsMissingPassword() {
    final var formClient = getFormClient();
    final var context = MockWebContext.create();
    assertFalse(formClient.getCredentials(context.addRequestParameter(formClient.getPasswordParameter(), PASSWORD), new MockSessionStore()).isPresent());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) Test(org.junit.Test)

Example 69 with MockSessionStore

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

the class DirectBasicAuthClientTests method testAuthenticationLowercase.

@Test
public void testAuthenticationLowercase() {
    final var client = new DirectBasicAuthClient(new SimpleTestUsernamePasswordAuthenticator());
    final var context = MockWebContext.create();
    final var header = USERNAME + ":" + USERNAME;
    context.addRequestHeader(HttpConstants.AUTHORIZATION_HEADER.toLowerCase(), "Basic " + Base64.getEncoder().encodeToString(header.getBytes(StandardCharsets.UTF_8)));
    final var credentials = (UsernamePasswordCredentials) client.getCredentials(context, new MockSessionStore()).get();
    final var profile = (CommonProfile) client.getUserProfile(credentials, context, new MockSessionStore()).get();
    assertEquals(USERNAME, profile.getId());
}
Also used : MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CommonProfile(org.pac4j.core.profile.CommonProfile) SimpleTestUsernamePasswordAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestUsernamePasswordAuthenticator) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) Test(org.junit.Test)

Example 70 with MockSessionStore

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

the class DirectDigestAuthClientTests method testMissingProfileCreator.

@Test
public void testMissingProfileCreator() {
    final var digestAuthClient = new DirectDigestAuthClient(new SimpleTestTokenAuthenticator(), null);
    TestsHelper.expectException(() -> digestAuthClient.getUserProfile(new DigestCredentials(TOKEN, HTTP_METHOD.POST.name(), null, null, null, null, null, null, null), MockWebContext.create(), new MockSessionStore()), TechnicalException.class, "profileCreator cannot be null");
}
Also used : DigestCredentials(org.pac4j.http.credentials.DigestCredentials) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) SimpleTestTokenAuthenticator(org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator) 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