Changeset - 6fc502c3c9c4
[Not reviewed]
0 2 0
Jan Kaluza - 10 years ago 2016-01-24 12:04:14
jkaluza@redhat.com
Slack: redirect to https instead of http
2 files changed with 13 insertions and 1 deletions:
0 comments (0 inline, 0 general)
libtransport/OAuth2.cpp
Show inline comments
 
@@ -39,61 +39,73 @@ namespace Transport {
 
DEFINE_LOGGER(logger, "OAuth2");
 

	
 
OAuth2::OAuth2(const std::string &clientId, const std::string &clientSecret,
 
			   const std::string &authURL, const std::string &tokenURL,
 
			   const std::string &redirectURL, const std::string &scope)
 
			: m_clientId(clientId), m_clientSecret(clientSecret),
 
			  m_authURL(authURL), m_tokenURL(tokenURL),
 
			  m_redirectURL(redirectURL), m_scope(scope) {
 

	
 
}
 

	
 
OAuth2::~OAuth2() {
 
	
 
}
 

	
 
std::string OAuth2::generateAuthURL() {
 
	std::string url = m_authURL + "?";
 

	
 
	url += "client_id=" + Util::urlencode(m_clientId);
 

	
 
	if (!m_scope.empty()) {
 
		url += "&scope=" + Util::urlencode(m_scope);
 
	}
 

	
 
	if (!m_redirectURL.empty()) {
 
		url += "&redirect_uri=" + Util::urlencode(m_redirectURL);
 
	}
 

	
 
	boost::uuids::uuid uuid = boost::uuids::random_generator()();
 
	m_state = boost::lexical_cast<std::string>(uuid);
 
	url += "&state=" + Util::urlencode(m_state);
 

	
 
	return url;
 
}
 

	
 
std::string OAuth2::requestToken(const std::string &code, std::string &token) {
 
	std::string url = m_tokenURL + "?";
 
	url += "client_id=" + Util::urlencode(m_clientId);
 
	url += "&client_secret=" + Util::urlencode(m_clientSecret);
 
	url += "&code=" + Util::urlencode(code);
 

	
 
	if (!m_redirectURL.empty()) {
 
		url += "&redirect_uri=" + Util::urlencode(m_redirectURL);
 
	}
 

	
 
	rapidjson::Document resp;
 
	HTTPRequest req(HTTPRequest::Get, url);
 
	if (!req.execute(resp)) {
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getError());
 
		return req.getError();
 
	}
 

	
 
	rapidjson::Value& access_token = resp["access_token"];
 
	if (!access_token.IsString()) {
 
		LOG4CXX_ERROR(logger, "No 'access_token' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "No 'access_token' object in the reply.";
 
	}
 

	
 
	token = access_token.GetString();
 
	if (token.empty()) {
 
		LOG4CXX_ERROR(logger, "Empty 'access_token' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "Empty 'access_token' object in the reply.";
 
	}
 

	
 
	return "";
 
}
 

	
 
}
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -17,97 +17,97 @@
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "SlackUserRegistration.h"
 
#include "SlackRosterManager.h"
 
#include "SlackFrontend.h"
 

	
 
#include "transport/UserManager.h"
 
#include "transport/StorageBackend.h"
 
#include "transport/Transport.h"
 
#include "transport/RosterManager.h"
 
#include "transport/User.h"
 
#include "transport/Logging.h"
 
#include "transport/Buddy.h"
 
#include "transport/Config.h"
 
#include "transport/OAuth2.h"
 
#include "transport/Util.h"
 
#include "transport/HTTPRequest.h"
 

	
 
#include "rapidjson/document.h"
 

	
 
#include <boost/shared_ptr.hpp>
 
#include <boost/thread.hpp>
 
#include <boost/date_time/posix_time/posix_time.hpp>
 
#include <boost/regex.hpp> 
 

	
 
using namespace Swift;
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SlackUserRegistration");
 

	
 
SlackUserRegistration::SlackUserRegistration(Component *component, UserManager *userManager,
 
								   StorageBackend *storageBackend)
 
: UserRegistration(component, userManager, storageBackend) {
 
	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/oauth2/" + CONFIG_STRING(m_config, "service.jid");
 
	std::string redirect_url = "https://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;
 
}
 

	
 
std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
	std::string url = "https://slack.com/api/team.info?token=" + Util::urlencode(token);
 

	
 
	rapidjson::Document resp;
 
	HTTPRequest req(HTTPRequest::Get, url);
 
	if (!req.execute(resp)) {
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getError());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &team = resp["team"];
 
	if (!team.IsObject()) {
 
		LOG4CXX_ERROR(logger, "No 'team' object in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	rapidjson::Value &domain = team["domain"];
 
	if (!domain.IsString()) {
 
		LOG4CXX_ERROR(logger, "No 'domain' string in the reply.");
 
		LOG4CXX_ERROR(logger, url);
 
		LOG4CXX_ERROR(logger, req.getRawData());
 
		return "";
 
	}
 

	
 
	return domain.GetString();
 
}
 

	
 
std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, const std::string &state) {
 
	OAuth2 *oauth2 = NULL;
 
	std::string token;
 
	std::vector<std::string> data;
0 comments (0 inline, 0 general)