Changeset - c9cd2218b172
[Not reviewed]
0 3 0
Jan Kaluza - 9 years ago 2016-01-24 13:04:32
jkaluza@redhat.com
Slack: get the bot_access_token when requesting the token and use it to connect RTM
3 files changed with 21 insertions and 3 deletions:
0 comments (0 inline, 0 general)
include/transport/OAuth2.h
Show inline comments
 
@@ -39,7 +39,7 @@ class OAuth2 {
 
			return m_state;
 
		}
 

	
 
		std::string requestToken(const std::string &code, std::string &error);
 
		std::string requestToken(const std::string &code, std::string &token, std::string &bot_token);
 

	
 
	private:
 
		std::string m_clientId;
libtransport/OAuth2.cpp
Show inline comments
 
@@ -71,7 +71,7 @@ std::string OAuth2::generateAuthURL() {
 
	return url;
 
}
 

	
 
std::string OAuth2::requestToken(const std::string &code, std::string &token) {
 
std::string OAuth2::requestToken(const std::string &code, std::string &token, std::string &bot_token) {
 
	std::string url = m_tokenURL + "?";
 
	url += "client_id=" + Util::urlencode(m_clientId);
 
	url += "&client_secret=" + Util::urlencode(m_clientSecret);
 
@@ -105,6 +105,14 @@ std::string OAuth2::requestToken(const std::string &code, std::string &token) {
 
		return "Empty 'access_token' object in the reply.";
 
	}
 

	
 
	rapidjson::Value& bot = resp["bot"];
 
	if (bot.IsObject()) {
 
		rapidjson::Value& bot_access_token = resp["bot_access_token"];
 
		if (!bot_access_token.IsString()) {
 
			bot_token = bot_access_token.GetString();
 
		}
 
	}
 

	
 
	return "";
 
}
 

	
spectrum/src/frontends/slack/SlackUserRegistration.cpp
Show inline comments
 
@@ -110,10 +110,12 @@ std::string SlackUserRegistration::getTeamDomain(const std::string &token) {
 
std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, const std::string &state) {
 
	OAuth2 *oauth2 = NULL;
 
	std::string token;
 
	std::string access_token;
 
	std::vector<std::string> data;
 

	
 
	if (state == "use_bot_token") {
 
		token = code;
 
		access_token = code;
 
	}
 
	else {
 
		if (m_auths.find(state) != m_auths.end()) {
 
@@ -124,10 +126,15 @@ std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, con
 
			return "Received state code '" + state + "' not found in state codes list.";
 
		}
 

	
 
		std::string error = oauth2->requestToken(code, token);
 
		std::string error = oauth2->requestToken(code, access_token, token);
 
		if (!error.empty())  {
 
			return error;
 
		}
 

	
 
		if (token.empty()) {
 
			LOG4CXX_INFO(logger, "Using 'token' as 'bot_access_token'");
 
			token = access_token;
 
		}
 
	}
 

	
 
	std::string domain = getTeamDomain(token);
 
@@ -154,6 +161,9 @@ std::string SlackUserRegistration::handleOAuth2Code(const std::string &code, con
 
	int type = (int) TYPE_STRING;
 
	m_storageBackend->getUserSetting(user.id, "bot_token", type, value);
 

	
 
	value = access_token;
 
	m_storageBackend->getUserSetting(user.id, "access_token", type, value);
 

	
 
	LOG4CXX_INFO(logger, "Registered Slack user " << user.jid);
 

	
 
	if (oauth2) {
0 comments (0 inline, 0 general)