forked from organicmaps/organicmaps
[oauth] Google auth
This commit is contained in:
parent
a04b216c15
commit
1232d77311
3 changed files with 40 additions and 4 deletions
|
@ -10,6 +10,7 @@ constexpr char const * kIZTestUser = "Testuser";
|
|||
constexpr char const * kIZTestPassword = "testtest";
|
||||
constexpr char const * kIZInvalidPassword = "123";
|
||||
constexpr char const * kFacebookToken = "CAAYYoGXMFUcBAHZBpDFyFPFQroYRMtzdCzXVFiqKcZAZB44jKjzW8WWoaPWI4xxl9EK8INIuTZAkhpURhwSiyOIKoWsgbqZAKEKIKZC3IdlUokPOEuaUpKQzgLTUcYNLiqgJogjUTL1s7Myqpf8cf5yoxQm32cqKZAdozrdx2df4FMJBSF7h0dXI49M2WjCyjPcEKntC4LfQsVwrZBn8uStvUJBVGMTwNWkZD";
|
||||
constexpr char const * kGoogleToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjVhZDc2OGE1ZDhjMTJlYmE3OGJiY2M5Yjg1ZGNlMzJhYzFjZGM3MzYifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhdWQiOiI0MTgwNTMwODI0ODktaTk3MGYwbHJvYjhsNGo1aTE2a2RlOGMydnBzODN0Y2wuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTQxMTI2Njc5NzEwNTQ0MTk2OTMiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXpwIjoiNDE4MDUzMDgyNDg5LTN2a2RzZ2E3cm9pNjI4ZGh2YTZydnN0MmQ4MGY5NWZuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJ6dmVyaWtAdGV4dHVhbC5ydSIsImlhdCI6MTQ1MjI2ODI0NCwiZXhwIjoxNDUyMjcxODQ0LCJuYW1lIjoiSWx5YSBadmVyZXYiLCJnaXZlbl9uYW1lIjoiSWx5YSIsImZhbWlseV9uYW1lIjoiWnZlcmV2IiwibG9jYWxlIjoicnUifQ.E55Fwdt--Jln6p-eKZS18U3KNf0233hfJtLZywOxGs9HMiZNG6xZrYwPM8OFGMhweplITtCokZR54wYDD113HH5Bmt5DbdZXgGZ8mZmqS3U_toNeHWI92Zfhew08OUDF_pR1ykV76KqjW4QGQnmeEYYs4O4I2Xw3nyUTAeTxleBHTgBNW-XZHTQc0l_gr3cWULCTuGOKGTSAO6ccVx34r8n1wfbHmWYGEtdNpJxK_AVCl64pCoXL-uEV7Cp3nSKFSW4Ei6b-DW6hygVuhMNWDUZGvxLm8CbQTOHTRRCpM5vuhcPEAQHlxZrmEpU7lLXZCDBEvM9JdDvDicg_WQNf3w";
|
||||
} // namespace
|
||||
|
||||
UNIT_TEST(OSM_Auth_InvalidLogin)
|
||||
|
@ -41,3 +42,15 @@ UNIT_TEST(OSM_Auth_Facebook)
|
|||
TEST_EQUAL(perm.first, OsmOAuth::ResponseCode::OK, ("permission with stored token request ok"));
|
||||
TEST(perm.second.find("write_api") != string::npos, ("can write to api"));
|
||||
}
|
||||
|
||||
|
||||
UNIT_TEST(OSM_Auth_Google)
|
||||
{
|
||||
OsmOAuth auth(kConsumerKey, kConsumerSecret, kTestServer, kTestServer);
|
||||
auto result = auth.AuthorizeGoogle(kGoogleToken);
|
||||
TEST_EQUAL(result, OsmOAuth::AuthResult::OK, ("login via google"));
|
||||
TEST(auth.IsAuthorized(), ("authorized"));
|
||||
OsmOAuth::Response const perm = auth.Request("/permissions");
|
||||
TEST_EQUAL(perm.first, OsmOAuth::ResponseCode::OK, ("permission with stored token request ok"));
|
||||
TEST(perm.second.find("write_api") != string::npos, ("can write to api"));
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ using alohalytics::HTTPClientPlatformWrapper;
|
|||
namespace osm
|
||||
{
|
||||
constexpr char const * kApiVersion = "/api/0.6";
|
||||
constexpr char const * kFacebookCallbackPart = "/auth/facebook_access_token/callback?access_token=";
|
||||
constexpr char const * kGoogleCallbackPart = "/auth/google_oauth2_access_token/callback?access_token=";
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -135,9 +137,9 @@ OsmOAuth::AuthResult OsmOAuth::LoginUserPassword(string const & login, string co
|
|||
}
|
||||
|
||||
// Signs a user in using a facebook token.
|
||||
OsmOAuth::AuthResult OsmOAuth::LoginFacebook(string const & facebookToken, SessionID const & sid) const
|
||||
OsmOAuth::AuthResult OsmOAuth::LoginSocial(string const & callbackPart, string const & socialToken, SessionID const & sid) const
|
||||
{
|
||||
string const url = m_baseUrl + "/auth/facebook_access_token/callback?access_token=" + facebookToken;
|
||||
string const url = m_baseUrl + callbackPart + socialToken;
|
||||
HTTPClientPlatformWrapper request(url);
|
||||
request.set_cookies(sid.m_cookies);
|
||||
if (!request.RunHTTPRequest())
|
||||
|
@ -235,7 +237,21 @@ OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken, T
|
|||
if (result != AuthResult::OK)
|
||||
return result;
|
||||
|
||||
result = LoginFacebook(facebookToken, sid);
|
||||
result = LoginSocial(kFacebookCallbackPart, facebookToken, sid);
|
||||
if (result != AuthResult::OK)
|
||||
return result;
|
||||
|
||||
return FetchAccessToken(sid, outKeySecret);
|
||||
}
|
||||
|
||||
OsmOAuth::AuthResult OsmOAuth::AuthorizeGoogle(string const & googleToken, TKeySecret & outKeySecret) const
|
||||
{
|
||||
SessionID sid;
|
||||
AuthResult result = FetchSessionId(sid);
|
||||
if (result != AuthResult::OK)
|
||||
return result;
|
||||
|
||||
result = LoginSocial(kGoogleCallbackPart, googleToken, sid);
|
||||
if (result != AuthResult::OK)
|
||||
return result;
|
||||
|
||||
|
@ -310,6 +326,11 @@ OsmOAuth::AuthResult OsmOAuth::AuthorizeFacebook(string const & facebookToken)
|
|||
return AuthorizeFacebook(facebookToken, m_tokenKeySecret);
|
||||
}
|
||||
|
||||
OsmOAuth::AuthResult OsmOAuth::AuthorizeGoogle(string const & googleToken)
|
||||
{
|
||||
return AuthorizeGoogle(googleToken, m_token);
|
||||
}
|
||||
|
||||
OsmOAuth::Response OsmOAuth::Request(string const & method, string const & httpMethod, string const & body) const
|
||||
{
|
||||
return Request(m_tokenKeySecret, method, httpMethod, body);
|
||||
|
|
|
@ -59,6 +59,7 @@ public:
|
|||
//@{
|
||||
AuthResult AuthorizePassword(string const & login, string const & password, TKeySecret & outKeySecret) const;
|
||||
AuthResult AuthorizeFacebook(string const & facebookToken, TKeySecret & outKeySecret) const;
|
||||
AuthResult AuthorizeGoogle(string const & googleToken, TKeySecret & outKeySecret) const;
|
||||
/// @param[method] The API method, must start with a forward slash.
|
||||
Response Request(TKeySecret const & keySecret, string const & method, string const & httpMethod = "GET", string const & body = "") const;
|
||||
//@}
|
||||
|
@ -70,6 +71,7 @@ public:
|
|||
bool IsAuthorized() const;
|
||||
AuthResult AuthorizePassword(string const & login, string const & password);
|
||||
AuthResult AuthorizeFacebook(string const & facebookToken);
|
||||
AuthResult AuthorizeGoogle(string const & googleToken);
|
||||
/// @param[method] The API method, must start with a forward slash.
|
||||
Response Request(string const & method, string const & httpMethod = "GET", string const & body = "") const;
|
||||
//@}
|
||||
|
@ -95,7 +97,7 @@ private:
|
|||
AuthResult FetchSessionId(SessionID & sid) const;
|
||||
AuthResult LogoutUser(SessionID const & sid) const;
|
||||
AuthResult LoginUserPassword(string const & login, string const & password, SessionID const & sid) const;
|
||||
AuthResult LoginFacebook(string const & facebookToken, SessionID const & sid) const;
|
||||
AuthResult LoginSocial(string const & callbackPart, string const & socialToken, SessionID const & sid) const;
|
||||
string SendAuthRequest(string const & requestTokenKey, SessionID const & sid) const;
|
||||
AuthResult FetchAccessToken(SessionID const & sid, TKeySecret & outKeySecret) const;
|
||||
AuthResult FetchAccessToken(SessionID const & sid);
|
||||
|
|
Loading…
Add table
Reference in a new issue