| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/signin/signin_manager_base.h" | 5 #include "chrome/browser/signin/signin_manager_base.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "chrome/browser/signin/token_service_factory.h" | 26 #include "chrome/browser/signin/token_service_factory.h" |
| 27 #include "chrome/browser/sync/sync_prefs.h" | 27 #include "chrome/browser/sync/sync_prefs.h" |
| 28 #include "chrome/browser/ui/global_error/global_error_service.h" | 28 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 29 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 29 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 30 #include "chrome/common/chrome_notification_types.h" | 30 #include "chrome/common/chrome_notification_types.h" |
| 31 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
| 33 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 34 #include "google_apis/gaia/gaia_constants.h" | 34 #include "google_apis/gaia/gaia_constants.h" |
| 35 #include "google_apis/gaia/gaia_urls.h" | 35 #include "google_apis/gaia/gaia_urls.h" |
| 36 #include "third_party/icu/public/i18n/unicode/regex.h" | |
| 37 | 36 |
| 38 using namespace signin_internals_util; | 37 using namespace signin_internals_util; |
| 39 | 38 |
| 40 using content::BrowserThread; | 39 using content::BrowserThread; |
| 41 | 40 |
| 42 // static | 41 // static |
| 43 bool SigninManagerBase::AreSigninCookiesAllowed(Profile* profile) { | 42 bool SigninManagerBase::AreSigninCookiesAllowed(Profile* profile) { |
| 44 CookieSettings* cookie_settings = | 43 CookieSettings* cookie_settings = |
| 45 CookieSettings::Factory::GetForProfile(profile); | 44 CookieSettings::Factory::GetForProfile(profile); |
| 46 return AreSigninCookiesAllowed(cookie_settings); | 45 return AreSigninCookiesAllowed(cookie_settings); |
| 47 } | 46 } |
| 48 | 47 |
| 49 // static | 48 // static |
| 50 bool SigninManagerBase::AreSigninCookiesAllowed( | 49 bool SigninManagerBase::AreSigninCookiesAllowed( |
| 51 CookieSettings* cookie_settings) { | 50 CookieSettings* cookie_settings) { |
| 52 return cookie_settings && | 51 return cookie_settings && |
| 53 cookie_settings->IsSettingCookieAllowed( | 52 cookie_settings->IsSettingCookieAllowed( |
| 54 GURL(GaiaUrls::GetInstance()->gaia_origin_url()), | 53 GURL(GaiaUrls::GetInstance()->gaia_origin_url()), |
| 55 GURL(GaiaUrls::GetInstance()->gaia_origin_url())); | 54 GURL(GaiaUrls::GetInstance()->gaia_origin_url())); |
| 56 } | 55 } |
| 57 | 56 |
| 58 // static | |
| 59 bool SigninManagerBase::IsAllowedUsername(const std::string& username, | |
| 60 const std::string& policy) { | |
| 61 if (policy.empty()) | |
| 62 return true; | |
| 63 | |
| 64 // Patterns like "*@foo.com" are not accepted by our regex engine (since they | |
| 65 // are not valid regular expressions - they should instead be ".*@foo.com"). | |
| 66 // For convenience, detect these patterns and insert a "." character at the | |
| 67 // front. | |
| 68 string16 pattern = UTF8ToUTF16(policy); | |
| 69 if (pattern[0] == L'*') | |
| 70 pattern.insert(pattern.begin(), L'.'); | |
| 71 | |
| 72 // See if the username matches the policy-provided pattern. | |
| 73 UErrorCode status = U_ZERO_ERROR; | |
| 74 const icu::UnicodeString icu_pattern(pattern.data(), pattern.length()); | |
| 75 icu::RegexMatcher matcher(icu_pattern, UREGEX_CASE_INSENSITIVE, status); | |
| 76 if (!U_SUCCESS(status)) { | |
| 77 LOG(ERROR) << "Invalid login regex: " << pattern << ", status: " << status; | |
| 78 // If an invalid pattern is provided, then prohibit *all* logins (better to | |
| 79 // break signin than to quietly allow users to sign in). | |
| 80 return false; | |
| 81 } | |
| 82 string16 username16 = UTF8ToUTF16(username); | |
| 83 icu::UnicodeString icu_input(username16.data(), username16.length()); | |
| 84 matcher.reset(icu_input); | |
| 85 status = U_ZERO_ERROR; | |
| 86 UBool match = matcher.matches(status); | |
| 87 DCHECK(U_SUCCESS(status)); | |
| 88 return !!match; // !! == convert from UBool to bool. | |
| 89 } | |
| 90 | |
| 91 SigninManagerBase::SigninManagerBase() | 57 SigninManagerBase::SigninManagerBase() |
| 92 : profile_(NULL), | 58 : profile_(NULL), |
| 93 weak_pointer_factory_(this) { | 59 weak_pointer_factory_(this) { |
| 94 } | 60 } |
| 95 | 61 |
| 96 SigninManagerBase::~SigninManagerBase() { | 62 SigninManagerBase::~SigninManagerBase() { |
| 97 DCHECK(!signin_global_error_.get()) << | 63 DCHECK(!signin_global_error_.get()) << |
| 98 "SigninManagerBase::Initialize called but not " | 64 "SigninManagerBase::Initialize called but not " |
| 99 "SigninManagerBase::Shutdown"; | 65 "SigninManagerBase::Shutdown"; |
| 100 } | 66 } |
| 101 | 67 |
| 102 void SigninManagerBase::Initialize(Profile* profile) { | 68 void SigninManagerBase::Initialize(Profile* profile) { |
| 103 // Should never call Initialize() twice. | 69 // Should never call Initialize() twice. |
| 104 DCHECK(!IsInitialized()); | 70 DCHECK(!IsInitialized()); |
| 105 profile_ = profile; | 71 profile_ = profile; |
| 106 signin_global_error_.reset(new SigninGlobalError(this, profile)); | 72 signin_global_error_.reset(new SigninGlobalError(this, profile)); |
| 107 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( | 73 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError( |
| 108 signin_global_error_.get()); | 74 signin_global_error_.get()); |
| 109 PrefService* local_state = g_browser_process->local_state(); | |
| 110 // local_state can be null during unit tests. | |
| 111 if (local_state) { | |
| 112 local_state_pref_registrar_.Init(local_state); | |
| 113 local_state_pref_registrar_.Add( | |
| 114 prefs::kGoogleServicesUsernamePattern, | |
| 115 base::Bind(&SigninManagerBase::OnGoogleServicesUsernamePatternChanged, | |
| 116 weak_pointer_factory_.GetWeakPtr())); | |
| 117 } | |
| 118 signin_allowed_.Init(prefs::kSigninAllowed, profile_->GetPrefs(), | |
| 119 base::Bind(&SigninManagerBase::OnSigninAllowedPrefChanged, | |
| 120 base::Unretained(this))); | |
| 121 | 75 |
| 122 // If the user is clearing the token service from the command line, then | 76 // If the user is clearing the token service from the command line, then |
| 123 // clear their login info also (not valid to be logged in without any | 77 // clear their login info also (not valid to be logged in without any |
| 124 // tokens). | 78 // tokens). |
| 125 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | 79 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 126 if (cmd_line->HasSwitch(switches::kClearTokenService)) | 80 if (cmd_line->HasSwitch(switches::kClearTokenService)) |
| 127 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 81 profile->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 128 | 82 |
| 129 std::string user = profile_->GetPrefs()->GetString( | 83 std::string user = profile_->GetPrefs()->GetString( |
| 130 prefs::kGoogleServicesUsername); | 84 prefs::kGoogleServicesUsername); |
| 131 if (!user.empty()) | 85 if (!user.empty()) |
| 132 SetAuthenticatedUsername(user); | 86 SetAuthenticatedUsername(user); |
| 133 | 87 |
| 134 InitTokenService(); | 88 InitTokenService(); |
| 135 | |
| 136 if ((!user.empty() && !IsAllowedUsername(user)) || !IsSigninAllowed()) { | |
| 137 // User is signed in, but the username is invalid - the administrator must | |
| 138 // have changed the policy since the last signin, so sign out the user. | |
| 139 SignOut(); | |
| 140 } | |
| 141 } | 89 } |
| 142 | 90 |
| 143 void SigninManagerBase::InitTokenService() { | 91 void SigninManagerBase::InitTokenService() { |
| 144 // TokenService can be null for unit tests. | 92 // TokenService can be null for unit tests. |
| 145 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | 93 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); |
| 146 if (token_service) | 94 if (token_service) |
| 147 token_service->Initialize(GaiaConstants::kChromeSource, profile_); | 95 token_service->Initialize(GaiaConstants::kChromeSource, profile_); |
| 148 // Note: ChromeOS will kick off TokenService::LoadTokensFromDB from | 96 // Note: ChromeOS will kick off TokenService::LoadTokensFromDB from |
| 149 // OAuthLoginManager once the rest of the Profile is fully initialized. | 97 // OAuthLoginManager once the rest of the Profile is fully initialized. |
| 150 } | 98 } |
| 151 | 99 |
| 152 bool SigninManagerBase::IsInitialized() const { | 100 bool SigninManagerBase::IsInitialized() const { |
| 153 return profile_ != NULL; | 101 return profile_ != NULL; |
| 154 } | 102 } |
| 155 | 103 |
| 156 bool SigninManagerBase::IsAllowedUsername(const std::string& username) const { | |
| 157 PrefService* local_state = g_browser_process->local_state(); | |
| 158 if (!local_state) | |
| 159 return true; // In a unit test with no local state - all names are allowed. | |
| 160 | |
| 161 std::string pattern = local_state->GetString( | |
| 162 prefs::kGoogleServicesUsernamePattern); | |
| 163 return IsAllowedUsername(username, pattern); | |
| 164 } | |
| 165 | |
| 166 bool SigninManagerBase::IsSigninAllowed() const { | 104 bool SigninManagerBase::IsSigninAllowed() const { |
| 167 return signin_allowed_.GetValue(); | 105 return profile_->GetPrefs()->GetBoolean(prefs::kSigninAllowed); |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 bool SigninManagerBase::IsSigninAllowedOnIOThread(ProfileIOData* io_data) { | |
| 172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 173 return io_data->signin_allowed()->GetValue(); | |
| 174 } | 106 } |
| 175 | 107 |
| 176 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { | 108 const std::string& SigninManagerBase::GetAuthenticatedUsername() const { |
| 177 return authenticated_username_; | 109 return authenticated_username_; |
| 178 } | 110 } |
| 179 | 111 |
| 180 void SigninManagerBase::SetAuthenticatedUsername(const std::string& username) { | 112 void SigninManagerBase::SetAuthenticatedUsername(const std::string& username) { |
| 181 if (!authenticated_username_.empty()) { | 113 if (!authenticated_username_.empty()) { |
| 182 DLOG_IF(ERROR, username != authenticated_username_) << | 114 DLOG_IF(ERROR, username != authenticated_username_) << |
| 183 "Tried to change the authenticated username to something different: " << | 115 "Tried to change the authenticated username to something different: " << |
| 184 "Current: " << authenticated_username_ << ", New: " << username; | 116 "Current: " << authenticated_username_ << ", New: " << username; |
| 185 return; | 117 return; |
| 186 } | 118 } |
| 187 authenticated_username_ = username; | 119 authenticated_username_ = username; |
| 188 // TODO(tim): We could go further in ensuring kGoogleServicesUsername and | 120 // TODO(tim): We could go further in ensuring kGoogleServicesUsername and |
| 189 // authenticated_username_ are consistent once established (e.g. remove | 121 // authenticated_username_ are consistent once established (e.g. remove |
| 190 // authenticated_username_ altogether). Bug 107160. | 122 // authenticated_username_ altogether). Bug 107160. |
| 191 | 123 |
| 192 NotifyDiagnosticsObservers(USERNAME, username); | 124 NotifyDiagnosticsObservers(USERNAME, username); |
| 193 | 125 |
| 194 // Go ahead and update the last signed in username here as well. Once a | 126 // Go ahead and update the last signed in username here as well. Once a |
| 195 // user is signed in the two preferences should match. Doing it here as | 127 // user is signed in the two preferences should match. Doing it here as |
| 196 // opposed to on signin allows us to catch the upgrade scenario. | 128 // opposed to on signin allows us to catch the upgrade scenario. |
| 197 profile_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername, username); | 129 profile_->GetPrefs()->SetString(prefs::kGoogleServicesLastUsername, username); |
| 198 } | 130 } |
| 199 | 131 |
| 200 void SigninManagerBase::SignOut() { | 132 void SigninManagerBase::clear_authenticated_username() { |
| 201 DCHECK(IsInitialized()); | |
| 202 | |
| 203 if (authenticated_username_.empty() && !AuthInProgress()) | |
| 204 return; | |
| 205 | |
| 206 GoogleServiceSignoutDetails details(authenticated_username_); | |
| 207 authenticated_username_.clear(); | 133 authenticated_username_.clear(); |
| 208 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | |
| 209 | |
| 210 // Erase (now) stale information from AboutSigninInternals. | |
| 211 NotifyDiagnosticsObservers(USERNAME, ""); | |
| 212 NotifyDiagnosticsObservers(LSID, ""); | |
| 213 NotifyDiagnosticsObservers( | |
| 214 signin_internals_util::SID, ""); | |
| 215 | |
| 216 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
| 217 content::NotificationService::current()->Notify( | |
| 218 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
| 219 content::Source<Profile>(profile_), | |
| 220 content::Details<const GoogleServiceSignoutDetails>(&details)); | |
| 221 token_service->ResetCredentialsInMemory(); | |
| 222 token_service->EraseTokensFromDB(); | |
| 223 } | 134 } |
| 224 | 135 |
| 225 bool SigninManagerBase::AuthInProgress() const { | 136 bool SigninManagerBase::AuthInProgress() const { |
| 226 // SigninManagerBase never kicks off auth processes itself. | 137 // SigninManagerBase never kicks off auth processes itself. |
| 227 return false; | 138 return false; |
| 228 } | 139 } |
| 229 | 140 |
| 230 void SigninManagerBase::Shutdown() { | 141 void SigninManagerBase::Shutdown() { |
| 231 if (signin_global_error_.get()) { | 142 if (signin_global_error_.get()) { |
| 232 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError( | 143 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError( |
| 233 signin_global_error_.get()); | 144 signin_global_error_.get()); |
| 234 signin_global_error_.reset(); | 145 signin_global_error_.reset(); |
| 235 } | 146 } |
| 236 } | 147 } |
| 237 | 148 |
| 238 void SigninManagerBase::OnGoogleServicesUsernamePatternChanged() { | |
| 239 if (!authenticated_username_.empty() && | |
| 240 !IsAllowedUsername(authenticated_username_)) { | |
| 241 // Signed in user is invalid according to the current policy so sign | |
| 242 // the user out. | |
| 243 SignOut(); | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 void SigninManagerBase::OnSigninAllowedPrefChanged() { | |
| 248 if (!IsSigninAllowed()) | |
| 249 SignOut(); | |
| 250 } | |
| 251 | |
| 252 void SigninManagerBase::AddSigninDiagnosticsObserver( | 149 void SigninManagerBase::AddSigninDiagnosticsObserver( |
| 253 SigninDiagnosticsObserver* observer) { | 150 SigninDiagnosticsObserver* observer) { |
| 254 signin_diagnostics_observers_.AddObserver(observer); | 151 signin_diagnostics_observers_.AddObserver(observer); |
| 255 } | 152 } |
| 256 | 153 |
| 257 void SigninManagerBase::RemoveSigninDiagnosticsObserver( | 154 void SigninManagerBase::RemoveSigninDiagnosticsObserver( |
| 258 SigninDiagnosticsObserver* observer) { | 155 SigninDiagnosticsObserver* observer) { |
| 259 signin_diagnostics_observers_.RemoveObserver(observer); | 156 signin_diagnostics_observers_.RemoveObserver(observer); |
| 260 } | 157 } |
| 261 | 158 |
| 262 void SigninManagerBase::NotifyDiagnosticsObservers( | 159 void SigninManagerBase::NotifyDiagnosticsObservers( |
| 263 const UntimedSigninStatusField& field, | 160 const UntimedSigninStatusField& field, |
| 264 const std::string& value) { | 161 const std::string& value) { |
| 265 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 162 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
| 266 signin_diagnostics_observers_, | 163 signin_diagnostics_observers_, |
| 267 NotifySigninValueChanged(field, value)); | 164 NotifySigninValueChanged(field, value)); |
| 268 } | 165 } |
| 269 | 166 |
| 270 void SigninManagerBase::NotifyDiagnosticsObservers( | 167 void SigninManagerBase::NotifyDiagnosticsObservers( |
| 271 const TimedSigninStatusField& field, | 168 const TimedSigninStatusField& field, |
| 272 const std::string& value) { | 169 const std::string& value) { |
| 273 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, | 170 FOR_EACH_OBSERVER(SigninDiagnosticsObserver, |
| 274 signin_diagnostics_observers_, | 171 signin_diagnostics_observers_, |
| 275 NotifySigninValueChanged(field, value)); | 172 NotifySigninValueChanged(field, value)); |
| 276 } | 173 } |
| OLD | NEW |