diff options
author | Alex Zolotarev <alex@maps.me> | 2016-01-11 09:40:44 +0300 |
---|---|---|
committer | Sergey Yershov <yershov@corp.mail.ru> | 2016-03-23 16:04:26 +0300 |
commit | 72a9f6e0d8c34d3308ccc60237dc22ff3ff3bb15 (patch) | |
tree | a1ee6ea561df046e211889399616df27d1c6e807 /editor/osm_auth.cpp | |
parent | 1bfcfd2f8887c707fb80c532dbfd65086bf37e32 (diff) |
Replaced ClientToken with pair<string, string> for ease of use.
Diffstat (limited to 'editor/osm_auth.cpp')
-rw-r--r-- | editor/osm_auth.cpp | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/editor/osm_auth.cpp b/editor/osm_auth.cpp index 6bb13d01bd..14c98a7804 100644 --- a/editor/osm_auth.cpp +++ b/editor/osm_auth.cpp @@ -21,6 +21,11 @@ constexpr char const * kApiVersion = "/api/0.6"; namespace { +inline bool IsKeySecretValid(TKeySecret const & t) +{ + return !(t.first.empty() || t.second.empty()); +} + string findAuthenticityToken(string const & body) { auto pos = body.find("name=\"authenticity_token\""); @@ -54,10 +59,8 @@ bool isLoggedIn(string const & contents) } } // namespace -OsmOAuth::OsmOAuth(string const & consumerKey, string const & consumerSecret, - string const & baseUrl, string const & apiUrl): - m_consumerKey(consumerKey), m_consumerSecret(consumerSecret), - m_baseUrl(baseUrl), m_apiUrl(apiUrl) +OsmOAuth::OsmOAuth(string const & consumerKey, string const & consumerSecret, string const & baseUrl, string const & apiUrl) + : m_consumerKeySecret(consumerKey, consumerSecret), m_baseUrl(baseUrl), m_apiUrl(apiUrl) { } @@ -175,10 +178,10 @@ string OsmOAuth::SendAuthRequest(string const & requestTokenKey, SessionID const } // Given a web session id, fetches an OAuth access token. -OsmOAuth::AuthResult OsmOAuth::FetchAccessToken(SessionID const & sid, ClientToken & token) const +OsmOAuth::AuthResult OsmOAuth::FetchAccessToken(SessionID const & sid, TKeySecret & outKeySecret) const { // Aquire a request token. - OAuth::Consumer consumer(m_consumerKey, m_consumerSecret); + OAuth::Consumer const consumer(m_consumerKeySecret.first, m_consumerKeySecret.second); OAuth::Client oauth(&consumer); string const requestTokenUrl = m_baseUrl + "/oauth/request_token"; string const requestTokenQuery = oauth.getURLQueryString(OAuth::Http::Get, requestTokenUrl + "?oauth_callback=oob"); @@ -203,15 +206,15 @@ OsmOAuth::AuthResult OsmOAuth::FetchAccessToken(SessionID const & sid, ClientTok OAuth::KeyValuePairs responseData = OAuth::ParseKeyValuePairs(request2.server_response()); OAuth::Token accessToken = OAuth::Token::extract(responseData); - token.m_key = accessToken.key(); - token.m_secret = accessToken.secret(); + outKeySecret.first = accessToken.key(); + outKeySecret.second = accessToken.secret(); LogoutUser(sid); return AuthResult::OK; } -OsmOAuth::AuthResult OsmOAuth::AuthorizePassword(string const & login, string const & password, ClientToken & token) const +OsmOAuth::AuthResult OsmOAuth::AuthorizePassword(string const & login, string const & password, TKeySecret & outKeySecret) const { SessionID sid; AuthResult result = FetchSessionId(sid); @@ -222,10 +225,10 @@ OsmOAuth::AuthResult OsmOAuth::AuthorizePassword(string const & login, string co if (result != AuthResult::OK) return result; - return FetchAccessToken(sid, token); + return FetchAccessToken(sid, outKeySecret); } -OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken, ClientToken & token) const +OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken, TKeySecret & outKeySecret) const { SessionID sid; AuthResult result = FetchSessionId(sid); @@ -236,14 +239,14 @@ OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken, C if (result != AuthResult::OK) return result; - return FetchAccessToken(sid, token); + return FetchAccessToken(sid, outKeySecret); } -OsmOAuth::Response OsmOAuth::Request(ClientToken const & token, string const & method, string const & httpMethod, string const & body) const +OsmOAuth::Response OsmOAuth::Request(TKeySecret const & keySecret, string const & method, string const & httpMethod, string const & body) const { - CHECK(token.IsValid(), ("Empty request token")); - OAuth::Consumer const consumer(m_consumerKey, m_consumerSecret); - OAuth::Token const oatoken(token.m_key, token.m_secret); + CHECK(IsKeySecretValid(keySecret), ("Empty request token")); + OAuth::Consumer const consumer(m_consumerKeySecret.first, m_consumerKeySecret.second); + OAuth::Token const oatoken(keySecret.first, keySecret.second); OAuth::Client oauth(&consumer, &oatoken); OAuth::Http::RequestType reqType; @@ -282,31 +285,34 @@ OsmOAuth::Response OsmOAuth::DirectRequest(string const & method, bool api) cons return Response(static_cast<ResponseCode>(request.error_code()), request.server_response()); } -OsmOAuth & OsmOAuth::SetToken(ClientToken const & token) +OsmOAuth & OsmOAuth::SetToken(TKeySecret const & keySecret) { - m_token.m_key = token.m_key; - m_token.m_secret = token.m_secret; + m_tokenKeySecret = keySecret; return *this; } +TKeySecret const & OsmOAuth::GetToken() const { return m_tokenKeySecret; } + +bool OsmOAuth::IsAuthorized() const { return IsKeySecretValid(m_tokenKeySecret); } + OsmOAuth::AuthResult OsmOAuth::FetchAccessToken(SessionID const & sid) { - return FetchAccessToken(sid, m_token); + return FetchAccessToken(sid, m_tokenKeySecret); } OsmOAuth::AuthResult OsmOAuth::AuthorizePassword(string const & login, string const & password) { - return AuthorizePassword(login, password, m_token); + return AuthorizePassword(login, password, m_tokenKeySecret); } OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken) { - return AuthorizeFacebook(facebookToken, m_token); + return AuthorizeFacebook(facebookToken, m_tokenKeySecret); } OsmOAuth::Response OsmOAuth::Request(string const & method, string const & httpMethod, string const & body) const { - return Request(m_token, method, httpMethod, body); + return Request(m_tokenKeySecret, method, httpMethod, body); } string DebugPrint(OsmOAuth::AuthResult const res) |