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();
}
}
}
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());
}
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()));
}
}
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);
}
}
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);
}
}
Aggregations