Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: chrome/browser/signin/signin_manager.h

Issue 12502017: signin: pull basic SigninManager functionality into new SigninManagerBase class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: deal with new enterprise_platform_keys_private_api Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // The signin manager encapsulates some functionality tracking 5 // The signin manager encapsulates some functionality tracking
6 // which user is signed in. When a user is signed in, a ClientLogin 6 // which user is signed in. See SigninManagerBase for full description of
7 // request is run on their behalf. Auth tokens are fetched from Google 7 // responsibilities. The class defined in this file provides functionality
8 // and the results are stored in the TokenService. 8 // required by all platforms except Chrome OS.
9 // 9 //
10 // **NOTE** on semantics of SigninManager: 10 // When a user is signed in, a ClientLogin request is run on their behalf.
11 // 11 // Auth tokens are fetched from Google and the results are stored in the
12 // Once a signin is successful, the username becomes "established" and will not 12 // TokenService.
13 // be cleared until a SignOut operation is performed (persists across 13 // TODO(tim): Bug 92948, 226464. ClientLogin is all but gone from use.
14 // restarts). Until that happens, the signin manager can still be used to
15 // refresh credentials, but changing the username is not permitted.
16 14
17 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 15 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
18 #define CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 16 #define CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
19 17
18 #if defined(OS_CHROMEOS)
19 // On Chrome OS, SigninManagerBase is all that exists.
20 #include "chrome/browser/signin/signin_manager_base.h"
21
22 #else
23
20 #include <string> 24 #include <string>
21 25
22 #include "base/compiler_specific.h" 26 #include "base/compiler_specific.h"
23 #include "base/gtest_prod_util.h" 27 #include "base/gtest_prod_util.h"
24 #include "base/logging.h" 28 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h" 29 #include "base/memory/scoped_ptr.h"
26 #include "base/observer_list.h" 30 #include "base/observer_list.h"
27 #include "base/prefs/pref_change_registrar.h" 31 #include "base/prefs/pref_change_registrar.h"
28 #include "base/prefs/pref_member.h" 32 #include "base/prefs/pref_member.h"
29 #include "chrome/browser/profiles/profile.h" 33 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_keyed_service.h" 34 #include "chrome/browser/profiles/profile_keyed_service.h"
31 #include "chrome/browser/signin/signin_internals_util.h" 35 #include "chrome/browser/signin/signin_internals_util.h"
36 #include "chrome/browser/signin/signin_manager_base.h"
32 #include "chrome/browser/signin/ubertoken_fetcher.h" 37 #include "chrome/browser/signin/ubertoken_fetcher.h"
33 #include "content/public/browser/notification_observer.h" 38 #include "content/public/browser/notification_observer.h"
34 #include "content/public/browser/notification_registrar.h" 39 #include "content/public/browser/notification_registrar.h"
35 #include "google_apis/gaia/gaia_auth_consumer.h" 40 #include "google_apis/gaia/gaia_auth_consumer.h"
36 #include "google_apis/gaia/google_service_auth_error.h" 41 #include "google_apis/gaia/google_service_auth_error.h"
37 #include "net/cookies/canonical_cookie.h" 42 #include "net/cookies/canonical_cookie.h"
38 43
39 class CookieSettings; 44 class CookieSettings;
40 class GaiaAuthFetcher; 45 class GaiaAuthFetcher;
41 class ProfileIOData; 46 class ProfileIOData;
42 class PrefService; 47 class PrefService;
43 class SigninGlobalError; 48 class SigninGlobalError;
44 49
45 namespace policy { 50 namespace policy {
46 class CloudPolicyClient; 51 class CloudPolicyClient;
47 } 52 }
48 53
49 // Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL. 54 class SigninManager : public SigninManagerBase,
50 // A listener might use this to make note of a username / password 55 public GaiaAuthConsumer,
51 // pair for encryption keys.
52 struct GoogleServiceSigninSuccessDetails {
53 GoogleServiceSigninSuccessDetails(const std::string& in_username,
54 const std::string& in_password)
55 : username(in_username),
56 password(in_password) {}
57 std::string username;
58 std::string password;
59 };
60
61 // Details for the Notification type NOTIFICATION_GOOGLE_SIGNED_OUT.
62 struct GoogleServiceSignoutDetails {
63 explicit GoogleServiceSignoutDetails(const std::string& in_username)
64 : username(in_username) {}
65 std::string username;
66 };
67
68 class SigninManager : public GaiaAuthConsumer,
69 public UbertokenConsumer, 56 public UbertokenConsumer,
70 public content::NotificationObserver, 57 public content::NotificationObserver {
71 public ProfileKeyedService {
72 public: 58 public:
73 // Methods to register or remove SigninDiagnosticObservers
74 void AddSigninDiagnosticsObserver(
75 signin_internals_util::SigninDiagnosticsObserver* observer);
76 void RemoveSigninDiagnosticsObserver(
77 signin_internals_util::SigninDiagnosticsObserver* observer);
78
79 // Returns true if the cookie policy for the given profile allows cookies
80 // for the Google signin domain.
81 static bool AreSigninCookiesAllowed(Profile* profile);
82 static bool AreSigninCookiesAllowed(CookieSettings* cookie_settings);
83
84 // Returns true if the username is allowed based on the policy string.
85 static bool IsAllowedUsername(const std::string& username,
86 const std::string& policy);
87
88 // Returns true if |url| is a web signin URL and should be hosted in an 59 // Returns true if |url| is a web signin URL and should be hosted in an
89 // isolated, privileged signin process. 60 // isolated, privileged signin process.
90 static bool IsWebBasedSigninFlowURL(const GURL& url); 61 static bool IsWebBasedSigninFlowURL(const GURL& url);
91 62
92 // This is used to distinguish URLs belonging to the special web signin flow 63 // This is used to distinguish URLs belonging to the special web signin flow
93 // running in the special signin process from other URLs on the same domain. 64 // running in the special signin process from other URLs on the same domain.
94 // We do not grant WebUI privilieges / bindings to this process or to URLs of 65 // We do not grant WebUI privilieges / bindings to this process or to URLs of
95 // this scheme; enforcement of privileges is handled separately by 66 // this scheme; enforcement of privileges is handled separately by
96 // OneClickSigninHelper. 67 // OneClickSigninHelper.
97 static const char* kChromeSigninEffectiveSite; 68 static const char* kChromeSigninEffectiveSite;
98 69
99 SigninManager(); 70 SigninManager();
100 virtual ~SigninManager(); 71 virtual ~SigninManager();
101 72
102 // If user was signed in, load tokens from DB if available.
103 void Initialize(Profile* profile);
104 bool IsInitialized() const;
105
106 // Returns true if the passed username is allowed by policy. Virtual for
107 // mocking in tests.
108 virtual bool IsAllowedUsername(const std::string& username) const;
109
110 // Returns true if a signin to Chrome is allowed (by policy or pref).
111 bool IsSigninAllowed() const;
112
113 // Checks if signin is allowed for the profile that owns |io_data|. This must
114 // be invoked on the IO thread, and can be used to check if signin is enabled
115 // on that thread.
116 static bool IsSigninAllowedOnIOThread(ProfileIOData* io_data);
117
118 // If a user has previously established a username and SignOut has not been
119 // called, this will return the username.
120 // Otherwise, it will return an empty string.
121 const std::string& GetAuthenticatedUsername() const;
122
123 // Sets the user name. Note: |username| should be already authenticated as
124 // this is a sticky operation (in contrast to StartSignIn).
125 // TODO(tim): Remove this in favor of passing username on construction by
126 // (by platform / depending on StartBehavior). Bug 88109.
127 void SetAuthenticatedUsername(const std::string& username);
128
129 // Attempt to sign in this user with ClientLogin. If successful, set a 73 // Attempt to sign in this user with ClientLogin. If successful, set a
130 // preference indicating the signed in user and send out a notification, 74 // preference indicating the signed in user and send out a notification,
131 // then start fetching tokens for the user. 75 // then start fetching tokens for the user.
132 // This is overridden for test subclasses that don't want to issue auth 76 // This is overridden for test subclasses that don't want to issue auth
133 // requests. 77 // requests.
134 virtual void StartSignIn(const std::string& username, 78 virtual void StartSignIn(const std::string& username,
135 const std::string& password, 79 const std::string& password,
136 const std::string& login_token, 80 const std::string& login_token,
137 const std::string& login_captcha); 81 const std::string& login_captcha);
138 82
(...skipping 18 matching lines...) Expand all
157 // Provide a challenge solution to a failed signin attempt with 101 // Provide a challenge solution to a failed signin attempt with
158 // StartSignInWithOAuth(). |type| and |token| come from the 102 // StartSignInWithOAuth(). |type| and |token| come from the
159 // GoogleServiceAuthError of the failed attempt. 103 // GoogleServiceAuthError of the failed attempt.
160 // |solution| is the answer typed by the user. 104 // |solution| is the answer typed by the user.
161 void ProvideOAuthChallengeResponse(GoogleServiceAuthError::State type, 105 void ProvideOAuthChallengeResponse(GoogleServiceAuthError::State type,
162 const std::string& token, 106 const std::string& token,
163 const std::string& solution); 107 const std::string& solution);
164 108
165 // Sign a user out, removing the preference, erasing all keys 109 // Sign a user out, removing the preference, erasing all keys
166 // associated with the user, and canceling all auth in progress. 110 // associated with the user, and canceling all auth in progress.
167 virtual void SignOut(); 111 virtual void SignOut() OVERRIDE;
168 112
169 // Returns true if there's a signin in progress. Virtual so it can be 113 // Returns true if there's a signin in progress.
170 // overridden by mocks. 114 virtual bool AuthInProgress() const OVERRIDE;
171 virtual bool AuthInProgress() const;
172 115
173 // If an authentication is in progress, return the username being 116 // If an authentication is in progress, return the username being
174 // authenticated. Returns an empty string if no auth is in progress. 117 // authenticated. Returns an empty string if no auth is in progress.
175 const std::string& GetUsernameForAuthInProgress() const; 118 const std::string& GetUsernameForAuthInProgress() const;
176 119
177 // Handles errors if a required user info key is not returned from the 120 // Handles errors if a required user info key is not returned from the
178 // GetUserInfo call. 121 // GetUserInfo call.
179 void OnGetUserInfoKeyNotFound(const std::string& key); 122 void OnGetUserInfoKeyNotFound(const std::string& key);
180 123
181 // Set the profile preference to turn off one-click sign-in so that it won't 124 // Set the profile preference to turn off one-click sign-in so that it won't
(...skipping 14 matching lines...) Expand all
196 139
197 // UbertokenConsumer 140 // UbertokenConsumer
198 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; 141 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
199 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; 142 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
200 143
201 // content::NotificationObserver 144 // content::NotificationObserver
202 virtual void Observe(int type, 145 virtual void Observe(int type,
203 const content::NotificationSource& source, 146 const content::NotificationSource& source,
204 const content::NotificationDetails& details) OVERRIDE; 147 const content::NotificationDetails& details) OVERRIDE;
205 148
206 SigninGlobalError* signin_global_error() {
207 return signin_global_error_.get();
208 }
209
210 const SigninGlobalError* signin_global_error() const {
211 return signin_global_error_.get();
212 }
213
214 // ProfileKeyedService implementation.
215 virtual void Shutdown() OVERRIDE;
216 149
217 // Tells the SigninManager whether to prohibit signout for this profile. 150 // Tells the SigninManager whether to prohibit signout for this profile.
218 // If |prohibit_signout| is true, then signout will be prohibited. 151 // If |prohibit_signout| is true, then signout will be prohibited.
219 void ProhibitSignout(bool prohibit_signout); 152 void ProhibitSignout(bool prohibit_signout);
220 153
221 // If true, signout is prohibited for this profile (calls to SignOut() are 154 // If true, signout is prohibited for this profile (calls to SignOut() are
222 // ignored). 155 // ignored).
223 bool IsSignoutProhibited() const; 156 bool IsSignoutProhibited() const;
224 157
225 // Allows the SigninManager to track the privileged signin process 158 // Allows the SigninManager to track the privileged signin process
226 // identified by |process_id| so that we can later ask (via IsSigninProcess) 159 // identified by |process_id| so that we can later ask (via IsSigninProcess)
227 // if it is safe to sign the user in from the current context (see 160 // if it is safe to sign the user in from the current context (see
228 // OneClickSigninHelper). All of this tracking state is reset once the 161 // OneClickSigninHelper). All of this tracking state is reset once the
229 // renderer process terminates. 162 // renderer process terminates.
230 void SetSigninProcess(int process_id); 163 void SetSigninProcess(int process_id);
231 bool IsSigninProcess(int process_id) const; 164 bool IsSigninProcess(int process_id) const;
232 bool HasSigninProcess() const; 165 bool HasSigninProcess() const;
233 166
234 protected: 167 protected:
235 // Weak pointer to parent profile (protected so FakeSigninManager can access 168 // If user was signed in, load tokens from DB if available.
236 // it). 169 virtual void InitTokenService() OVERRIDE;
237 Profile* profile_;
238
239 // Used to show auth errors in the wrench menu. The SigninGlobalError is
240 // different than most GlobalErrors in that its lifetime is controlled by
241 // SigninManager (so we can expose a reference for use in the wrench menu).
242 scoped_ptr<SigninGlobalError> signin_global_error_;
243 170
244 // Flag saying whether signing out is allowed. 171 // Flag saying whether signing out is allowed.
245 bool prohibit_signout_; 172 bool prohibit_signout_;
246 173
247 private: 174 private:
248 enum SigninType { 175 enum SigninType {
249 SIGNIN_TYPE_NONE, 176 SIGNIN_TYPE_NONE,
250 SIGNIN_TYPE_CLIENT_LOGIN, 177 SIGNIN_TYPE_CLIENT_LOGIN,
251 SIGNIN_TYPE_WITH_CREDENTIALS, 178 SIGNIN_TYPE_WITH_CREDENTIALS,
252 SIGNIN_TYPE_CLIENT_OAUTH, 179 SIGNIN_TYPE_CLIENT_OAUTH,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 // Called to handle an error from a GAIA auth fetch. Sets the last error 212 // Called to handle an error from a GAIA auth fetch. Sets the last error
286 // to |error|, sends out a notification of login failure, and clears the 213 // to |error|, sends out a notification of login failure, and clears the
287 // transient signin data if |clear_transient_data| is true. 214 // transient signin data if |clear_transient_data| is true.
288 void HandleAuthError(const GoogleServiceAuthError& error, 215 void HandleAuthError(const GoogleServiceAuthError& error,
289 bool clear_transient_data); 216 bool clear_transient_data);
290 217
291 // Called to tell GAIA that we will no longer be using the current refresh 218 // Called to tell GAIA that we will no longer be using the current refresh
292 // token. 219 // token.
293 void RevokeOAuthLoginToken(); 220 void RevokeOAuthLoginToken();
294 221
295 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 222 #if defined(ENABLE_CONFIGURATION_POLICY)
296 // Callback invoked once policy registration is complete. If registration 223 // Callback invoked once policy registration is complete. If registration
297 // fails, |client| will be null. 224 // fails, |client| will be null.
298 void OnRegisteredForPolicy(scoped_ptr<policy::CloudPolicyClient> client); 225 void OnRegisteredForPolicy(scoped_ptr<policy::CloudPolicyClient> client);
299 226
300 // Callback invoked when a policy fetch request has completed. |success| is 227 // Callback invoked when a policy fetch request has completed. |success| is
301 // true if policy was successfully fetched. 228 // true if policy was successfully fetched.
302 void OnPolicyFetchComplete(bool success); 229 void OnPolicyFetchComplete(bool success);
303 230
304 // Called to create a new profile, which is then signed in with the 231 // Called to create a new profile, which is then signed in with the
305 // in-progress auth credentials currently stored in this object. 232 // in-progress auth credentials currently stored in this object.
306 void TransferCredentialsToNewProfile(); 233 void TransferCredentialsToNewProfile();
307 234
308 // Helper function that loads policy with the passed CloudPolicyClient, then 235 // Helper function that loads policy with the passed CloudPolicyClient, then
309 // completes the signin process. 236 // completes the signin process.
310 void LoadPolicyWithCachedClient(); 237 void LoadPolicyWithCachedClient();
311 238
312 // Callback invoked once a profile is created, so we can complete the 239 // Callback invoked once a profile is created, so we can complete the
313 // credentials transfer and load policy. 240 // credentials transfer and load policy.
314 void CompleteSigninForNewProfile(Profile* profile, 241 void CompleteSigninForNewProfile(Profile* profile,
315 Profile::CreateStatus status); 242 Profile::CreateStatus status);
316 243
317 // Cancels the in-progress signin for this profile. 244 // Cancels the in-progress signin for this profile.
318 void CancelSignin(); 245 void CancelSignin();
319 #endif // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 246
247 #endif // defined(ENABLE_CONFIGURATION_POLICY)
320 248
321 // Invoked once policy has been loaded to complete user signin. 249 // Invoked once policy has been loaded to complete user signin.
322 void CompleteSigninAfterPolicyLoad(); 250 void CompleteSigninAfterPolicyLoad();
323 251
324 // ClientLogin identity. 252 // ClientLogin identity.
325 std::string possibly_invalid_username_; 253 std::string possibly_invalid_username_;
326 std::string password_; // This is kept empty whenever possible. 254 std::string password_; // This is kept empty whenever possible.
327 bool had_two_factor_error_; 255 bool had_two_factor_error_;
328 256
329 void CleanupNotificationRegistration(); 257 void CleanupNotificationRegistration();
330 258
331 void OnGoogleServicesUsernamePatternChanged();
332
333 void OnSigninAllowedPrefChanged();
334
335 // Helper methods to notify all registered diagnostics observers with.
336 void NotifyDiagnosticsObservers(
337 const signin_internals_util::UntimedSigninStatusField& field,
338 const std::string& value);
339 void NotifyDiagnosticsObservers(
340 const signin_internals_util::TimedSigninStatusField& field,
341 const std::string& value);
342
343 // Result of the last client login, kept pending the lookup of the 259 // Result of the last client login, kept pending the lookup of the
344 // canonical email. 260 // canonical email.
345 ClientLoginResult last_result_; 261 ClientLoginResult last_result_;
346 262
347 // Actual client login handler. 263 // Actual client login handler.
348 scoped_ptr<GaiaAuthFetcher> client_login_; 264 scoped_ptr<GaiaAuthFetcher> client_login_;
349 265
350 // Registrar for notifications from the TokenService. 266 // Registrar for notifications from the TokenService.
351 content::NotificationRegistrar registrar_; 267 content::NotificationRegistrar registrar_;
352 268
353 // UbertokenFetcher to login to user to the web property. 269 // UbertokenFetcher to login to user to the web property.
354 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_; 270 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_;
355 271
356 // OAuth revocation fetcher for sign outs. 272 // OAuth revocation fetcher for sign outs.
357 scoped_ptr<GaiaAuthFetcher> revoke_token_fetcher_; 273 scoped_ptr<GaiaAuthFetcher> revoke_token_fetcher_;
358 274
359 // Helper object to listen for changes to signin preferences stored in non-
360 // profile-specific local prefs (like kGoogleServicesUsernamePattern).
361 PrefChangeRegistrar local_state_pref_registrar_;
362
363 // Helper object to listen for changes to the signin allowed preference.
364 BooleanPrefMember signin_allowed_;
365
366 // Actual username after successful authentication.
367 std::string authenticated_username_;
368
369 // The type of sign being performed. This value is valid only between a call 275 // The type of sign being performed. This value is valid only between a call
370 // to one of the StartSigninXXX methods and when the sign in is either 276 // to one of the StartSigninXXX methods and when the sign in is either
371 // successful or not. 277 // successful or not.
372 SigninType type_; 278 SigninType type_;
373 279
374 // Temporarily saves the oauth2 refresh and access tokens when signing in 280 // Temporarily saves the oauth2 refresh and access tokens when signing in
375 // with credentials. These will be passed to TokenService so that it does 281 // with credentials. These will be passed to TokenService so that it does
376 // not need to mint new ones. 282 // not need to mint new ones.
377 ClientOAuthResult temp_oauth_login_tokens_; 283 ClientOAuthResult temp_oauth_login_tokens_;
378 284
379 // The list of SigninDiagnosticObservers.
380 ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
381 signin_diagnostics_observers_;
382
383 base::WeakPtrFactory<SigninManager> weak_pointer_factory_; 285 base::WeakPtrFactory<SigninManager> weak_pointer_factory_;
384 286
385 // See SetSigninProcess. Tracks the currently active signin process 287 // See SetSigninProcess. Tracks the currently active signin process
386 // by ID, if there is one. 288 // by ID, if there is one.
387 int signin_process_id_; 289 int signin_process_id_;
388 290
389 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 291 #if defined(ENABLE_CONFIGURATION_POLICY)
390 // CloudPolicyClient reference we keep while determining whether to create 292 // CloudPolicyClient reference we keep while determining whether to create
391 // a new profile for an enterprise user or not. 293 // a new profile for an enterprise user or not.
392 scoped_ptr<policy::CloudPolicyClient> policy_client_; 294 scoped_ptr<policy::CloudPolicyClient> policy_client_;
393 #endif 295 #endif
394 296
395 DISALLOW_COPY_AND_ASSIGN(SigninManager); 297 DISALLOW_COPY_AND_ASSIGN(SigninManager);
396 }; 298 };
397 299
300 #endif // !defined(OS_CHROMEOS)
301
398 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 302 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/signin/signin_global_error_unittest.cc ('k') | chrome/browser/signin/signin_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698