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

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: Created 7 years, 9 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 // See SigninManagerBase for full description of responsibilities.
6 // which user is signed in. When a user is signed in, a ClientLogin 6 // The class defined in this file provides functionality required by non
7 // request is run on their behalf. Auth tokens are fetched from Google 7 // non-Chrome OS platforms.
8 // and the results are stored in the TokenService.
Roger Tawa OOO till Jul 10th 2013/03/27 17:36:33 Since the ClientLogin is staying as part of this c
tim (not reviewing) 2013/03/29 18:03:16 Done.
9 //
10 // **NOTE** on semantics of SigninManager:
11 //
12 // Once a signin is successful, the username becomes "established" and will not
13 // be cleared until a SignOut operation is performed (persists across
14 // restarts). Until that happens, the signin manager can still be used to
15 // refresh credentials, but changing the username is not permitted.
16 8
17 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 9 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
18 #define CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 10 #define CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
19 11
20 #include <string> 12 #include <string>
21 13
22 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
23 #include "base/gtest_prod_util.h" 15 #include "base/gtest_prod_util.h"
24 #include "base/logging.h" 16 #include "base/logging.h"
25 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
26 #include "base/observer_list.h" 18 #include "base/observer_list.h"
27 #include "base/prefs/public/pref_change_registrar.h" 19 #include "base/prefs/public/pref_change_registrar.h"
28 #include "base/prefs/public/pref_member.h" 20 #include "base/prefs/public/pref_member.h"
29 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_keyed_service.h" 22 #include "chrome/browser/profiles/profile_keyed_service.h"
31 #include "chrome/browser/signin/signin_internals_util.h" 23 #include "chrome/browser/signin/signin_internals_util.h"
24 #include "chrome/browser/signin/signin_manager_base.h"
32 #include "chrome/browser/signin/ubertoken_fetcher.h" 25 #include "chrome/browser/signin/ubertoken_fetcher.h"
33 #include "content/public/browser/notification_observer.h" 26 #include "content/public/browser/notification_observer.h"
34 #include "content/public/browser/notification_registrar.h" 27 #include "content/public/browser/notification_registrar.h"
35 #include "google_apis/gaia/gaia_auth_consumer.h" 28 #include "google_apis/gaia/gaia_auth_consumer.h"
36 #include "google_apis/gaia/google_service_auth_error.h" 29 #include "google_apis/gaia/google_service_auth_error.h"
37 #include "net/cookies/canonical_cookie.h" 30 #include "net/cookies/canonical_cookie.h"
38 31
39 class CookieSettings; 32 class CookieSettings;
40 class GaiaAuthFetcher; 33 class GaiaAuthFetcher;
41 class ProfileIOData; 34 class ProfileIOData;
42 class PrefService; 35 class PrefService;
43 class SigninGlobalError; 36 class SigninGlobalError;
44 37
45 namespace policy { 38 namespace policy {
46 class CloudPolicyClient; 39 class CloudPolicyClient;
47 } 40 }
48 41
49 // Details for the Notification type GOOGLE_SIGNIN_SUCCESSFUL. 42 class SigninManager : public SigninManagerBase,
50 // A listener might use this to make note of a username / password 43 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, 44 public UbertokenConsumer,
70 public content::NotificationObserver, 45 public content::NotificationObserver {
71 public ProfileKeyedService {
72 public: 46 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 47 // Returns true if |url| is a web signin URL and should be hosted in an
89 // isolated, privileged signin process. 48 // isolated, privileged signin process.
90 static bool IsWebBasedSigninFlowURL(const GURL& url); 49 static bool IsWebBasedSigninFlowURL(const GURL& url);
91 50
92 // This is used to distinguish URLs belonging to the special web signin flow 51 // 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. 52 // 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 53 // We do not grant WebUI privilieges / bindings to this process or to URLs of
95 // this scheme; enforcement of privileges is handled separately by 54 // this scheme; enforcement of privileges is handled separately by
96 // OneClickSigninHelper. 55 // OneClickSigninHelper.
97 static const char* kChromeSigninEffectiveSite; 56 static const char* kChromeSigninEffectiveSite;
98 57
99 SigninManager(); 58 SigninManager();
100 virtual ~SigninManager(); 59 virtual ~SigninManager();
101 60
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 61 // Attempt to sign in this user with ClientLogin. If successful, set a
130 // preference indicating the signed in user and send out a notification, 62 // preference indicating the signed in user and send out a notification,
131 // then start fetching tokens for the user. 63 // then start fetching tokens for the user.
132 // This is overridden for test subclasses that don't want to issue auth 64 // This is overridden for test subclasses that don't want to issue auth
133 // requests. 65 // requests.
134 virtual void StartSignIn(const std::string& username, 66 virtual void StartSignIn(const std::string& username,
135 const std::string& password, 67 const std::string& password,
136 const std::string& login_token, 68 const std::string& login_token,
137 const std::string& login_captcha); 69 const std::string& login_captcha);
138 70
(...skipping 18 matching lines...) Expand all
157 // Provide a challenge solution to a failed signin attempt with 89 // Provide a challenge solution to a failed signin attempt with
158 // StartSignInWithOAuth(). |type| and |token| come from the 90 // StartSignInWithOAuth(). |type| and |token| come from the
159 // GoogleServiceAuthError of the failed attempt. 91 // GoogleServiceAuthError of the failed attempt.
160 // |solution| is the answer typed by the user. 92 // |solution| is the answer typed by the user.
161 void ProvideOAuthChallengeResponse(GoogleServiceAuthError::State type, 93 void ProvideOAuthChallengeResponse(GoogleServiceAuthError::State type,
162 const std::string& token, 94 const std::string& token,
163 const std::string& solution); 95 const std::string& solution);
164 96
165 // Sign a user out, removing the preference, erasing all keys 97 // Sign a user out, removing the preference, erasing all keys
166 // associated with the user, and canceling all auth in progress. 98 // associated with the user, and canceling all auth in progress.
167 virtual void SignOut(); 99 virtual void SignOut() OVERRIDE;
168 100
169 // Returns true if there's a signin in progress. Virtual so it can be 101 // Returns true if there's a signin in progress. Virtual so it can be
170 // overridden by mocks. 102 // overridden by mocks.
171 virtual bool AuthInProgress() const; 103 virtual bool AuthInProgress() const;
172 104
173 // Handles errors if a required user info key is not returned from the 105 // Handles errors if a required user info key is not returned from the
174 // GetUserInfo call. 106 // GetUserInfo call.
175 void OnGetUserInfoKeyNotFound(const std::string& key); 107 void OnGetUserInfoKeyNotFound(const std::string& key);
176 108
177 // Set the profile preference to turn off one-click sign-in so that it won't 109 // Set the profile preference to turn off one-click sign-in so that it won't
(...skipping 13 matching lines...) Expand all
191 123
192 // UbertokenConsumer 124 // UbertokenConsumer
193 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; 125 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
194 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; 126 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
195 127
196 // content::NotificationObserver 128 // content::NotificationObserver
197 virtual void Observe(int type, 129 virtual void Observe(int type,
198 const content::NotificationSource& source, 130 const content::NotificationSource& source,
199 const content::NotificationDetails& details) OVERRIDE; 131 const content::NotificationDetails& details) OVERRIDE;
200 132
201 SigninGlobalError* signin_global_error() {
202 return signin_global_error_.get();
203 }
204
205 const SigninGlobalError* signin_global_error() const {
206 return signin_global_error_.get();
207 }
208
209 // ProfileKeyedService implementation.
210 virtual void Shutdown() OVERRIDE;
211
212 // Tells the SigninManager to prohibit signout for this profile. 133 // Tells the SigninManager to prohibit signout for this profile.
213 void ProhibitSignout(); 134 void ProhibitSignout();
214 135
215 // If true, signout is prohibited for this profile (calls to SignOut() are 136 // If true, signout is prohibited for this profile (calls to SignOut() are
216 // ignored). 137 // ignored).
217 bool IsSignoutProhibited() const; 138 bool IsSignoutProhibited() const;
218 139
219 // Allows the SigninManager to track the privileged signin process 140 // Allows the SigninManager to track the privileged signin process
220 // identified by |process_id| so that we can later ask (via IsSigninProcess) 141 // identified by |process_id| so that we can later ask (via IsSigninProcess)
221 // if it is safe to sign the user in from the current context (see 142 // if it is safe to sign the user in from the current context (see
222 // OneClickSigninHelper). All of this tracking state is reset once the 143 // OneClickSigninHelper). All of this tracking state is reset once the
223 // renderer process terminates. 144 // renderer process terminates.
224 void SetSigninProcess(int process_id); 145 void SetSigninProcess(int process_id);
225 bool IsSigninProcess(int process_id) const; 146 bool IsSigninProcess(int process_id) const;
226 bool HasSigninProcess() const; 147 bool HasSigninProcess() const;
227 148
228 protected: 149 protected:
229 // Weak pointer to parent profile (protected so FakeSigninManager can access 150 virtual bool ShouldSignOut() OVERRIDE;
230 // it). 151 // If user was signed in, load tokens from DB if available.
231 Profile* profile_; 152 virtual void InitTokenService() OVERRIDE;
232
233 // Used to show auth errors in the wrench menu. The SigninGlobalError is
234 // different than most GlobalErrors in that its lifetime is controlled by
235 // SigninManager (so we can expose a reference for use in the wrench menu).
236 scoped_ptr<SigninGlobalError> signin_global_error_;
237 153
238 // Flag saying whether signing out is allowed. 154 // Flag saying whether signing out is allowed.
239 bool prohibit_signout_; 155 bool prohibit_signout_;
240 156
241 private: 157 private:
242 enum SigninType { 158 enum SigninType {
243 SIGNIN_TYPE_NONE, 159 SIGNIN_TYPE_NONE,
244 SIGNIN_TYPE_CLIENT_LOGIN, 160 SIGNIN_TYPE_CLIENT_LOGIN,
245 SIGNIN_TYPE_WITH_CREDENTIALS, 161 SIGNIN_TYPE_WITH_CREDENTIALS,
246 SIGNIN_TYPE_CLIENT_OAUTH, 162 SIGNIN_TYPE_CLIENT_OAUTH,
247 }; 163 };
248 164
249 std::string SigninTypeToString(SigninType type); 165 std::string SigninTypeToString(SigninType type);
250 166
251 friend class FakeSigninManager;
252 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ClearTransientSigninData); 167 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ClearTransientSigninData);
253 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ProvideSecondFactorSuccess); 168 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ProvideSecondFactorSuccess);
254 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ProvideSecondFactorFailure); 169 FRIEND_TEST_ALL_PREFIXES(SigninManagerTest, ProvideSecondFactorFailure);
255 170
256 // Called to setup the transient signin data during one of the 171 // Called to setup the transient signin data during one of the
257 // StartSigninXXX methods. |type| indicates which of the methods is being 172 // StartSigninXXX methods. |type| indicates which of the methods is being
258 // used to perform the signin while |username| and |password| identify the 173 // used to perform the signin while |username| and |password| identify the
259 // account to be signed in. Returns false and generates an auth error if the 174 // account to be signed in. Returns false and generates an auth error if the
260 // passed |username| is not allowed by policy. 175 // passed |username| is not allowed by policy.
261 bool PrepareForSignin(SigninType type, 176 bool PrepareForSignin(SigninType type,
(...skipping 13 matching lines...) Expand all
275 // Will clear in memory data but leaves the db as such so when the browser 190 // Will clear in memory data but leaves the db as such so when the browser
276 // restarts we can use the old token(which might throw a password error). 191 // restarts we can use the old token(which might throw a password error).
277 void ClearTransientSigninData(); 192 void ClearTransientSigninData();
278 193
279 // Called to handle an error from a GAIA auth fetch. Sets the last error 194 // Called to handle an error from a GAIA auth fetch. Sets the last error
280 // to |error|, sends out a notification of login failure, and clears the 195 // to |error|, sends out a notification of login failure, and clears the
281 // transient signin data if |clear_transient_data| is true. 196 // transient signin data if |clear_transient_data| is true.
282 void HandleAuthError(const GoogleServiceAuthError& error, 197 void HandleAuthError(const GoogleServiceAuthError& error,
283 bool clear_transient_data); 198 bool clear_transient_data);
284 199
285 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 200 #if defined(ENABLE_CONFIGURATION_POLICY)
286 // Callback invoked once policy registration is complete. If registration 201 // Callback invoked once policy registration is complete. If registration
287 // fails, |client| will be null. 202 // fails, |client| will be null.
288 void OnRegisteredForPolicy(scoped_ptr<policy::CloudPolicyClient> client); 203 void OnRegisteredForPolicy(scoped_ptr<policy::CloudPolicyClient> client);
289 204
290 // Callback invoked when a policy fetch request has completed. |success| is 205 // Callback invoked when a policy fetch request has completed. |success| is
291 // true if policy was successfully fetched. 206 // true if policy was successfully fetched.
292 void OnPolicyFetchComplete(bool success); 207 void OnPolicyFetchComplete(bool success);
293 208
294 // Called to create a new profile, which is then signed in with the 209 // Called to create a new profile, which is then signed in with the
295 // in-progress auth credentials currently stored in this object. 210 // in-progress auth credentials currently stored in this object.
296 void TransferCredentialsToNewProfile(); 211 void TransferCredentialsToNewProfile();
297 212
298 // Helper function that loads policy with the passed CloudPolicyClient, then 213 // Helper function that loads policy with the passed CloudPolicyClient, then
299 // completes the signin process. 214 // completes the signin process.
300 void LoadPolicyWithCachedClient(); 215 void LoadPolicyWithCachedClient();
301 216
302 // Callback invoked once a profile is created, so we can complete the 217 // Callback invoked once a profile is created, so we can complete the
303 // credentials transfer and load policy. 218 // credentials transfer and load policy.
304 void CompleteSigninForNewProfile(Profile* profile, 219 void CompleteSigninForNewProfile(Profile* profile,
305 Profile::CreateStatus status); 220 Profile::CreateStatus status);
306 #endif // defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 221 #endif // defined(ENABLE_CONFIGURATION_POLICY)
307 222
308 // Invoked once policy has been loaded to complete user signin. 223 // Invoked once policy has been loaded to complete user signin.
309 void CompleteSigninAfterPolicyLoad(); 224 void CompleteSigninAfterPolicyLoad();
310 225
311 // ClientLogin identity. 226 // ClientLogin identity.
312 std::string possibly_invalid_username_; 227 std::string possibly_invalid_username_;
313 std::string password_; // This is kept empty whenever possible. 228 std::string password_; // This is kept empty whenever possible.
314 bool had_two_factor_error_; 229 bool had_two_factor_error_;
315 230
316 void CleanupNotificationRegistration(); 231 void CleanupNotificationRegistration();
317 232
318 void OnGoogleServicesUsernamePatternChanged();
319
320 void OnSigninAllowedPrefChanged();
321
322 // Helper methods to notify all registered diagnostics observers with.
323 void NotifyDiagnosticsObservers(
324 const signin_internals_util::UntimedSigninStatusField& field,
325 const std::string& value);
326 void NotifyDiagnosticsObservers(
327 const signin_internals_util::TimedSigninStatusField& field,
328 const std::string& value);
329
330 // Result of the last client login, kept pending the lookup of the 233 // Result of the last client login, kept pending the lookup of the
331 // canonical email. 234 // canonical email.
332 ClientLoginResult last_result_; 235 ClientLoginResult last_result_;
333 236
334 // Actual client login handler. 237 // Actual client login handler.
335 scoped_ptr<GaiaAuthFetcher> client_login_; 238 scoped_ptr<GaiaAuthFetcher> client_login_;
336 239
337 // Registrar for notifications from the TokenService. 240 // Registrar for notifications from the TokenService.
338 content::NotificationRegistrar registrar_; 241 content::NotificationRegistrar registrar_;
339 242
340 // UbertokenFetcher to login to user to the web property. 243 // UbertokenFetcher to login to user to the web property.
341 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_; 244 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_;
342 245
343 // Helper object to listen for changes to signin preferences stored in non-
344 // profile-specific local prefs (like kGoogleServicesUsernamePattern).
345 PrefChangeRegistrar local_state_pref_registrar_;
346
347 // Helper object to listen for changes to the signin allowed preference.
348 BooleanPrefMember signin_allowed_;
349
350 // Actual username after successful authentication.
351 std::string authenticated_username_;
352
353 // The type of sign being performed. This value is valid only between a call 246 // The type of sign being performed. This value is valid only between a call
354 // to one of the StartSigninXXX methods and when the sign in is either 247 // to one of the StartSigninXXX methods and when the sign in is either
355 // successful or not. 248 // successful or not.
356 SigninType type_; 249 SigninType type_;
357 250
358 // Temporarily saves the oauth2 refresh and access tokens when signing in 251 // Temporarily saves the oauth2 refresh and access tokens when signing in
359 // with credentials. These will be passed to TokenService so that it does 252 // with credentials. These will be passed to TokenService so that it does
360 // not need to mint new ones. 253 // not need to mint new ones.
361 ClientOAuthResult temp_oauth_login_tokens_; 254 ClientOAuthResult temp_oauth_login_tokens_;
362 255
363 // The list of SigninDiagnosticObservers.
364 ObserverList<signin_internals_util::SigninDiagnosticsObserver, true>
365 signin_diagnostics_observers_;
366
367 base::WeakPtrFactory<SigninManager> weak_pointer_factory_; 256 base::WeakPtrFactory<SigninManager> weak_pointer_factory_;
368 257
369 // See SetSigninProcess. Tracks the currently active signin process 258 // See SetSigninProcess. Tracks the currently active signin process
370 // by ID, if there is one. 259 // by ID, if there is one.
371 int signin_process_id_; 260 int signin_process_id_;
372 261
373 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 262 #if defined(ENABLE_CONFIGURATION_POLICY)
374 // CloudPolicyClient reference we keep while determining whether to create 263 // CloudPolicyClient reference we keep while determining whether to create
375 // a new profile for an enterprise user or not. 264 // a new profile for an enterprise user or not.
376 scoped_ptr<policy::CloudPolicyClient> policy_client_; 265 scoped_ptr<policy::CloudPolicyClient> policy_client_;
377 #endif 266 #endif
378 267
379 DISALLOW_COPY_AND_ASSIGN(SigninManager); 268 DISALLOW_COPY_AND_ASSIGN(SigninManager);
380 }; 269 };
381 270
382 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_ 271 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698