Changeset - 0785badb98b0
[Not reviewed]
0 1 1
HanzZ - 14 years ago 2011-08-21 19:46:55
hanzz.k@gmail.com
UserRegistry refactorization
2 files changed with 130 insertions and 63 deletions:
0 comments (0 inline, 0 general)
include/transport/userregistry.h
Show inline comments
 
@@ -30,74 +30,18 @@ namespace Transport {
 
	
 
class UserRegistry : public Swift::UserRegistry {
 
	public:
 
		UserRegistry(Config *cfg) {config = cfg;}
 
		~UserRegistry() {}
 
		void isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password) {
 
			if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
 
				if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
 
					session->handlePasswordValid();
 
				}
 
				else {
 
					session->handlePasswordInvalid();
 
				}
 
				return;
 
			}
 
		UserRegistry(Config *cfg);
 
		virtual ~UserRegistry();
 

	
 
			std::string key = user.toBare().toString();
 
		void isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password);
 

	
 
			// Users try to connect twice
 
			if (users.find(key) != users.end()) {
 
				// Kill the first session if the second password is same
 
				if (Swift::safeByteArrayToString(password) == users[key].password) {
 
					Swift::ServerFromClientSession *tmp = users[key].session;
 
					users[key].session = session;
 
					tmp->handlePasswordInvalid();
 
				}
 
				else {
 
					session->handlePasswordInvalid();
 
					std::cout << "invalid " << session << "\n";
 
					return;
 
				}
 
			}
 
			std::cout << "adding " << session << "\n";
 
			users[key].password = Swift::safeByteArrayToString(password);
 
			users[key].session = session;
 
			onConnectUser(user);
 
		void stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session);
 

	
 
			return;
 
		}
 
		void onPasswordValid(const Swift::JID &user);
 

	
 
		void stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session) {
 
			std::cout << "stopping " << session << "\n";
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				if (users[key].session == session) {
 
					std::cout << "DISCONNECT USER\n";
 
					onDisconnectUser(user);
 
					users.erase(key);
 
				}
 
			}
 
		}
 
		void onPasswordInvalid(const Swift::JID &user);
 

	
 
		void onPasswordValid(const Swift::JID &user) {
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				users[key].session->handlePasswordValid();
 
				users.erase(key);
 
			}
 
		}
 

	
 
		void onPasswordInvalid(const Swift::JID &user) {
 
			std::string key = user.toBare().toString();
 
			if (users.find(key) != users.end()) {
 
				users[key].session->handlePasswordInvalid();
 
				users.erase(key);
 
			}
 
		}
 

	
 
		const std::string &getUserPassword(const std::string &barejid) {
 
			return users[barejid].password;
 
		}
 
		const std::string &getUserPassword(const std::string &barejid);
 

	
 
		boost::signal<void (const Swift::JID &user)> onConnectUser;
 
		boost::signal<void (const Swift::JID &user)> onDisconnectUser;
src/userregistry.cpp
Show inline comments
 
new file 100644
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/UserRegistry.h"
 
#include "transport/userregistry.h"
 
#include "log4cxx/logger.h"
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("UserRegistry");
 

	
 
UserRegistry::UserRegistry(Config *cfg) {
 
	config = cfg;
 
}
 

	
 
UserRegistry::~UserRegistry() { }
 

	
 
void UserRegistry::isValidUserPassword(const Swift::JID& user, Swift::ServerFromClientSession *session, const Swift::SafeByteArray& password) {
 
	if (!CONFIG_STRING(config, "service.admin_username").empty() && user.getNode() == CONFIG_STRING(config, "service.admin_username")) {
 
		if (Swift::safeByteArrayToString(password) == CONFIG_STRING(config, "service.admin_password")) {
 
			session->handlePasswordValid();
 
		}
 
		else {
 
			session->handlePasswordInvalid();
 
		}
 
		return;
 
	}
 

	
 
	std::string key = user.toBare().toString();
 

	
 
	// Users try to connect twice
 
	if (users.find(key) != users.end()) {
 
		// Kill the first session if the second password is same
 
		if (Swift::safeByteArrayToString(password) == users[key].password) {
 
			LOG4CXX_INFO(logger, key << ": Removing previous session and making this one active");
 
			Swift::ServerFromClientSession *tmp = users[key].session;
 
			users[key].session = session;
 
			tmp->handlePasswordInvalid();
 
		}
 
		else {
 
			LOG4CXX_INFO(logger, key << ": Possible break-in attemp (user logged as different one with bad password)");
 
			session->handlePasswordInvalid();
 
			return;
 
		}
 
	}
 

	
 
	LOG4CXX_INFO(logger, key << ": Connecting this user to find if password is valid");
 

	
 
	users[key].password = Swift::safeByteArrayToString(password);
 
	users[key].session = session;
 
	onConnectUser(user);
 

	
 
	return;
 
}
 

	
 
void UserRegistry::stopLogin(const Swift::JID& user, Swift::ServerFromClientSession *session) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		if (users[key].session == session) {
 
			LOG4CXX_INFO(logger, key << ": Stopping login process (user probably disconnected while logging in)");
 
			onDisconnectUser(user);
 
			users.erase(key);
 
		}
 
		else {
 
			LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in), but this is not active session");
 
		}
 
	}
 
	else {
 
		LOG4CXX_WARN(logger, key << ": Stopping login process (user probably disconnected while logging in) for invalid user");
 
	}
 
}
 

	
 
void UserRegistry::onPasswordValid(const Swift::JID &user) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		LOG4CXX_INFO(logger, key << ": Password is valid");
 
		users[key].session->handlePasswordValid();
 
		users.erase(key);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, key << ": onPasswordValid called for invalid user");
 
	}
 
}
 

	
 
void UserRegistry::onPasswordInvalid(const Swift::JID &user) {
 
	std::string key = user.toBare().toString();
 
	if (users.find(key) != users.end()) {
 
		LOG4CXX_INFO(logger, key << ": Password is invalid");
 
		users[key].session->handlePasswordInvalid();
 
		users.erase(key);
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, key << ": onPasswordInvalid called for invalid user");
 
	}
 
}
 

	
 
const std::string &UserRegistry::getUserPassword(const std::string &barejid) {
 
	return users[barejid].password;
 
}
 

	
 
}
0 comments (0 inline, 0 general)