Skip to content

Commit

Permalink
Merge pull request #345 from rpseng/master
Browse files Browse the repository at this point in the history
Fix for the default password max length (as per the 1.6 spec) and other minor changes
  • Loading branch information
TVolden authored Apr 28, 2024
2 parents 6c636a2 + fb5d0fb commit 088b78c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OCPP-J/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

dependencies {
compile project(':common')
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.google.code.gson:gson:2.8.9'
compile 'org.java-websocket:Java-WebSocket:1.5.3'
testCompile 'junit:junit:4.13.2'
testCompile 'org.mockito:mockito-core:4.11.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public class JSONConfiguration {

private JSONConfiguration() {}

private static final JSONConfiguration instance = new JSONConfiguration();

public static JSONConfiguration get() {
return new JSONConfiguration();
return instance;
}

public <T> JSONConfiguration setParameter(String name, T value) {
Expand Down
10 changes: 5 additions & 5 deletions OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class WebSocketListener implements Listener {
private static final int TIMEOUT_IN_MILLIS = 10000;

private static final int OCPPJ_CP_MIN_PASSWORD_LENGTH = 16;
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 20;
private static final int OCPPJ_CP_MAX_PASSWORD_LENGTH = 40;

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

Expand Down Expand Up @@ -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")) {
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,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")) {
Expand All @@ -178,21 +178,21 @@ 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 (protocolVersion == null || protocolVersion == ProtocolVersion.OCPP1_6) {
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");
} else {
if (password == null
|| password.length < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
|| password.length > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
|| password.length() < configuration.getParameter(JSONConfiguration.OCPP2J_CP_MIN_PASSWORD_LENGTH, OCPP2J_CP_MIN_PASSWORD_LENGTH)
|| password.length() > configuration.getParameter(JSONConfiguration.OCPP2J_CP_MAX_PASSWORD_LENGTH, OCPP2J_CP_MAX_PASSWORD_LENGTH))
throw new InvalidDataException(401, "Invalid password length");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void started() throws Exception {
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) {
Expand Down

0 comments on commit 088b78c

Please sign in to comment.