From cb6af593b6a6716d650fcd04981ecc456d3a6560 2012-09-13 10:13:44 From: HanzZ Date: 2012-09-13 10:13:44 Subject: [PATCH] registration.needRegistration works --- diff --git a/include/transport/networkplugin.h b/include/transport/networkplugin.h index f54269a0a233cbffd57f77f35f3a053afad71a3e..b565f43a837cc65f4aaf6e90af9a6a52da2ab976 100644 --- a/include/transport/networkplugin.h +++ b/include/transport/networkplugin.h @@ -37,14 +37,16 @@ class NetworkPlugin { class PluginConfig { public: - PluginConfig() : m_needPassword(true) {} + PluginConfig() : m_needPassword(true), m_needRegistration(false) {} virtual ~PluginConfig() {} + void setNeedRegistration(bool needRegistration = false) { m_needRegistration = needRegistration; } void setNeedPassword(bool needPassword = true) { m_needPassword = needPassword; } void setExtraFields(const std::vector &fields) { m_extraFields = fields; } private: bool m_needPassword; + bool m_needRegistration; std::vector m_extraFields; friend class NetworkPlugin; diff --git a/plugin/cpp/networkplugin.cpp b/plugin/cpp/networkplugin.cpp index 4455c37ed8629caf0a055920b492246dfd4ec49a..6b289cd5f70fbb74b35043c4fd44a75de7a53f38 100644 --- a/plugin/cpp/networkplugin.cpp +++ b/plugin/cpp/networkplugin.cpp @@ -65,6 +65,7 @@ NetworkPlugin::~NetworkPlugin() { void NetworkPlugin::sendConfig(const PluginConfig &cfg) { std::string data = "[registration]"; data += std::string("needPassword=") + (cfg.m_needPassword ? "1" : "0") + "\n"; + data += std::string("needRegistration=") + (cfg.m_needRegistration ? "1" : "0") + "\n"; for (std::vector::const_iterator it = cfg.m_extraFields.begin(); it != cfg.m_extraFields.end(); it++) { data += std::string("extraField=") + (*it) + "\n"; diff --git a/src/config.cpp b/src/config.cpp index 2f57b168148e26c8a5c90400c5bcfa2e45a5378a..432fb86f6f2e010bbf0b689ed04f02867e0c9c09 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -288,6 +288,7 @@ void Config::updateBackendConfig(const std::string &backendConfig) { options_description opts("Backend options"); opts.add_options() ("registration.needPassword", value()->default_value(true), "") + ("registration.needRegistration", value()->default_value(false), "") ("registration.extraField", value >()->multitoken(), "") ; diff --git a/src/tests/usermanager.cpp b/src/tests/usermanager.cpp index 5ae281a0d17c435badf6f604756169444cae6621..80a78cf7fbf7102410594765460c31848bb80e23 100644 --- a/src/tests/usermanager.cpp +++ b/src/tests/usermanager.cpp @@ -24,6 +24,8 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_TEST_SUITE(UserManagerTest); CPPUNIT_TEST(connectUser); CPPUNIT_TEST(connectUserTransportDisabled); + CPPUNIT_TEST(connectUserRegistrationNeeded); + CPPUNIT_TEST(connectUserRegistrationNeededRegistered); CPPUNIT_TEST(handleProbePresence); CPPUNIT_TEST(disconnectUser); CPPUNIT_TEST_SUITE_END(); @@ -49,6 +51,24 @@ class UserManagerTest : public CPPUNIT_NS :: TestFixture, public BasicTest { CPPUNIT_ASSERT(!user); } + void connectUserRegistrationNeeded() { + cfg->updateBackendConfig("[registration]\nneedRegistration=1\n"); + CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); + userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password")); + loop->processEvents(); + CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); + CPPUNIT_ASSERT(streamEnded); + } + + void connectUserRegistrationNeededRegistered() { + addUser(); + cfg->updateBackendConfig("[registration]\nneedRegistration=1\n"); + CPPUNIT_ASSERT_EQUAL(0, userManager->getUserCount()); + userRegistry->isValidUserPassword(Swift::JID("user@localhost/resource"), serverFromClientSession.get(), Swift::createSafeByteArray("password")); + loop->processEvents(); + CPPUNIT_ASSERT_EQUAL(1, userManager->getUserCount()); + CPPUNIT_ASSERT(!streamEnded); + } void handleProbePresence() { Swift::Presence::ref response = Swift::Presence::create(); diff --git a/src/usermanager.cpp b/src/usermanager.cpp index c381fe1d28e79566504b9db58a4fc83f176b6164..9fb0db6d90ddba9233476e2a129fee522bc36958 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -200,11 +200,16 @@ void UserManager::handlePresence(Swift::Presence::ref presence) { UserInfo res; bool registered = m_storageBackend ? m_storageBackend->getUser(userkey, res) : false; - // In server mode, there's no registration, but we store users into database - // (if storagebackend is available) because of caching. Passwords are not stored - // in server mode. + // In server mode, we don't need registration normally, but for networks like IRC + // or Twitter where there's no real authorization using password, we have to force + // registration otherwise some data (like bookmarked rooms) could leak. if (m_component->inServerMode()) { if (!registered) { + // If we need registration, stop login process because user is not registered + if (CONFIG_BOOL_DEFAULTED(m_component->getConfig(), "registration.needRegistration", false)) { + m_userRegistry->onPasswordInvalid(presence->getFrom()); + return; + } res.password = ""; res.uin = presence->getFrom().getNode(); res.jid = userkey;