From c9cd2218b17274de9d74d2df5beff0eb30dbc293 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Sun, 24 Jan 2016 13:04:32 +0100 Subject: [PATCH] Slack: get the bot_access_token when requesting the token and use it to connect RTM --- include/transport/OAuth2.h | 2 +- libtransport/OAuth2.cpp | 10 +++++++++- .../src/frontends/slack/SlackUserRegistration.cpp | 12 +++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/include/transport/OAuth2.h b/include/transport/OAuth2.h index c8612d17..1b4b012d 100644 --- a/include/transport/OAuth2.h +++ b/include/transport/OAuth2.h @@ -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; diff --git a/libtransport/OAuth2.cpp b/libtransport/OAuth2.cpp index 6b3841c7..219623f0 100644 --- a/libtransport/OAuth2.cpp +++ b/libtransport/OAuth2.cpp @@ -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 ""; } diff --git a/spectrum/src/frontends/slack/SlackUserRegistration.cpp b/spectrum/src/frontends/slack/SlackUserRegistration.cpp index 7d770ca6..058b04b8 100644 --- a/spectrum/src/frontends/slack/SlackUserRegistration.cpp +++ b/spectrum/src/frontends/slack/SlackUserRegistration.cpp @@ -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 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) {