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