Skip to content

Commit 4e91e2f

Browse files
Send authentication error in body
1 parent 6b02012 commit 4e91e2f

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Diff for: http-server/src/main/java/com/facebook/airlift/http/server/AuthenticationFilter.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import java.io.IOException;
3131
import java.io.InputStream;
32+
import java.io.PrintWriter;
3233
import java.security.Principal;
3334
import java.util.LinkedHashSet;
3435
import java.util.List;
@@ -37,6 +38,7 @@
3738
import static com.google.common.io.ByteStreams.copy;
3839
import static com.google.common.io.ByteStreams.nullOutputStream;
3940
import static com.google.common.net.HttpHeaders.WWW_AUTHENTICATE;
41+
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
4042
import static java.util.Objects.requireNonNull;
4143
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED;
4244

@@ -102,7 +104,18 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
102104
if (messages.isEmpty()) {
103105
messages.add("Unauthorized");
104106
}
105-
response.sendError(SC_UNAUTHORIZED, Joiner.on(" | ").join(messages));
107+
// The error string is used by clients for exception messages and
108+
// is presented to the end user, thus it should be a single line.
109+
String error = Joiner.on(" | ").join(messages);
110+
111+
// Clients should use the response body rather than the HTTP status
112+
// message (which does not exist with HTTP/2), but the status message
113+
// still needs to be sent for compatibility with existing clients.
114+
response.setStatus(SC_UNAUTHORIZED, error);
115+
response.setContentType(PLAIN_TEXT_UTF_8.toString());
116+
try (PrintWriter writer = response.getWriter()) {
117+
writer.write(error);
118+
}
106119
}
107120

108121
private static ServletRequest withPrincipal(HttpServletRequest request, Principal principal)

0 commit comments

Comments
 (0)