Slack: get the bot_access_token when requesting the token and use it to connect RTM

This commit is contained in:
Jan Kaluza 2016-01-24 13:04:32 +01:00
parent 2084556ded
commit c9cd2218b1
3 changed files with 21 additions and 3 deletions

View file

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

View file

@ -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 "";
}

View file

@ -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) {