EventBusAuthenticationService implements AuthenticationService { @Override public void authenticate(final JsonObject authRequest, final Handler<AsyncResult<HonoUser>> authenticationResultHandler) { final DeliveryOptions options = new DeliveryOptions().setSendTimeout(AUTH_REQUEST_TIMEOUT_MILLIS); vertx.eventBus().request(AuthenticationConstants.EVENT_BUS_ADDRESS_AUTHENTICATION_IN, authRequest, options, reply -> { if (reply.succeeded()) { final JsonObject result = (JsonObject) reply.result().body(); final String token = result.getString(AuthenticationConstants.FIELD_TOKEN); log.debug("received token [length: {}] in response to authentication request", token.length()); try { final Jws<Claims> expandedToken = tokenValidator.expand(result.getString(AuthenticationConstants.FIELD_TOKEN)); authenticationResultHandler.handle(Future.succeededFuture(new HonoUserImpl(expandedToken, token))); } catch (final JwtException e) { authenticationResultHandler.handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, e))); } } else { final ServiceInvocationException resultException; if (reply.cause() instanceof ReplyException) { switch (((ReplyException) reply.cause()).failureType()) { case TIMEOUT: log.debug("timeout processing authentication request", reply.cause()); resultException = new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, reply.cause()); break; case NO_HANDLERS: log.debug("could not process authentication request", reply.cause()); resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause()); break; case RECIPIENT_FAILURE: final int statusCode = ((ReplyException) reply.cause()).failureCode(); if (statusCode < 400 || statusCode >= 600) { log.error("got illegal status code in authentication response exception: {}", statusCode); resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause().getMessage()); } else { resultException = StatusCodeMapper.from(statusCode, reply.cause().getMessage()); } break; default: resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause()); } } else { resultException = new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, reply.cause()); } authenticationResultHandler.handle(Future.failedFuture(resultException)); } }); } EventBusAuthenticationService(final Vertx vertx, final AuthTokenHelper validator);  EventBusAuthenticationService(final Vertx vertx, final AuthTokenHelper validator,
            final String[] supportedSASLMechanisms); @Override String[] getSupportedSaslMechanisms(); @Override void authenticate(final JsonObject authRequest, final Handler<AsyncResult<HonoUser>> authenticationResultHandler);  }
@Test public void testAuthenticateSuccess(final VertxTestContext ctx) { final String token = createTestToken(); authRequestConsumer = vertx.eventBus().consumer(AuthenticationConstants.EVENT_BUS_ADDRESS_AUTHENTICATION_IN, message -> { message.reply(AuthenticationConstants.getAuthenticationReply(token)); }); final EventBusAuthenticationService eventBusAuthService = new EventBusAuthenticationService(vertx, authTokenHelperForValidating); eventBusAuthService.authenticate(new JsonObject(), ctx.succeeding(t -> { ctx.verify(() -> assertThat(t.getToken()).isEqualTo(token)); ctx.completeNow(); })); }
@Test public void testAuthenticateFailureNoHandler(final VertxTestContext ctx) { final EventBusAuthenticationService eventBusAuthService = new EventBusAuthenticationService(vertx, authTokenHelperForValidating); eventBusAuthService.authenticate(new JsonObject(), ctx.failing(t -> { ctx.verify(() -> { assertThat(t).isInstanceOf(ServerErrorException.class); assertThat(((ServiceInvocationException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_INTERNAL_ERROR); }); ctx.completeNow(); })); }
@Test public void testAuthenticateFailureValidStatusCode(final VertxTestContext ctx) { final String failureMessage = "failureMessage"; authRequestConsumer = vertx.eventBus().consumer(AuthenticationConstants.EVENT_BUS_ADDRESS_AUTHENTICATION_IN, message -> { message.fail(HttpURLConnection.HTTP_UNAUTHORIZED, failureMessage); }); final EventBusAuthenticationService eventBusAuthService = new EventBusAuthenticationService(vertx, authTokenHelperForValidating); eventBusAuthService.authenticate(new JsonObject(), ctx.failing(t -> { ctx.verify(() -> { assertThat(t).isInstanceOf(ClientErrorException.class); assertThat(((ServiceInvocationException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAUTHORIZED); assertThat(t.getMessage()).isEqualTo(failureMessage); }); ctx.completeNow(); })); }
@Test public void testAuthenticateFailureInvalidStatusCode(final VertxTestContext ctx) { final String failureMessage = "failureMessage"; authRequestConsumer = vertx.eventBus().consumer(AuthenticationConstants.EVENT_BUS_ADDRESS_AUTHENTICATION_IN, message -> { message.fail(200, failureMessage); }); final EventBusAuthenticationService eventBusAuthService = new EventBusAuthenticationService(vertx, authTokenHelperForValidating); eventBusAuthService.authenticate(new JsonObject(), ctx.failing(t -> { ctx.verify(() -> { assertThat(t).isInstanceOf(ServerErrorException.class); assertThat(((ServiceInvocationException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_INTERNAL_ERROR); assertThat(t.getMessage()).isEqualTo(failureMessage); }); ctx.completeNow(); })); }