Changed willMessage and password in CONNECT message from String to byte[] (fix issue #94 and #95)

This commit is contained in:
andsel
2015-09-16 23:25:15 +02:00
parent 9a7ec7fd62
commit e08cf95135
18 changed files with 58 additions and 49 deletions
@@ -57,7 +57,7 @@ public class InterceptConnectMessage extends InterceptAbstractMessage {
return msg.getUsername();
}
public String getPassword() {
public byte[] getPassword() {
return msg.getPassword();
}
@@ -65,7 +65,7 @@ public class InterceptConnectMessage extends InterceptAbstractMessage {
return msg.getWillTopic();
}
public String getWillMessage() {
public byte[] getWillMessage() {
return msg.getWillMessage();
}
}
@@ -177,7 +177,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
//handle user authentication
if (msg.isUserFlag()) {
String pwd = null;
byte[] pwd = null;
if (msg.isPasswordFlag()) {
pwd = msg.getPassword();
} else if (!this.allowAnonymous) {
@@ -225,7 +225,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
//Handle will flag
if (msg.isWillFlag()) {
AbstractMessage.QOSType willQos = AbstractMessage.QOSType.values()[msg.getWillQos()];
byte[] willPayload = msg.getWillMessage().getBytes();
byte[] willPayload = msg.getWillMessage();
ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(willPayload.length).put(willPayload).flip();
//save the will testament in the clientID store
WillMessage will = new WillMessage(msg.getWillTopic(), bb, msg.isWillRetain(),willQos );
@@ -4,7 +4,7 @@ package org.eclipse.moquette.spi.impl.security;
* Created by andrea on 8/23/14.
*/
public class AcceptAllAuthenticator implements IAuthenticator {
public boolean checkValid(String username, String password) {
public boolean checkValid(String username, byte[] password) {
return true;
}
}
@@ -106,7 +106,7 @@ public class FileAuthenticator implements IAuthenticator {
}
}
public boolean checkValid(String username, String password) {
public boolean checkValid(String username, byte[] password) {
if (username == null || password == null) {
LOG.info("username or password was null");
return false;
@@ -115,11 +115,7 @@ public class FileAuthenticator implements IAuthenticator {
if (foundPwq == null) {
return false;
}
try {
m_digest.update(password.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
m_digest.update(password);
byte[] digest = m_digest.digest();
String encodedPasswd = new String(Hex.encodeHex(digest));
return foundPwq.equals(encodedPasswd);
@@ -22,5 +22,5 @@ package org.eclipse.moquette.spi.impl.security;
*/
public interface IAuthenticator {
boolean checkValid(String username, String password);
boolean checkValid(String username, byte[] password);
}
@@ -79,7 +79,7 @@ public class ConfigurationClassLoaderTest implements IAuthenticator, IAuthorizat
}
@Override
public boolean checkValid(String username, String password) {
public boolean checkValid(String username, byte[] password) {
return true;
}
@@ -111,7 +111,7 @@ public class ServerLowlevelMessagesIntegrationTests {
connectMessage.setClientID("FAKECLNT");
connectMessage.setKeepAlive(keepAlive);
connectMessage.setWillFlag(true);
connectMessage.setWillMessage(willTestamentMsg);
connectMessage.setWillMessage(willTestamentMsg.getBytes());
connectMessage.setWillTopic(willTestamentTopic);
connectMessage.setWillQos((byte) QOSType.MOST_ONE.ordinal());
@@ -25,13 +25,13 @@ import org.eclipse.moquette.spi.impl.security.IAuthenticator;
*/
class MockAuthenticator implements IAuthenticator {
private Map<String, String> m_userPwds;
private Map<String, byte[]> m_userPwds;
MockAuthenticator(Map<String, String> userPwds) {
MockAuthenticator(Map<String, byte[]> userPwds) {
m_userPwds = userPwds;
}
public boolean checkValid(String username, String password) {
public boolean checkValid(String username, byte[] password) {
if (!m_userPwds.containsKey(username)) {
return false;
}
@@ -52,9 +52,9 @@ public class ProtocolProcessorTest {
final static String BAD_FORMATTED_TOPIC = "#MQTTClient";
final static String TEST_USER = "fakeuser";
final static String TEST_PWD = "fakepwd";
final static byte[] TEST_PWD = "fakepwd".getBytes();
final static String EVIL_TEST_USER = "eviluser";
final static String EVIL_TEST_PWD = "unsecret";
final static byte[] EVIL_TEST_PWD = "unsecret".getBytes();
final static List<InterceptHandler> EMPTY_OBSERVERS = Collections.emptyList();
final static BrokerInterceptor NO_OBSERVERS_INTERCEPTOR = new BrokerInterceptor(EMPTY_OBSERVERS);
@@ -164,7 +164,7 @@ public class ProtocolProcessorTest {
m_sessionStore = memStorage;
//m_storageService.initStore();
Map<String, String> users = new HashMap<>();
Map<String, byte[]> users = new HashMap<>();
users.put(TEST_USER, TEST_PWD);
m_mockAuthenticator = new MockAuthenticator(users);
@@ -202,7 +202,7 @@ public class ProtocolProcessorTest {
connMsg.setClientID("123");
connMsg.setWillFlag(true);
connMsg.setWillTopic("topic");
connMsg.setWillMessage("Topic message");
connMsg.setWillMessage("Topic message".getBytes());
//Exercise
@@ -31,7 +31,7 @@ public class FileAuthenticatorTest {
String file = getClass().getResource("/password_file.conf").getPath();
IAuthenticator auth = new FileAuthenticator(null, file);
assertTrue(auth.checkValid("testuser", "passwd"));
assertTrue(auth.checkValid("testuser", "passwd".getBytes()));
}
@Test
@@ -39,14 +39,14 @@ public class FileAuthenticatorTest {
String file = getClass().getResource("/password_file.conf").getPath();
IAuthenticator auth = new FileAuthenticator(null, file);
assertFalse(auth.checkValid("testuser2", "passwd"));
assertFalse(auth.checkValid("testuser2", "passwd".getBytes()));
}
@Test
public void loadPasswordFile_verifyDirectoryRef() {
IAuthenticator auth = new FileAuthenticator("", "");
assertFalse(auth.checkValid("testuser2", "passwd"));
assertFalse(auth.checkValid("testuser2", "passwd".getBytes()));
}
}