Search in sources :

Example 26 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class TicketAndLogoutRequestExtractor method uncompressLogoutMessage.

protected String uncompressLogoutMessage(final String originalMessage) {
    final var binaryMessage = Base64.getMimeDecoder().decode(originalMessage);
    Inflater decompresser = null;
    try {
        // decompress the bytes
        decompresser = new Inflater();
        decompresser.setInput(binaryMessage);
        final var result = new byte[binaryMessage.length * DECOMPRESSION_FACTOR];
        final var resultLength = decompresser.inflate(result);
        // decode the bytes into a String
        return new String(result, 0, resultLength, "UTF-8");
    } catch (final Exception e) {
        logger.error("Unable to decompress logout message", e);
        throw new TechnicalException(e);
    } finally {
        if (decompresser != null) {
            decompresser.end();
        }
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) Inflater(java.util.zip.Inflater) TechnicalException(org.pac4j.core.exception.TechnicalException)

Example 27 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class DirectCasClientTests method testTicketExistsValidationOccurs.

@Test
public void testTicketExistsValidationOccurs() {
    final var 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 var client = new DirectCasClient(configuration);
    final var context = MockWebContext.create();
    context.setFullRequestURL(CALLBACK_URL + "?" + CasConfiguration.TICKET_PARAMETER + "=" + TICKET);
    context.addRequestParameter(CasConfiguration.TICKET_PARAMETER, TICKET);
    final var credentials = (TokenCredentials) client.getCredentials(context, new MockSessionStore()).get();
    assertEquals(TICKET, credentials.getToken());
    final var 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) TechnicalException(org.pac4j.core.exception.TechnicalException) MockSessionStore(org.pac4j.core.context.session.MockSessionStore) CasConfiguration(org.pac4j.cas.config.CasConfiguration) TokenCredentials(org.pac4j.core.credentials.TokenCredentials) Test(org.junit.Test)

Example 28 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class CasRestAuthenticator method validate.

@Override
public void validate(final Credentials cred, final WebContext context, final SessionStore sessionStore) {
    final var credentials = (UsernamePasswordCredentials) cred;
    if (credentials == null || credentials.getPassword() == null || credentials.getUsername() == null) {
        throw new TechnicalException("Credentials are required");
    }
    final var ticketGrantingTicketId = requestTicketGrantingTicket(credentials.getUsername(), credentials.getPassword(), context);
    if (CommonHelper.isNotBlank(ticketGrantingTicketId)) {
        credentials.setUserProfile(new CasRestProfile(ticketGrantingTicketId, credentials.getUsername()));
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) CasRestProfile(org.pac4j.cas.profile.CasRestProfile) UsernamePasswordCredentials(org.pac4j.core.credentials.UsernamePasswordCredentials)

Example 29 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class AbstractCasRestClient method requestServiceTicket.

public TokenCredentials requestServiceTicket(final String serviceURL, final CasRestProfile profile, final WebContext context) {
    HttpURLConnection connection = null;
    try {
        final var endpointURL = new URL(configuration.computeFinalRestUrl(context));
        final var ticketURL = new URL(endpointURL, endpointURL.getPath() + "/" + profile.getTicketGrantingTicketId());
        connection = HttpUtils.openPostConnection(ticketURL);
        final var payload = HttpUtils.encodeQueryParam("service", serviceURL);
        final var out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
        out.write(payload);
        out.close();
        final var responseCode = connection.getResponseCode();
        if (responseCode == HttpConstants.OK) {
            try (var in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                return new TokenCredentials(in.readLine());
            }
        }
        throw new TechnicalException("Service ticket request for `" + profile + "` failed: " + HttpUtils.buildHttpErrorMessage(connection));
    } catch (final IOException e) {
        throw new TechnicalException(e);
    } finally {
        HttpUtils.closeConnection(connection);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TechnicalException(org.pac4j.core.exception.TechnicalException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter) TokenCredentials(org.pac4j.core.credentials.TokenCredentials)

Example 30 with TechnicalException

use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.

the class AbstractCasRestClient method destroyTicketGrantingTicket.

public void destroyTicketGrantingTicket(final CasRestProfile profile, final WebContext context) {
    HttpURLConnection connection = null;
    try {
        final var endpointURL = new URL(configuration.computeFinalRestUrl(context));
        final var deleteURL = new URL(endpointURL, endpointURL.getPath() + "/" + profile.getTicketGrantingTicketId());
        connection = HttpUtils.openDeleteConnection(deleteURL);
        final var responseCode = connection.getResponseCode();
        if (responseCode != HttpConstants.OK) {
            throw new TechnicalException("TGT delete request for `" + profile + "` failed: " + HttpUtils.buildHttpErrorMessage(connection));
        }
    } catch (final IOException e) {
        throw new TechnicalException(e);
    } finally {
        HttpUtils.closeConnection(connection);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TechnicalException(org.pac4j.core.exception.TechnicalException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

TechnicalException (org.pac4j.core.exception.TechnicalException)81 IOException (java.io.IOException)26 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)7 HashMap (java.util.HashMap)7 OAuthException (com.github.scribejava.core.exceptions.OAuthException)6 JWT (com.nimbusds.jwt.JWT)6 ParseException (com.nimbusds.oauth2.sdk.ParseException)6 HttpURLConnection (java.net.HttpURLConnection)6 Test (org.junit.Test)6 OidcCredentials (org.pac4j.oidc.credentials.OidcCredentials)6 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)5 SignedJWT (com.nimbusds.jwt.SignedJWT)5 ArrayList (java.util.ArrayList)5 ComponentInitializationException (net.shibboleth.utilities.java.support.component.ComponentInitializationException)5 JOSEException (com.nimbusds.jose.JOSEException)4 URL (java.net.URL)4 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)3 HTTPResponse (com.nimbusds.oauth2.sdk.http.HTTPResponse)3 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)3