private Future<Void> checkTopic(final MqttContext context) { if (context.topic() == null) { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed topic name")); } else { return Future.succeededFuture(); } }
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, String.format("Content-Type %s does not match payload", ctx.contentType()))); } else { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "adapter is not enabled for tenant")); final ClientErrorException e = (ClientErrorException) t; LOG.debug("cannot process message [endpoint: {}] from device [tenantId: {}, deviceId: {}]: {} - {}", endpointName, tenant, deviceId, e.getErrorCode(), e.getMessage()); } else { LOG.debug("cannot process message [endpoint: {}] from device [tenantId: {}, deviceId: {}]",
/** * Verifies that the auth provider fails an authentication request with a 401 * {@code ClientErrorException} if the auth-id is unknown. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsWith401ForNonExistingAuthId(final TestContext ctx) { // WHEN trying to authenticate using an auth-id that is not known when(credentialsClient.get(anyString(), eq("user"), any(JsonObject.class), any())) .thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND))); provider.authenticate(new JsonObject(), ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }
/** * Verifies that the provider fails to validate wrong credentials. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsForWrongCredentials(final TestContext ctx) { when(pwdEncoder.matches(eq("wrong_pwd"), any(JsonObject.class))).thenReturn(false); deviceCredentials = UsernamePasswordCredentials.create("device@DEFAULT_TENANT", "wrong_pwd", false); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(e -> { final ClientErrorException error = (ClientErrorException) e; ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, error.getErrorCode()); })); }); }
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, String.format("Content-Type %s does not match payload", ctx.contentType()))); } else { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "adapter is not enabled for tenant")); final ClientErrorException e = (ClientErrorException) t; LOG.debug("cannot process message [endpoint: {}] from device [tenantId: {}, deviceId: {}]: {} - {}", endpointName, tenant, deviceId, e.getErrorCode(), e.getMessage()); } else { LOG.debug("cannot process message [endpoint: {}] from device [tenantId: {}, deviceId: {}]",
/** * Verifies that credentials validation fails if the credentials on record are disabled. * * @param ctx The vert.x test context. */ @Test public void testValidateFailsIfCredentialsAreDisabled(final TestContext ctx) { // WHEN trying to authenticate a disabled device final AbstractDeviceCredentials creds = getDeviceCredentials("type", "tenant", "identity"); final CredentialsObject credentialsOnRecord = getCredentialsObject("type", "identity", "device", false) .addSecret(CredentialsObject.emptySecret(Instant.now().minusSeconds(120), null)); when(credentialsClient.get(eq("type"), eq("identity"), any(JsonObject.class), any())) .thenReturn(Future.succeededFuture(credentialsOnRecord)); provider.authenticate(creds, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }
private Future<Void> checkTopic(final MqttContext context) { if (context.topic() == null) { return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, "malformed topic name")); } else { return Future.succeededFuture(); } }
/** * Verifies that the auth provider fails an authentication request with a 401 * {@code ClientErrorException} if the credentials cannot be parsed. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsWith401ForMalformedCredentials(final TestContext ctx) { // WHEN trying to authenticate using malformed credentials // that do not contain a tenant provider = getProvider(null, NoopTracerFactory.create()); provider.authenticate(new JsonObject(), ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }
/** * Fails a routing context with HTTP status code 400 (Bad Request) and an optional message. * * @param ctx The vert.x routing context to fail. * @param msg The message to write to the response's body (may be {@code null}). * @throws NullPointerException if routing context is {@code null}. */ public static void badRequest(final RoutingContext ctx, final String msg) { failWithHeaders( ctx, new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, msg), null); }
/** * Verifies that credentials validation fails if none of the secrets on record are * valid any more. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsIfNoSecretsAreValidAnymore(final TestContext ctx) { credentialsOnRecord.addSecret(CredentialsObject.emptySecret(null, Instant.now().minusSeconds(120))); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }); }
/** * Fails a routing context with HTTP status code 400 (Bad Request) and an optional message. * * @param ctx The vert.x routing context to fail. * @param msg The message to write to the response's body (may be {@code null}). * @throws NullPointerException if routing context is {@code null}. */ public static void badRequest(final RoutingContext ctx, final String msg) { failWithHeaders( ctx, new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST, msg), null); }
/** * Verifies that the adapter fails a request to retrieve a token for a gateway that does not * belong to the same tenant as the device it wants to act on behalf of. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionFailsWith403ForNonMatchingTenant(final TestContext ctx) { // GIVEN an adapter adapter = newProtocolAdapter(properties, null); // WHEN a gateway tries to get an assertion for a device from another tenant adapter.getRegistrationAssertion( "tenant A", "device", new Device("tenant B", "gateway"), mock(SpanContext.class)).setHandler(ctx.asyncAssertFailure(t -> { // THEN the request fails with a 403 Forbidden error ctx.assertEquals(HttpURLConnection.HTTP_FORBIDDEN, ((ClientErrorException) t).getErrorCode()); })); }
/** * {@inheritDoc} */ @Override public final void shutdown(final Handler<AsyncResult<Void>> completionHandler) { Objects.requireNonNull(completionHandler); if (shuttingDown.compareAndSet(Boolean.FALSE, Boolean.TRUE)) { closeConnection(completionHandler); } else { completionHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "already shutting down"))); } }
/** * Verifies that credentials validation fails if none of the secrets on record are * valid yet. * * @param ctx The vert.x test context. */ @Test public void testAuthenticateFailsIfNoSecretsAreValidYet(final TestContext ctx) { credentialsOnRecord.addSecret(CredentialsObject.emptySecret(Instant.now().plusSeconds(120), null)); vertx.runOnContext(go -> { provider.authenticate(deviceCredentials, null, ctx.asyncAssertFailure(t -> { // THEN authentication fails with a 401 client error ctx.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, ((ClientErrorException) t).getErrorCode()); })); }); } }
/** * {@inheritDoc} */ @Override public final void disconnect(final Handler<AsyncResult<Void>> completionHandler) { Objects.requireNonNull(completionHandler); if (disconnecting.compareAndSet(Boolean.FALSE, Boolean.TRUE)) { closeConnection(completionHandler); } else { completionHandler.handle(Future.failedFuture( new ClientErrorException(HttpURLConnection.HTTP_CONFLICT, "already disconnecting"))); } }
/** * Processes a request for a non-standard operation. * <p> * Subclasses should override this method in order to support additional, custom * operations that are not defined by Hono's Credentials API. * <p> * This default implementation simply returns a future that is failed with a * {@link ClientErrorException} with an error code <em>400 Bad Request</em>. * * @param request The request to process. * @return A future indicating the outcome of the service invocation. */ protected Future<EventBusMessage> processCustomCredentialsMessage(final EventBusMessage request) { log.debug("invalid operation in request message [{}]", request.getOperation()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); }
/** * Processes a request for a non-standard operation. * <p> * Subclasses should override this method in order to support additional, custom * operations that are not defined by Hono's Credentials API. * <p> * This default implementation simply returns a future that is failed with a * {@link ClientErrorException} with an error code <em>400 Bad Request</em>. * * @param request The request to process. * @return A future indicating the outcome of the service invocation. */ protected Future<EventBusMessage> processCustomCredentialsMessage(final EventBusMessage request) { log.debug("invalid operation in request message [{}]", request.getOperation()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); };
/** * Processes a request for a non-standard operation. * <p> * Subclasses should override this method in order to support additional, custom * operations that are not defined by Hono's Tenant API. * <p> * This default implementation simply returns a future that is failed with a * {@link ClientErrorException} with an error code <em>400 Bad Request</em>. * * @param request The request to process. * @return A future indicating the outcome of the service invocation. */ protected Future<EventBusMessage> processCustomTenantMessage(final EventBusMessage request) { log.debug("invalid operation in request message [{}]", request.getOperation()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); }
/** * Processes a request for a non-standard operation. * <p> * Subclasses should override this method in order to support additional, custom * operations that are not defined by Hono's Tenant API. * <p> * This default implementation simply returns a future that is failed with a * {@link ClientErrorException} with an error code <em>400 Bad Request</em>. * * @param request The request to process. * @return A future indicating the outcome of the service invocation. */ protected Future<EventBusMessage> processCustomTenantMessage(final EventBusMessage request) { log.debug("invalid operation in request message [{}]", request.getOperation()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); }
/** * Processes a request for a non-standard operation. * <p> * Subclasses should override this method in order to support additional, custom * operations that are not defined by Hono's Device Registration API. * <p> * This default implementation simply returns a future that is failed with a * {@link ClientErrorException} with an error code <em>400 Bad Request</em>. * * @param request The request to process. * @return A future indicating the outcome of the service invocation. */ protected Future<EventBusMessage> processCustomRegistrationMessage(final EventBusMessage request) { log.debug("invalid operation in request message [{}]", request.getOperation()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); }