private Future<DeviceUser> authenticate(final DeviceCredentials credentials, final Span currentSpan) { final Future<DeviceUser> result = Future.future(); usernamePasswordAuthProvider.authenticate(credentials, handler -> { if (handler.succeeded()) { final DeviceUser authenticatedDevice = handler.result(); currentSpan.log("device authenticated"); LOG.debug("successfully authenticated device [tenant-id: {}, auth-id: {}, device-id: {}]", authenticatedDevice.getTenantId(), credentials.getAuthId(), authenticatedDevice.getDeviceId()); result.complete(authenticatedDevice); } else { LOG.debug("Failed to authenticate device [tenant-id: {}, auth-id: {}] ", credentials.getTenantId(), credentials.getAuthId(), handler.cause()); result.fail(handler.cause()); } }); return result; }
/** * Retrieves credentials from the Credentials service. * * @param deviceCredentials The credentials provided by the device. * @param spanContext The {@code SpanContext} (may be {@code null}). * @return A future containing the credentials on record as retrieved from * Hono's <em>Credentials</em> API. * @throws NullPointerException if device credentials is {@code null}. */ protected final Future<CredentialsObject> getCredentialsForDevice(final DeviceCredentials deviceCredentials, final SpanContext spanContext) { Objects.requireNonNull(deviceCredentials); if (credentialsServiceClient == null) { return Future.failedFuture(new IllegalStateException("Credentials API client is not set")); } else { return getCredentialsClient(deviceCredentials.getTenantId()).compose(client -> client.get(deviceCredentials.getType(), deviceCredentials.getAuthId(), new JsonObject(), spanContext)); } }
/** * Retrieves credentials from the Credentials service. * * @param deviceCredentials The credentials provided by the device. * @return A future containing the credentials on record as retrieved from * Hono's <em>Credentials</em> API. * @throws NullPointerException if device credentials is {@code null}. */ protected final Future<CredentialsObject> getCredentialsForDevice(final DeviceCredentials deviceCredentials) { Objects.requireNonNull(deviceCredentials); if (credentialsServiceClient == null) { return Future.failedFuture(new IllegalStateException("Credentials API client is not set")); } else { return getCredentialsClient(deviceCredentials.getTenantId()).compose(client -> client.get(deviceCredentials.getType(), deviceCredentials.getAuthId())); } }
private Future<Device> handleEndpointConnectionWithAuthentication(final MqttEndpoint endpoint, final Span currentSpan) { final Future<DeviceCredentials> credentialsTracker = getCredentials(endpoint); return credentialsTracker .compose(credentials -> authenticate(credentials, currentSpan)) .compose(device -> CompositeFuture.all( getTenantConfiguration(device.getTenantId(), currentSpan.context()) .compose(tenant -> isAdapterEnabled(tenant)), checkDeviceRegistration(device, currentSpan.context())) .map(ok -> device)) .compose(device -> createLinks(device, currentSpan)) .compose(device -> registerHandlers(endpoint, device)) .recover(t -> { if (credentialsTracker.result() == null) { LOG.debug("error establishing connection with device", t); } else { LOG.debug("cannot establish connection with device [tenant-id: {}, auth-id: {}]", credentialsTracker.result().getTenantId(), credentialsTracker.result().getAuthId(), t); } return Future.failedFuture(t); }); }