Search in sources :

Example 1 with HttpMessage

use of org.apereo.cas.util.http.HttpMessage in project cas by apereo.

the class YubiKeyMultifactorAuthenticationProvider method isAvailable.

@Override
protected boolean isAvailable() {
    try {
        final String[] endpoints = client.getWsapiUrls();
        for (final String endpoint : endpoints) {
            LOGGER.debug("Pinging YubiKey API endpoint at [{}]", endpoint);
            final HttpMessage msg = this.httpClient.sendMessageToEndPoint(new URL(endpoint));
            final String message = msg != null ? msg.getMessage() : null;
            if (StringUtils.isNotBlank(message)) {
                final String response = EncodingUtils.urlDecode(message);
                LOGGER.debug("Received YubiKey ping response [{}]", response);
                return true;
            }
        }
    } catch (final Exception e) {
        LOGGER.warn(e.getMessage(), e);
    }
    return false;
}
Also used : HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL)

Example 2 with HttpMessage

use of org.apereo.cas.util.http.HttpMessage in project cas by apereo.

the class BaseDuoSecurityAuthenticationService method ping.

@Override
public boolean ping() {
    try {
        final String url = buildUrlHttpScheme(getApiHost().concat("/rest/v1/ping"));
        LOGGER.debug("Contacting Duo @ [{}]", url);
        final HttpMessage msg = this.httpClient.sendMessageToEndPoint(new URL(url));
        if (msg != null) {
            final String response = URLDecoder.decode(msg.getMessage(), StandardCharsets.UTF_8.name());
            LOGGER.debug("Received Duo ping response [{}]", response);
            final JsonNode result = MAPPER.readTree(response);
            if (result.has(RESULT_KEY_RESPONSE) && result.has(RESULT_KEY_STAT) && result.get(RESULT_KEY_RESPONSE).asText().equalsIgnoreCase("pong") && result.get(RESULT_KEY_STAT).asText().equalsIgnoreCase("OK")) {
                return true;
            }
            LOGGER.warn("Could not reach/ping Duo. Response returned is [{}]", result);
        }
    } catch (final Exception e) {
        LOGGER.warn("Pinging Duo has failed with error: [{}]", e.getMessage(), e);
    }
    return false;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL)

Example 3 with HttpMessage

use of org.apereo.cas.util.http.HttpMessage in project cas by apereo.

the class YubiKeyMultifactorAuthenticationProviderTests method verifyFails.

@Test
public void verifyFails() throws Exception {
    val client = mock(YubicoClient.class);
    when(client.getWsapiUrls()).thenThrow(new RuntimeException());
    val http = mock(HttpClient.class);
    when(http.sendMessageToEndPoint(any(URL.class))).thenReturn(new HttpMessage(new URL("http://localhost:1234"), "message"));
    val provider = new YubiKeyMultifactorAuthenticationProvider(client, http);
    val service = MultifactorAuthenticationTestUtils.getRegisteredService();
    assertFalse(provider.isAvailable(service));
}
Also used : lombok.val(lombok.val) HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMessage

use of org.apereo.cas.util.http.HttpMessage in project cas by apereo.

the class BaseDuoAuthenticationService method ping.

@Override
public boolean ping() {
    try {
        final String url = buildUrlHttpScheme(getApiHost().concat("/rest/v1/ping"));
        LOGGER.debug("Contacting Duo @ [{}]", url);
        final HttpMessage msg = this.httpClient.sendMessageToEndPoint(new URL(url));
        if (msg != null) {
            final String response = URLDecoder.decode(msg.getMessage(), StandardCharsets.UTF_8.name());
            LOGGER.debug("Received Duo ping response [{}]", response);
            final JsonNode result = MAPPER.readTree(response);
            if (result.has(RESULT_KEY_RESPONSE) && result.has(RESULT_KEY_STAT) && result.get(RESULT_KEY_RESPONSE).asText().equalsIgnoreCase("pong") && result.get(RESULT_KEY_STAT).asText().equalsIgnoreCase("OK")) {
                return true;
            }
            LOGGER.warn("Could not reach/ping Duo. Response returned is [{}]", result);
        }
    } catch (final Exception e) {
        LOGGER.warn("Pinging Duo has failed with error: [{}]", e.getMessage(), e);
    }
    return false;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL)

Example 5 with HttpMessage

use of org.apereo.cas.util.http.HttpMessage in project cas by apereo.

the class YubiKeyMultifactorAuthenticationProviderTests method getMultifactorAuthenticationProvider.

@Override
@SneakyThrows
public AbstractMultifactorAuthenticationProvider getMultifactorAuthenticationProvider() {
    val client = mock(YubicoClient.class);
    when(client.getWsapiUrls()).thenReturn(new String[] { "http://localhost:1234" });
    val http = mock(HttpClient.class);
    when(http.sendMessageToEndPoint(any(URL.class))).thenReturn(new HttpMessage(new URL("http://localhost:1234"), "message"));
    return new YubiKeyMultifactorAuthenticationProvider(client, http);
}
Also used : lombok.val(lombok.val) HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL) SneakyThrows(lombok.SneakyThrows)

Aggregations

URL (java.net.URL)5 HttpMessage (org.apereo.cas.util.http.HttpMessage)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 lombok.val (lombok.val)2 SneakyThrows (lombok.SneakyThrows)1 Test (org.junit.jupiter.api.Test)1