{"action":"created","comment":{"url":"https://api.github.com/repos/mapsme/omim/pulls/comments/48855557","id":48855557,"diff_hunk":"@@ -0,0 +1,207 @@\n+#include \"editor/osm_auth.hpp\"\n+\n+#include \"base/logging.hpp\"\n+#include \"coding/url_encode.hpp\"\n+#include \"std/iostream.hpp\"\n+#include \"std/map.hpp\"\n+#include \"3party/liboauthcpp/include/liboauthcpp/liboauthcpp.h\"\n+#include \"3party/Alohalytics/src/http_client.h\"\n+\n+using alohalytics::HTTPClientPlatformWrapper;\n+\n+namespace osm\n+{\n+\n+namespace\n+{\n+string const kOSMSessionCookie = \"_osm_session\";\n+\n+string findAuthenticityToken(string const & body)\n+{\n+  auto pos = body.find(\"name=\\\"authenticity_token\\\"\");\n+  if (pos == string::npos)\n+    return string();\n+  string const kValue = \"value=\\\"\";\n+  auto start = body.find(kValue, pos);\n+  if (start == string::npos)\n+    return string();\n+  start += kValue.length();\n+  auto const end = body.find(\"\\\"\", start);\n+  return end == string::npos ? string() : body.substr(start, end - start);\n+}\n+\n+string buildPostRequest(map<string, string> const & params)\n+{\n+  string result;\n+  for (auto it = params.begin(); it != params.end(); ++it)\n+  {\n+    if (it != params.begin())\n+      result += \"&\";\n+    result += it->first + \"=\" + UrlEncode(it->second);\n+  }\n+  return result;\n+}\n+\n+// Trying to determine whether it's a login page.\n+bool isLoggedIn(string const & contents)\n+{\n+  return contents.find(\"<form id=\\\"login_form\\\"\") == string::npos;\n+}\n+}  // namespace\n+\n+// Opens a login page and extract a cookie and a secret token.\n+bool OsmOAuth::FetchSessionId(OsmOAuth::SessionID & sid) const\n+{\n+  HTTPClientPlatformWrapper request(m_baseUrl + \"/login?cookie_test=true\");\n+  if (!(request.RunHTTPRequest() && request.error_code() == 200))\n+    return false;\n+  string const sessionCookie = request.cookie_by_name(kOSMSessionCookie);\n+  if (!sessionCookie.empty())\n+    sid.m_id = sessionCookie;\n+  sid.m_token = findAuthenticityToken(request.server_response());\n+  return !sid.m_id.empty() && !sid.m_token.empty();\n+}\n+\n+// Log a user out.\n+bool OsmOAuth::LogoutUser(SessionID const & sid) const\n+{\n+  HTTPClientPlatformWrapper request(m_baseUrl + \"/logout\");\n+  request.set_cookies(kOSMSessionCookie + \"=\" + sid.m_id);\n+  request.RunHTTPRequest(); // we don't care for the result.\n+  return true;\n+}\n+\n+// Signs a user id using login and password.\n+bool OsmOAuth::LoginUserPassword(string const & login, string const & password, SessionID const & sid) const\n+{\n+  map<string, string> params;\n+  params[\"username\"] = login;\n+  params[\"password\"] = password;\n+  params[\"referer\"] = \"/\";\n+  params[\"commit\"] = \"Login\";\n+  params[\"authenticity_token\"] = sid.m_token;\n+  HTTPClientPlatformWrapper request(m_baseUrl + \"/login\");\n+  request.set_body_data(buildPostRequest(params), \"application/x-www-form-urlencoded\");\n+  request.set_cookies(kOSMSessionCookie + \"=\" + sid.m_id);\n+  if (!(request.RunHTTPRequest() && request.error_code() == 200 && request.was_redirected()))\n+    return false;\n+  // Since we don't know whether the request was redirected or not, we need to check page contents.\n+  return isLoggedIn(request.server_response());\n+}\n+\n+// Signs a user in using a facebook token.\n+bool OsmOAuth::LoginFacebook(string const & facebookToken, SessionID const & sid) const\n+{\n+  string const url = m_baseUrl + \"/auth/facebook_access_token/callback?access_token=\" + facebookToken;\n+  HTTPClientPlatformWrapper request(url);\n+  request.set_cookies(kOSMSessionCookie + \"=\" + sid.m_id);\n+  if (!(request.RunHTTPRequest() && request.error_code() == 200 && request.was_redirected()))\n+    return false;\n+  return isLoggedIn(request.server_response());\n+}\n+\n+// Fakes a buttons press, so a user accepts requested permissions.\n+string OsmOAuth::SendAuthRequest(string const & requestTokenKey, SessionID const & sid) const\n+{\n+  map<string, string> params;\n+  params[\"oauth_token\"] = requestTokenKey;\n+  params[\"oauth_callback\"] = \"\";\n+  params[\"authenticity_token\"] = sid.m_token;\n+  params[\"allow_read_prefs\"] = \"yes\";\n+  params[\"allow_write_api\"] = \"yes\";\n+  params[\"commit\"] = \"Save changes\";\n+  HTTPClientPlatformWrapper request(m_baseUrl + \"/oauth/authorize\");\n+  request.set_body_data(buildPostRequest(params), \"application/x-www-form-urlencoded\");\n+  request.set_cookies(kOSMSessionCookie + \"=\" + sid.m_id);\n+\n+  if (!request.RunHTTPRequest())\n+    return string();\n+\n+  string const callbackURL = request.url_received();\n+  string const vKey = \"oauth_verifier=\";\n+  auto const pos = callbackURL.find(vKey);\n+  if (pos == string::npos)\n+    return string();\n+  auto const end = callbackURL.find(\"&\", pos);\n+  return callbackURL.substr(pos + vKey.length(), end == string::npos ? end : end - pos - vKey.length()+ 1);\n+}\n+\n+// Given a web session id, fetches an OAuth access token.\n+OsmOAuth::AuthResult OsmOAuth::FetchAccessToken(SessionID const & sid, ClientToken & token) const\n+{\n+  // Aquire a request token.\n+  OAuth::Consumer consumer(m_consumerKey, m_consumerSecret);\n+  OAuth::Client oauth(&consumer);\n+  string const requestTokenUrl = m_baseUrl + \"/oauth/request_token\";\n+  string const requestTokenQuery = oauth.getURLQueryString(OAuth::Http::Get, requestTokenUrl + \"?oauth_callback=oob\");\n+  HTTPClientPlatformWrapper request(requestTokenUrl + \"?\" + requestTokenQuery);\n+  if (!(request.RunHTTPRequest() && request.error_code() == 200 && !request.was_redirected()))\n+    return OsmOAuth::AuthResult::NoOAuth;\n+  OAuth::Token requestToken = OAuth::Token::extract(request.server_response());\n+  \n+  // Faking a button press for access rights.\n+  string const pin = SendAuthRequest(requestToken.key(), sid);\n+  if (pin.empty())\n+    return OsmOAuth::AuthResult::FailAuth;\n+  requestToken.setPin(pin);\n+\n+  // Got pin, exchange it for the access token.\n+  oauth = OAuth::Client(&consumer, &requestToken);\n+  string const accessTokenUrl = m_baseUrl + \"/oauth/access_token\";\n+  string const queryString = oauth.getURLQueryString(OAuth::Http::Get, accessTokenUrl, \"\", true);\n+  HTTPClientPlatformWrapper request2(accessTokenUrl + \"?\" + queryString);\n+  if (!(request2.RunHTTPRequest() && request2.error_code() == 200 && !request2.was_redirected()))\n+    return AuthResult::NoAccess;\n+  OAuth::KeyValuePairs responseData = OAuth::ParseKeyValuePairs(request2.server_response());\n+  OAuth::Token accessToken = OAuth::Token::extract(responseData);\n+\n+  token.m_key = accessToken.key();\n+  token.m_secret = accessToken.secret();\n+\n+  LogoutUser(sid);\n+  \n+  return OsmOAuth::AuthResult::OK;\n+}\n+\n+OsmOAuth::AuthResult OsmOAuth::AuthorizePassword(string const & login, string const & password, ClientToken & token) const\n+{\n+  SessionID sid;\n+  if (!FetchSessionId(sid))\n+    return OsmOAuth::AuthResult::FailCookie;","path":"editor/osm_auth.cpp","position":170,"original_position":170,"commit_id":"7ebf93f2acb719a9d98b1bbd5d1bca59f24a1949","original_commit_id":"7ebf93f2acb719a9d98b1bbd5d1bca59f24a1949","user":{"login":"Zverik","id":766031,"avatar_url":"https://avatars.githubusercontent.com/u/766031?v=3","gravatar_id":"","url":"https://api.github.com/users/Zverik","html_url":"https://github.com/Zverik","followers_url":"https://api.github.com/users/Zverik/followers","following_url":"https://api.github.com/users/Zverik/following{/other_user}","gists_url":"https://api.github.com/users/Zverik/gists{/gist_id}","starred_url":"https://api.github.com/users/Zverik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zverik/subscriptions","organizations_url":"https://api.github.com/users/Zverik/orgs","repos_url":"https://api.github.com/users/Zverik/repos","events_url":"https://api.github.com/users/Zverik/events{/privacy}","received_events_url":"https://api.github.com/users/Zverik/received_events","type":"User","site_admin":false},"body":"Это общая ошибка получения сессии. Там внутри может быть и ошибка сети, и ошибка протокола, и ошибка на стороне сервера, и ошибка обработки html. Функция возвращает bool, поэтому всё объединено в одну ошибку.","created_at":"2016-01-05T15:26:20Z","updated_at":"2016-01-05T15:26:20Z","html_url":"https://github.com/mapsme/omim/pull/1218#discussion_r48855557","pull_request_url":"https://api.github.com/repos/mapsme/omim/pulls/1218","_links":{"self":{"href":"https://api.github.com/repos/mapsme/omim/pulls/comments/48855557"},"html":{"href":"https://github.com/mapsme/omim/pull/1218#discussion_r48855557"},"pull_request":{"href":"https://api.github.com/repos/mapsme/omim/pulls/1218"}}},"pull_request":{"url":"https://api.github.com/repos/mapsme/omim/pulls/1218","id":54981530,"html_url":"https://github.com/mapsme/omim/pull/1218","diff_url":"https://github.com/mapsme/omim/pull/1218.diff","patch_url":"https://github.com/mapsme/omim/pull/1218.patch","issue_url":"https://api.github.com/repos/mapsme/omim/issues/1218","number":1218,"state":"open","locked":false,"title":"OAuth authentication to osm.org, base classes","user":{"login":"Zverik","id":766031,"avatar_url":"https://avatars.githubusercontent.com/u/766031?v=3","gravatar_id":"","url":"https://api.github.com/users/Zverik","html_url":"https://github.com/Zverik","followers_url":"https://api.github.com/users/Zverik/followers","following_url":"https://api.github.com/users/Zverik/following{/other_user}","gists_url":"https://api.github.com/users/Zverik/gists{/gist_id}","starred_url":"https://api.github.com/users/Zverik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zverik/subscriptions","organizations_url":"https://api.github.com/users/Zverik/orgs","repos_url":"https://api.github.com/users/Zverik/repos","events_url":"https://api.github.com/users/Zverik/events{/privacy}","received_events_url":"https://api.github.com/users/Zverik/received_events","type":"User","site_admin":false},"body":"Самая основа: библиотека для построения запросов OAuth и класс для автоматического логина по паролю и, в перспективе, по фейсбучному токену. Вместо логина и пароля нужно будет хранить две строки клиентского токена.","created_at":"2016-01-04T14:45:27Z","updated_at":"2016-01-05T15:26:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e621a976debf375c1a68e8ef5a6dd067399f53a9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mapsme/omim/pulls/1218/commits","review_comments_url":"https://api.github.com/repos/mapsme/omim/pulls/1218/comments","review_comment_url":"https://api.github.com/repos/mapsme/omim/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mapsme/omim/issues/1218/comments","statuses_url":"https://api.github.com/repos/mapsme/omim/statuses/7ebf93f2acb719a9d98b1bbd5d1bca59f24a1949","head":{"label":"Zverik:oauth-ed","ref":"oauth-ed","sha":"7ebf93f2acb719a9d98b1bbd5d1bca59f24a1949","user":{"login":"Zverik","id":766031,"avatar_url":"https://avatars.githubusercontent.com/u/766031?v=3","gravatar_id":"","url":"https://api.github.com/users/Zverik","html_url":"https://github.com/Zverik","followers_url":"https://api.github.com/users/Zverik/followers","following_url":"https://api.github.com/users/Zverik/following{/other_user}","gists_url":"https://api.github.com/users/Zverik/gists{/gist_id}","starred_url":"https://api.github.com/users/Zverik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zverik/subscriptions","organizations_url":"https://api.github.com/users/Zverik/orgs","repos_url":"https://api.github.com/users/Zverik/repos","events_url":"https://api.github.com/users/Zverik/events{/privacy}","received_events_url":"https://api.github.com/users/Zverik/received_events","type":"User","site_admin":false},"repo":{"id":43427426,"name":"omim","full_name":"Zverik/omim","owner":{"login":"Zverik","id":766031,"avatar_url":"https://avatars.githubusercontent.com/u/766031?v=3","gravatar_id":"","url":"https://api.github.com/users/Zverik","html_url":"https://github.com/Zverik","followers_url":"https://api.github.com/users/Zverik/followers","following_url":"https://api.github.com/users/Zverik/following{/other_user}","gists_url":"https://api.github.com/users/Zverik/gists{/gist_id}","starred_url":"https://api.github.com/users/Zverik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Zverik/subscriptions","organizations_url":"https://api.github.com/users/Zverik/orgs","repos_url":"https://api.github.com/users/Zverik/repos","events_url":"https://api.github.com/users/Zverik/events{/privacy}","received_events_url":"https://api.github.com/users/Zverik/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Zverik/omim","description":"MAPS.ME - Offline OpenStreetMap maps for iOS/Android/Mac/Linux/Windows","fork":true,"url":"https://api.github.com/repos/Zverik/omim","forks_url":"https://api.github.com/repos/Zverik/omim/forks","keys_url":"https://api.github.com/repos/Zverik/omim/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Zverik/omim/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Zverik/omim/teams","hooks_url":"https://api.github.com/repos/Zverik/omim/hooks","issue_events_url":"https://api.github.com/repos/Zverik/omim/issues/events{/number}","events_url":"https://api.github.com/repos/Zverik/omim/events","assignees_url":"https://api.github.com/repos/Zverik/omim/assignees{/user}","branches_url":"https://api.github.com/repos/Zverik/omim/branches{/branch}","tags_url":"https://api.github.com/repos/Zverik/omim/tags","blobs_url":"https://api.github.com/repos/Zverik/omim/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Zverik/omim/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Zverik/omim/git/refs{/sha}","trees_url":"https://api.github.com/repos/Zverik/omim/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Zverik/omim/statuses/{sha}","languages_url":"https://api.github.com/repos/Zverik/omim/languages","stargazers_url":"https://api.github.com/repos/Zverik/omim/stargazers","contributors_url":"https://api.github.com/repos/Zverik/omim/contributors","subscribers_url":"https://api.github.com/repos/Zverik/omim/subscribers","subscription_url":"https://api.github.com/repos/Zverik/omim/subscription","commits_url":"https://api.github.com/repos/Zverik/omim/commits{/sha}","git_commits_url":"https://api.github.com/repos/Zverik/omim/git/commits{/sha}","comments_url":"https://api.github.com/repos/Zverik/omim/comments{/number}","issue_comment_url":"https://api.github.com/repos/Zverik/omim/issues/comments{/number}","contents_url":"https://api.github.com/repos/Zverik/omim/contents/{+path}","compare_url":"https://api.github.com/repos/Zverik/omim/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Zverik/omim/merges","archive_url":"https://api.github.com/repos/Zverik/omim/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Zverik/omim/downloads","issues_url":"https://api.github.com/repos/Zverik/omim/issues{/number}","pulls_url":"https://api.github.com/repos/Zverik/omim/pulls{/number}","milestones_url":"https://api.github.com/repos/Zverik/omim/milestones{/number}","notifications_url":"https://api.github.com/repos/Zverik/omim/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Zverik/omim/labels{/name}","releases_url":"https://api.github.com/repos/Zverik/omim/releases{/id}","created_at":"2015-09-30T10:43:06Z","updated_at":"2015-09-30T10:45:15Z","pushed_at":"2016-01-05T15:02:27Z","git_url":"git://github.com/Zverik/omim.git","ssh_url":"git@github.com:Zverik/omim.git","clone_url":"https://github.com/Zverik/omim.git","svn_url":"https://github.com/Zverik/omim","homepage":null,"size":1939750,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mapsme:osm-editor","ref":"osm-editor","sha":"2e10e406c44d887f01807e7fdd817f37573ba6ca","user":{"login":"mapsme","id":14185141,"avatar_url":"https://avatars.githubusercontent.com/u/14185141?v=3","gravatar_id":"","url":"https://api.github.com/users/mapsme","html_url":"https://github.com/mapsme","followers_url":"https://api.github.com/users/mapsme/followers","following_url":"https://api.github.com/users/mapsme/following{/other_user}","gists_url":"https://api.github.com/users/mapsme/gists{/gist_id}","starred_url":"https://api.github.com/users/mapsme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapsme/subscriptions","organizations_url":"https://api.github.com/users/mapsme/orgs","repos_url":"https://api.github.com/users/mapsme/repos","events_url":"https://api.github.com/users/mapsme/events{/privacy}","received_events_url":"https://api.github.com/users/mapsme/received_events","type":"Organization","site_admin":false},"repo":{"id":42798033,"name":"omim","full_name":"mapsme/omim","owner":{"login":"mapsme","id":14185141,"avatar_url":"https://avatars.githubusercontent.com/u/14185141?v=3","gravatar_id":"","url":"https://api.github.com/users/mapsme","html_url":"https://github.com/mapsme","followers_url":"https://api.github.com/users/mapsme/followers","following_url":"https://api.github.com/users/mapsme/following{/other_user}","gists_url":"https://api.github.com/users/mapsme/gists{/gist_id}","starred_url":"https://api.github.com/users/mapsme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapsme/subscriptions","organizations_url":"https://api.github.com/users/mapsme/orgs","repos_url":"https://api.github.com/users/mapsme/repos","events_url":"https://api.github.com/users/mapsme/events{/privacy}","received_events_url":"https://api.github.com/users/mapsme/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mapsme/omim","description":"MAPS.ME - Offline OpenStreetMap maps for iOS/Android/Mac/Linux/Windows","fork":false,"url":"https://api.github.com/repos/mapsme/omim","forks_url":"https://api.github.com/repos/mapsme/omim/forks","keys_url":"https://api.github.com/repos/mapsme/omim/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mapsme/omim/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mapsme/omim/teams","hooks_url":"https://api.github.com/repos/mapsme/omim/hooks","issue_events_url":"https://api.github.com/repos/mapsme/omim/issues/events{/number}","events_url":"https://api.github.com/repos/mapsme/omim/events","assignees_url":"https://api.github.com/repos/mapsme/omim/assignees{/user}","branches_url":"https://api.github.com/repos/mapsme/omim/branches{/branch}","tags_url":"https://api.github.com/repos/mapsme/omim/tags","blobs_url":"https://api.github.com/repos/mapsme/omim/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mapsme/omim/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mapsme/omim/git/refs{/sha}","trees_url":"https://api.github.com/repos/mapsme/omim/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mapsme/omim/statuses/{sha}","languages_url":"https://api.github.com/repos/mapsme/omim/languages","stargazers_url":"https://api.github.com/repos/mapsme/omim/stargazers","contributors_url":"https://api.github.com/repos/mapsme/omim/contributors","subscribers_url":"https://api.github.com/repos/mapsme/omim/subscribers","subscription_url":"https://api.github.com/repos/mapsme/omim/subscription","commits_url":"https://api.github.com/repos/mapsme/omim/commits{/sha}","git_commits_url":"https://api.github.com/repos/mapsme/omim/git/commits{/sha}","comments_url":"https://api.github.com/repos/mapsme/omim/comments{/number}","issue_comment_url":"https://api.github.com/repos/mapsme/omim/issues/comments{/number}","contents_url":"https://api.github.com/repos/mapsme/omim/contents/{+path}","compare_url":"https://api.github.com/repos/mapsme/omim/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mapsme/omim/merges","archive_url":"https://api.github.com/repos/mapsme/omim/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mapsme/omim/downloads","issues_url":"https://api.github.com/repos/mapsme/omim/issues{/number}","pulls_url":"https://api.github.com/repos/mapsme/omim/pulls{/number}","milestones_url":"https://api.github.com/repos/mapsme/omim/milestones{/number}","notifications_url":"https://api.github.com/repos/mapsme/omim/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mapsme/omim/labels{/name}","releases_url":"https://api.github.com/repos/mapsme/omim/releases{/id}","created_at":"2015-09-20T02:57:13Z","updated_at":"2016-01-05T03:16:55Z","pushed_at":"2016-01-05T15:21:17Z","git_url":"git://github.com/mapsme/omim.git","ssh_url":"git@github.com:mapsme/omim.git","clone_url":"https://github.com/mapsme/omim.git","svn_url":"https://github.com/mapsme/omim","homepage":null,"size":1988472,"stargazers_count":1565,"watchers_count":1565,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":202,"mirror_url":null,"open_issues_count":103,"forks":202,"open_issues":103,"watchers":1565,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mapsme/omim/pulls/1218"},"html":{"href":"https://github.com/mapsme/omim/pull/1218"},"issue":{"href":"https://api.github.com/repos/mapsme/omim/issues/1218"},"comments":{"href":"https://api.github.com/repos/mapsme/omim/issues/1218/comments"},"review_comments":{"href":"https://api.github.com/repos/mapsme/omim/pulls/1218/comments"},"review_comment":{"href":"https://api.github.com/repos/mapsme/omim/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mapsme/omim/pulls/1218/commits"},"statuses":{"href":"https://api.github.com/repos/mapsme/omim/statuses/7ebf93f2acb719a9d98b1bbd5d1bca59f24a1949"}}}}