This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit dcf48fb74276bde9828faa310be4d4ec3fce916f Author: Mark Thomas <ma...@apache.org> AuthorDate: Fri Sep 9 08:56:40 2022 +0100 Refactor and use i18n for exception messages --- java/org/apache/tomcat/websocket/Authenticator.java | 18 ++++++++++++++++++ .../apache/tomcat/websocket/BasicAuthenticator.java | 6 ++---- .../apache/tomcat/websocket/DigestAuthenticator.java | 6 ++---- .../apache/tomcat/websocket/LocalStrings.properties | 3 +++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/java/org/apache/tomcat/websocket/Authenticator.java b/java/org/apache/tomcat/websocket/Authenticator.java index 4ff78281d1..30787205cb 100644 --- a/java/org/apache/tomcat/websocket/Authenticator.java +++ b/java/org/apache/tomcat/websocket/Authenticator.java @@ -21,11 +21,15 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.tomcat.util.res.StringManager; + /** * Base class for the authentication methods used by the WebSocket client. */ public abstract class Authenticator { + private static final StringManager sm = StringManager.getManager(Authenticator.class); + private static final Pattern pattern = Pattern.compile("(\\w+)\\s*=\\s*(\"([^\"]+)\"|([^,=\"]+))\\s*,?"); @@ -91,4 +95,18 @@ public abstract class Authenticator { return parameterMap; } + + + protected void validateUsername(String userName) throws AuthenticationException { + if (userName == null) { + throw new AuthenticationException(sm.getString("authenticator.nullUserName")); + } + } + + + protected void validatePassword(String password) throws AuthenticationException { + if (password == null) { + throw new AuthenticationException(sm.getString("authenticator.nullPassword")); + } + } } diff --git a/java/org/apache/tomcat/websocket/BasicAuthenticator.java b/java/org/apache/tomcat/websocket/BasicAuthenticator.java index 68dcccdd5f..63b0a3d0e5 100644 --- a/java/org/apache/tomcat/websocket/BasicAuthenticator.java +++ b/java/org/apache/tomcat/websocket/BasicAuthenticator.java @@ -36,10 +36,8 @@ public class BasicAuthenticator extends Authenticator { String userName = (String) userProperties.get(Constants.WS_AUTHENTICATION_USER_NAME); String userPassword = (String) userProperties.get(Constants.WS_AUTHENTICATION_PASSWORD); - if (userName == null || userPassword == null) { - throw new AuthenticationException( - "Failed to perform Basic authentication due to missing user/password"); - } + validateUsername(userName); + validatePassword(userPassword); Map<String, String> parameterMap = parseAuthenticateHeader(authenticateHeader); diff --git a/java/org/apache/tomcat/websocket/DigestAuthenticator.java b/java/org/apache/tomcat/websocket/DigestAuthenticator.java index fef0106013..6552001c1c 100644 --- a/java/org/apache/tomcat/websocket/DigestAuthenticator.java +++ b/java/org/apache/tomcat/websocket/DigestAuthenticator.java @@ -42,10 +42,8 @@ public class DigestAuthenticator extends Authenticator { String userName = (String) userProperties.get(Constants.WS_AUTHENTICATION_USER_NAME); String userPassword = (String) userProperties.get(Constants.WS_AUTHENTICATION_PASSWORD); - if (userName == null || userPassword == null) { - throw new AuthenticationException( - "Failed to perform Digest authentication due to missing user/password"); - } + validateUsername(userName); + validatePassword(userPassword); Map<String, String> parameterMap = parseAuthenticateHeader(authenticateHeader); diff --git a/java/org/apache/tomcat/websocket/LocalStrings.properties b/java/org/apache/tomcat/websocket/LocalStrings.properties index c755121bb3..c86a75efab 100644 --- a/java/org/apache/tomcat/websocket/LocalStrings.properties +++ b/java/org/apache/tomcat/websocket/LocalStrings.properties @@ -29,6 +29,9 @@ asyncChannelWrapperSecure.unexpectedHandshakeState=Unexpected state [{0}] during asyncChannelWrapperSecure.wrongStateRead=Flag that indicates a read is in progress was found to be false (it should have been true) when trying to complete a read operation asyncChannelWrapperSecure.wrongStateWrite=Flag that indicates a write is in progress was found to be false (it should have been true) when trying to complete a write operation +authenticator.nullPassword=No password was provided to use for authentication +authenticator.nullUserName=No user name was provided to use for authentication + backgroundProcessManager.processFailed=A background process failed caseInsensitiveKeyMap.nullKey=Null keys are not permitted --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org