Changeset - 7ca6c5d88f3f
[Not reviewed]
0 1 0
Jan Kaluza - 10 years ago 2016-01-22 13:54:59
jkaluza@redhat.com
Web interface: Allow registering new user accounts also to regular clients
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -137,194 +137,194 @@ bool Server::start() {
 
		mg_mgr_poll(&m_mgr, 1000);
 
	}
 

	
 
	return true;
 
}
 

	
 
bool Server::check_password(const std::string &user, const std::string &password) {
 
	if (m_user == user && m_password == password) {
 
		return true;
 
	}
 

	
 
	UserInfo info;
 
	if (m_storage && m_storage->getUser(user, info) == true && info.password == password) {
 
		return true;
 
	}
 

	
 
	return false;
 
}
 

	
 
// Allocate new session object
 
Server::session *Server::new_session(const std::string &user) {
 
	Server::session *session = new Server::session;
 

	
 
	my_strlcpy(session->user, user.c_str(), sizeof(session->user));
 
	snprintf(session->random, sizeof(session->random), "%d", rand());
 
	generate_session_id(session->session_id, session->random, session->user);
 
	session->expire = time(0) + SESSION_TTL;
 
	session->admin = std::string(user) == m_user;
 

	
 
	sessions[session->session_id] = session;
 
	return session;
 
}
 

	
 
// Get session object for the connection. Caller must hold the lock.
 
Server::session *Server::get_session(struct http_message *hm) {
 
	time_t now = time(NULL);
 
	char session_id[255];
 
	struct mg_str *hdr = mg_get_http_header(hm, "Cookie");
 
	int len = mg_http_parse_header(hdr, "session", session_id, sizeof(session_id));
 
	session_id[len] = 0;
 

	
 
	if (sessions.find(session_id) == sessions.end()) {
 
		return NULL;
 
	}
 

	
 
	if (sessions[session_id]->expire != 0 && sessions[session_id]->expire > now) {
 
		return sessions[session_id];
 
	}
 

	
 
	return NULL;
 
}
 

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

	
 
	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);
 
	}
 

	
 
	if (check_password(user, password) && (session = new_session(user)) != NULL) {
 
		std::cout << "User authorized\n";
 
		mg_printf(conn, "HTTP/1.1 302 Found\r\n"
 
			"Set-Cookie: session=%s; max-age=3600; http-only\r\n"  // Session ID
 
			"Set-Cookie: user=%s\r\n"  // Set user, needed by Javascript code
 
			"Set-Cookie: admin=%s\r\n"  // Set user, needed by Javascript code
 
			"Set-Cookie: base_location=%s\r\n"  // Set user, needed by Javascript code
 
			"Set-Cookie: original_url=/; max-age=0\r\n"  // Delete original_url
 
			"Location: %s%sinstances\r\n\r\n",
 
			session->session_id, session->user, session->admin ? "1" : "0", CONFIG_STRING(m_config, "service.base_location").c_str(), host.c_str(), CONFIG_STRING(m_config, "service.base_location").c_str());
 
	} else {
 
		// Authentication failure, redirect to login.
 
		redirect_to(conn, hm, "/login");
 
	}
 
}
 

	
 
bool Server::is_authorized(const struct mg_connection *conn, struct http_message *hm) {
 
	Server::session *session;
 
	char valid_id[33];
 
	bool authorized = false;
 

	
 
	// Always authorize accesses to login page and to authorize URI
 
	if (!mg_vcmp(&hm->uri, "/login") ||
 
		!mg_vcmp(&hm->uri, "/login/") ||
 
		!mg_vcmp(&hm->uri, "/form.css") ||
 
		!mg_vcmp(&hm->uri, "/style.css") ||
 
		!mg_vcmp(&hm->uri, "/logo.png") ||
 
		!mg_vcmp(&hm->uri, "/users") ||
 
		!mg_vcmp(&hm->uri, "/users/add") ||
 
		!mg_vcmp(&hm->uri, "/users/register.shtml") ||
 
		!mg_vcmp(&hm->uri, "/api/v1/users/add") ||
 
		!mg_vcmp(&hm->uri, "/authorize")) {
 
		return true;
 
	}
 

	
 
	if ((session = get_session(hm)) != NULL) {
 
		generate_session_id(valid_id, session->random, session->user);
 
		if (strcmp(valid_id, session->session_id) == 0) {
 
			session->expire = time(0) + SESSION_TTL;
 
			authorized = true;
 
		}
 
	}
 

	
 
	return authorized;
 
}
 

	
 
void Server::redirect_to(struct mg_connection *conn, struct http_message *hm, const char *where) {
 
	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);
 
	}
 

	
 
	where = where + 1;
 

	
 
	mg_printf(conn, "HTTP/1.1 302 Found\r\n"
 
		"Set-Cookie: original_url=/\r\n"
 
		"Location: %s%s%s\r\n\r\n", host.c_str(), CONFIG_STRING(m_config, "service.base_location").c_str(), where);
 
}
 

	
 
void Server::print_html(struct mg_connection *conn, struct http_message *hm, const std::string &html) {
 
	mg_printf(conn,
 
			"HTTP/1.1 200 OK\r\n"
 
			"Content-Type: text/html\r\n"
 
			"Content-Length: %d\r\n"        // Always set Content-Length
 
			"\r\n"
 
			"%s%s%s",
 
			(int) html.size() + m_header.size() + m_footer.size(), m_header.c_str(), html.c_str(), m_footer.c_str());
 
}
 

	
 
std::string Server::send_command(const std::string &jid, const std::string &cmd) {
 
	Swift::SimpleEventLoop eventLoop;
 
	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 

	
 
	ask_local_server(m_config, networkFactories, jid, cmd);
 
	struct timeval td_start,td_end;
 
	float elapsed = 0; 
 
	gettimeofday(&td_start, NULL);
 
	gettimeofday(&td_end, NULL);
 

	
 
	time_t started = time(NULL);
 
	while(get_response().empty() && td_end.tv_sec - td_start.tv_sec < 1) {
 
		gettimeofday(&td_end, NULL);
 
		eventLoop.runOnce();
 
	}
 

	
 
	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>";
0 comments (0 inline, 0 general)