[editor] Forgot password, registration link

This commit is contained in:
Ilya Zverev 2016-01-22 19:24:06 +03:00 committed by Sergey Yershov
parent 7f3d5e2a63
commit ef7a46792d
3 changed files with 43 additions and 4 deletions

View file

@ -9,6 +9,7 @@ namespace
constexpr char const * kIZTestUser = "Testuser";
constexpr char const * kIZTestPassword = "testtest";
constexpr char const * kIZInvalidPassword = "123";
constexpr char const * kIZForgotPasswordEmail = "test@example.com";
constexpr char const * kFacebookToken = "CAAYYoGXMFUcBAHZBpDFyFPFQroYRMtzdCzXVFiqKcZAZB44jKjzW8WWoaPWI4xxl9EK8INIuTZAkhpURhwSiyOIKoWsgbqZAKEKIKZC3IdlUokPOEuaUpKQzgLTUcYNLiqgJogjUTL1s7Myqpf8cf5yoxQm32cqKZAdozrdx2df4FMJBSF7h0dXI49M2WjCyjPcEKntC4LfQsVwrZBn8uStvUJBVGMTwNWkZD";
//constexpr char const * kGoogleToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjVhZDc2OGE1ZDhjMTJlYmE3OGJiY2M5Yjg1ZGNlMzJhYzFjZGM3MzYifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhdWQiOiI0MTgwNTMwODI0ODktaTk3MGYwbHJvYjhsNGo1aTE2a2RlOGMydnBzODN0Y2wuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTQxMTI2Njc5NzEwNTQ0MTk2OTMiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXpwIjoiNDE4MDUzMDgyNDg5LTN2a2RzZ2E3cm9pNjI4ZGh2YTZydnN0MmQ4MGY5NWZuLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJ6dmVyaWtAdGV4dHVhbC5ydSIsImlhdCI6MTQ1MjI2ODI0NCwiZXhwIjoxNDUyMjcxODQ0LCJuYW1lIjoiSWx5YSBadmVyZXYiLCJnaXZlbl9uYW1lIjoiSWx5YSIsImZhbWlseV9uYW1lIjoiWnZlcmV2IiwibG9jYWxlIjoicnUifQ.E55Fwdt--Jln6p-eKZS18U3KNf0233hfJtLZywOxGs9HMiZNG6xZrYwPM8OFGMhweplITtCokZR54wYDD113HH5Bmt5DbdZXgGZ8mZmqS3U_toNeHWI92Zfhew08OUDF_pR1ykV76KqjW4QGQnmeEYYs4O4I2Xw3nyUTAeTxleBHTgBNW-XZHTQc0l_gr3cWULCTuGOKGTSAO6ccVx34r8n1wfbHmWYGEtdNpJxK_AVCl64pCoXL-uEV7Cp3nSKFSW4Ei6b-DW6hygVuhMNWDUZGvxLm8CbQTOHTRRCpM5vuhcPEAQHlxZrmEpU7lLXZCDBEvM9JdDvDicg_WQNf3w";
} // namespace
@ -54,3 +55,12 @@ 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_ForgotPassword)
{
OsmOAuth auth = OsmOAuth::IZServerAuth();
auto result = auth.RestorePassword(kIZForgotPasswordEmail);
TEST_EQUAL(result, OsmOAuth::AuthResult::OK, ("Correct email"));
result = auth.RestorePassword("incorrect@ema.il");
TEST_EQUAL(result, OsmOAuth::AuthResult::NoEmail, ("Incorrect email"));
}

View file

@ -98,9 +98,9 @@ OsmOAuth OsmOAuth::ServerAuth()
}
// Opens a login page and extract a cookie and a secret token.
OsmOAuth::AuthResult OsmOAuth::FetchSessionId(OsmOAuth::SessionID & sid) const
OsmOAuth::AuthResult OsmOAuth::FetchSessionId(OsmOAuth::SessionID & sid, string const & subUrl) const
{
HTTPClientPlatformWrapper request(m_baseUrl + "/login?cookie_test=true");
HTTPClientPlatformWrapper request(m_baseUrl + subUrl + "?cookie_test=true");
if (!request.RunHTTPRequest())
return AuthResult::NetworkError;
if (request.error_code() != 200)
@ -296,6 +296,30 @@ OsmOAuth::TUrlKeySecret OsmOAuth::GetGoogleOAuthURL() const
return TUrlKeySecret(url, requestToken);
}
OsmOAuth::AuthResult OsmOAuth::RestorePassword(string const & email) const
{
string const kForgotPasswordUrlPart = "/user/forgot-password";
SessionID sid;
AuthResult result = FetchSessionId(sid, kForgotPasswordUrlPart);
if (result != AuthResult::OK)
return result;
map<string, string> params;
params["user[email]"] = email;
params["authenticity_token"] = sid.m_token;
params["commit"] = "Reset password";
HTTPClientPlatformWrapper request(m_baseUrl + kForgotPasswordUrlPart);
request.set_body_data(BuildPostRequest(params), "application/x-www-form-urlencoded");
request.set_cookies(sid.m_cookies);
if (!request.RunHTTPRequest())
return AuthResult::NetworkError;
string const content = request.server_response();
return content.find("<div class=\"flash notice\">") == string::npos ? AuthResult::NoEmail : AuthResult::OK;
}
OsmOAuth::Response OsmOAuth::Request(TKeySecret const & keySecret, string const & method, string const & httpMethod, string const & body) const
{
CHECK(IsKeySecretValid(keySecret), ("Empty request token"));
@ -391,6 +415,7 @@ string DebugPrint(OsmOAuth::AuthResult const res)
case OsmOAuth::AuthResult::NoAccess: return "NoAccess";
case OsmOAuth::AuthResult::NetworkError: return "NetworkError";
case OsmOAuth::AuthResult::ServerError: return "ServerError";
case OsmOAuth::AuthResult::NoEmail: return "NoEmail";
}
return "Unknown";
}

View file

@ -8,6 +8,7 @@ namespace osm
using TKeySecret = pair<string /*key*/, string /*secret*/>;
/// All methods that interact with the OSM server are blocking and not asynchronous.
class OsmOAuth
{
public:
@ -21,7 +22,8 @@ public:
FailAuth,
NoAccess,
NetworkError,
ServerError
ServerError,
NoEmail
};
/// A result of a request. Has readable values for all OSM API return codes.
@ -64,6 +66,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;
AuthResult RestorePassword(string const & email) 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;
//@}
@ -86,6 +89,7 @@ public:
TUrlKeySecret GetGoogleOAuthURL() const;
AuthResult FinishAuthorization(TKeySecret const & requestToken, string const & verifier, TKeySecret & outKeySecret) const;
AuthResult FinishAuthorization(TKeySecret const & requestToken, string const & verifier);
string GetRegistrationURL() const { return m_baseUrl + "/user/new"; }
//@}
/// Tokenless GET request, for convenience.
@ -106,7 +110,7 @@ private:
/// Key and secret to sign every OAuth request.
TKeySecret m_tokenKeySecret;
AuthResult FetchSessionId(SessionID & sid) const;
AuthResult FetchSessionId(SessionID & sid, string const & subUrl = "/login") const;
AuthResult LogoutUser(SessionID const & sid) const;
AuthResult LoginUserPassword(string const & login, string const & password, SessionID const & sid) const;
AuthResult LoginSocial(string const & callbackPart, string const & socialToken, SessionID const & sid) const;