Search in sources :

Example 1 with MqttAuth

use of io.vertx.mqtt.MqttAuth in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method getMqttEndpointAuthenticated.

private MqttEndpoint getMqttEndpointAuthenticated() {
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    when(endpoint.auth()).thenReturn(new MqttAuth("sensor1@DEFAULT_TENANT", "test"));
    return endpoint;
}
Also used : MqttAuth(io.vertx.mqtt.MqttAuth) MqttEndpoint(io.vertx.mqtt.MqttEndpoint)

Example 2 with MqttAuth

use of io.vertx.mqtt.MqttAuth in project hono by eclipse.

the class ConnectPacketAuthHandler method parseCredentials.

/**
 * Extracts credentials from a client's MQTT <em>CONNECT</em> packet.
 * <p>
 * The JSON object returned will contain
 * <ul>
 * <li>a <em>username</em> property containing the corresponding value from the MQTT CONNECT packet,</li>
 * <li>a <em>password</em> property containing the corresponding value from the MQTT CONNECT packet and</li>
 * <li>a {@link ExecutionContextAuthHandler#PROPERTY_CLIENT_IDENTIFIER} property containing the
 * MQTT client identifier</li>
 * </ul>
 *
 * @param context The MQTT context for the client's CONNECT packet.
 * @return A future indicating the outcome of the operation.
 *         The future will succeed with the client's credentials extracted from the CONNECT packet
 *         or it will fail with a {@link org.eclipse.hono.client.ServiceInvocationException} indicating the
 *         cause of the failure.
 * @throws NullPointerException if the context is {@code null}
 * @throws IllegalArgumentException if the context does not contain an MQTT endpoint.
 */
@Override
public Future<JsonObject> parseCredentials(final MqttConnectContext context) {
    Objects.requireNonNull(context);
    if (context.deviceEndpoint() == null) {
        throw new IllegalArgumentException("no device endpoint");
    }
    final Promise<JsonObject> result = Promise.promise();
    final MqttAuth auth = context.deviceEndpoint().auth();
    if (auth == null) {
        result.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "device did not provide credentials in CONNECT packet"));
    } else if (auth.getUsername() == null || auth.getPassword() == null) {
        result.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "device provided malformed credentials in CONNECT packet"));
    } else {
        final JsonObject credentialsJSON = new JsonObject().put(CredentialsConstants.FIELD_USERNAME, auth.getUsername()).put(CredentialsConstants.FIELD_PASSWORD, auth.getPassword()).put(PROPERTY_CLIENT_IDENTIFIER, context.deviceEndpoint().clientIdentifier());
        // spanContext of MqttContext not injected into json here since authenticateDevice(mqttContext) will
        // pass on the MqttContext as well as the json into authProvider.authenticate()
        result.complete(credentialsJSON);
    }
    return result.future();
}
Also used : MqttAuth(io.vertx.mqtt.MqttAuth) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException)

Example 3 with MqttAuth

use of io.vertx.mqtt.MqttAuth in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method getMqttEndpointAuthenticated.

private MqttEndpoint getMqttEndpointAuthenticated(final String username, final String password) {
    final SSLSession sslSession = mock(SSLSession.class);
    when(sslSession.getCipherSuite()).thenReturn("BUMLUX_CIPHER");
    final MqttEndpoint endpoint = mockEndpoint();
    when(endpoint.auth()).thenReturn(new MqttAuth(username, password));
    when(endpoint.subscribeHandler(VertxMockSupport.anyHandler())).thenReturn(endpoint);
    when(endpoint.unsubscribeHandler(VertxMockSupport.anyHandler())).thenReturn(endpoint);
    when(endpoint.isCleanSession()).thenReturn(Boolean.FALSE);
    when(endpoint.sslSession()).thenReturn(sslSession);
    return endpoint;
}
Also used : MqttAuth(io.vertx.mqtt.MqttAuth) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) SSLSession(javax.net.ssl.SSLSession)

Example 4 with MqttAuth

use of io.vertx.mqtt.MqttAuth in project hono by eclipse.

the class ConnectPacketAuthHandlerTest method testParseCredentialsIncludesMqttClientId.

/**
 * Verifies that the handler includes the MQTT client identifier in the authentication
 * information retrieved from a device's CONNECT packet.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testParseCredentialsIncludesMqttClientId(final VertxTestContext ctx) {
    // GIVEN an auth handler configured with an auth provider
    // WHEN trying to authenticate a device using a username and password
    final MqttAuth auth = mock(MqttAuth.class);
    when(auth.getUsername()).thenReturn("sensor1@DEFAULT_TENANT");
    when(auth.getPassword()).thenReturn("secret");
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    when(endpoint.auth()).thenReturn(auth);
    when(endpoint.clientIdentifier()).thenReturn("mqtt-device");
    final MqttConnectContext context = MqttConnectContext.fromConnectPacket(endpoint, span);
    authHandler.parseCredentials(context).onComplete(ctx.succeeding(info -> {
        ctx.verify(() -> {
            assertThat(info.getString(CredentialsConstants.FIELD_USERNAME)).isEqualTo("sensor1@DEFAULT_TENANT");
            assertThat(info.getString(CredentialsConstants.FIELD_PASSWORD)).isEqualTo("secret");
            assertThat(info.getString(X509AuthHandler.PROPERTY_CLIENT_IDENTIFIER)).isEqualTo("mqtt-device");
        });
        ctx.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Test(org.junit.jupiter.api.Test) DeviceCredentialsAuthProvider(org.eclipse.hono.adapter.auth.device.DeviceCredentialsAuthProvider) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) MqttAuth(io.vertx.mqtt.MqttAuth) Span(io.opentracing.Span) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) Mockito.mock(org.mockito.Mockito.mock) MqttAuth(io.vertx.mqtt.MqttAuth) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) Test(org.junit.jupiter.api.Test)

Aggregations

MqttAuth (io.vertx.mqtt.MqttAuth)4 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Span (io.opentracing.Span)1 JsonObject (io.vertx.core.json.JsonObject)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1 SSLSession (javax.net.ssl.SSLSession)1 DeviceCredentialsAuthProvider (org.eclipse.hono.adapter.auth.device.DeviceCredentialsAuthProvider)1 ClientErrorException (org.eclipse.hono.client.ClientErrorException)1 TracingMockSupport (org.eclipse.hono.test.TracingMockSupport)1 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1