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
 
@@ -185,98 +185,98 @@ Server::session *Server::get_session(struct http_message *hm) {
 

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