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