1 // Copyright 2014 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.
5 #ifndef CHROMEOS_LOGIN_AUTH_CRYPTOHOME_AUTHENTICATOR_H_
6 #define CHROMEOS_LOGIN_AUTH_CRYPTOHOME_AUTHENTICATOR_H_
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "base/task_runner.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/login/auth/auth_attempt_state.h"
18 #include "chromeos/login/auth/auth_attempt_state_resolver.h"
19 #include "chromeos/login/auth/authenticator.h"
20 #include "chromeos/login/auth/test_attempt_state.h"
21 #include "google_apis/gaia/gaia_auth_consumer.h"
31 class AuthStatusConsumer;
33 // Authenticates a Chromium OS user against cryptohome.
34 // Relies on the fact that online authentications has been already performed
35 // (i.e. using_oauth_ is true).
37 // At a high, level, here's what happens:
38 // AuthenticateToLogin() calls a Cryptohome's method to perform offline login.
39 // Resultes are stored in a AuthAttemptState owned by CryptohomeAuthenticator
40 // and then call Resolve(). Resolve() will attempt to
41 // determine which AuthState we're in, based on the info at hand.
42 // It then triggers further action based on the calculated AuthState; this
43 // further action might include calling back the passed-in AuthStatusConsumer
44 // to signal that login succeeded or failed, waiting for more outstanding
45 // operations to complete, or triggering some more Cryptohome method calls.
49 // Add new user: CONTINUE > CONTINUE > CREATE_NEW > CONTINUE > ONLINE_LOGIN
50 // Login as existing user: CONTINUE > OFFLINE_LOGIN
51 // Login as existing user (failure): CONTINUE > FAILED_MOUNT
52 // Change password detected:
53 // GAIA online ok: CONTINUE > CONTINUE > NEED_OLD_PW
54 // Recreate: CREATE_NEW > CONTINUE > ONLINE_LOGIN
55 // Old password failure: NEED_OLD_PW
56 // Old password ok: RECOVER_MOUNT > CONTINUE > ONLINE_LOGIN
58 class CHROMEOS_EXPORT CryptohomeAuthenticator
59 : public Authenticator,
60 public AuthAttemptStateResolver {
63 CONTINUE = 0, // State indeterminate; try again with more info.
64 NO_MOUNT = 1, // Cryptohome doesn't exist yet.
65 FAILED_MOUNT = 2, // Failed to mount existing cryptohome.
66 FAILED_REMOVE = 3, // Failed to remove existing cryptohome.
67 FAILED_TMPFS = 4, // Failed to mount tmpfs for guest user.
68 FAILED_TPM = 5, // Failed to mount/create cryptohome, TPM error.
69 CREATE_NEW = 6, // Need to create cryptohome for a new user.
70 RECOVER_MOUNT = 7, // After RecoverEncryptedData, mount cryptohome.
71 POSSIBLE_PW_CHANGE = 8, // Offline login failed, user may have changed pw.
72 NEED_NEW_PW = 9, // Obsolete (ClientLogin): user changed pw,
73 // we have the old one.
74 NEED_OLD_PW = 10, // User changed pw, and we have the new one
76 HAVE_NEW_PW = 11, // Obsolete (ClientLogin): We have verified new pw,
77 // time to migrate key.
78 OFFLINE_LOGIN = 12, // Login succeeded offline.
79 DEMO_LOGIN = 13, // Logged in as the demo user.
80 ONLINE_LOGIN = 14, // Offline and online login succeeded.
81 UNLOCK = 15, // Screen unlock succeeded.
82 ONLINE_FAILED = 16, // Obsolete (ClientLogin): Online login disallowed,
83 // but offline succeeded.
84 GUEST_LOGIN = 17, // Logged in guest mode.
85 PUBLIC_ACCOUNT_LOGIN = 18, // Logged into a public account.
86 SUPERVISED_USER_LOGIN = 19, // Logged in as a supervised user.
87 LOGIN_FAILED = 20, // Login denied.
88 OWNER_REQUIRED = 21, // Login is restricted to the owner only.
89 FAILED_USERNAME_HASH = 22, // Failed GetSanitizedUsername request.
90 KIOSK_ACCOUNT_LOGIN = 23, // Logged into a kiosk account.
91 REMOVED_DATA_AFTER_FAILURE = 24, // Successfully removed the user's
92 // cryptohome after a login failure.
95 CryptohomeAuthenticator(scoped_refptr<base::TaskRunner> task_runner,
96 AuthStatusConsumer* consumer);
98 // Authenticator overrides.
99 virtual void CompleteLogin(content::BrowserContext* context,
100 const UserContext& user_context) override;
102 // Given |user_context|, this method attempts to authenticate to your
103 // Chrome OS device. As soon as we have successfully mounted the encrypted
104 // home directory for the user, we will call consumer_->OnAuthSuccess()
105 // with the username.
106 // Upon failure to login consumer_->OnAuthFailure() is called
107 // with an error message.
109 // Uses |context| when doing URL fetches.
110 virtual void AuthenticateToLogin(content::BrowserContext* context,
111 const UserContext& user_context) override;
113 // Given |user_context|, this method attempts to authenticate to the cached
114 // user_context. This will never contact the server even if it's online.
115 // The auth result is sent to AuthStatusConsumer in a same way as
116 // AuthenticateToLogin does.
117 virtual void AuthenticateToUnlock(const UserContext& user_context) override;
119 // Initiates supervised user login.
120 // Creates cryptohome if missing or mounts existing one and
121 // notifies consumer on the success/failure.
122 virtual void LoginAsSupervisedUser(const UserContext& user_context) override;
124 // Initiates retail mode login.
125 // Mounts tmpfs and notifies consumer on the success/failure.
126 virtual void LoginRetailMode() override;
128 // Initiates incognito ("browse without signing in") login.
129 // Mounts tmpfs and notifies consumer on the success/failure.
130 virtual void LoginOffTheRecord() override;
132 // Initiates login into a public session.
133 // Mounts an ephemeral cryptohome and notifies consumer on the
135 virtual void LoginAsPublicSession(const UserContext& user_context) override;
137 // Initiates login into the kiosk mode account identified by |app_user_id|.
138 // Mounts an ephemeral guest cryptohome if |use_guest_mount| is |true|.
139 // Otherwise, mounts a public cryptohome, which will be ephemeral if the
140 // |DeviceEphemeralUsersEnabled| policy is enabled and non-ephemeral
142 virtual void LoginAsKioskAccount(const std::string& app_user_id,
143 bool use_guest_mount) override;
145 // These methods must be called on the UI thread, as they make DBus calls
146 // and also call back to the login UI.
147 virtual void OnRetailModeAuthSuccess() override;
148 virtual void OnAuthSuccess() override;
149 virtual void OnAuthFailure(const AuthFailure& error) override;
150 virtual void RecoverEncryptedData(const std::string& old_password) override;
151 virtual void ResyncEncryptedData() override;
153 // AuthAttemptStateResolver overrides.
154 // Attempts to make a decision and call back |consumer_| based on
155 // the state we have gathered at the time of call. If a decision
156 // can't be made, defers until the next time this is called.
157 // When a decision is made, will call back to |consumer_| on the UI thread.
159 // Must be called on the UI thread.
160 virtual void Resolve() override;
162 void OnOffTheRecordAuthSuccess();
163 void OnPasswordChangeDetected();
166 virtual ~CryptohomeAuthenticator();
168 typedef base::Callback<void(bool is_owner)> IsOwnerCallback;
170 // Method to be implemented in child. Return |true| if user specified in
171 // |context| exists on device.
172 virtual bool IsKnownUser(const UserContext& context) = 0;
174 // Method to be implemented in child. Return |true| if device is running
176 virtual bool IsSafeMode() = 0;
178 // Method to be implemented in child. Have to call |callback| with boolean
179 // parameter that indicates if user in |context| can act as an owner in
181 virtual void CheckSafeModeOwnership(const UserContext& context,
182 const IsOwnerCallback& callback) = 0;
185 friend class CryptohomeAuthenticatorTest;
186 FRIEND_TEST_ALL_PREFIXES(CryptohomeAuthenticatorTest,
187 ResolveOwnerNeededDirectFailedMount);
188 FRIEND_TEST_ALL_PREFIXES(CryptohomeAuthenticatorTest,
189 ResolveOwnerNeededMount);
190 FRIEND_TEST_ALL_PREFIXES(CryptohomeAuthenticatorTest,
191 ResolveOwnerNeededFailedMount);
193 // Removes the cryptohome of the user.
194 void RemoveEncryptedData();
196 // Returns the AuthState we're in, given the status info we have at
198 // Must be called on the IO thread.
199 AuthState ResolveState();
201 // Helper for ResolveState().
202 // Given that some cryptohome operation has failed, determine which of the
203 // possible failure states we're in.
204 // Must be called on the IO thread.
205 AuthState ResolveCryptohomeFailureState();
207 // Helper for ResolveState().
208 // Given that some cryptohome operation has succeeded, determine which of
209 // the possible states we're in.
210 // Must be called on the IO thread.
211 AuthState ResolveCryptohomeSuccessState();
213 // Helper for ResolveState().
214 // Given that some online auth operation has succeeded, determine which of
215 // the possible success states we're in.
216 // Must be called on the IO thread.
217 AuthState ResolveOnlineSuccessState(AuthState offline_state);
220 void set_attempt_state(TestAttemptState* new_state) { // takes ownership.
221 current_state_.reset(new_state);
224 // Used for testing to set the expected state of an owner check.
225 void SetOwnerState(bool owner_check_finished, bool check_result);
227 // checks if the current mounted home contains the owner case and either
228 // continues or fails the log-in. Used for policy lost mitigation "safe-mode".
229 // Returns true if the owner check has been successful or if it is not needed.
232 // Handles completion of the ownership check and continues login.
233 void OnOwnershipChecked(bool is_owner);
235 // Signal login completion status for cases when a new user is added via
236 // an external authentication provider (i.e. GAIA extension).
237 void ResolveLoginCompletionStatus();
239 scoped_refptr<base::TaskRunner> task_runner_;
241 scoped_ptr<AuthAttemptState> current_state_;
242 bool migrate_attempted_;
243 bool remove_attempted_;
244 bool resync_attempted_;
245 bool ephemeral_mount_attempted_;
246 bool check_key_attempted_;
248 // When the user has changed her password, but gives us the old one, we will
249 // be able to mount her cryptohome, but online authentication will fail.
250 // This allows us to present the same behavior to the caller, regardless
251 // of the order in which we receive these results.
252 bool already_reported_success_;
253 base::Lock success_lock_; // A lock around |already_reported_success_|.
255 // Flags signaling whether the owner verification has been done and the result
257 bool owner_is_verified_;
258 bool user_can_login_;
260 // Flag indicating to delete the user's cryptohome the login fails.
261 bool remove_user_data_on_failure_;
263 // When |remove_user_data_on_failure_| is set, we delay calling
264 // consumer_->OnAuthFailure() until we removed the user cryptohome.
265 const AuthFailure* delayed_login_failure_;
267 DISALLOW_COPY_AND_ASSIGN(CryptohomeAuthenticator);
270 } // namespace chromeos
272 #endif // CHROMEOS_LOGIN_AUTH_CRYPTOHOME_AUTHENTICATOR_H_