Skip to content

Commit 0e98525

Browse files
committed
Testing password lenbth to be between 16 and 20 bytes, charge point name to be string rather than byte[]
1 parent c00d273 commit 0e98525

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class WebSocketListener implements Listener {
5050

5151
private static final int TIMEOUT_IN_MILLIS = 10000;
5252

53-
private static final int OCPPJ_CP_PASSWORD_LENGTH = 20;
53+
private static final int OCPPJ_CP_MIN_PASSWORD_LENGTH = 16;
54+
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 20;
5455

5556
private final ISessionFactory sessionFactory;
5657
private final List<Draft> drafts;
@@ -124,7 +125,8 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket web
124125
.InternetAddress(webSocket.getRemoteSocketAddress())
125126
.build();
126127

127-
byte[] username = null, password = null;
128+
String username = null;
129+
byte[] password = null;
128130
if (clientHandshake.hasFieldValue("Authorization")) {
129131
String authorization = clientHandshake.getFieldValue("Authorization");
130132
if (authorization != null && authorization.toLowerCase().startsWith("basic")) {
@@ -134,15 +136,16 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket web
134136
// split credentials on username and password
135137
for (int i = 0; i < credDecoded.length; i++) {
136138
if (credDecoded[i] == ':') {
137-
username = Arrays.copyOfRange(credDecoded, 0, i);
139+
username = new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
138140
if (i != credDecoded.length - 1) {
139141
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
140142
}
141143
break;
142144
}
143145
}
144146
}
145-
if (password == null || password.length != OCPPJ_CP_PASSWORD_LENGTH) throw new InvalidDataException(401, "Invalid password length");
147+
if (password == null || password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH || password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
148+
throw new InvalidDataException(401, "Invalid password length");
146149
}
147150

148151
try {

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,6 +28,6 @@ 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, byte[] username, byte[] password) throws AuthenticationException;
31+
void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException;
3232
void newSession(ISession session, SessionInformation information);
3333
}

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

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

8585
@Override
86-
public void authenticateSession(SessionInformation information, byte[] username, byte[] password) throws AuthenticationException {
86+
public void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException {
8787
serverEvents.authenticateSession(information, username, password);
8888
}
8989

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-
default void authenticateSession(SessionInformation information, byte[] username, byte[] password) throws AuthenticationException {}
32+
default void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException {}
3333

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

0 commit comments

Comments
 (0)