diff --git a/libtransport/AdminInterface.cpp b/libtransport/AdminInterface.cpp index ad171006..03a82a7c 100644 --- a/libtransport/AdminInterface.cpp +++ b/libtransport/AdminInterface.cpp @@ -313,17 +313,12 @@ void AdminInterface::handleQuery(Swift::Message::ref message) { 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 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(); diff --git a/spectrum/src/frontends/slack/SlackUserRegistration.cpp b/spectrum/src/frontends/slack/SlackUserRegistration.cpp index bb96f6dd..007c259c 100644 --- a/spectrum/src/frontends/slack/SlackUserRegistration.cpp +++ b/spectrum/src/frontends/slack/SlackUserRegistration.cpp @@ -62,7 +62,7 @@ SlackUserRegistration::~SlackUserRegistration(){ } std::string SlackUserRegistration::createOAuth2URL(const std::vector &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", diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index d57eda4e..073a3359 100644 --- a/spectrum_manager/src/server.cpp +++ b/spectrum_manager/src/server.cpp @@ -16,6 +16,10 @@ 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]; @@ -551,6 +555,13 @@ void Server::serve_instances_register(struct mg_connection *conn, struct http_me 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"); } @@ -649,6 +660,10 @@ void Server::serve_instances(struct mg_connection *conn, struct http_message *hm 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; @@ -684,6 +699,8 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) { 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); } diff --git a/spectrum_manager/src/server.h b/spectrum_manager/src/server.h index 5c1066d9..f4fa17f9 100644 --- a/spectrum_manager/src/server.h +++ b/spectrum_manager/src/server.h @@ -72,6 +72,7 @@ class Server { 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);