Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "components/signin/core/browser/chrome_connected_header_helper.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "components/google/core/browser/google_util.h" | |
| 13 #include "components/signin/core/common/profile_management_switches.h" | |
| 14 #include "google_apis/gaia/gaia_auth_util.h" | |
| 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace signin { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const char kEnableAccountConsistencyAttrName[] = "enable_account_consistency"; | |
| 23 const char kGaiaIdAttrName[] = "id"; | |
| 24 const char kProfileModeAttrName[] = "mode"; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 extern const char kChromeConnectedHeader[] = "X-Chrome-Connected"; | |
| 29 | |
| 30 // static | |
| 31 std::string ChromeConnectedHeaderHelper::BuildRequestCookieIfPossible( | |
| 32 const GURL& url, | |
| 33 const std::string& account_id, | |
| 34 const content_settings::CookieSettings* cookie_settings, | |
| 35 int profile_mode_mask) { | |
| 36 ChromeConnectedHeaderHelper chrome_connected_helper; | |
| 37 return chrome_connected_helper.BuildRequestHeaderIfPossible( | |
| 38 false /* is_header_request */, url, account_id, cookie_settings, | |
| 39 profile_mode_mask); | |
| 40 } | |
| 41 | |
| 42 bool ChromeConnectedHeaderHelper::IsUrlEligibleToIncludeGaiaId( | |
| 43 const GURL& url, | |
| 44 bool is_header_request) { | |
| 45 if (is_header_request) { | |
| 46 // GAIA Id is only necessary for Drive. Don't set it otherwise. | |
|
msarda
2017/06/08 23:43:04
Let's be consistent and use "Gaia ID" everywhere.
droger
2017/06/09 09:52:24
Done.
| |
| 47 return IsDriveOrigin(url.GetOrigin()); | |
| 48 } | |
| 49 | |
| 50 // Cookie requests don't have the granularity to only include the GAIA Id | |
| 51 // for Drive origin. Set it on all google.com instead. | |
| 52 if (!url.SchemeIsCryptographic()) | |
| 53 return false; | |
| 54 | |
| 55 const std::string kGoogleDomain = "google.com"; | |
| 56 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( | |
| 57 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 58 return domain == kGoogleDomain; | |
| 59 } | |
| 60 | |
| 61 bool ChromeConnectedHeaderHelper::IsDriveOrigin(const GURL& url) { | |
| 62 if (!url.SchemeIsCryptographic()) | |
| 63 return false; | |
| 64 | |
| 65 const GURL kGoogleDriveURL("https://drive.google.com"); | |
| 66 const GURL kGoogleDocsURL("https://docs.google.com"); | |
| 67 return url == kGoogleDriveURL || url == kGoogleDocsURL; | |
| 68 } | |
| 69 | |
| 70 bool ChromeConnectedHeaderHelper::IsUrlEligibleForRequestHeader( | |
| 71 const GURL& url) { | |
| 72 // Only set the header for Drive and Gaia always, and other Google | |
| 73 // properties if account consistency is enabled. Vasquette, which is | |
| 74 // integrated with most Google properties, needs the header to redirect | |
| 75 // certain user actions to Chrome native UI. Drive and Gaia need the header | |
| 76 // to tell if the current user is connected. The drive path is a temporary | |
| 77 // workaround until the more generic chrome.principals API is available. | |
|
msarda
2017/06/08 23:43:04
You may remove the comment about the "chrome.princ
droger
2017/06/09 09:52:24
Done.
| |
| 78 | |
| 79 // Consider the account id sensitive and limit it to secure domains. | |
|
msarda
2017/06/08 23:43:04
Consistency: "id" or "ID" everywhere.
droger
2017/06/09 09:52:24
Done.
| |
| 80 if (!url.SchemeIsCryptographic()) | |
| 81 return false; | |
| 82 | |
| 83 GURL origin(url.GetOrigin()); | |
| 84 bool is_enable_account_consistency = | |
| 85 switches::IsAccountConsistencyMirrorEnabled(); | |
| 86 bool is_google_url = is_enable_account_consistency && | |
| 87 (google_util::IsGoogleDomainUrl( | |
| 88 url, google_util::ALLOW_SUBDOMAIN, | |
| 89 google_util::DISALLOW_NON_STANDARD_PORTS) || | |
| 90 google_util::IsYoutubeDomainUrl( | |
| 91 url, google_util::ALLOW_SUBDOMAIN, | |
| 92 google_util::DISALLOW_NON_STANDARD_PORTS)); | |
| 93 return is_google_url || IsDriveOrigin(origin) || | |
| 94 gaia::IsGaiaSignonRealm(origin); | |
| 95 } | |
| 96 | |
| 97 std::string ChromeConnectedHeaderHelper::BuildRequestHeader( | |
| 98 bool is_header_request, | |
| 99 const GURL& url, | |
| 100 const std::string& account_id, | |
| 101 int profile_mode_mask) { | |
| 102 if (account_id.empty()) | |
| 103 return std::string(); | |
| 104 | |
| 105 std::vector<std::string> parts; | |
| 106 if (IsUrlEligibleToIncludeGaiaId(url, is_header_request)) { | |
| 107 // Only set the GAIA Id on domains that actually requires it. | |
| 108 parts.push_back( | |
| 109 base::StringPrintf("%s=%s", kGaiaIdAttrName, account_id.c_str())); | |
| 110 } | |
| 111 parts.push_back( | |
| 112 base::StringPrintf("%s=%s", kProfileModeAttrName, | |
| 113 base::IntToString(profile_mode_mask).c_str())); | |
| 114 parts.push_back(base::StringPrintf( | |
| 115 "%s=%s", kEnableAccountConsistencyAttrName, | |
| 116 switches::IsAccountConsistencyMirrorEnabled() ? "true" : "false")); | |
| 117 | |
| 118 return base::JoinString(parts, is_header_request ? "," : ":"); | |
| 119 } | |
| 120 | |
| 121 } // namespace signin | |
| OLD | NEW |