| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "google_apis/gaia/fake_gaia.h" | 5 #include "google_apis/gaia/fake_gaia.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/linked_ptr.h" |
| 16 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
| 23 #include "net/base/url_util.h" | 24 #include "net/base/url_util.h" |
| 25 #include "net/cookies/parsed_cookie.h" |
| 24 #include "net/http/http_status_code.h" | 26 #include "net/http/http_status_code.h" |
| 25 #include "net/test/embedded_test_server/http_request.h" | 27 #include "net/test/embedded_test_server/http_request.h" |
| 26 #include "net/test/embedded_test_server/http_response.h" | 28 #include "net/test/embedded_test_server/http_response.h" |
| 27 #include "url/url_parse.h" | 29 #include "url/url_parse.h" |
| 28 | 30 |
| 29 #define REGISTER_RESPONSE_HANDLER(url, method) \ | 31 #define REGISTER_RESPONSE_HANDLER(url, method) \ |
| 30 request_handlers_.insert(std::make_pair( \ | 32 request_handlers_.insert(std::make_pair( \ |
| 31 url.path(), base::Bind(&FakeGaia::method, base::Unretained(this)))) | 33 url.path(), base::Bind(&FakeGaia::method, base::Unretained(this)))) |
| 32 | 34 |
| 33 #define REGISTER_PATH_RESPONSE_HANDLER(path, method) \ | 35 #define REGISTER_PATH_RESPONSE_HANDLER(path, method) \ |
| 34 request_handlers_.insert(std::make_pair( \ | 36 request_handlers_.insert(std::make_pair( \ |
| 35 path, base::Bind(&FakeGaia::method, base::Unretained(this)))) | 37 path, base::Bind(&FakeGaia::method, base::Unretained(this)))) |
| 36 | 38 |
| 37 using namespace net::test_server; | 39 using namespace net::test_server; |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 const base::FilePath::CharType kServiceLogin[] = | 43 const base::FilePath::CharType kServiceLogin[] = |
| 42 FILE_PATH_LITERAL("google_apis/test/service_login.html"); | 44 FILE_PATH_LITERAL("google_apis/test/service_login.html"); |
| 43 | 45 |
| 44 // OAuth2 Authentication header value prefix. | 46 // OAuth2 Authentication header value prefix. |
| 45 const char kAuthHeaderBearer[] = "Bearer "; | 47 const char kAuthHeaderBearer[] = "Bearer "; |
| 46 const char kAuthHeaderOAuth[] = "OAuth "; | 48 const char kAuthHeaderOAuth[] = "OAuth "; |
| 47 | 49 |
| 50 typedef std::map<std::string, std::string> CookieMap; |
| 51 |
| 52 // Parses cookie name-value map our of |request|. |
| 53 CookieMap GetRequestCookies(const HttpRequest& request) { |
| 54 CookieMap result; |
| 55 std::map<std::string, std::string>::const_iterator iter = |
| 56 request.headers.find("Cookie"); |
| 57 if (iter != request.headers.end()) { |
| 58 std::vector<std::string> cookie_nv_pairs; |
| 59 base::SplitString(iter->second, ' ', &cookie_nv_pairs); |
| 60 for(std::vector<std::string>::const_iterator cookie_line = |
| 61 cookie_nv_pairs.begin(); |
| 62 cookie_line != cookie_nv_pairs.end(); |
| 63 ++cookie_line) { |
| 64 std::vector<std::string> name_value; |
| 65 base::SplitString(*cookie_line, '=', &name_value); |
| 66 if (name_value.size() != 2) |
| 67 continue; |
| 68 |
| 69 std::string value = name_value[1]; |
| 70 if (value.size() && value[value.size() - 1] == ';') |
| 71 value = value.substr(0, value.size() -1); |
| 72 |
| 73 result.insert(std::make_pair(name_value[0], value)); |
| 74 } |
| 75 } |
| 76 return result; |
| 77 } |
| 78 |
| 79 // Extracts the |access_token| from authorization header of |request|. |
| 80 bool GetAccessToken(const HttpRequest& request, |
| 81 const char* auth_token_prefix, |
| 82 std::string* access_token) { |
| 83 std::map<std::string, std::string>::const_iterator auth_header_entry = |
| 84 request.headers.find("Authorization"); |
| 85 if (auth_header_entry != request.headers.end()) { |
| 86 if (StartsWithASCII(auth_header_entry->second, auth_token_prefix, true)) { |
| 87 *access_token = auth_header_entry->second.substr( |
| 88 strlen(auth_token_prefix)); |
| 89 return true; |
| 90 } |
| 91 } |
| 92 |
| 93 return false; |
| 94 } |
| 95 |
| 48 } | 96 } |
| 49 | 97 |
| 50 FakeGaia::AccessTokenInfo::AccessTokenInfo() | 98 FakeGaia::AccessTokenInfo::AccessTokenInfo() |
| 51 : expires_in(3600) {} | 99 : expires_in(3600) {} |
| 52 | 100 |
| 53 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} | 101 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} |
| 54 | 102 |
| 103 FakeGaia::MergeSessionParams::MergeSessionParams() { |
| 104 } |
| 105 |
| 106 FakeGaia::MergeSessionParams::~MergeSessionParams() { |
| 107 } |
| 108 |
| 55 FakeGaia::FakeGaia() { | 109 FakeGaia::FakeGaia() { |
| 56 base::FilePath source_root_dir; | 110 base::FilePath source_root_dir; |
| 57 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); | 111 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); |
| 58 CHECK(base::ReadFileToString( | 112 CHECK(base::ReadFileToString( |
| 59 source_root_dir.Append(base::FilePath(kServiceLogin)), | 113 source_root_dir.Append(base::FilePath(kServiceLogin)), |
| 60 &service_login_response_)); | 114 &service_login_response_)); |
| 61 } | 115 } |
| 62 | 116 |
| 63 FakeGaia::~FakeGaia() {} | 117 FakeGaia::~FakeGaia() {} |
| 64 | 118 |
| 65 void FakeGaia::SetAuthTokens(const std::string& auth_code, | 119 void FakeGaia::SetMergeSessionParams( |
| 66 const std::string& refresh_token, | 120 const MergeSessionParams& params) { |
| 67 const std::string& access_token, | 121 merge_session_params_ = params; |
| 68 const std::string& gaia_uber_token, | |
| 69 const std::string& session_sid_cookie, | |
| 70 const std::string& session_lsid_cookie) { | |
| 71 fake_auth_code_ = auth_code; | |
| 72 fake_refresh_token_ = refresh_token; | |
| 73 fake_access_token_ = access_token; | |
| 74 fake_gaia_uber_token_ = gaia_uber_token; | |
| 75 fake_session_sid_cookie_ = session_sid_cookie; | |
| 76 fake_session_lsid_cookie_ = session_lsid_cookie; | |
| 77 } | 122 } |
| 78 | 123 |
| 79 void FakeGaia::Initialize() { | 124 void FakeGaia::Initialize() { |
| 80 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 125 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
| 81 // Handles /ServiceLogin GAIA call. | 126 // Handles /ServiceLogin GAIA call. |
| 82 REGISTER_RESPONSE_HANDLER( | 127 REGISTER_RESPONSE_HANDLER( |
| 83 gaia_urls->service_login_url(), HandleServiceLogin); | 128 gaia_urls->service_login_url(), HandleServiceLogin); |
| 84 | 129 |
| 85 // Handles /ServiceLoginAuth GAIA call. | 130 // Handles /ServiceLoginAuth GAIA call. |
| 86 REGISTER_RESPONSE_HANDLER( | 131 REGISTER_RESPONSE_HANDLER( |
| (...skipping 23 matching lines...) Expand all Loading... |
| 110 REGISTER_RESPONSE_HANDLER( | 155 REGISTER_RESPONSE_HANDLER( |
| 111 gaia_urls->oauth2_token_info_url(), HandleTokenInfo); | 156 gaia_urls->oauth2_token_info_url(), HandleTokenInfo); |
| 112 | 157 |
| 113 // Handles /SSO GAIA call (not GAIA, made up for SAML tests). | 158 // Handles /SSO GAIA call (not GAIA, made up for SAML tests). |
| 114 REGISTER_PATH_RESPONSE_HANDLER("/SSO", HandleSSO); | 159 REGISTER_PATH_RESPONSE_HANDLER("/SSO", HandleSSO); |
| 115 } | 160 } |
| 116 | 161 |
| 117 void FakeGaia::HandleProgramaticAuth( | 162 void FakeGaia::HandleProgramaticAuth( |
| 118 const HttpRequest& request, | 163 const HttpRequest& request, |
| 119 BasicHttpResponse* http_response) { | 164 BasicHttpResponse* http_response) { |
| 165 http_response->set_code(net::HTTP_UNAUTHORIZED); |
| 166 if (merge_session_params_.auth_code.empty()) { |
| 167 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 168 return; |
| 169 } |
| 170 |
| 120 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 171 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
| 121 std::string scope; | 172 std::string scope; |
| 122 if (!GetQueryParameter(request.content, "scope", &scope) || | 173 if (!GetQueryParameter(request.content, "scope", &scope) || |
| 123 gaia_urls->oauth1_login_scope() != scope) { | 174 gaia_urls->oauth1_login_scope() != scope) { |
| 124 http_response->set_code(net::HTTP_BAD_REQUEST); | 175 return; |
| 176 } |
| 177 |
| 178 CookieMap cookies = GetRequestCookies(request); |
| 179 CookieMap::const_iterator sid_iter = cookies.find("SID"); |
| 180 if (sid_iter == cookies.end() || |
| 181 sid_iter->second != merge_session_params_.auth_sid_cookie) { |
| 182 LOG(ERROR) << "/o/oauth2/programmatic_auth missing SID cookie"; |
| 183 return; |
| 184 } |
| 185 CookieMap::const_iterator lsid_iter = cookies.find("LSID"); |
| 186 if (lsid_iter == cookies.end() || |
| 187 lsid_iter->second != merge_session_params_.auth_lsid_cookie) { |
| 188 LOG(ERROR) << "/o/oauth2/programmatic_auth missing LSID cookie"; |
| 125 return; | 189 return; |
| 126 } | 190 } |
| 127 | 191 |
| 128 std::string client_id; | 192 std::string client_id; |
| 129 if (!GetQueryParameter(request.content, "client_id", &client_id) || | 193 if (!GetQueryParameter(request.content, "client_id", &client_id) || |
| 130 gaia_urls->oauth2_chrome_client_id() != client_id) { | 194 gaia_urls->oauth2_chrome_client_id() != client_id) { |
| 131 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 132 return; | 195 return; |
| 133 } | 196 } |
| 134 | 197 |
| 135 http_response->AddCustomHeader( | 198 http_response->AddCustomHeader( |
| 136 "Set-Cookie", | 199 "Set-Cookie", |
| 137 base::StringPrintf( | 200 base::StringPrintf( |
| 138 "oauth_code=%s; Path=/o/GetOAuth2Token; Secure; HttpOnly;", | 201 "oauth_code=%s; Path=/o/GetOAuth2Token; Secure; HttpOnly;", |
| 139 fake_auth_code_.c_str())); | 202 merge_session_params_.auth_code.c_str())); |
| 140 http_response->set_code(net::HTTP_OK); | 203 http_response->set_code(net::HTTP_OK); |
| 141 http_response->set_content_type("text/html"); | 204 http_response->set_content_type("text/html"); |
| 142 } | 205 } |
| 143 | 206 |
| 144 void FakeGaia::HandleServiceLogin(const HttpRequest& request, | 207 void FakeGaia::HandleServiceLogin(const HttpRequest& request, |
| 145 BasicHttpResponse* http_response) { | 208 BasicHttpResponse* http_response) { |
| 146 http_response->set_code(net::HTTP_OK); | 209 http_response->set_code(net::HTTP_OK); |
| 147 http_response->set_content(service_login_response_); | 210 http_response->set_content(service_login_response_); |
| 148 http_response->set_content_type("text/html"); | 211 http_response->set_content_type("text/html"); |
| 149 } | 212 } |
| 150 | 213 |
| 151 void FakeGaia::HandleOAuthLogin(const HttpRequest& request, | 214 void FakeGaia::HandleOAuthLogin(const HttpRequest& request, |
| 152 BasicHttpResponse* http_response) { | 215 BasicHttpResponse* http_response) { |
| 153 http_response->set_code(net::HTTP_BAD_REQUEST); | 216 http_response->set_code(net::HTTP_UNAUTHORIZED); |
| 217 if (merge_session_params_.gaia_uber_token.empty()) { |
| 218 http_response->set_code(net::HTTP_FORBIDDEN); |
| 219 return; |
| 220 } |
| 221 |
| 154 std::string access_token; | 222 std::string access_token; |
| 155 if (!GetAccessToken(request, kAuthHeaderOAuth, &access_token)) { | 223 if (!GetAccessToken(request, kAuthHeaderOAuth, &access_token)) { |
| 156 LOG(ERROR) << "/OAuthLogin missing access token in the header"; | 224 LOG(ERROR) << "/OAuthLogin missing access token in the header"; |
| 157 return; | 225 return; |
| 158 } | 226 } |
| 159 | 227 |
| 160 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 228 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
| 161 std::string request_query = request_url.query(); | 229 std::string request_query = request_url.query(); |
| 162 | 230 |
| 163 std::string source; | 231 std::string source; |
| 164 if (!GetQueryParameter(request_query, "source", &source)) { | 232 if (!GetQueryParameter(request_query, "source", &source)) { |
| 165 LOG(ERROR) << "Missing 'source' param in /OAuthLogin call"; | 233 LOG(ERROR) << "Missing 'source' param in /OAuthLogin call"; |
| 166 return; | 234 return; |
| 167 } | 235 } |
| 168 | 236 |
| 169 std::string issue_uberauth; | 237 std::string issue_uberauth; |
| 170 if (GetQueryParameter(request_query, "issueuberauth", &issue_uberauth) && | 238 if (GetQueryParameter(request_query, "issueuberauth", &issue_uberauth) && |
| 171 issue_uberauth == "1") { | 239 issue_uberauth == "1") { |
| 172 http_response->set_content(fake_gaia_uber_token_); | 240 http_response->set_content(merge_session_params_.gaia_uber_token); |
| 173 http_response->set_code(net::HTTP_OK); | 241 http_response->set_code(net::HTTP_OK); |
| 174 // Issue GAIA uber token. | 242 // Issue GAIA uber token. |
| 175 } else { | 243 } else { |
| 176 LOG(FATAL) << "/OAuthLogin for SID/LSID is not supported"; | 244 LOG(FATAL) << "/OAuthLogin for SID/LSID is not supported"; |
| 177 } | 245 } |
| 178 } | 246 } |
| 179 | 247 |
| 180 void FakeGaia::HandleMergeSession(const HttpRequest& request, | 248 void FakeGaia::HandleMergeSession(const HttpRequest& request, |
| 181 BasicHttpResponse* http_response) { | 249 BasicHttpResponse* http_response) { |
| 182 http_response->set_code(net::HTTP_BAD_REQUEST); | 250 http_response->set_code(net::HTTP_UNAUTHORIZED); |
| 251 if (merge_session_params_.session_sid_cookie.empty() || |
| 252 merge_session_params_.session_lsid_cookie.empty()) { |
| 253 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 254 return; |
| 255 } |
| 183 | 256 |
| 184 std::string uber_token; | 257 std::string uber_token; |
| 185 if (!GetQueryParameter(request.content, "uberauth", &uber_token) || | 258 if (!GetQueryParameter(request.content, "uberauth", &uber_token) || |
| 186 uber_token != fake_gaia_uber_token_) { | 259 uber_token != merge_session_params_.gaia_uber_token) { |
| 187 LOG(ERROR) << "Missing or invalid 'uberauth' param in /MergeSession call"; | 260 LOG(ERROR) << "Missing or invalid 'uberauth' param in /MergeSession call"; |
| 188 return; | 261 return; |
| 189 } | 262 } |
| 190 | 263 |
| 191 std::string continue_url; | 264 std::string continue_url; |
| 192 if (!GetQueryParameter(request.content, "continue", &continue_url)) { | 265 if (!GetQueryParameter(request.content, "continue", &continue_url)) { |
| 193 LOG(ERROR) << "Missing or invalid 'continue' param in /MergeSession call"; | 266 LOG(ERROR) << "Missing or invalid 'continue' param in /MergeSession call"; |
| 194 return; | 267 return; |
| 195 } | 268 } |
| 196 | 269 |
| 197 std::string source; | 270 std::string source; |
| 198 if (!GetQueryParameter(request.content, "source", &source)) { | 271 if (!GetQueryParameter(request.content, "source", &source)) { |
| 199 LOG(ERROR) << "Missing or invalid 'source' param in /MergeSession call"; | 272 LOG(ERROR) << "Missing or invalid 'source' param in /MergeSession call"; |
| 200 return; | 273 return; |
| 201 } | 274 } |
| 202 | 275 |
| 203 http_response->AddCustomHeader( | 276 http_response->AddCustomHeader( |
| 204 "Set-Cookie", | 277 "Set-Cookie", |
| 205 base::StringPrintf( | 278 base::StringPrintf( |
| 206 "SID=%s; LSID=%s; Path=/; Secure; HttpOnly;", | 279 "SID=%s; Path=/; HttpOnly;", |
| 207 fake_session_sid_cookie_.c_str(), | 280 merge_session_params_.session_sid_cookie.c_str())); |
| 208 fake_session_lsid_cookie_.c_str())); | 281 http_response->AddCustomHeader( |
| 282 "Set-Cookie", |
| 283 base::StringPrintf( |
| 284 "LSID=%s; Path=/; HttpOnly;", |
| 285 merge_session_params_.session_lsid_cookie.c_str())); |
| 209 // TODO(zelidrag): Not used now. | 286 // TODO(zelidrag): Not used now. |
| 210 http_response->set_content("OK"); | 287 http_response->set_content("OK"); |
| 211 http_response->set_code(net::HTTP_OK); | 288 http_response->set_code(net::HTTP_OK); |
| 212 } | 289 } |
| 213 | 290 |
| 214 | 291 |
| 215 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, | 292 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, |
| 216 BasicHttpResponse* http_response) { | 293 BasicHttpResponse* http_response) { |
| 217 std::string continue_url = | 294 std::string continue_url = |
| 218 GaiaUrls::GetInstance()->service_login_url().spec(); | 295 GaiaUrls::GetInstance()->service_login_url().spec(); |
| 219 GetQueryParameter(request.content, "continue", &continue_url); | 296 GetQueryParameter(request.content, "continue", &continue_url); |
| 220 | 297 |
| 221 std::string redirect_url = continue_url; | 298 std::string redirect_url = continue_url; |
| 222 | 299 |
| 223 std::string email; | 300 std::string email; |
| 224 if (GetQueryParameter(request.content, "Email", &email) && | 301 if (GetQueryParameter(request.content, "Email", &email) && |
| 225 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { | 302 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { |
| 226 GURL url(saml_account_idp_map_[email]); | 303 GURL url(saml_account_idp_map_[email]); |
| 227 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); | 304 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); |
| 228 url = net::AppendQueryParameter(url, "RelayState", continue_url); | 305 url = net::AppendQueryParameter(url, "RelayState", continue_url); |
| 229 redirect_url = url.spec(); | 306 redirect_url = url.spec(); |
| 230 } | 307 } |
| 231 | 308 |
| 309 if (!merge_session_params_.auth_sid_cookie.empty() && |
| 310 !merge_session_params_.auth_lsid_cookie.empty()) { |
| 311 http_response->AddCustomHeader( |
| 312 "Set-Cookie", |
| 313 base::StringPrintf( |
| 314 "SID=%s; Path=/; HttpOnly;", |
| 315 merge_session_params_.auth_sid_cookie.c_str())); |
| 316 http_response->AddCustomHeader( |
| 317 "Set-Cookie", |
| 318 base::StringPrintf( |
| 319 "LSID=%s; Path=/; HttpOnly;", |
| 320 merge_session_params_.auth_lsid_cookie.c_str())); |
| 321 } |
| 322 |
| 232 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 323 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
| 233 http_response->AddCustomHeader("Location", redirect_url); | 324 http_response->AddCustomHeader("Location", redirect_url); |
| 234 } | 325 } |
| 235 | 326 |
| 236 void FakeGaia::HandleSSO(const HttpRequest& request, | 327 void FakeGaia::HandleSSO(const HttpRequest& request, |
| 237 BasicHttpResponse* http_response) { | 328 BasicHttpResponse* http_response) { |
| 238 std::string relay_state; | 329 std::string relay_state; |
| 239 GetQueryParameter(request.content, "RelayState", &relay_state); | 330 GetQueryParameter(request.content, "RelayState", &relay_state); |
| 240 std::string redirect_url = relay_state; | 331 std::string redirect_url = relay_state; |
| 241 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 332 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 253 GetQueryParameter(request.content, "scope", &scope); | 344 GetQueryParameter(request.content, "scope", &scope); |
| 254 | 345 |
| 255 if (!GetQueryParameter(request.content, "grant_type", &grant_type)) { | 346 if (!GetQueryParameter(request.content, "grant_type", &grant_type)) { |
| 256 http_response->set_code(net::HTTP_BAD_REQUEST); | 347 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 257 LOG(ERROR) << "No 'grant_type' param in /o/oauth2/token"; | 348 LOG(ERROR) << "No 'grant_type' param in /o/oauth2/token"; |
| 258 return; | 349 return; |
| 259 } | 350 } |
| 260 | 351 |
| 261 if (grant_type == "authorization_code") { | 352 if (grant_type == "authorization_code") { |
| 262 if (!GetQueryParameter(request.content, "code", &auth_code) || | 353 if (!GetQueryParameter(request.content, "code", &auth_code) || |
| 263 auth_code != fake_auth_code_) { | 354 auth_code != merge_session_params_.auth_code) { |
| 264 http_response->set_code(net::HTTP_BAD_REQUEST); | 355 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 265 LOG(ERROR) << "No 'code' param in /o/oauth2/token"; | 356 LOG(ERROR) << "No 'code' param in /o/oauth2/token"; |
| 266 return; | 357 return; |
| 267 } | 358 } |
| 268 | 359 |
| 269 if (GaiaUrls::GetInstance()->oauth1_login_scope() != scope) { | 360 if (GaiaUrls::GetInstance()->oauth1_login_scope() != scope) { |
| 270 http_response->set_code(net::HTTP_BAD_REQUEST); | 361 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 271 LOG(ERROR) << "Invalid scope for /o/oauth2/token - " << scope; | 362 LOG(ERROR) << "Invalid scope for /o/oauth2/token - " << scope; |
| 272 return; | 363 return; |
| 273 } | 364 } |
| 274 | 365 |
| 275 base::DictionaryValue response_dict; | 366 base::DictionaryValue response_dict; |
| 276 response_dict.SetString("refresh_token", fake_refresh_token_); | 367 response_dict.SetString("refresh_token", |
| 277 response_dict.SetString("access_token", fake_access_token_); | 368 merge_session_params_.refresh_token); |
| 369 response_dict.SetString("access_token", |
| 370 merge_session_params_.access_token); |
| 278 response_dict.SetInteger("expires_in", 3600); | 371 response_dict.SetInteger("expires_in", 3600); |
| 279 FormatJSONResponse(response_dict, http_response); | 372 FormatJSONResponse(response_dict, http_response); |
| 280 } else if (GetQueryParameter(request.content, | 373 } else if (GetQueryParameter(request.content, |
| 281 "refresh_token", | 374 "refresh_token", |
| 282 &refresh_token) && | 375 &refresh_token) && |
| 283 GetQueryParameter(request.content, | 376 GetQueryParameter(request.content, |
| 284 "client_id", | 377 "client_id", |
| 285 &client_id) && | 378 &client_id) && |
| 286 (token_info = FindAccessTokenInfo(refresh_token, | 379 (token_info = FindAccessTokenInfo(refresh_token, |
| 287 client_id, | 380 client_id, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 response_dict.SetString("issueAdvice", "auto"); | 438 response_dict.SetString("issueAdvice", "auto"); |
| 346 response_dict.SetString("expiresIn", | 439 response_dict.SetString("expiresIn", |
| 347 base::IntToString(token_info->expires_in)); | 440 base::IntToString(token_info->expires_in)); |
| 348 response_dict.SetString("token", token_info->token); | 441 response_dict.SetString("token", token_info->token); |
| 349 FormatJSONResponse(response_dict, http_response); | 442 FormatJSONResponse(response_dict, http_response); |
| 350 } else { | 443 } else { |
| 351 http_response->set_code(net::HTTP_BAD_REQUEST); | 444 http_response->set_code(net::HTTP_BAD_REQUEST); |
| 352 } | 445 } |
| 353 } | 446 } |
| 354 | 447 |
| 355 | |
| 356 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { | 448 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { |
| 357 // The scheme and host of the URL is actually not important but required to | 449 // The scheme and host of the URL is actually not important but required to |
| 358 // get a valid GURL in order to parse |request.relative_url|. | 450 // get a valid GURL in order to parse |request.relative_url|. |
| 359 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 451 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
| 360 std::string request_path = request_url.path(); | 452 std::string request_path = request_url.path(); |
| 361 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 453 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
| 362 RequestHandlerMap::iterator iter = request_handlers_.find(request_path); | 454 RequestHandlerMap::iterator iter = request_handlers_.find(request_path); |
| 363 if (iter != request_handlers_.end()) { | 455 if (iter != request_handlers_.end()) { |
| 364 LOG(WARNING) << "Serving request " << request_path; | 456 LOG(WARNING) << "Serving request " << request_path; |
| 365 iter->second.Run(request, http_response.get()); | 457 iter->second.Run(request, http_response.get()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 | 507 |
| 416 // static | 508 // static |
| 417 bool FakeGaia::GetQueryParameter(const std::string& query, | 509 bool FakeGaia::GetQueryParameter(const std::string& query, |
| 418 const std::string& key, | 510 const std::string& key, |
| 419 std::string* value) { | 511 std::string* value) { |
| 420 // Name and scheme actually don't matter, but are required to get a valid URL | 512 // Name and scheme actually don't matter, but are required to get a valid URL |
| 421 // for parsing. | 513 // for parsing. |
| 422 GURL query_url("http://localhost?" + query); | 514 GURL query_url("http://localhost?" + query); |
| 423 return net::GetValueForKeyInQuery(query_url, key, value); | 515 return net::GetValueForKeyInQuery(query_url, key, value); |
| 424 } | 516 } |
| 425 | |
| 426 // static | |
| 427 bool FakeGaia::GetAccessToken(const HttpRequest& request, | |
| 428 const char* auth_token_prefix, | |
| 429 std::string* access_token) { | |
| 430 std::map<std::string, std::string>::const_iterator auth_header_entry = | |
| 431 request.headers.find("Authorization"); | |
| 432 if (auth_header_entry != request.headers.end()) { | |
| 433 if (StartsWithASCII(auth_header_entry->second, auth_token_prefix, true)) { | |
| 434 *access_token = auth_header_entry->second.substr( | |
| 435 strlen(auth_token_prefix)); | |
| 436 return true; | |
| 437 } | |
| 438 } | |
| 439 | |
| 440 return false; | |
| 441 } | |
| OLD | NEW |