Middlewares { public static <E, R> Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity( Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware, Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed) { return ar -> rc -> authed.apply(r -> ac -> entityMiddleware.apply(ar.apply(ac)).invoke(rc)).invoke(rc); } private  Middlewares(); static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator(
      Supplier<Set<String>> supplier); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> exceptionAndRequestIdHandler(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer(
      Tracer tracer, String service); static Middleware<Requested<Authenticated<Response<T>>>, SyncHandler<Response<T>>> authed(
      RequestAuthenticator authenticator); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware,
      Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      RequestAuthenticator authenticator,
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator(
      RequestAuthenticator authenticator);  }
@Test public void testAuthedEntityProvidesIdToken() throws Exception { final EntityMiddleware em = EntityMiddleware.forCodec(JacksonEntityCodec.forMapper(Json.OBJECT_MAPPER)); final RequestContext requestContext = mock(RequestContext.class); final Request request = Request.forUri("/", "PUT") .withPayload(ByteString.encodeUtf8("\"hello \"")) .withHeader(HttpHeaders.AUTHORIZATION, "Bearer s3cr3tp455w0rd"); when(requestContext.request()).thenReturn(request); final String email = "foo@bar.net"; when(authenticator.authenticate(any())).thenReturn(() -> Optional.of(idToken)); when(idToken.getPayload()).thenReturn(idTokenPayload); when(idTokenPayload.getEmail()).thenReturn(email); final Response<ByteString> response = Middlewares.authedEntity(authenticator, em.response(String.class)) .apply(ac -> rc -> payload -> Response.forPayload(payload + ac.user().orElseThrow().getPayload().getEmail())) .invoke(requestContext); assertThat(Json.deserialize(response.payload().orElseThrow(), String.class), is("hello " + email)); }