Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/arc/arc_auth_fetcher.h" | |
| 6 | |
| 7 #include "base/strings/string_split.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "google_apis/gaia/gaia_constants.h" | |
| 12 #include "google_apis/gaia/gaia_urls.h" | |
| 13 #include "net/http/http_status_code.h" | |
| 14 #include "net/url_request/url_fetcher.h" | |
| 15 | |
| 16 namespace arc { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kGMSCoreClientId[] = | |
| 21 "1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.googleusercontent.com"; | |
| 22 const char kCookiePartSecure[] = "secure"; | |
| 23 const char kCookiePartHttpOnly[] = "httponly"; | |
| 24 const char kCookiePartCodePrefix[] = "oauth_code="; | |
| 25 const int kCookiePartCodePrefixLength = arraysize(kCookiePartCodePrefix) - 1; | |
| 26 | |
| 27 static bool CookiePartsContains(const std::vector<std::string>& parts, | |
| 28 const char* part) { | |
| 29 for (std::vector<std::string>::const_iterator it = parts.begin(); | |
| 30 it != parts.end(); ++it) { | |
| 31 if (base::LowerCaseEqualsASCII(*it, part)) | |
| 32 return true; | |
| 33 } | |
| 34 return false; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 ArcAuthFetcher::ArcAuthFetcher(Profile* profile, Delegate* delegate) | |
| 40 : profile_(profile), delegate_(delegate) { | |
| 41 FetchAuthCode(); | |
| 42 } | |
| 43 | |
| 44 ArcAuthFetcher::~ArcAuthFetcher() {} | |
| 45 | |
| 46 // static | |
| 47 GURL ArcAuthFetcher::CreateURL(Profile* profile) { | |
| 48 DCHECK(profile != nullptr); | |
| 49 | |
| 50 std::string query_string = base::StringPrintf( | |
| 51 "?scope=%s&client_id=%s&email=%s", GaiaConstants::kOAuth1LoginScope, | |
| 52 kGMSCoreClientId, profile->GetProfileUserName().c_str()); | |
| 53 | |
| 54 return GaiaUrls::GetInstance()->client_login_to_oauth2_url().Resolve( | |
| 55 query_string); | |
| 56 } | |
| 57 | |
| 58 void ArcAuthFetcher::FetchAuthCode() { | |
| 59 auth_fetcher_ = | |
| 60 net::URLFetcher::Create(CreateURL(profile_), net::URLFetcher::GET, this); | |
| 61 auth_fetcher_->SetRequestContext(profile_->GetRequestContext()); | |
|
xiyuan
2016/01/26 23:37:05
Looks like the class does not need the whole |prof
khmel
2016/01/27 22:36:20
Good point! Thanks
| |
| 62 // Executed asynchronously. | |
| 63 auth_fetcher_->Start(); | |
| 64 } | |
| 65 | |
| 66 // static | |
| 67 bool ArcAuthFetcher::ParseAuthCode(const net::URLFetcher* source, | |
| 68 std::string* code) { | |
| 69 DCHECK(source != nullptr && code != nullptr); | |
| 70 net::ResponseCookies::const_iterator iter; | |
| 71 const net::ResponseCookies& cookies = source->GetCookies(); | |
| 72 for (iter = cookies.begin(); iter != cookies.end(); ++iter) { | |
| 73 std::vector<std::string> parts = base::SplitString( | |
| 74 *iter, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 75 // Per documentation, the cookie should have Secure and HttpOnly. | |
| 76 if (!CookiePartsContains(parts, kCookiePartSecure) || | |
| 77 !CookiePartsContains(parts, kCookiePartHttpOnly)) { | |
| 78 continue; | |
| 79 } | |
| 80 | |
| 81 std::vector<std::string>::const_iterator iter; | |
| 82 for (iter = parts.begin(); iter != parts.end(); ++iter) { | |
| 83 const std::string& part = *iter; | |
| 84 if (base::StartsWith(part, kCookiePartCodePrefix, | |
| 85 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 86 *code = part.substr(kCookiePartCodePrefixLength); | |
| 87 return true; | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 void ArcAuthFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 96 if (!source->GetStatus().is_success() || | |
| 97 source->GetResponseCode() != net::HTTP_OK) { | |
| 98 VLOG(2) << "Failed to receive auth response. Response code: " | |
| 99 << source->GetResponseCode() << "."; | |
| 100 delegate_->OnAuthCodeFailed(); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 std::string auth_code; | |
| 105 if (!ParseAuthCode(source, &auth_code)) { | |
| 106 delegate_->OnAuthCodeNeedUI(); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 delegate_->OnAuthCodeFetched(auth_code); | |
| 111 } | |
| 112 | |
| 113 } // namespace arc | |
| OLD | NEW |