Skip to content

Commit ab63b7f

Browse files
authored
Merge pull request #219 from askogvold/basic_auth
Supporting basic authorization for websocket
2 parents 191b155 + 80f7df4 commit ab63b7f

3 files changed

Lines changed: 52 additions & 45 deletions

File tree

org.eclipse.paho.client.mqttv3.test/src/test/java/org/eclipse/paho/client/mqttv3/test/WebSocketTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,27 @@ public void largePayloadTest() throws Exception{
247247

248248
}
249249

250+
@Test
251+
public void testBasicAuth() throws Exception {
252+
String methodName = Utility.getMethodName();
253+
LoggingUtilities.banner(log, cclass, methodName);
250254

255+
String userInfo = "username:password";
251256

257+
String clientId = methodName;
258+
259+
URI serverURIWithUserInfo = new URI(serverURI.getScheme(),
260+
userInfo,
261+
serverURI.getHost(),
262+
serverURI.getPort(),
263+
serverURI.getPath(),
264+
serverURI.getQuery(),
265+
serverURI.getFragment());
266+
267+
IMqttClient client = clientFactory.createMqttClient(serverURIWithUserInfo, clientId);
268+
client.connect();
269+
client.disconnect();
270+
}
252271

253272
// -------------------------------------------------------------
254273
// Helper methods/classes

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
package org.eclipse.paho.client.mqttv3;
2323

24+
import java.net.URI;
25+
import java.net.URISyntaxException;
2426
import java.util.Hashtable;
2527
import java.util.Properties;
2628
import java.util.Timer;
@@ -358,7 +360,7 @@ protected NetworkModule[] createNetworkModules(String address, MqttConnectOption
358360
* supplied address URI.
359361
*
360362
* @param address the URI for the server.
361-
* @param Connect options
363+
* @param options Connect options
362364
* @return a network module appropriate to the specified address.
363365
*/
364366
private NetworkModule createNetworkModule(String address, MqttConnectOptions options) throws MqttException, MqttSecurityException {
@@ -367,18 +369,25 @@ private NetworkModule createNetworkModule(String address, MqttConnectOptions opt
367369
log.fine(CLASS_NAME,methodName, "115", new Object[] {address});
368370

369371
NetworkModule netModule;
370-
String shortAddress;
371-
String host;
372-
int port;
373372
SocketFactory factory = options.getSocketFactory();
374373

375374
int serverURIType = MqttConnectOptions.validateURI(address);
376375

376+
URI uri;
377+
try {
378+
uri = new URI(address);
379+
} catch (URISyntaxException e) {
380+
throw new IllegalArgumentException("Malformed URI: " + address, e);
381+
}
382+
383+
String host = uri.getHost();
384+
int port = uri.getPort(); // -1 if not defined
385+
377386
switch (serverURIType) {
378387
case MqttConnectOptions.URI_TYPE_TCP :
379-
shortAddress = address.substring(6);
380-
host = getHostName(shortAddress);
381-
port = getPort(shortAddress, 1883);
388+
if (port == -1) {
389+
port = 1883;
390+
}
382391
if (factory == null) {
383392
factory = SocketFactory.getDefault();
384393
}
@@ -389,9 +398,9 @@ else if (factory instanceof SSLSocketFactory) {
389398
((TCPNetworkModule)netModule).setConnectTimeout(options.getConnectionTimeout());
390399
break;
391400
case MqttConnectOptions.URI_TYPE_SSL:
392-
shortAddress = address.substring(6);
393-
host = getHostName(shortAddress);
394-
port = getPort(shortAddress, 8883);
401+
if (port == -1) {
402+
port = 8883;
403+
}
395404
SSLSocketFactoryFactory factoryFactory = null;
396405
if (factory == null) {
397406
// try {
@@ -421,9 +430,9 @@ else if ((factory instanceof SSLSocketFactory) == false) {
421430
}
422431
break;
423432
case MqttConnectOptions.URI_TYPE_WS:
424-
shortAddress = address.substring(5);
425-
host = getHostName(shortAddress);
426-
port = getPort(shortAddress, 80);
433+
if (port == -1) {
434+
port = 80;
435+
}
427436
if (factory == null) {
428437
factory = SocketFactory.getDefault();
429438
}
@@ -434,9 +443,9 @@ else if (factory instanceof SSLSocketFactory) {
434443
((WebSocketNetworkModule)netModule).setConnectTimeout(options.getConnectionTimeout());
435444
break;
436445
case MqttConnectOptions.URI_TYPE_WSS:
437-
shortAddress = address.substring(6);
438-
host = getHostName(shortAddress);
439-
port = getPort(shortAddress, 443);
446+
if (port == -1) {
447+
port = 443;
448+
}
440449
SSLSocketFactoryFactory wSSFactoryFactory = null;
441450
if (factory == null) {
442451
wSSFactoryFactory = new SSLSocketFactoryFactory();
@@ -471,33 +480,6 @@ else if ((factory instanceof SSLSocketFactory) == false) {
471480
return netModule;
472481
}
473482

474-
private int getPort(String uri, int defaultPort) {
475-
int port;
476-
int portIndex = uri.lastIndexOf(':');
477-
if (portIndex == -1) {
478-
port = defaultPort;
479-
}
480-
else {
481-
int slashIndex = uri.indexOf('/');
482-
if (slashIndex == -1) {
483-
slashIndex = uri.length();
484-
}
485-
port = Integer.parseInt(uri.substring(portIndex + 1, slashIndex));
486-
}
487-
return port;
488-
}
489-
490-
private String getHostName(String uri) {
491-
int portIndex = uri.indexOf(':');
492-
if (portIndex == -1) {
493-
portIndex = uri.indexOf('/');
494-
}
495-
if (portIndex == -1) {
496-
portIndex = uri.length();
497-
}
498-
return uri.substring(0, portIndex);
499-
}
500-
501483
/* (non-Javadoc)
502484
* @see org.eclipse.paho.client.mqttv3.IMqttAsyncClient#connect(java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)
503485
*/

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketHandshake.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void execute() throws IOException {
7777
/**
7878
* Builds and sends the HTTP Header GET Request
7979
* for the socket.
80-
* @param Base64 encoded key
80+
* @param key Base64 encoded key
8181
* @throws IOException
8282
*/
8383
private void sendHandshakeRequest(String key) throws IOException{
@@ -99,6 +99,12 @@ private void sendHandshakeRequest(String key) throws IOException{
9999
pw.print("Sec-WebSocket-Key: " + key + LINE_SEPARATOR);
100100
pw.print("Sec-WebSocket-Protocol: mqttv3.1" + LINE_SEPARATOR);
101101
pw.print("Sec-WebSocket-Version: 13" + LINE_SEPARATOR);
102+
103+
String userInfo = srvUri.getUserInfo();
104+
if(userInfo != null) {
105+
pw.print("Authorization: Basic " + Base64.encode(userInfo) + LINE_SEPARATOR);
106+
}
107+
102108
pw.print(LINE_SEPARATOR);
103109
pw.flush();
104110
} catch (URISyntaxException e) {
@@ -108,7 +114,7 @@ private void sendHandshakeRequest(String key) throws IOException{
108114

109115
/**
110116
* Receives the Handshake response and verifies that it is valid.
111-
* @param Base64 encoded key
117+
* @param key Base64 encoded key
112118
* @throws IOException
113119
*/
114120
private void receiveHandshakeResponse(String key) throws IOException {

0 commit comments

Comments
 (0)