Skip to content

Commit

Permalink
Log authentication exceptions in debug mode
Browse files Browse the repository at this point in the history
Signed-off-by: Arjan Tijms <arjan.tijms@gmail.com>
  • Loading branch information
arjantijms committed May 25, 2022
1 parent b403f7c commit 28fe14c
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import static org.apache.catalina.Globals.WRAPPED_RESPONSE;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
Expand Down Expand Up @@ -154,6 +155,7 @@ public class RealmAdapter extends RealmBase implements RealmInitializer, PostCon

private static final Logger _logger = LogDomains.getLogger(RealmAdapter.class, WEB_LOGGER);
private static final ResourceBundle resourceBundle = _logger.getResourceBundle();
private static final boolean debug = isDebug();

private static final String SERVER_AUTH_CONTEXT = "__jakarta.security.auth.message.ServerAuthContext";
private static final String MESSAGE_INFO = "__jakarta.security.auth.message.MessageInfo";
Expand Down Expand Up @@ -1346,14 +1348,10 @@ private boolean validate(HttpRequest request, HttpResponse response, LoginConfig
httpServletRequest.setAttribute(MESSAGE_INFO, messageInfo);
}
} catch (AuthException ae) {
if (_logger.isLoggable(FINE)) {
_logger.log(FINE, "Jakarta Authentication: http msg authentication fail", ae);
}
_logger.log(debug? SEVERE : FINE, "Jakarta Authentication: http msg authentication fail", ae);
httpServletResponse.setStatus(SC_INTERNAL_SERVER_ERROR);
} catch (RuntimeException e) {
if (_logger.isLoggable(FINE)) {
_logger.log(FINE, "Jakarta Authentication: Exception during validateRequest", e);
}
_logger.log(debug? SEVERE : FINE, "Jakarta Authentication: Exception during validateRequest", e);
httpServletResponse.sendError(SC_INTERNAL_SERVER_ERROR);
}

Expand Down Expand Up @@ -1788,4 +1786,12 @@ private Key findDigestKey(DigestAlgorithmParameter[] digestParameters) {
throw new RuntimeException("No key found in parameters");
}

private static boolean isDebug() {
try {
return ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
} catch(Exception e) {
return false;
}
}

}

0 comments on commit 28fe14c

Please sign in to comment.