Changeset - ed670fc5a939
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2016-01-21 12:47:52
jkaluza@redhat.com
Web interface: remove useless serve_users* methods from Server class
2 files changed with 0 insertions and 113 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -295,266 +295,156 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd)
 
	std::string response = get_response();
 
	if (response == "Empty response") {
 
		return "";
 
	}
 

	
 
	return response;
 
}
 

	
 
// void Server::serve_onlineusers(struct mg_connection *conn, struct http_message *hm) {
 
// 	Server:session *session = get_session(hm);
 
// 	if (!session->admin) {
 
// 		redirect_to(conn, hm, "/");
 
// 		return;
 
// 	}
 
// 
 
// 	std::string html;
 
// 	std::string jid = get_http_var(hm, "jid");
 
// 
 
// 	html += std::string("<h2>") + jid + " online users</h2><table><tr><th>JID<th>Command</th></tr>";
 
// 
 
// 	Swift::SimpleEventLoop eventLoop;
 
// 	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
// 
 
// 	ask_local_server(m_config, networkFactories, jid, "online_users");
 
// 	eventLoop.runUntilEvents();
 
// 	while(get_response().empty()) {
 
// 		eventLoop.runUntilEvents();
 
// 	}
 
// 
 
// 	std::string response = get_response();
 
// 	std::vector<std::string> users;
 
// 	boost::split(users, response, boost::is_any_of("\n"));
 
// 
 
// 	BOOST_FOREACH(std::string &user, users) {
 
// 		html += "<tr><td>" + user + "</td><td></td></tr>";
 
// 	}
 
// 
 
// 	html += "</table><a href=\"/\">Back to main page</a>";
 
// 	html += "</body></html>";
 
// 	print_html(conn, hm, html);
 
// }
 
// 
 
// void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) {
 
// 	Server:session *session = get_session(hm);
 
// 	if (!session->admin) {
 
// 		redirect_to(conn, hm, "/");
 
// 		return;
 
// 	}
 
// 
 
// 	std::string html;
 
// 	std::string jid = get_http_var(hm, "jid");
 
// 	std::string cmd = get_http_var(hm, "cmd");
 
// 
 
// 	html += std::string("<h2>") + jid + " command result</h2>";
 
// 
 
// 	Swift::SimpleEventLoop eventLoop;
 
// 	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 
// 
 
// 	ask_local_server(m_config, networkFactories, jid, cmd);
 
// 	while(get_response().empty()) {
 
// 		eventLoop.runUntilEvents();
 
// 	}
 
// 
 
// 	std::string response = get_response();
 
// 	
 
// 	html += "<pre>" + response + "</pre>";
 
// 
 
// 	html += "<a href=\"/\">Back to main page</a>";
 
// 	html += "</body></html>";
 
// 	print_html(conn, hm, html);
 
// }
 

	
 
void Server::serve_logout(struct mg_connection *conn, struct http_message *hm) {
 
	std::string host;
 
	mg_str *host_hdr = mg_get_http_header(hm, "Host");
 
	if (host_hdr) {
 
		if (!CONFIG_STRING(m_config, "service.cert").empty()) {
 
			host += "https://";
 
		}
 
		else {
 
			host += "http://";
 
		}
 
		host += std::string(host_hdr->p, host_hdr->len);
 
	}
 

	
 
	Server:session *session = get_session(hm);
 
	mg_printf(conn, "HTTP/1.1 302 Found\r\n"
 
		"Set-Cookie: session=%s; max-age=0\r\n"
 
		"Set-Cookie: admin=%s; max-age=0\r\n"
 
		"Location: %s%s\r\n\r\n",
 
		session->session_id, session->admin ? "1" : "0", host.c_str(), CONFIG_STRING(m_config, "service.base_location").c_str());
 

	
 
	sessions.erase(session->session_id);
 
	delete session;
 
}
 

	
 
void Server::serve_users_add(struct mg_connection *conn, struct http_message *hm) {
 
	std::string user = get_http_var(hm, "user");
 
	std::string password = get_http_var(hm, "password");
 

	
 
	if (!user.empty() && !password.empty()) {
 
		if (m_storage) {
 
			UserInfo dummy;
 
			bool registered = m_storage->getUser(user, dummy);
 
			if (!registered) {
 
				UserInfo info;
 
				info.jid = user;
 
				info.password = password;
 
				m_storage->setUser(info);
 
			}
 
			else {
 
				redirect_to(conn, hm, "/users?error=This+username+is+already+registered");
 
				return;
 
			}
 
		}
 
	}
 
	redirect_to(conn, hm, "/users?ok=1");
 
}
 

	
 
