From cf20205e849a258ee5f4893a1d916ef8091f12d1 Mon Sep 17 00:00:00 2001 From: rpseng Date: Tue, 23 Apr 2024 22:12:00 -0300 Subject: [PATCH] Fix password decoding. --- .../main/java/eu/chargetime/ocpp/WebSocketListener.java | 8 ++++---- .../src/main/java/eu/chargetime/ocpp/ListenerEvents.java | 2 +- ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java | 2 +- .../src/main/java/eu/chargetime/ocpp/ServerEvents.java | 2 +- .../main/java/eu/chargetime/ocpp/test/DummyHandlers.java | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java b/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java index 346936cdb..9f026cbbd 100644 --- a/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java +++ b/OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java @@ -146,7 +146,7 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( .build(); String username = null; - byte[] password = null; + String password = null; if (clientHandshake.hasFieldValue("Authorization")) { String authorization = clientHandshake.getFieldValue("Authorization"); if (authorization != null && authorization.toLowerCase().startsWith("basic")) { @@ -159,15 +159,15 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( username = new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8); if (i + 1 < credDecoded.length) { - password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length); + password = new String(Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length)); } break; } } } if (password == null - || password.length < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH) - || password.length > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH)) + || password.length() < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH) + || password.length() > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH)) throw new InvalidDataException(401, "Invalid password length"); } diff --git a/ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java b/ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java index 3e8be78d0..a40da3fab 100644 --- a/ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java +++ b/ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java @@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal import eu.chargetime.ocpp.model.SessionInformation; public interface ListenerEvents { - void authenticateSession(SessionInformation information, String username, byte[] password) + void authenticateSession(SessionInformation information, String username, String password) throws AuthenticationException; void newSession(ISession session, SessionInformation information); diff --git a/ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java b/ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java index d62abe934..d80171161 100644 --- a/ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java +++ b/ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java @@ -81,7 +81,7 @@ public void open(String hostname, int port, ServerEvents serverEvents) { @Override public void authenticateSession( - SessionInformation information, String username, byte[] password) + SessionInformation information, String username, String password) throws AuthenticationException { serverEvents.authenticateSession(information, username, password); } diff --git a/ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java b/ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java index 2e66f0f30..593778d7d 100644 --- a/ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java +++ b/ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java @@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal import java.util.UUID; public interface ServerEvents { - void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException; + void authenticateSession(SessionInformation information, String username, String password) throws AuthenticationException; void newSession(UUID sessionIndex, SessionInformation information); diff --git a/ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/DummyHandlers.java b/ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/DummyHandlers.java index 0ec7e8509..7db86d059 100644 --- a/ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/DummyHandlers.java +++ b/ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/DummyHandlers.java @@ -203,7 +203,7 @@ public ServerEvents generateServerEventsHandler() { return new ServerEvents() { @Override public void authenticateSession( - SessionInformation information, String username, byte[] password) throws AuthenticationException {} + SessionInformation information, String username, String password) throws AuthenticationException {} @Override public void newSession(UUID sessionIndex, SessionInformation information) {