Skip to content

Commit 72f6ca3

Browse files
committed
Add support for HTTP Basic Authentication
This change adds support for HTTP Basic Authentication to the OCPP-J client, via two additional JSONConfiguration parameters for username and password. When both are set, the HTTP Basic Authentication header will be added to the websocket connect request. Note that HTTP Basic Authentication transmits username and password without encryption, so for secure communication, it must be used within a trusted channel, e.g. a secure websocket connection (WSS) or maybe an encrypted VPN connection. This change has originally been prepared by our former colleague: rtzll@8652108 and has successfully been tested in a plugfest.
1 parent 3b41c63 commit 72f6ca3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class JSONConfiguration {
3434
public static final String REUSE_ADDR_PARAMETER = "REUSE_ADDR";
3535
public static final String PROXY_PARAMETER = "PROXY";
3636
public static final String PING_INTERVAL_PARAMETER = "PING_INTERVAL";
37+
public static final String USERNAME_PARAMETER = "USERNAME";
38+
public static final String PASSWORD_PARAMETER = "PASSWORD";
3739

3840
private final HashMap<String, Object> parameters = new HashMap<>();
3941

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ of this software and associated documentation files (the "Software"), to deal
3131
import java.net.ConnectException;
3232
import java.net.Proxy;
3333
import java.net.URI;
34+
import java.util.Base64;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
3438
import org.java_websocket.client.WebSocketClient;
3539
import org.java_websocket.drafts.Draft;
3640
import org.java_websocket.exceptions.WebsocketNotConnectedException;
@@ -63,8 +67,17 @@ public WebSocketTransmitter(Draft draft) {
6367
public void connect(String uri, RadioEvents events) {
6468
final URI resource = URI.create(uri);
6569

70+
Map<String,String> httpHeaders = new HashMap<>();
71+
String username = configuration.getParameter(JSONConfiguration.USERNAME_PARAMETER);
72+
String password = configuration.getParameter(JSONConfiguration.PASSWORD_PARAMETER);
73+
if (username != null && password != null) {
74+
String credentials = username + ":" + password;
75+
byte[] base64Credentials = Base64.getEncoder().encode(credentials.getBytes());
76+
httpHeaders.put("Authorization", "Basic " + new String(base64Credentials));
77+
}
78+
6679
client =
67-
new WebSocketClient(resource, draft) {
80+
new WebSocketClient(resource, draft, httpHeaders) {
6881
@Override
6982
public void onOpen(ServerHandshake serverHandshake) {
7083
logger.debug("On connection open (HTTP status: {})", serverHandshake.getHttpStatus());

0 commit comments

Comments
 (0)