Middlewares { public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator( RequestAuthenticator authenticator) { return h -> rc -> { final Optional<GoogleIdToken> idToken = auth(rc, authenticator).user(); if (!"GET".equals(rc.request().method()) && idToken.isEmpty()) { return completedFuture( Response.forStatus(Status.UNAUTHORIZED.withReasonPhrase("Unauthorized access"))); } return h.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 testAuthValidatorForGet() throws Exception { RequestContext requestContext = mock(RequestContext.class); Request request = Request.forUri("/", "GET"); when(requestContext.request()).thenReturn(request); Response<Object> response = awaitResponse(Middlewares.authenticator(authenticator) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testAuthValidatorForPut() throws Exception { RequestContext requestContext = mock(RequestContext.class); Request request = Request.forUri("/", "PUT") .withPayload(ByteString.encodeUtf8("hello")); when(requestContext.request()).thenReturn(request); Response<Object> response = awaitResponse(Middlewares.authenticator(authenticator) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.UNAUTHORIZED))); }