void Server::serve_users_remove(struct mg_connection *conn, struct http_message *hm) {
 
	Server:session *session = get_session(hm);
 
	if (!session->admin) {
 
		redirect_to(conn, hm, "/");
 
		return;
 
	}
 

	
 
	if (!m_storage) {
 
		return;
 
	}
 

	
 
	std::string user = get_http_var(hm, "user");
 
	UserInfo info;
 
	m_storage->getUser(user, info);
 
	m_storage->removeUser(info.id);
 
	redirect_to(conn, hm, "/users");
 
}
 

	
 
void Server::serve_users(struct mg_connection *conn, struct http_message *hm) {
 
	std::string html;
 
	Server:session *session = get_session(hm);
 
	if (!session) {
 
		std::string ok = get_http_var(hm, "ok");
 
		if (!ok.empty()) {
 
			redirect_to(conn, hm, "/");
 
			return;
 
		}
 
		html += "<h2>Register new Spectrum 2 master account</h2>";
 
	}
 
	else {
 
		html += "<h2>Spectrum 2 manager users</h2>";
 

	
 
		if (!session->admin) {
 
			html += "<p>Only Spectrum 2 manager administrator can access this page.</p>";
 
			print_html(conn, hm, html);
 
			return;
 
		}
 

	
 
		html += "<p>Here, you can add new users who will have access to this web interface. "
 
				"These users will be able to register new accounts on all Spectrum 2 instances "
 
				"running on these server. They won't be able to change any Spectrum 2 instance "
 
				"configuration influencing other users.</p>";
 
	}
 

	
 
	std::string error = get_http_var(hm, "error");
 
	if (!error.empty()) {
 
		html += "<p><b>Error: " + error +  "</b></p>";
 
	}
 

	
 
	if (!m_storage) {
 
		print_html(conn, hm, html);
 
		return;
 
	}
 

	
 
	html += "<form action=\"/users/add\" class=\"basic-grey\" method=\"POST\"> \
 
	<h1>Register user \
 
		<span>Register new user to Spectrum 2 manager web interface.</span> \
 
	</h1> \
 
	<label> \
 
		<span>Username:</span> \
 
		<input type=\"text\" id=\"user\" name=\"user\"placeholder=\"Username\"></textarea> \
 
	</label> \
 
	<label><span>Password:</span> \
 
		<input type=\"password\" id=\"password\" name=\"password\" placeholder=\"Password\"></textarea> \
 
	</label> \
 
	<label> \
 
		<span>&nbsp;</span> \
 
		<input type=\"submit\" class=\"button\" value=\"Add user\" />\
 
	</label> \
 
</form><br/>";
 
	std::vector<std::string> users;
 
	m_storage->getUsers(users);
 

	
 
	if (session) {
 
		html += "<table><tr><th>User<th>Action</th></tr>";
 
		BOOST_FOREACH(std::string &user, users) {
 
			html += "<tr>";
 
			html += "<td><a href=\"/users?jid=" + user + "\">" + user + "</a></td>";
 
			html += "<td><a href=\"/users/remove?user=" + user + "\">Remove</a></td>";
 
			html += "</tr>";
 
		}
 
		html += "</table>";
 
	}
 

	
 
	print_html(conn, hm, html);
 
}
 

	
 
void Server::serve_oauth2(struct mg_connection *conn, struct http_message *hm) {
 
	std::cout << "OAUTH2 handler\n";
 
}
 

	
 
