Chromium Code Reviews| 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" | |
| 11 #include "base/bind_helpers.h" | |
| 10 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 12 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 13 #include "base/logging.h" | 15 #include "base/logging.h" |
| 14 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | |
| 18 #include "base/values.h" | 21 #include "base/values.h" |
| 19 #include "google_apis/gaia/gaia_urls.h" | 22 #include "google_apis/gaia/gaia_urls.h" |
| 20 #include "net/base/url_util.h" | 23 #include "net/base/url_util.h" |
| 21 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
| 22 #include "net/test/embedded_test_server/http_request.h" | 25 #include "net/test/embedded_test_server/http_request.h" |
| 23 #include "net/test/embedded_test_server/http_response.h" | 26 #include "net/test/embedded_test_server/http_response.h" |
| 24 #include "url/url_parse.h" | 27 #include "url/url_parse.h" |
| 25 | 28 |
| 29 #define REGISTER_RESPONSE_HANDLER(url, method) \ | |
| 30 request_handlers_.insert(std::make_pair( \ | |
| 31 url.path(), base::Bind(&FakeGaia::method, base::Unretained(this)))) | |
| 32 | |
| 26 using namespace net::test_server; | 33 using namespace net::test_server; |
| 27 | 34 |
| 28 namespace { | 35 namespace { |
| 36 | |
| 29 const base::FilePath::CharType kServiceLogin[] = | 37 const base::FilePath::CharType kServiceLogin[] = |
| 30 FILE_PATH_LITERAL("google_apis/test/service_login.html"); | 38 FILE_PATH_LITERAL("google_apis/test/service_login.html"); |
| 39 | |
| 40 // OAuth2 Authentication header value prefix. | |
| 41 const char kAuthHeaderBearer[] = "Bearer "; | |
| 42 const char kAuthHeaderOAuth[] = "OAuth "; | |
| 43 | |
| 31 } | 44 } |
| 32 | 45 |
| 33 FakeGaia::AccessTokenInfo::AccessTokenInfo() | 46 FakeGaia::AccessTokenInfo::AccessTokenInfo() |
| 34 : expires_in(3600) {} | 47 : expires_in(3600) {} |
| 35 | 48 |
| 36 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} | 49 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} |
| 37 | 50 |
| 38 FakeGaia::FakeGaia() { | 51 FakeGaia::FakeGaia() { |
| 39 base::FilePath source_root_dir; | 52 base::FilePath source_root_dir; |
| 40 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); | 53 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); |
| 41 CHECK(base::ReadFileToString( | 54 CHECK(base::ReadFileToString( |
| 42 source_root_dir.Append(base::FilePath(kServiceLogin)), | 55 source_root_dir.Append(base::FilePath(kServiceLogin)), |
| 43 &service_login_response_)); | 56 &service_login_response_)); |
| 44 } | 57 } |
| 45 | 58 |
| 46 FakeGaia::~FakeGaia() {} | 59 FakeGaia::~FakeGaia() {} |
| 47 | 60 |
| 61 void FakeGaia::SetAuthTokens(const std::string& auth_code, | |
| 62 const std::string& refresh_token, | |
| 63 const std::string& access_token, | |
| 64 const std::string& gaia_uber_token, | |
| 65 const std::string& session_sid_cookie, | |
| 66 const std::string& session_lsid_cookie) { | |
| 67 fake_auth_code_ = auth_code; | |
| 68 fake_refresh_token_ = refresh_token; | |
| 69 fake_access_token_ = access_token; | |
| 70 fake_gaia_uber_token_ = gaia_uber_token; | |
| 71 fake_session_sid_cookie_ = session_sid_cookie; | |
| 72 fake_session_lsid_cookie_ = session_lsid_cookie; | |
| 73 } | |
| 74 | |
| 75 void FakeGaia::Initialize() { | |
| 76 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
| 77 // Handles /ServiceLogin GAIA call. | |
| 78 REGISTER_RESPONSE_HANDLER( | |
| 79 gaia_urls->service_login_url(), HandleServiceLogin); | |
| 80 | |
| 81 // Handles /ServiceLoginAuth GAIA call. | |
| 82 REGISTER_RESPONSE_HANDLER( | |
| 83 gaia_urls->service_login_auth_url(), HandleServiceLoginAuth); | |
| 84 | |
| 85 // Handles /o/oauth2/programmatic_auth GAIA call. | |
| 86 REGISTER_RESPONSE_HANDLER( | |
| 87 gaia_urls->client_login_to_oauth2_url(), HandleProgramaticAuth); | |
| 88 | |
| 89 // Handles /o/oauth2/token GAIA call. | |
| 90 REGISTER_RESPONSE_HANDLER( | |
| 91 gaia_urls->oauth2_token_url(), HandleAuthToken); | |
| 92 | |
| 93 // Handles /OAuthLogin GAIA call. | |
| 94 REGISTER_RESPONSE_HANDLER( | |
| 95 gaia_urls->oauth1_login_url(), HandleOAuthLogin); | |
| 96 | |
| 97 // Handles /MergeSession GAIA call. | |
| 98 REGISTER_RESPONSE_HANDLER( | |
| 99 gaia_urls->merge_session_url(), HandleMergeSession); | |
| 100 | |
| 101 // Handles /oauth2/v2/IssueToken GAIA call. | |
| 102 REGISTER_RESPONSE_HANDLER( | |
| 103 gaia_urls->oauth2_issue_token_url(), HandleIssueToken); | |
| 104 | |
| 105 // Handles /oauth2/v2/tokeninfo GAIA call. | |
| 106 REGISTER_RESPONSE_HANDLER( | |
| 107 gaia_urls->oauth2_token_info_url(), HandleTokenInfo); | |
| 108 } | |
| 109 | |
| 110 void FakeGaia::HandleProgramaticAuth( | |
| 111 const HttpRequest& request, | |
| 112 BasicHttpResponse* http_response) { | |
| 113 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
| 114 std::string scope; | |
| 115 if (!GetQueryParameter(request.content, "scope", &scope) || | |
| 116 gaia_urls->oauth1_login_scope() != scope) { | |
| 117 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 std::string client_id; | |
| 122 if (!GetQueryParameter(request.content, "client_id", &client_id) || | |
| 123 gaia_urls->oauth2_chrome_client_id() != client_id) { | |
| 124 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 http_response->AddCustomHeader( | |
| 129 "Set-Cookie", | |
| 130 base::StringPrintf( | |
| 131 "oauth_code=%s; Path=/o/GetOAuth2Token; Secure; HttpOnly;", | |
| 132 fake_auth_code_.c_str())); | |
| 133 http_response->set_code(net::HTTP_OK); | |
| 134 http_response->set_content_type("text/html"); | |
| 135 } | |
| 136 | |
| 137 void FakeGaia::HandleServiceLogin(const HttpRequest& request, | |
| 138 BasicHttpResponse* http_response) { | |
| 139 http_response->set_code(net::HTTP_OK); | |
| 140 http_response->set_content(service_login_response_); | |
| 141 http_response->set_content_type("text/html"); | |
| 142 } | |
| 143 | |
| 144 void FakeGaia::HandleOAuthLogin(const HttpRequest& request, | |
| 145 BasicHttpResponse* http_response) { | |
| 146 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 147 std::string access_token; | |
| 148 if (!GetAccessToken(request, kAuthHeaderOAuth, &access_token)) { | |
| 149 LOG(ERROR) << "/OAuthLogin missing access token in the header"; | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | |
| 154 std::string request_query = request_url.query(); | |
| 155 | |
| 156 std::string source; | |
| 157 if (!GetQueryParameter(request_query, "source", &source)) { | |
| 158 LOG(ERROR) << "Missing 'source' param in /OAuthLogin call"; | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 std::string issue_uberauth; | |
| 163 if (GetQueryParameter(request_query, "issueuberauth", &issue_uberauth) && | |
| 164 issue_uberauth == "1") { | |
| 165 http_response->set_content(fake_gaia_uber_token_); | |
| 166 http_response->set_code(net::HTTP_OK); | |
| 167 // Issue GAIA uber token. | |
| 168 } else { | |
| 169 LOG(FATAL) << "/OAuthLogin for SID/LSID is not supported"; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void FakeGaia::HandleMergeSession(const HttpRequest& request, | |
| 174 BasicHttpResponse* http_response) { | |
| 175 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 176 | |
| 177 std::string uber_token; | |
| 178 if (!GetQueryParameter(request.content, "uberauth", &uber_token) || | |
| 179 uber_token != fake_gaia_uber_token_) { | |
| 180 LOG(ERROR) << "Missing or invalid 'uberauth' param in /MergeSession call"; | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 std::string continue_url; | |
| 185 if (!GetQueryParameter(request.content, "continue", &continue_url)) { | |
| 186 LOG(ERROR) << "Missing or invalid 'continue' param in /MergeSession call"; | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 std::string source; | |
| 191 if (!GetQueryParameter(request.content, "source", &source)) { | |
| 192 LOG(ERROR) << "Missing or invalid 'source' param in /MergeSession call"; | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 http_response->AddCustomHeader( | |
| 197 "Set-Cookie", | |
| 198 base::StringPrintf( | |
| 199 "SID=%s; LSID=%s; Path=/; Secure; HttpOnly;", | |
| 200 fake_session_sid_cookie_.c_str(), | |
| 201 fake_session_lsid_cookie_.c_str())); | |
| 202 // TODO(zelidrag): Not used now. | |
| 203 http_response->set_content("OK"); | |
| 204 http_response->set_code(net::HTTP_OK); | |
| 205 } | |
| 206 | |
| 207 | |
| 208 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, | |
| 209 BasicHttpResponse* http_response) { | |
| 210 std::string continue_url = | |
| 211 GaiaUrls::GetInstance()->service_login_url().spec(); | |
| 212 GetQueryParameter(request.content, "continue", &continue_url); | |
| 213 | |
| 214 std::string redirect_url = continue_url; | |
| 215 | |
| 216 std::string email; | |
| 217 if (GetQueryParameter(request.content, "Email", &email) && | |
| 218 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { | |
| 219 GURL url(saml_account_idp_map_[email]); | |
| 220 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); | |
| 221 url = net::AppendQueryParameter(url, "RelayState", continue_url); | |
| 222 redirect_url = url.spec(); | |
| 223 } | |
| 224 | |
| 225 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | |
| 226 http_response->AddCustomHeader("Location", redirect_url); | |
| 227 } | |
| 228 | |
| 229 void FakeGaia::HandleSSO(const HttpRequest& request, | |
|
xiyuan
2013/12/17 16:52:37
Do we need to REGISTER_RESPONSE_HANDLER HandleSSO
zel
2013/12/17 17:10:30
Done.
| |
| 230 BasicHttpResponse* http_response) { | |
| 231 std::string relay_state; | |
| 232 GetQueryParameter(request.content, "RelayState", &relay_state); | |
| 233 std::string redirect_url = relay_state; | |
| 234 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | |
| 235 http_response->AddCustomHeader("Location", redirect_url); | |
| 236 } | |
| 237 | |
| 238 void FakeGaia::HandleAuthToken(const HttpRequest& request, | |
| 239 BasicHttpResponse* http_response) { | |
| 240 std::string grant_type; | |
| 241 std::string refresh_token; | |
| 242 std::string client_id; | |
| 243 std::string scope; | |
| 244 std::string auth_code; | |
| 245 const AccessTokenInfo* token_info = NULL; | |
| 246 GetQueryParameter(request.content, "scope", &scope); | |
| 247 | |
| 248 if (!GetQueryParameter(request.content, "grant_type", &grant_type)) { | |
| 249 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 250 LOG(ERROR) << "No 'grant_type' param in /o/oauth2/token"; | |
| 251 return; | |
| 252 } | |
| 253 | |
| 254 if (grant_type == "authorization_code") { | |
| 255 if (!GetQueryParameter(request.content, "code", &auth_code) || | |
| 256 auth_code != fake_auth_code_) { | |
| 257 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 258 LOG(ERROR) << "No 'code' param in /o/oauth2/token"; | |
| 259 return; | |
| 260 } | |
| 261 | |
| 262 if (GaiaUrls::GetInstance()->oauth1_login_scope() != scope) { | |
| 263 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 264 LOG(ERROR) << "Invalid scope for /o/oauth2/token - " << scope; | |
| 265 return; | |
| 266 } | |
| 267 | |
| 268 base::DictionaryValue response_dict; | |
| 269 response_dict.SetString("refresh_token", fake_refresh_token_); | |
| 270 response_dict.SetString("access_token", fake_access_token_); | |
| 271 response_dict.SetInteger("expires_in", 3600); | |
| 272 FormatJSONResponse(response_dict, http_response); | |
| 273 } else if (GetQueryParameter(request.content, | |
| 274 "refresh_token", | |
| 275 &refresh_token) && | |
| 276 GetQueryParameter(request.content, | |
| 277 "client_id", | |
| 278 &client_id) && | |
| 279 (token_info = FindAccessTokenInfo(refresh_token, | |
| 280 client_id, | |
| 281 scope))) { | |
| 282 base::DictionaryValue response_dict; | |
| 283 response_dict.SetString("access_token", token_info->token); | |
| 284 response_dict.SetInteger("expires_in", 3600); | |
| 285 FormatJSONResponse(response_dict, http_response); | |
| 286 } else { | |
| 287 LOG(ERROR) << "Bad request for /o/oauth2/token - " | |
| 288 << "refresh_token = " << refresh_token | |
| 289 << ", scope = " << scope | |
| 290 << ", client_id = " << client_id; | |
| 291 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 void FakeGaia::HandleTokenInfo(const HttpRequest& request, | |
| 296 BasicHttpResponse* http_response) { | |
| 297 const AccessTokenInfo* token_info = NULL; | |
| 298 std::string access_token; | |
| 299 if (GetQueryParameter(request.content, "access_token", &access_token)) { | |
| 300 for (AccessTokenInfoMap::const_iterator entry( | |
| 301 access_token_info_map_.begin()); | |
| 302 entry != access_token_info_map_.end(); | |
| 303 ++entry) { | |
| 304 if (entry->second.token == access_token) { | |
| 305 token_info = &(entry->second); | |
| 306 break; | |
| 307 } | |
| 308 } | |
| 309 } | |
| 310 | |
| 311 if (token_info) { | |
| 312 base::DictionaryValue response_dict; | |
| 313 response_dict.SetString("issued_to", token_info->issued_to); | |
| 314 response_dict.SetString("audience", token_info->audience); | |
| 315 response_dict.SetString("user_id", token_info->user_id); | |
| 316 std::vector<std::string> scope_vector(token_info->scopes.begin(), | |
| 317 token_info->scopes.end()); | |
| 318 response_dict.SetString("scope", JoinString(scope_vector, " ")); | |
| 319 response_dict.SetInteger("expires_in", token_info->expires_in); | |
| 320 response_dict.SetString("email", token_info->email); | |
| 321 FormatJSONResponse(response_dict, http_response); | |
| 322 } else { | |
| 323 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 324 } | |
| 325 } | |
| 326 | |
| 327 void FakeGaia::HandleIssueToken(const HttpRequest& request, | |
| 328 BasicHttpResponse* http_response) { | |
| 329 std::string access_token; | |
| 330 std::string scope; | |
| 331 std::string client_id; | |
| 332 const AccessTokenInfo* token_info = NULL; | |
| 333 if (GetAccessToken(request, kAuthHeaderBearer, &access_token) && | |
| 334 GetQueryParameter(request.content, "scope", &scope) && | |
| 335 GetQueryParameter(request.content, "client_id", &client_id) && | |
| 336 (token_info = FindAccessTokenInfo(access_token, client_id, scope))) { | |
| 337 base::DictionaryValue response_dict; | |
| 338 response_dict.SetString("issueAdvice", "auto"); | |
| 339 response_dict.SetString("expiresIn", | |
| 340 base::IntToString(token_info->expires_in)); | |
| 341 response_dict.SetString("token", token_info->token); | |
| 342 FormatJSONResponse(response_dict, http_response); | |
| 343 } else { | |
| 344 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 | |
| 48 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { | 349 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { |
| 49 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
| 50 | |
| 51 // The scheme and host of the URL is actually not important but required to | 350 // The scheme and host of the URL is actually not important but required to |
| 52 // get a valid GURL in order to parse |request.relative_url|. | 351 // get a valid GURL in order to parse |request.relative_url|. |
| 53 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 352 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
| 54 std::string request_path = request_url.path(); | 353 std::string request_path = request_url.path(); |
| 55 | |
| 56 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 354 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
| 57 if (request_path == gaia_urls->service_login_url().path()) { | 355 RequestHandlerMap::iterator iter = request_handlers_.find(request_path); |
| 58 http_response->set_code(net::HTTP_OK); | 356 if (iter != request_handlers_.end()) { |
| 59 http_response->set_content(service_login_response_); | 357 LOG(WARNING) << "Serving request " << request_path; |
| 60 http_response->set_content_type("text/html"); | 358 iter->second.Run(request, http_response.get()); |
| 61 } else if (request_path == gaia_urls->service_login_auth_url().path()) { | 359 } else { |
| 62 std::string continue_url = gaia_urls->service_login_url().spec(); | 360 LOG(ERROR) << "Unhandled request " << request_path; |
| 63 GetQueryParameter(request.content, "continue", &continue_url); | 361 return scoped_ptr<HttpResponse>(); // Request not understood. |
| 64 std::string redirect_url = continue_url; | |
| 65 | |
| 66 std::string email; | |
| 67 if (GetQueryParameter(request.content, "Email", &email) && | |
| 68 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { | |
| 69 GURL url(saml_account_idp_map_[email]); | |
| 70 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); | |
| 71 url = net::AppendQueryParameter(url, "RelayState", continue_url); | |
| 72 redirect_url = url.spec(); | |
| 73 } | |
| 74 | |
| 75 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | |
| 76 http_response->AddCustomHeader("Location", redirect_url); | |
| 77 } else if (request_path == gaia_urls->oauth2_token_url().path()) { | |
| 78 std::string refresh_token; | |
| 79 std::string client_id; | |
| 80 std::string scope; | |
| 81 const AccessTokenInfo* token_info = NULL; | |
| 82 GetQueryParameter(request.content, "scope", &scope); | |
| 83 if (GetQueryParameter(request.content, "refresh_token", &refresh_token) && | |
| 84 GetQueryParameter(request.content, "client_id", &client_id) && | |
| 85 (token_info = GetAccessTokenInfo(refresh_token, client_id, scope))) { | |
| 86 base::DictionaryValue response_dict; | |
| 87 response_dict.SetString("access_token", token_info->token); | |
| 88 response_dict.SetInteger("expires_in", 3600); | |
| 89 FormatJSONResponse(response_dict, http_response.get()); | |
| 90 } else { | |
| 91 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 92 } | |
| 93 } else if (request_path == gaia_urls->oauth2_token_info_url().path()) { | |
| 94 const AccessTokenInfo* token_info = NULL; | |
| 95 std::string access_token; | |
| 96 if (GetQueryParameter(request.content, "access_token", &access_token)) { | |
| 97 for (AccessTokenInfoMap::const_iterator entry( | |
| 98 access_token_info_map_.begin()); | |
| 99 entry != access_token_info_map_.end(); | |
| 100 ++entry) { | |
| 101 if (entry->second.token == access_token) { | |
| 102 token_info = &(entry->second); | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 if (token_info) { | |
| 109 base::DictionaryValue response_dict; | |
| 110 response_dict.SetString("issued_to", token_info->issued_to); | |
| 111 response_dict.SetString("audience", token_info->audience); | |
| 112 response_dict.SetString("user_id", token_info->user_id); | |
| 113 std::vector<std::string> scope_vector(token_info->scopes.begin(), | |
| 114 token_info->scopes.end()); | |
| 115 response_dict.SetString("scope", JoinString(scope_vector, " ")); | |
| 116 response_dict.SetInteger("expires_in", token_info->expires_in); | |
| 117 response_dict.SetString("email", token_info->email); | |
| 118 FormatJSONResponse(response_dict, http_response.get()); | |
| 119 } else { | |
| 120 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 121 } | |
| 122 } else if (request_path == gaia_urls->oauth2_issue_token_url().path()) { | |
| 123 std::string access_token; | |
| 124 std::map<std::string, std::string>::const_iterator auth_header_entry = | |
| 125 request.headers.find("Authorization"); | |
| 126 if (auth_header_entry != request.headers.end()) { | |
| 127 if (StartsWithASCII(auth_header_entry->second, "Bearer ", true)) | |
| 128 access_token = auth_header_entry->second.substr(7); | |
| 129 } | |
| 130 | |
| 131 std::string scope; | |
| 132 std::string client_id; | |
| 133 const AccessTokenInfo* token_info = NULL; | |
| 134 if (GetQueryParameter(request.content, "scope", &scope) && | |
| 135 GetQueryParameter(request.content, "client_id", &client_id) && | |
| 136 (token_info = GetAccessTokenInfo(access_token, client_id, scope))) { | |
| 137 base::DictionaryValue response_dict; | |
| 138 response_dict.SetString("issueAdvice", "auto"); | |
| 139 response_dict.SetString("expiresIn", | |
| 140 base::IntToString(token_info->expires_in)); | |
| 141 response_dict.SetString("token", token_info->token); | |
| 142 FormatJSONResponse(response_dict, http_response.get()); | |
| 143 } else { | |
| 144 http_response->set_code(net::HTTP_BAD_REQUEST); | |
| 145 } | |
| 146 } else if (request_path == "/SSO") { | |
| 147 std::string relay_state; | |
| 148 GetQueryParameter(request.content, "RelayState", &relay_state); | |
| 149 std::string redirect_url = relay_state; | |
| 150 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | |
| 151 http_response->AddCustomHeader("Location", redirect_url); | |
| 152 } else { | |
| 153 // Request not understood. | |
| 154 return scoped_ptr<HttpResponse>(); | |
| 155 } | 362 } |
| 156 | 363 |
| 157 return http_response.PassAs<HttpResponse>(); | 364 return http_response.PassAs<HttpResponse>(); |
| 158 } | 365 } |
| 159 | 366 |
| 160 void FakeGaia::IssueOAuthToken(const std::string& auth_token, | 367 void FakeGaia::IssueOAuthToken(const std::string& auth_token, |
| 161 const AccessTokenInfo& token_info) { | 368 const AccessTokenInfo& token_info) { |
| 162 access_token_info_map_.insert(std::make_pair(auth_token, token_info)); | 369 access_token_info_map_.insert(std::make_pair(auth_token, token_info)); |
| 163 } | 370 } |
| 164 | 371 |
| 165 void FakeGaia::RegisterSamlUser(const std::string& account_id, | 372 void FakeGaia::RegisterSamlUser(const std::string& account_id, |
| 166 const GURL& saml_idp) { | 373 const GURL& saml_idp) { |
| 167 saml_account_idp_map_[account_id] = saml_idp; | 374 saml_account_idp_map_[account_id] = saml_idp; |
| 168 } | 375 } |
| 169 | 376 |
| 170 // static | |
| 171 bool FakeGaia::GetQueryParameter(const std::string& query, | |
| 172 const std::string& key, | |
| 173 std::string* value) { | |
| 174 // Name and scheme actually don't matter, but are required to get a valid URL | |
| 175 // for parsing. | |
| 176 GURL query_url("http://localhost?" + query); | |
| 177 return net::GetValueForKeyInQuery(query_url, key, value); | |
| 178 } | |
| 179 | |
| 180 void FakeGaia::FormatJSONResponse(const base::DictionaryValue& response_dict, | 377 void FakeGaia::FormatJSONResponse(const base::DictionaryValue& response_dict, |
| 181 BasicHttpResponse* http_response) { | 378 BasicHttpResponse* http_response) { |
| 182 std::string response_json; | 379 std::string response_json; |
| 183 base::JSONWriter::Write(&response_dict, &response_json); | 380 base::JSONWriter::Write(&response_dict, &response_json); |
| 184 http_response->set_content(response_json); | 381 http_response->set_content(response_json); |
| 185 http_response->set_code(net::HTTP_OK); | 382 http_response->set_code(net::HTTP_OK); |
| 186 } | 383 } |
| 187 | 384 |
| 188 const FakeGaia::AccessTokenInfo* FakeGaia::GetAccessTokenInfo( | 385 const FakeGaia::AccessTokenInfo* FakeGaia::FindAccessTokenInfo( |
| 189 const std::string& auth_token, | 386 const std::string& auth_token, |
| 190 const std::string& client_id, | 387 const std::string& client_id, |
| 191 const std::string& scope_string) const { | 388 const std::string& scope_string) const { |
| 192 if (auth_token.empty() || client_id.empty()) | 389 if (auth_token.empty() || client_id.empty()) |
| 193 return NULL; | 390 return NULL; |
| 194 | 391 |
| 195 std::vector<std::string> scope_list; | 392 std::vector<std::string> scope_list; |
| 196 base::SplitString(scope_string, ' ', &scope_list); | 393 base::SplitString(scope_string, ' ', &scope_list); |
| 197 ScopeSet scopes(scope_list.begin(), scope_list.end()); | 394 ScopeSet scopes(scope_list.begin(), scope_list.end()); |
| 198 | 395 |
| 199 for (AccessTokenInfoMap::const_iterator entry( | 396 for (AccessTokenInfoMap::const_iterator entry( |
| 200 access_token_info_map_.lower_bound(auth_token)); | 397 access_token_info_map_.lower_bound(auth_token)); |
| 201 entry != access_token_info_map_.upper_bound(auth_token); | 398 entry != access_token_info_map_.upper_bound(auth_token); |
| 202 ++entry) { | 399 ++entry) { |
| 203 if (entry->second.audience == client_id && | 400 if (entry->second.audience == client_id && |
| 204 (scope_string.empty() || entry->second.scopes == scopes)) { | 401 (scope_string.empty() || entry->second.scopes == scopes)) { |
| 205 return &(entry->second); | 402 return &(entry->second); |
| 206 } | 403 } |
| 207 } | 404 } |
| 208 | 405 |
| 209 return NULL; | 406 return NULL; |
| 210 } | 407 } |
| 408 | |
| 409 // static | |
| 410 bool FakeGaia::GetQueryParameter(const std::string& query, | |
| 411 const std::string& key, | |
| 412 std::string* value) { | |
| 413 // Name and scheme actually don't matter, but are required to get a valid URL | |
| 414 // for parsing. | |
| 415 GURL query_url("http://localhost?" + query); | |
| 416 return net::GetValueForKeyInQuery(query_url, key, value); | |
| 417 } | |
| 418 | |
| 419 // static | |
| 420 bool FakeGaia::GetAccessToken(const HttpRequest& request, | |
| 421 const char* auth_token_prefix, | |
| 422 std::string* access_token) { | |
| 423 std::map<std::string, std::string>::const_iterator auth_header_entry = | |
| 424 request.headers.find("Authorization"); | |
| 425 if (auth_header_entry != request.headers.end()) { | |
| 426 if (StartsWithASCII(auth_header_entry->second, auth_token_prefix, true)) { | |
| 427 *access_token = auth_header_entry->second.substr( | |
| 428 strlen(auth_token_prefix)); | |
| 429 return true; | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 return false; | |
| 434 } | |
| OLD | NEW |