Skip to content

Commit

Permalink
Add support for HTTP Basic Authentication
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robert-s-ubi committed Oct 2, 2020
1 parent 3b41c63 commit 72f6ca3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class JSONConfiguration {
public static final String REUSE_ADDR_PARAMETER = "REUSE_ADDR";
public static final String PROXY_PARAMETER = "PROXY";
public static final String PING_INTERVAL_PARAMETER = "PING_INTERVAL";
public static final String USERNAME_PARAMETER = "USERNAME";
public static final String PASSWORD_PARAMETER = "PASSWORD";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ of this software and associated documentation files (the "Software"), to deal
import java.net.ConnectException;
import java.net.Proxy;
import java.net.URI;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
Expand Down Expand Up @@ -63,8 +67,17 @@ public WebSocketTransmitter(Draft draft) {
public void connect(String uri, RadioEvents events) {
final URI resource = URI.create(uri);

Map<String,String> httpHeaders = new HashMap<>();
String username = configuration.getParameter(JSONConfiguration.USERNAME_PARAMETER);
String password = configuration.getParameter(JSONConfiguration.PASSWORD_PARAMETER);
if (username != null && password != null) {
String credentials = username + ":" + password;
byte[] base64Credentials = Base64.getEncoder().encode(credentials.getBytes());
httpHeaders.put("Authorization", "Basic " + new String(base64Credentials));
}

client =
new WebSocketClient(resource, draft) {
new WebSocketClient(resource, draft, httpHeaders) {
@Override
public void onOpen(ServerHandshake serverHandshake) {
logger.debug("On connection open (HTTP status: {})", serverHandshake.getHttpStatus());
Expand Down

0 comments on commit 72f6ca3

Please sign in to comment.