Search in sources :

Example 11 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testAlreadyAuthenticatedNotAuthorized.

@Test
public void testAlreadyAuthenticatedNotAuthorized() {
    final CommonProfile profile = new CommonProfile();
    final LinkedHashMap<String, CommonProfile> profiles = new LinkedHashMap<>();
    profiles.put(NAME, profile);
    context.getSessionStore().set(context, Pac4jConstants.USER_PROFILES, profiles);
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, new MockCredentials(), new CommonProfile());
    authorizers = NAME;
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    config.addAuthorizer(NAME, (context, prof) -> ID.equals(((CommonProfile) prof.get(0)).getId()));
    call();
    assertEquals(403, context.getResponseStatus());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 12 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testRedirectByIndirectClient.

@Test
public void testRedirectByIndirectClient() {
    final IndirectClient indirectClient = new MockIndirectClient(NAME, RedirectAction.redirect(PAC4J_URL), new MockCredentials(), new CommonProfile());
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    clients = NAME;
    call();
    assertEquals(302, context.getResponseStatus());
    assertEquals(PAC4J_URL, context.getResponseLocation());
}
Also used : MockCredentials(org.pac4j.core.credentials.MockCredentials) CommonProfile(org.pac4j.core.profile.CommonProfile) Test(org.junit.Test)

Example 13 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testDoubleDirectClientSupportingMultiProfile.

@Test
public void testDoubleDirectClientSupportingMultiProfile() {
    final CommonProfile profile = new CommonProfile();
    profile.setId(NAME);
    final CommonProfile profile2 = new CommonProfile();
    profile2.setId(VALUE);
    final DirectClient directClient = new MockDirectClient(NAME, new MockCredentials(), profile);
    final DirectClient directClient2 = new MockDirectClient(VALUE, new MockCredentials(), profile2);
    config.setClients(new Clients(CALLBACK_URL, directClient, directClient2));
    clients = NAME + "," + VALUE;
    multiProfile = true;
    call();
    assertEquals(-1, context.getResponseStatus());
    assertEquals(1, nbCall);
    final LinkedHashMap<String, CommonProfile> profiles = (LinkedHashMap<String, CommonProfile>) context.getRequestAttribute(Pac4jConstants.USER_PROFILES);
    assertEquals(2, profiles.size());
    assertTrue(profiles.containsValue(profile));
    assertTrue(profiles.containsValue(profile2));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 14 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultSecurityLogicTests method testDoubleDirectClient.

@Test
public void testDoubleDirectClient() {
    final CommonProfile profile = new CommonProfile();
    profile.setId(NAME);
    final CommonProfile profile2 = new CommonProfile();
    profile2.setId(VALUE);
    final DirectClient directClient = new MockDirectClient(NAME, new MockCredentials(), profile);
    final DirectClient directClient2 = new MockDirectClient(VALUE, new MockCredentials(), profile2);
    config.setClients(new Clients(CALLBACK_URL, directClient, directClient2));
    clients = NAME + "," + VALUE;
    call();
    assertEquals(-1, context.getResponseStatus());
    assertEquals(1, nbCall);
    final LinkedHashMap<String, CommonProfile> profiles = (LinkedHashMap<String, CommonProfile>) context.getRequestAttribute(Pac4jConstants.USER_PROFILES);
    assertEquals(1, profiles.size());
    assertTrue(profiles.containsValue(profile));
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockCredentials(org.pac4j.core.credentials.MockCredentials) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 15 with MockCredentials

use of org.pac4j.core.credentials.MockCredentials in project pac4j by pac4j.

the class DefaultCallbackLogicTests method testCallbackWithOriginallyRequestedUrl.

@Test
public void testCallbackWithOriginallyRequestedUrl() {
    HttpSession session = request.getSession();
    final String originalSessionId = session.getId();
    session.setAttribute(Pac4jConstants.REQUESTED_URL, PAC4J_URL);
    request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, NAME);
    final CommonProfile profile = new CommonProfile();
    final IndirectClient indirectClient = new MockIndirectClient(NAME, null, new MockCredentials(), profile);
    config.setClients(new Clients(CALLBACK_URL, indirectClient));
    config.getClients().init();
    call();
    session = request.getSession();
    final String newSessionId = session.getId();
    final LinkedHashMap<String, CommonProfile> profiles = (LinkedHashMap<String, CommonProfile>) session.getAttribute(Pac4jConstants.USER_PROFILES);
    assertTrue(profiles.containsValue(profile));
    assertEquals(1, profiles.size());
    assertNotEquals(newSessionId, originalSessionId);
    assertEquals(302, response.getStatus());
    assertEquals(PAC4J_URL, response.getRedirectedUrl());
}
Also used : CommonProfile(org.pac4j.core.profile.CommonProfile) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) MockCredentials(org.pac4j.core.credentials.MockCredentials) HttpSession(javax.servlet.http.HttpSession) MockIndirectClient(org.pac4j.core.client.MockIndirectClient) IndirectClient(org.pac4j.core.client.IndirectClient) Clients(org.pac4j.core.client.Clients) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 MockCredentials (org.pac4j.core.credentials.MockCredentials)16 CommonProfile (org.pac4j.core.profile.CommonProfile)16 LinkedHashMap (java.util.LinkedHashMap)9 Clients (org.pac4j.core.client.Clients)4 HttpSession (javax.servlet.http.HttpSession)3 IndirectClient (org.pac4j.core.client.IndirectClient)3 MockIndirectClient (org.pac4j.core.client.MockIndirectClient)3 MockDirectClient (org.pac4j.core.client.MockDirectClient)1