Search in sources :

Example 76 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class CasProxyReceptorTests method testMissingPgtiou.

@Test
public void testMissingPgtiou() {
    final CasProxyReceptor client = new CasProxyReceptor();
    client.setCallbackUrl(CALLBACK_URL);
    final MockWebContext context = MockWebContext.create();
    TestsHelper.expectException(() -> client.getCredentials(context.addRequestParameter(CasProxyReceptor.PARAM_PROXY_GRANTING_TICKET_IOU, VALUE)), HttpAction.class, "Perfoming a 200 HTTP action");
    assertEquals(200, context.getResponseStatus());
    assertEquals("", context.getResponseContent());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Example 77 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class CasProxyReceptorTests method testOk.

@Test
public void testOk() {
    final CasProxyReceptor client = new CasProxyReceptor();
    client.setCallbackUrl(CALLBACK_URL);
    final MockWebContext context = MockWebContext.create().addRequestParameter(CasProxyReceptor.PARAM_PROXY_GRANTING_TICKET, VALUE).addRequestParameter(CasProxyReceptor.PARAM_PROXY_GRANTING_TICKET_IOU, VALUE);
    TestsHelper.expectException(() -> client.getCredentials(context), HttpAction.class, "Perfoming a 200 HTTP action");
    assertEquals(200, context.getResponseStatus());
    assertTrue(context.getResponseContent().length() > 0);
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

Example 78 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class DirectCasClientTests method testNoTokenRedirectionExpected.

@Test
public void testNoTokenRedirectionExpected() {
    final CasConfiguration configuration = new CasConfiguration();
    configuration.setLoginUrl(LOGIN_URL);
    final DirectCasClient client = new DirectCasClient(configuration);
    final MockWebContext context = MockWebContext.create();
    context.setFullRequestURL(CALLBACK_URL);
    final HttpAction action = (HttpAction) TestsHelper.expectException(() -> client.getCredentials(context));
    assertEquals(302, action.getCode());
    assertEquals(addParameter(LOGIN_URL, CasConfiguration.SERVICE_PARAMETER, CALLBACK_URL), context.getResponseHeaders().get(HttpConstants.LOCATION_HEADER));
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) CasConfiguration(org.pac4j.cas.config.CasConfiguration) HttpAction(org.pac4j.core.exception.HttpAction) Test(org.junit.Test)

Example 79 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class DirectCasProxyClientTests method testTokenExistsValidationOccurs.

@Test
public void testTokenExistsValidationOccurs() {
    final CasConfiguration configuration = new CasConfiguration();
    configuration.setLoginUrl(LOGIN_URL);
    configuration.setProtocol(CasProtocol.CAS30_PROXY);
    configuration.setDefaultTicketValidator((ticket, service) -> {
        if (TICKET.equals(ticket) && CALLBACK_URL.equals(service)) {
            return new AssertionImpl(TICKET);
        }
        throw new TechnicalException("Bad ticket or service");
    });
    final DirectCasProxyClient client = new DirectCasProxyClient(configuration, CALLBACK_URL);
    final MockWebContext context = MockWebContext.create();
    context.setFullRequestURL(CALLBACK_URL + "?" + CasConfiguration.TICKET_PARAMETER + "=" + TICKET);
    context.addRequestParameter(CasConfiguration.TICKET_PARAMETER, TICKET);
    final TokenCredentials credentials = client.getCredentials(context);
    assertEquals(TICKET, credentials.getToken());
    final CommonProfile profile = credentials.getUserProfile();
    assertTrue(profile instanceof CasProfile);
    assertEquals(TICKET, profile.getId());
}
Also used : AssertionImpl(org.jasig.cas.client.validation.AssertionImpl) CasProfile(org.pac4j.cas.profile.CasProfile) MockWebContext(org.pac4j.core.context.MockWebContext) TechnicalException(org.pac4j.core.exception.TechnicalException) CasConfiguration(org.pac4j.cas.config.CasConfiguration) CommonProfile(org.pac4j.core.profile.CommonProfile) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 80 with MockWebContext

use of org.pac4j.core.context.MockWebContext in project pac4j by pac4j.

the class CasRestClientIT method internalTestRestBasic.

private void internalTestRestBasic(final CasRestBasicAuthClient client, int nbAttributes) {
    final MockWebContext context = MockWebContext.create();
    final String token = USER + ":" + USER;
    context.addRequestHeader(VALUE, NAME + Base64.getEncoder().encodeToString(token.getBytes(StandardCharsets.UTF_8)));
    final UsernamePasswordCredentials credentials = client.getCredentials(context);
    final CasRestProfile profile = client.getUserProfile(credentials, context);
    assertEquals(USER, profile.getId());
    assertNotNull(profile.getTicketGrantingTicketId());
    final TokenCredentials casCreds = client.requestServiceTicket(PAC4J_BASE_URL, profile, context);
    final CasProfile casProfile = client.validateServiceTicket(PAC4J_BASE_URL, casCreds, context);
    assertNotNull(casProfile);
    assertEquals(USER, casProfile.getId());
    assertEquals(nbAttributes, casProfile.getAttributes().size());
    client.destroyTicketGrantingTicket(profile, context);
    TestsHelper.expectException(() -> client.requestServiceTicket(PAC4J_BASE_URL, profile, context), TechnicalException.class, "Service ticket request for `#CasRestProfile# | id: " + USER + " | attributes: {} | roles: [] | permissions: [] | " + "isRemembered: false | clientName: CasRestBasicAuthClient | linkedId: null |` failed: (404) Not Found");
}
Also used : CasProfile(org.pac4j.cas.profile.CasProfile) MockWebContext(org.pac4j.core.context.MockWebContext) CasRestProfile(org.pac4j.cas.profile.CasRestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials) TokenCredentials(org.pac4j.core.credentials.TokenCredentials)

Aggregations

MockWebContext (org.pac4j.core.context.MockWebContext)96 Test (org.junit.Test)91 TokenCredentials (org.pac4j.core.credentials.TokenCredentials)20 CommonProfile (org.pac4j.core.profile.CommonProfile)18 CasConfiguration (org.pac4j.cas.config.CasConfiguration)10 UsernamePasswordCredentials (org.pac4j.core.credentials.UsernamePasswordCredentials)5 HttpAction (org.pac4j.core.exception.HttpAction)5 CasProfile (org.pac4j.cas.profile.CasProfile)4 SimpleTestTokenAuthenticator (org.pac4j.http.credentials.authenticator.test.SimpleTestTokenAuthenticator)4 DigestCredentials (org.pac4j.http.credentials.DigestCredentials)3 AssertionImpl (org.jasig.cas.client.validation.AssertionImpl)2 CasRestProfile (org.pac4j.cas.profile.CasRestProfile)2 Credentials (org.pac4j.core.credentials.Credentials)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 KerberosCredentials (org.pac4j.kerberos.credentials.KerberosCredentials)2 URL (java.net.URL)1 IndirectClient (org.pac4j.core.client.IndirectClient)1 Cookie (org.pac4j.core.context.Cookie)1 PathParameterCallbackUrlResolver (org.pac4j.core.http.callback.PathParameterCallbackUrlResolver)1 JavaSerializationHelper (org.pac4j.core.util.JavaSerializationHelper)1