Search in sources :

Example 1 with DeviceCredentials

use of org.eclipse.hono.service.auth.device.DeviceCredentials in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapter method handleEndpointConnectionWithAuthentication.

private void handleEndpointConnectionWithAuthentication(final MqttEndpoint endpoint) {
    if (endpoint.auth() == null) {
        LOG.debug("connection request from device [clientId: {}] rejected: {}", endpoint.clientIdentifier(), "device did not provide credentials in CONNECT packet");
        endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
    } else {
        final DeviceCredentials credentials = getCredentials(endpoint.auth());
        if (credentials == null) {
            LOG.debug("connection request from device [clientId: {}] rejected: {}", endpoint.clientIdentifier(), "device provided malformed credentials in CONNECT packet");
            endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
        } else {
            getTenantConfiguration(credentials.getTenantId()).compose(tenantConfig -> {
                if (tenantConfig.isAdapterEnabled(getTypeName())) {
                    LOG.debug("protocol adapter [{}] is enabled for tenant [{}]", getTypeName(), credentials.getTenantId());
                    return Future.succeededFuture(tenantConfig);
                } else {
                    LOG.debug("protocol adapter [{}] is disabled for tenant [{}]", getTypeName(), credentials.getTenantId());
                    return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "adapter disabled for tenant"));
                }
            }).compose(tenantConfig -> {
                final Future<Device> result = Future.future();
                getCredentialsAuthProvider().authenticate(credentials, result.completer());
                return result;
            }).map(authenticatedDevice -> {
                LOG.debug("successfully authenticated device [tenant-id: {}, auth-id: {}, device-id: {}]", authenticatedDevice.getTenantId(), credentials.getAuthId(), authenticatedDevice.getDeviceId());
                onAuthenticationSuccess(endpoint, authenticatedDevice);
                return null;
            }).otherwise(t -> {
                LOG.debug("cannot authenticate device [tenant-id: {}, auth-id: {}]", credentials.getTenantId(), credentials.getAuthId(), t);
                if (ServerErrorException.class.isInstance(t)) {
                    // one of the services we depend on might not be available (yet)
                    endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);
                } else {
                    // validation of credentials has failed
                    endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
                }
                return null;
            });
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) LoggerFactory(org.slf4j.LoggerFactory) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) Autowired(org.springframework.beans.factory.annotation.Autowired) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) MqttServer(io.vertx.mqtt.MqttServer) CompositeFuture(io.vertx.core.CompositeFuture) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) MessageSender(org.eclipse.hono.client.MessageSender) AbstractProtocolAdapterBase(org.eclipse.hono.service.AbstractProtocolAdapterBase) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) UsernamePasswordCredentials(org.eclipse.hono.service.auth.device.UsernamePasswordCredentials) Logger(org.slf4j.Logger) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) DeviceCredentials(org.eclipse.hono.service.auth.device.DeviceCredentials) ServerErrorException(org.eclipse.hono.client.ServerErrorException) EndpointType(org.eclipse.hono.util.EndpointType) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Objects(java.util.Objects) Buffer(io.vertx.core.buffer.Buffer) MqttAuth(io.vertx.mqtt.MqttAuth) Device(org.eclipse.hono.service.auth.device.Device) Device(org.eclipse.hono.service.auth.device.Device) ClientErrorException(org.eclipse.hono.client.ClientErrorException) DeviceCredentials(org.eclipse.hono.service.auth.device.DeviceCredentials)

Aggregations

MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 MqttAuth (io.vertx.mqtt.MqttAuth)1 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)1 MqttServer (io.vertx.mqtt.MqttServer)1 MqttServerOptions (io.vertx.mqtt.MqttServerOptions)1 HttpURLConnection (java.net.HttpURLConnection)1 Objects (java.util.Objects)1 Message (org.apache.qpid.proton.message.Message)1 ClientErrorException (org.eclipse.hono.client.ClientErrorException)1 MessageSender (org.eclipse.hono.client.MessageSender)1 ServerErrorException (org.eclipse.hono.client.ServerErrorException)1 ServiceInvocationException (org.eclipse.hono.client.ServiceInvocationException)1 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)1 AbstractProtocolAdapterBase (org.eclipse.hono.service.AbstractProtocolAdapterBase)1 Device (org.eclipse.hono.service.auth.device.Device)1