Skip to content

Commit

Permalink
WIP: Fix for #59.
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Jan 21, 2024
1 parent c7c02be commit d02380f
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public IWindowsIdentity doFilter(final HttpServletRequest request, final HttpSer
if (securityContext.isContinue()) {
response.setHeader("Connection", "keep-alive");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
final String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,10 @@ public String getOutputText() {
this.writer.flush();
return this.bytes.toString(StandardCharsets.UTF_8);
}

@Override
public void setContentLength(int len) {
setHeader("Content-Length", Integer.toString(len));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void testChallengePOST() throws IOException, ServletException {
this.filter.doFilter(request, response, null);
Assertions.assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));
Assertions.assertEquals("keep-alive", response.getHeader("Connection"));
Assertions.assertEquals(2, response.getHeaderNamesSize());
Assertions.assertEquals(3, response.getHeaderNamesSize());
Assertions.assertEquals(401, response.getStatus());
} finally {
if (clientContext != null) {
Expand Down Expand Up @@ -205,10 +205,26 @@ void testNegotiate() throws IOException, ServletException {
break;
}

Assertions.assertEquals(401, response.getStatus());

// security package requested is one negotiate continues with
Assertions.assertTrue(response.getHeader("WWW-Authenticate").startsWith(securityPackage + " "));

// keep-alive, NTLM is a connection-oriented protocol
Assertions.assertEquals("keep-alive", response.getHeader("Connection"));
Assertions.assertEquals(2, response.getHeaderNamesSize());
Assertions.assertEquals(401, response.getStatus());

// Connection: keep-alive
// WWW-Authenticate: ...
// Content-Length: ...
Assertions.assertEquals(3, response.getHeaderNamesSize());

// response has a body and a content length (.NET clients require this)
int contentLength = Integer.parseInt(response.getHeader("Content-Length"));
Assertions.assertTrue(contentLength > 0);
String content = response.getOutputText();
Assertions.assertEquals(contentLength, content.length());

// continue token
final String continueToken = response.getHeader("WWW-Authenticate")
.substring(securityPackage.length() + 1);
final byte[] continueTokenBytes = Base64.getDecoder().decode(continueToken);
Expand Down Expand Up @@ -285,7 +301,7 @@ void testChallengeNTLMPOST() throws IOException, ServletException {
final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
Assertions.assertEquals(1, wwwAuthenticates.length);
Assertions.assertTrue(wwwAuthenticates[0].startsWith("NTLM "));
Assertions.assertEquals(2, response.getHeaderNamesSize());
Assertions.assertEquals(3, response.getHeaderNamesSize());
Assertions.assertEquals("keep-alive", response.getHeader("Connection"));
Assertions.assertEquals(401, response.getStatus());
}
Expand Down Expand Up @@ -314,7 +330,7 @@ void testChallengeNTLMPUT() throws IOException, ServletException {
final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
Assertions.assertEquals(1, wwwAuthenticates.length);
Assertions.assertTrue(wwwAuthenticates[0].startsWith("NTLM "));
Assertions.assertEquals(2, response.getHeaderNamesSize());
Assertions.assertEquals(3, response.getHeaderNamesSize());
Assertions.assertEquals("keep-alive", response.getHeader("Connection"));
Assertions.assertEquals(401, response.getStatus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ private boolean negotiate(final Request request, final HttpServletResponse respo
try {
if (securityContext.isContinue() || ntlmPost) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public boolean authenticate(final Request request, final HttpServletResponse res
try {
if (securityContext.isContinue()) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
final String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ private boolean negotiate(final Request request, final HttpServletResponse respo
try {
if (securityContext.isContinue() || ntlmPost) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public boolean authenticate(final Request request, final HttpServletResponse res
try {
if (securityContext.isContinue()) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
final String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ private boolean negotiate(final Request request, final HttpServletResponse respo
try {
if (securityContext.isContinue() || ntlmPost) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public boolean authenticate(final Request request, final HttpServletResponse res
try {
if (securityContext.isContinue()) {
response.setHeader("Connection", "keep-alive");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
final String body = "Unauthorized";
response.getWriter().write(body);
response.setContentLength(body.length());
response.flushBuffer();
return false;
}
Expand Down

0 comments on commit d02380f

Please sign in to comment.