void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	struct http_message *hm = (struct http_message *) p;
 

	
 
	if (ev == MG_EV_SSI_CALL) {
 
		mbuf_resize(&conn->send_mbuf, conn->send_mbuf.size * 2);
 
		std::string resp(conn->send_mbuf.buf, conn->send_mbuf.len);
 
		boost::replace_all(resp, "href=\"/", std::string("href=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		boost::replace_all(resp, "src=\"/", std::string("src=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		boost::replace_all(resp, "action=\"/", std::string("action=\"") + CONFIG_STRING(m_config, "service.base_location"));
 
		strcpy(conn->send_mbuf.buf, resp.c_str());
 
		mbuf_trim(&conn->send_mbuf);
 
		return;
 
	}
 

	
 
	if (ev != MG_EV_HTTP_REQUEST) {
 
		return;
 
	}
 

	
 
	hm->uri.p += CONFIG_STRING(m_config, "service.base_location").size() - 1;
 
	hm->uri.len -= CONFIG_STRING(m_config, "service.base_location").size() - 1;
 

	
 
	if (!is_authorized(conn, hm)) {
 
		redirect_to(conn, hm, "/login");
 
	} else if (mg_vcmp(&hm->uri, "/authorize") == 0) {
 
		authorize(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/logout") == 0) {
 
		serve_logout(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users") == 0) {
 
// 		serve_users(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users/add") == 0) {
 
// 		serve_users_add(conn, hm);
 
// 	} else if (mg_vcmp(&hm->uri, "/users/remove") == 0) {
 
// 		serve_users_remove(conn, hm);
 
	} else if (has_prefix(&hm->uri, "/oauth2")) {
 
		serve_oauth2(conn, hm);
 
	} else if (has_prefix(&hm->uri, "/api/v1/")) {
 
		m_apiServer->handleRequest(this, get_session(hm), conn, hm);
 
	} else {
 
		if (hm->uri.p[hm->uri.len - 1] != '/') {
 
			std::string url(hm->uri.p, hm->uri.len);
 
			if (url.find(".") == std::string::npos) {
 
				url += "/";
 
				redirect_to(conn, hm, url.c_str());
 
				conn->flags |= MG_F_SEND_AND_CLOSE;
 
				return;
 
			}
 
		}
 
		mg_serve_http(conn, hm, s_http_server_opts);
 
	}
 

	
 
	conn->flags |= MG_F_SEND_AND_CLOSE;
 
}
 

	
 

	
 

	
 

	
spectrum_manager/src/server.h
Show inline comments
 
/**
 
 * 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
 
 */
 

	
 
#pragma once
 

	
 
#include <boost/program_options.hpp>
 
#include <boost/foreach.hpp>
 
#include <boost/format.hpp>
 
#include <boost/algorithm/string.hpp>
 
#include <boost/assign.hpp>
 
#include <boost/bind.hpp>
 
#include <boost/signal.hpp>
 

	
 
#include "mongoose.h"
 
#include "managerconfig.h"
 

	
 
#include "transport/Config.h"
 
#include "transport/SQLite3Backend.h"
 
#include "transport/MySQLBackend.h"
 
#include "transport/PQXXBackend.h"
 
#include "transport/StorageBackend.h"
 

	
 
using namespace Transport;
 

	
 
class APIServer;
 

	
 
class Server {
 
	public:
 
		struct session {
 
			char session_id[33];      // Session ID, must be unique
 
			char random[20];          // Random data used for extra user validation
 
			char user[255];  // Authenticated user
 
			time_t expire;            // Expiration timestamp, UTC
 
			bool admin;
 
		};
 

	
 
		/// Constructor.
 
		Server(ManagerConfig *config, const std::string &config_file);
 

	
 
		/// Destructor
 
		virtual ~Server();
 

	
 
		bool start();
 

	
 
		void event_handler(struct mg_connection *nc, int ev, void *p);
 

	
 
		void redirect_to(struct mg_connection *conn, struct http_message *hm, const char *where);
 

	
 
		std::string send_command(const std::string &jid, const std::string &cmd);
 

	
 
	private:
 
		void serve_users(struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_add(struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_remove(struct mg_connection *conn, struct http_message *hm);
 
		void serve_logout(struct mg_connection *conn, struct http_message *hm);
 
		void serve_oauth2(struct mg_connection *conn, struct http_message *hm);
 
		void print_html(struct mg_connection *conn, struct http_message *hm, const std::string &html);
 

	
 
	private:
 
		bool check_password(const std::string &user, const std::string &password);
 
		session *new_session(const std::string &user);
 
		session *get_session(struct http_message *hm);
 

	
 
		void authorize(struct mg_connection *conn, struct http_message *hm);
 

	
 
		bool is_authorized(const struct mg_connection *conn, struct http_message *hm);
 

	
 
	private:
 
		struct mg_mgr m_mgr;
 
		struct mg_connection *m_nc;
 

	
 
		std::map<std::string, session *> sessions;
 
		std::string m_user;
 
		std::string m_password;
 
		ManagerConfig *m_config;
 
		std::string m_header;
 
		std::string m_footer;
 
		Config *m_storageCfg;
 
		StorageBackend *m_storage;
 
		APIServer *m_apiServer;
 
};
0 comments (0 inline, 0 general)