Search in sources :

Example 31 with MockWebContext

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

the class DirectCasClientTests method testTicketExistsValidationOccurs.

@Test
public void testTicketExistsValidationOccurs() {
    final CasConfiguration configuration = new CasConfiguration();
    configuration.setLoginUrl(LOGIN_URL);
    configuration.setDefaultTicketValidator((ticket, service) -> {
        if (TICKET.equals(ticket) && CALLBACK_URL.equals(service)) {
            return new AssertionImpl(TICKET);
        }
        throw new TechnicalException("Bad ticket or service");
    });
    final DirectCasClient client = new DirectCasClient(configuration);
    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 32 with MockWebContext

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

the class CasRestClientIT method internalTestRestForm.

private void internalTestRestForm(final Authenticator authenticator) {
    final CasRestFormClient client = new CasRestFormClient();
    client.setConfiguration(getConfig());
    client.setAuthenticator(authenticator);
    final MockWebContext context = MockWebContext.create();
    context.addRequestParameter(client.getUsernameParameter(), USER);
    context.addRequestParameter(client.getPasswordParameter(), USER);
    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());
    assertTrue(casProfile.getAttributes().size() > 0);
}
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)

Example 33 with MockWebContext

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

the class KerberosClientTests method testAuthentication.

@Test
public void testAuthentication() {
    when(krbValidator.validateTicket(any())).thenReturn(new KerberosTicketValidation("garry", null, null, null));
    final DirectKerberosClient client = new DirectKerberosClient(new KerberosAuthenticator(krbValidator));
    final MockWebContext context = MockWebContext.create();
    context.addRequestHeader(HttpConstants.AUTHORIZATION_HEADER, "Negotiate " + new String(KERBEROS_TICKET, StandardCharsets.UTF_8));
    final KerberosCredentials credentials = client.getCredentials(context);
    assertEquals(new String(Base64.getDecoder().decode(KERBEROS_TICKET), StandardCharsets.UTF_8), new String(credentials.getKerberosTicket(), StandardCharsets.UTF_8));
    final CommonProfile profile = client.getUserProfile(credentials, context);
    assertEquals("garry", profile.getId());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) KerberosAuthenticator(org.pac4j.kerberos.credentials.authenticator.KerberosAuthenticator) CommonProfile(org.pac4j.core.profile.CommonProfile) KerberosCredentials(org.pac4j.kerberos.credentials.KerberosCredentials) KerberosTicketValidation(org.pac4j.kerberos.credentials.authenticator.KerberosTicketValidation) Test(org.junit.Test)

Example 34 with MockWebContext

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

the class KerberosClientsKerbyTests method mockWebRequestContext.

private MockWebContext mockWebRequestContext(String spnegoWebTicket) {
    System.out.println("spnegoWebTicket:" + spnegoWebTicket);
    final MockWebContext context = MockWebContext.create();
    context.addRequestHeader(HttpConstants.AUTHORIZATION_HEADER, "Negotiate " + spnegoWebTicket);
    return context;
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext)

Example 35 with MockWebContext

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

the class KerberosClientsKerbyTests method testDirectIncorrectAuth.

// =====================
// Indirect client below
// =====================
@Test
public void testDirectIncorrectAuth() {
    // a request with an incorrect Kerberos token, yields NULL credentials also
    final MockWebContext context = MockWebContext.create().addRequestHeader(HttpConstants.AUTHORIZATION_HEADER, "Negotiate " + "AAAbbAA123");
    assertNull(setupDirectKerberosClient().getCredentials(context));
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) Test(org.junit.Test)

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