/** * Gets the device id in an address structure. * * @param device The device. * @return tenantId and deviceId as an address. * @throws NullPointerException if device is {@code null}. */ public static final String asAddress(final Device device) { return String.format("%s/%s", device.getTenantId(), device.getDeviceId()); } }
@Override public final String toString() { return String.format("device [%s: %s, %s: %s]", CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, getDeviceId(), CredentialsConstants.FIELD_PAYLOAD_TENANT_ID, getTenantId()); }
/** * Gets the device id in an address structure. * * @param device The device. * @return tenantId and deviceId as an address. * @throws NullPointerException if device is {@code null}. */ public static final String asAddress(final Device device) { return String.format("%s/%s", device.getTenantId(), device.getDeviceId()); } }
@Override public final String toString() { return String.format("device [%s: %s, %s: %s]", CredentialsConstants.FIELD_PAYLOAD_DEVICE_ID, getDeviceId(), CredentialsConstants.FIELD_PAYLOAD_TENANT_ID, getTenantId()); }
private Future<String> getGatewayId(final String tenantId, final String deviceId, final Device authenticatedDevice) { final Future<String> result = Future.future(); if (authenticatedDevice == null) { result.complete(null); } else if (tenantId.equals(authenticatedDevice.getTenantId())) { if (deviceId.equals(authenticatedDevice.getDeviceId())) { result.complete(null); } else { result.complete(authenticatedDevice.getDeviceId()); } } else { result.fail(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "cannot publish data for device of other tenant")); } return result; }
private Future<String> getGatewayId(final String tenantId, final String deviceId, final Device authenticatedDevice) { final Future<String> result = Future.future(); if (authenticatedDevice == null) { result.complete(null); } else if (tenantId.equals(authenticatedDevice.getTenantId())) { if (deviceId.equals(authenticatedDevice.getDeviceId())) { result.complete(null); } else { result.complete(authenticatedDevice.getDeviceId()); } } else { result.fail(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "cannot publish data for device of other tenant")); } return result; }
/** * Gets the device identifier for this context. * * @return The device identifier. */ String getDeviceId() { return isDeviceAuthenticated() ? authenticatedDevice.getDeviceId() : resource.getResourceId(); }
/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
void handlePostTelemetry(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadTelemetryMessage(ctx, device.getTenantId(), device.getDeviceId()); } else { handle401(ctx); } }
void handlePostEvent(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadEventMessage(ctx, device.getTenantId(), device.getDeviceId()); } else { handle401(ctx); } }
void handlePostTelemetry(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadTelemetryMessage(ctx, device.getTenantId(), device.getDeviceId()); } else { handle401(ctx); } }
void handlePostEvent(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadEventMessage(ctx, device.getTenantId(), device.getDeviceId()); } else { handle401(ctx); } }
@Override public final void authenticate( final T deviceCredentials, final SpanContext spanContext, final Handler<AsyncResult<DeviceUser>> resultHandler) { Objects.requireNonNull(deviceCredentials); Objects.requireNonNull(resultHandler); getCredentialsForDevice(deviceCredentials, spanContext) .recover(t -> { if (!(t instanceof ServiceInvocationException)) { return Future.failedFuture(t); } final ServiceInvocationException e = (ServiceInvocationException) t; if (e.getErrorCode() == HttpURLConnection.HTTP_NOT_FOUND) { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials")); } else { return Future.failedFuture(t); } }).compose(credentialsOnRecord -> validateCredentials(deviceCredentials, credentialsOnRecord)) .compose(d -> Future.succeededFuture(new DeviceUser(d.getTenantId(), d.getDeviceId()))) .setHandler(resultHandler); }
@Override public final void authenticate( final DeviceCredentials deviceCredentials, final Handler<AsyncResult<DeviceUser>> resultHandler) { Objects.requireNonNull(deviceCredentials); Objects.requireNonNull(resultHandler); getCredentialsForDevice(deviceCredentials) .recover(t -> { if (!(t instanceof ServiceInvocationException)) { return Future.failedFuture(t); } final ServiceInvocationException e = (ServiceInvocationException) t; if (e.getErrorCode() == HttpURLConnection.HTTP_NOT_FOUND) { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials")); } else { return Future.failedFuture(t); } }).compose(credentialsOnRecord -> validateCredentials(deviceCredentials, credentialsOnRecord)) .compose(d -> Future.succeededFuture(new DeviceUser(d.getTenantId(), d.getDeviceId()))) .setHandler(resultHandler); }
void handlePostCommandResponse(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadCommandResponseMessage(ctx, device.getTenantId(), device.getDeviceId(), getCommandRequestIdParam(ctx), getCommandResponseStatusParam(ctx)); } else { handle401(ctx); } }
void handlePostCommandResponse(final RoutingContext ctx) { if (Device.class.isInstance(ctx.user())) { final Device device = (Device) ctx.user(); uploadCommandResponseMessage(ctx, device.getTenantId(), device.getDeviceId(), getCommandRequestIdParam(ctx), getCommandResponseStatusParam(ctx)); } else { handle401(ctx); } }
/** * Closes a connection to a client. * * @param endpoint The connection to close. * @param authenticatedDevice Optional authenticated device information, may be {@code null}. */ protected final void close(final MqttEndpoint endpoint, final Device authenticatedDevice) { onClose(endpoint); sendDisconnectedEvent(endpoint.clientIdentifier(), authenticatedDevice); if (authenticatedDevice == null) { LOG.debug("connection to anonymous device [clientId: {}] closed", endpoint.clientIdentifier()); metrics.decrementUnauthenticatedConnections(); } else { LOG.debug("connection to device [tenant-id: {}, device-id: {}] closed", authenticatedDevice.getTenantId(), authenticatedDevice.getDeviceId()); metrics.decrementConnections(authenticatedDevice.getTenantId()); } if (endpoint.isConnected()) { LOG.debug("closing connection with client [client ID: {}]", endpoint.clientIdentifier()); endpoint.close(); } else { LOG.trace("client has already closed connection"); } }
private Span newSpan(final String operationName, final Device authenticatedDevice) { final Span span = tracer.buildSpan(operationName) .ignoreActiveSpan() .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER) .withTag(Tags.COMPONENT.getKey(), getTypeName()) .withTag(TracingHelper.TAG_AUTHENTICATED.getKey(), authenticatedDevice != null) .start(); if (authenticatedDevice != null) { span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, authenticatedDevice.getTenantId()); span.setTag(MessageHelper.APP_PROPERTY_DEVICE_ID, authenticatedDevice.getDeviceId()); } return span; }
private Span newSpan(final String operationName, final MqttEndpoint endpoint, final Device authenticatedDevice) { final Span span = tracer.buildSpan(operationName) .ignoreActiveSpan() .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER) .withTag(Tags.COMPONENT.getKey(), getTypeName()) .withTag(TracingHelper.TAG_CLIENT_ID.getKey(), endpoint.clientIdentifier()) .withTag(TracingHelper.TAG_AUTHENTICATED.getKey(), authenticatedDevice != null) .start(); if (authenticatedDevice != null) { span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, authenticatedDevice.getTenantId()); span.setTag(MessageHelper.APP_PROPERTY_DEVICE_ID, authenticatedDevice.getDeviceId()); } return span; }