Changeset - 8c1c1970130e
[Not reviewed]
0 4 0
Jan Kaluza - 10 years ago 2016-01-07 21:08:38
jkaluza@redhat.com
Spectrum2 manager server: handle OAuth2
4 files changed with 22 insertions and 9 deletions:
0 comments (0 inline, 0 general)
libtransport/AdminInterface.cpp
Show inline comments
 
@@ -304,35 +304,30 @@ void AdminInterface::handleQuery(Swift::Message::ref message) {
 
			std::string error = m_component->getFrontend()->setOAuth2Code(args[1], args[2]);
 
			if (error.empty()) {
 
				message->setBody("OAuth2 code and state set.");
 
			}
 
			else {
 
				message->setBody(error);
 
			}
 
		}
 
		else {
 
			message->setBody("Bad argument count. See 'help'.");
 
		}
 
	}
 
	else if (message->getBody().find("get_oauth2_url ") == 0) {
 
	else if (message->getBody().find("get_oauth2_url") == 0) {
 
		std::string body = message->getBody();
 
		std::vector<std::string> args;
 
		boost::split(args, body, boost::is_any_of(" "));
 
		if (args.size() == 3) {
 
			std::string url = m_component->getFrontend()->getOAuth2URL(args);
 
			message->setBody(url);
 
		}
 
		else {
 
			message->setBody("Bad argument count. See 'help'.");
 
		}
 
		std::string url = m_component->getFrontend()->getOAuth2URL(args);
 
		message->setBody(url);
 
	}
 
	else if (message->getBody() == "registration_fields") {
 
		std::string fields = m_component->getFrontend()->getRegistrationFields();
 
		message->setBody(fields);
 
	}
 
	else if (message->getBody().find("help") == 0) {
 
		std::string help;
 
		help += "General:\n";
 
		help += "    status - shows instance status\n";
 
		help += "    reload - Reloads config file\n";
 
		help += "    uptime - returns ptime in seconds\n";
 
		help += "Users:\n";
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -53,25 +53,25 @@ SlackUserRegistration::SlackUserRegistration(Component *component, UserManager *
 
	m_component = component;
 
	m_config = m_component->getConfig();
 
	m_storageBackend = storageBackend;
 
	m_userManager = userManager;
 

	
 
}
 

	
 
SlackUserRegistration::~SlackUserRegistration(){
 
	
 
}
 

	
 
std::string SlackUserRegistration::createOAuth2URL(const std::vector<std::string> &args) {
 
	std::string redirect_url = "http://slack.spectrum.im/auth/" + CONFIG_STRING(m_config, "service.jid");
 
	std::string redirect_url = "http://slack.spectrum.im/oauth2/" + CONFIG_STRING(m_config, "service.jid");
 
	OAuth2 *oauth2 = new OAuth2(CONFIG_STRING_DEFAULTED(m_config, "service.client_id",""),
 
						  CONFIG_STRING_DEFAULTED(m_config, "service.client_secret",""),
 
						  "https://slack.com/oauth/authorize",
 
						  "https://slack.com/api/oauth.access",
 
						  redirect_url,
 
						  "channels:read channels:write team:read im:read im:write chat:write:bot");
 
	std::string url = oauth2->generateAuthURL();
 

	
 
	m_auths[oauth2->getState()] = oauth2;
 
	m_authsData[oauth2->getState()] = args;
 

	
 
	return url;
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -7,24 +7,28 @@
 
#include <string.h>
 
#include <time.h>
 
#include <stdarg.h>
 
#include <pthread.h>
 
#include <fstream>
 
#include <string>
 
#include <cerrno>
 

	
 
#define SESSION_TTL 120
 

	
 
static struct mg_serve_http_opts s_http_server_opts;
 

	
 
static int has_prefix(const struct mg_str *uri, const char *prefix) {
 
	size_t prefix_len = strlen(prefix);
 
	return uri->len >= prefix_len && memcmp(uri->p, prefix, prefix_len) == 0;
 
}
 

	
 
static std::string get_http_var(const struct http_message *hm, const char *name) {
 
	char data[4096];
 
	data[0] = '\0';
 

	
 
	mg_get_http_var(&hm->body, name, data, sizeof(data));
 
	if (data[0] != '\0') {
 
		return data;
 
	}
 

	
 
	mg_get_http_var(&hm->query_string, name, data, sizeof(data));
 
	if (data[0] != '\0') {
 
@@ -542,24 +546,31 @@ void Server::serve_instances_register(struct mg_connection *conn, struct http_me
 
			</label> \
 
			<input type=\"hidden\" name=\"instance\" value=\"" + instance + "\"></input> \
 
			</form><br/>";
 
		print_html(conn, hm, html);
 
	}
 
	else {
 
		std::string response = send_command(instance, "register " + jid + " " + uin + " " + password);
 
		if (!response.empty()) {
 
			std::string value = jid;
 
			int type = (int) TYPE_STRING;
 
			m_storage->updateUserSetting(info.id, instance, value);
 
		}
 

	
 
		response = send_command(instance, "get_oauth2_url " + jid);
 
		if (!response.empty()) {
 
			redirect_to(conn, hm, response.c_str());
 
			return;
 
		}
 

	
 
		redirect_to(conn, hm, "/instances");
 
	}
 

	
 
}
 

	
 
void Server::serve_instances(struct mg_connection *conn, struct http_message *hm) {
 
	std::string jid = get_http_var(hm, "jid");
 
	if (!jid.empty()) {
 
		serve_instance(conn, hm, jid);
 
		return;
 
	}
 

	
 
@@ -640,24 +651,28 @@ void Server::serve_instances(struct mg_connection *conn, struct http_message *hm
 
					html += "<td>No available action</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_HTTP_REQUEST) {
 
		return;
 
	}
 

	
 
	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, "/") == 0) {
 
@@ -675,22 +690,24 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	} else if (mg_vcmp(&hm->uri, "/instances/stop") == 0) {
 
		serve_instances_stop(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/register") == 0) {
 
		serve_instances_register(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/unregister") == 0) {
 
		serve_instances_unregister(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") == 0) {
 
		serve_oauth2(conn, hm);
 
	} else {
 
		mg_serve_http(conn, hm, s_http_server_opts);
 
	}
 

	
 
	conn->flags |= MG_F_SEND_AND_CLOSE;
 
}
 

	
 

	
 

	
 

	
spectrum_manager/src/server.h
Show inline comments
 
@@ -63,24 +63,25 @@ class Server {
 
		void serve_instance(struct mg_connection *conn, struct http_message *hm, const std::string &jid);
 
		void serve_instances(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_start(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_stop(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_register(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_unregister(struct mg_connection *conn, struct http_message *hm);
 
		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_onlineusers(struct mg_connection *conn, struct http_message *hm);
 
		void serve_cmd(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);
 
		std::string send_command(const std::string &jid, const std::string &cmd);
 

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

	
0 comments (0 inline, 0 general)