Added handling of allow_anonymous, fix issue #34

This commit is contained in:
andrea
2015-04-25 19:49:41 +02:00
parent 966e6399f6
commit 9d8e9df44e
9 changed files with 85 additions and 18 deletions
@@ -59,6 +59,7 @@ class ConfigurationParser {
m_properties.put(WEB_SOCKET_PORT_PROPERTY_NAME, Integer.toString(WEBSOCKET_PORT));
m_properties.put(PASSWORD_FILE_PROPERTY_NAME, "");
m_properties.put(PERSISTENT_STORE_PROPERTY_NAME, DEFAULT_PERSISTENT_PATH);
m_properties.put(ALLOW_ANONYMOUS, true);
}
/**
@@ -97,6 +97,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
private Map<String, ConnectionDescriptor> m_clientIDs = new HashMap<>();
private SubscriptionsStore subscriptions;
private boolean allowAnonymous;
private IMessagesStore m_messagesStore;
private ISessionsStore m_sessionsStore;
private IAuthenticator m_authenticator;
@@ -114,13 +115,16 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
* @param storageService the persistent store to use for save/load of messages
* for QoS1 and QoS2 handling.
* @param sessionsStore the clients sessions store, used to persist subscriptions.
* @param authenticator the authenticator used in connect messages
* @param authenticator the authenticator used in connect messages.
* @param allowAnonymous true connection to clients without credentials.
*/
void init(SubscriptionsStore subscriptions, IMessagesStore storageService,
ISessionsStore sessionsStore,
IAuthenticator authenticator) {
IAuthenticator authenticator,
boolean allowAnonymous) {
//m_clientIDs = clientIDs;
this.subscriptions = subscriptions;
this.allowAnonymous = allowAnonymous;
LOG.debug("subscription tree on init {}", subscriptions.dumpTree());
m_authenticator = authenticator;
m_messagesStore = storageService;
@@ -199,13 +203,17 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
String pwd = null;
if (msg.isPasswordFlag()) {
pwd = msg.getPassword();
}
if (!m_authenticator.checkValid(msg.getUsername(), pwd)) {
ConnAckMessage okResp = new ConnAckMessage();
okResp.setReturnCode(ConnAckMessage.BAD_USERNAME_OR_PASSWORD);
session.write(okResp);
} else if (!this.allowAnonymous) {
failedCredetials(session);
return;
}
if (!m_authenticator.checkValid(msg.getUsername(), pwd)) {
failedCredetials(session);
return;
}
} else if (!this.allowAnonymous) {
failedCredetials(session);
return;
}
subscriptions.activate(msg.getClientID());
@@ -233,6 +241,12 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
}
}
private void failedCredetials(ServerChannel session) {
ConnAckMessage okResp = new ConnAckMessage();
okResp.setReturnCode(ConnAckMessage.BAD_USERNAME_OR_PASSWORD);
session.write(okResp);
}
/**
* Republish QoS1 and QoS2 messages stored into the session for the clientID.
* */
@@ -42,6 +42,7 @@ import java.util.concurrent.TimeUnit;
import static org.eclipse.moquette.commons.Constants.PASSWORD_FILE_PROPERTY_NAME;
import static org.eclipse.moquette.commons.Constants.PERSISTENT_STORE_PROPERTY_NAME;
import static org.eclipse.moquette.commons.Constants.ALLOW_ANONYMOUS;
/**
*
@@ -187,7 +188,7 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
//List<Subscription> storedSubscriptions = m_sessionsStore.listAllSubscriptions();
//subscriptions.init(storedSubscriptions);
subscriptions.init(m_sessionsStore);
String passwdPath = props.getProperty(PASSWORD_FILE_PROPERTY_NAME, "");
String configPath = System.getProperty("moquette.path", null);
IAuthenticator authenticator;
@@ -196,8 +197,9 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
} else {
authenticator = new FileAuthenticator(configPath, passwdPath);
}
m_processor.init(subscriptions, m_storageService, m_sessionsStore, authenticator);
boolean allowAnonymous = Boolean.parseBoolean(props.getProperty(ALLOW_ANONYMOUS, "true"));
m_processor.init(subscriptions, m_storageService, m_sessionsStore, authenticator, allowAnonymous);
}