Skip to content

Commit 088b78c

Browse files
authored
Merge pull request #345 from rpseng/master
Fix for the default password max length (as per the 1.6 spec) and other minor changes
2 parents 6c636a2 + fb5d0fb commit 088b78c

File tree

9 files changed

+20
-18
lines changed

9 files changed

+20
-18
lines changed

OCPP-J/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
dependencies {
66
compile project(':common')
7-
compile 'com.google.code.gson:gson:2.8.0'
7+
compile 'com.google.code.gson:gson:2.8.9'
88
compile 'org.java-websocket:Java-WebSocket:1.5.3'
99
testCompile 'junit:junit:4.13.2'
1010
testCompile 'org.mockito:mockito-core:4.11.0'

OCPP-J/src/main/java/eu/chargetime/ocpp/JSONConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public class JSONConfiguration {
4949

5050
private JSONConfiguration() {}
5151

52+
private static final JSONConfiguration instance = new JSONConfiguration();
53+
5254
public static JSONConfiguration get() {
53-
return new JSONConfiguration();
55+
return instance;
5456
}
5557

5658
public <T> JSONConfiguration setParameter(String name, T value) {

OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class WebSocketListener implements Listener {
5151
private static final int TIMEOUT_IN_MILLIS = 10000;
5252

5353
private static final int OCPPJ_CP_MIN_PASSWORD_LENGTH = 16;
54-
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 20;
54+
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 40;
5555

5656
private static final String HTTP_HEADER_PROXIED_ADDRESS = "X-Forwarded-For";
5757

@@ -146,7 +146,7 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
146146
.build();
147147

148148
String username = null;
149-
byte[] password = null;
149+
String password = null;
150150
if (clientHandshake.hasFieldValue("Authorization")) {
151151
String authorization = clientHandshake.getFieldValue("Authorization");
152152
if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
@@ -159,15 +159,15 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
159159
username =
160160
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
161161
if (i + 1 < credDecoded.length) {
162-
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
162+
password = new String(Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length));
163163
}
164164
break;
165165
}
166166
}
167167
}
168168
if (password == null
169-
|| password.length < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
170-
|| password.length > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
169+
|| password.length() < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
170+
|| password.length() > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
171171
throw new InvalidDataException(401, "Invalid password length");
172172
}
173173

ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ of this software and associated documentation files (the "Software"), to deal
2828
import eu.chargetime.ocpp.model.SessionInformation;
2929

3030
public interface ListenerEvents {
31-
void authenticateSession(SessionInformation information, String username, byte[] password)
31+
void authenticateSession(SessionInformation information, String username, String password)
3232
throws AuthenticationException;
3333

3434
void newSession(ISession session, SessionInformation information);

ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void open(String hostname, int port, ServerEvents serverEvents) {
8181

8282
@Override
8383
public void authenticateSession(
84-
SessionInformation information, String username, byte[] password)
84+
SessionInformation information, String username, String password)
8585
throws AuthenticationException {
8686
serverEvents.authenticateSession(information, username, password);
8787
}

ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929
import java.util.UUID;
3030

3131
public interface ServerEvents {
32-
void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException;
32+
void authenticateSession(SessionInformation information, String username, String password) throws AuthenticationException;
3333

3434
void newSession(UUID sessionIndex, SessionInformation information);
3535

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/DummyHandlers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public ServerEvents generateServerEventsHandler() {
203203
return new ServerEvents() {
204204
@Override
205205
public void authenticateSession(
206-
SessionInformation information, String username, byte[] password) throws AuthenticationException {}
206+
SessionInformation information, String username, String password) throws AuthenticationException {}
207207

208208
@Override
209209
public void newSession(UUID sessionIndex, SessionInformation information) {

ocpp-v2/src/main/java/eu/chargetime/ocpp/MultiProtocolWebSocketListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
165165
.build();
166166

167167
String username = null;
168-
byte[] password = null;
168+
String password = null;
169169
if (clientHandshake.hasFieldValue("Authorization")) {
170170
String authorization = clientHandshake.getFieldValue("Authorization");
171171
if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
@@ -178,21 +178,21 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
178178
username =
179179
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
180180
if (i + 1 < credDecoded.length) {
181-
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
181+
password = new String(Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length));
182182
}
183183
break;
184184
}
185185
}
186186
}
187187
if (protocolVersion == null || protocolVersion == ProtocolVersion.OCPP1_6) {
188188
if (password == null
189-
|| password.length < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
190-
|| password.length > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
189+
|| password.length() < configuration.getParameter(JSONConfiguration.OCPPJ_CP_MIN_PASSWORD_LENGTH, OCPPJ_CP_MIN_PASSWORD_LENGTH)
190+
|| password.length() > configuration.getParameter(JSONConfiguration.OCPPJ_CP_MAX_PASSWORD_LENGTH, OCPPJ_CP_MAX_PASSWORD_LENGTH))
191191
throw new InvalidDataException(401, "Invalid password length");
192192
} else {
193193
if (password == null
194-
|| password.length < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
195-
|| password.length > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
194+
|| password.length() < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
195+
|| password.length() > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
196196
throw new InvalidDataException(401, "Invalid password length");
197197
}
198198
}

ocpp-v2_0-test/src/main/java/eu/chargetime/ocpp/test/FakeCentralSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void started() throws Exception {
7474
new ServerEvents() {
7575
@Override
7676
public void authenticateSession(
77-
SessionInformation information, String username, byte[] password) throws AuthenticationException {}
77+
SessionInformation information, String username, String password) throws AuthenticationException {}
7878

7979
@Override
8080
public void newSession(UUID sessionIndex, SessionInformation information) {

0 commit comments

Comments
 (0)