Search in sources :

Example 1 with KerberosCredentials

use of org.pac4j.kerberos.credentials.KerberosCredentials 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 2 with KerberosCredentials

use of org.pac4j.kerberos.credentials.KerberosCredentials in project pac4j by pac4j.

the class KerberosClientTests method testMissingKerberosHeader.

@Test
public void testMissingKerberosHeader() {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    final DirectKerberosClient client = new DirectKerberosClient(new KerberosAuthenticator(krbValidator));
    KerberosCredentials credentials = client.getCredentials(new J2EContext(request, response));
    assertNull(credentials);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) KerberosAuthenticator(org.pac4j.kerberos.credentials.authenticator.KerberosAuthenticator) KerberosCredentials(org.pac4j.kerberos.credentials.KerberosCredentials) HttpServletResponse(javax.servlet.http.HttpServletResponse) J2EContext(org.pac4j.core.context.J2EContext) Test(org.junit.Test)

Example 3 with KerberosCredentials

use of org.pac4j.kerberos.credentials.KerberosCredentials in project pac4j by pac4j.

the class KerberosClientsKerbyTests method checkWithGoodTicket.

private void checkWithGoodTicket(Client<KerberosCredentials, KerberosProfile> client) throws Exception {
    String spnegoWebTicket = SpnegoServiceTicketHelper.getGSSTicket(clientPrincipal, clientPassword, serviceName);
    // mock web request
    final MockWebContext context = mockWebRequestContext(spnegoWebTicket);
    final KerberosCredentials credentials = client.getCredentials(context);
    assertNotNull(credentials);
    System.out.println(credentials);
    final KerberosProfile profile = client.getUserProfile(credentials, context);
    assertNotNull(profile);
    assertEquals(clientPrincipal, profile.getId());
}
Also used : MockWebContext(org.pac4j.core.context.MockWebContext) KerberosProfile(org.pac4j.kerberos.profile.KerberosProfile) KerberosCredentials(org.pac4j.kerberos.credentials.KerberosCredentials)

Example 4 with KerberosCredentials

use of org.pac4j.kerberos.credentials.KerberosCredentials in project pac4j by pac4j.

the class IndirectKerberosClient method retrieveCredentials.

@Override
protected KerberosCredentials retrieveCredentials(final WebContext context) {
    CommonHelper.assertNotNull("credentialsExtractor", getCredentialsExtractor());
    CommonHelper.assertNotNull("authenticator", getAuthenticator());
    // set the www-authenticate in case of error
    context.setResponseHeader(HttpConstants.AUTHENTICATE_HEADER, "Negotiate");
    final KerberosCredentials credentials;
    try {
        // retrieve credentials
        credentials = getCredentialsExtractor().extract(context);
        logger.debug("kerberos credentials : {}", credentials);
        if (credentials == null) {
            throw HttpAction.unauthorized(context);
        }
        // validate credentials
        getAuthenticator().validate(credentials, context);
    } catch (final CredentialsException e) {
        throw HttpAction.unauthorized(context);
    }
    return credentials;
}
Also used : KerberosCredentials(org.pac4j.kerberos.credentials.KerberosCredentials) CredentialsException(org.pac4j.core.exception.CredentialsException)

Aggregations

KerberosCredentials (org.pac4j.kerberos.credentials.KerberosCredentials)4 Test (org.junit.Test)2 MockWebContext (org.pac4j.core.context.MockWebContext)2 KerberosAuthenticator (org.pac4j.kerberos.credentials.authenticator.KerberosAuthenticator)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 J2EContext (org.pac4j.core.context.J2EContext)1 CredentialsException (org.pac4j.core.exception.CredentialsException)1 CommonProfile (org.pac4j.core.profile.CommonProfile)1 KerberosTicketValidation (org.pac4j.kerberos.credentials.authenticator.KerberosTicketValidation)1 KerberosProfile (org.pac4j.kerberos.profile.KerberosProfile)1