Middlewares { static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> httpLogger( RequestAuthenticator authenticator) { return httpLogger(LOG, authenticator); } 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 testHTTPLoggingForGet() throws Exception { RequestContext requestContext = mock(RequestContext.class); Request request = Request.forUri("/", "GET"); when(requestContext.request()).thenReturn(request); Response<Object> response = awaitResponse(Middlewares.httpLogger(authenticator) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testAuditLoggingForPut() 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.httpLogger(authenticator) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); assertThat(response, hasStatus(withCode(Status.OK))); }
@Test public void testHttpLoggerHidesAuthHeader() throws Exception { RequestContext requestContext = mock(RequestContext.class); Request request = Request.forUri("/", "PUT") .withPayload(ByteString.encodeUtf8("hello")) .withHeader(HttpHeaders.AUTHORIZATION, "Bearer s3cr3tp455w0rd"); when(requestContext.request()).thenReturn(request); String email = "foo@bar.net"; when(authenticator.authenticate(any())).thenReturn(() -> Optional.of(idToken)); when(idToken.getPayload()).thenReturn(idTokenPayload); when(idTokenPayload.getEmail()).thenReturn(email); awaitResponse(Middlewares.httpLogger(log, authenticator) .apply(mockInnerHandler(requestContext)) .invoke(requestContext)); verify(log).info("{}{} {} by {} with headers {} parameters {} and payload {}", "[AUDIT] ", request.method(), request.uri(), email, Map.of(HttpHeaders.AUTHORIZATION, "<hidden>"), Map.of(), request.payload().orElseThrow().utf8()); }