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 #include "chrome/browser/chromeos/login/auth/online_attempt.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chromeos/login/auth/auth_attempt_state.h"
14 #include "chromeos/login/auth/auth_attempt_state_resolver.h"
15 #include "chromeos/login/auth/key.h"
16 #include "chromeos/login/auth/user_context.h"
17 #include "components/user_manager/user_type.h"
18 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "google_apis/gaia/gaia_auth_consumer.h"
21 #include "google_apis/gaia/gaia_auth_fetcher.h"
22 #include "google_apis/gaia/gaia_constants.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/net_errors.h"
25 #include "net/url_request/url_request_status.h"
27 using content::BrowserThread;
32 const int OnlineAttempt::kClientLoginTimeoutMs = 10000;
34 OnlineAttempt::OnlineAttempt(AuthAttemptState* current_attempt,
35 AuthAttemptStateResolver* callback)
36 : attempt_(current_attempt),
40 DCHECK(attempt_->user_type == user_manager::USER_TYPE_REGULAR);
43 OnlineAttempt::~OnlineAttempt() {
45 if (client_fetcher_.get())
46 client_fetcher_->CancelRequest();
49 void OnlineAttempt::Initiate(content::BrowserContext* auth_context) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51 client_fetcher_.reset(
52 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource,
53 auth_context->GetRequestContext()));
54 BrowserThread::PostTask(
55 BrowserThread::UI, FROM_HERE,
56 base::Bind(&OnlineAttempt::TryClientLogin, weak_factory_.GetWeakPtr()));
59 void OnlineAttempt::OnClientLoginSuccess(
60 const GaiaAuthConsumer::ClientLoginResult& unused) {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 VLOG(1) << "Online login successful!";
64 weak_factory_.InvalidateWeakPtrs();
66 if (attempt_->hosted_policy() == GaiaAuthFetcher::HostedAccountsAllowed &&
67 attempt_->is_first_time_user()) {
68 // First time user, and we don't know if the account is HOSTED or not.
69 // Since we don't allow HOSTED accounts to log in, we need to try
70 // again, without allowing HOSTED accounts.
72 // NOTE: we used to do this in the opposite order, so that we'd only
73 // try the HOSTED pathway if GOOGLE-only failed. This breaks CAPTCHA
75 attempt_->DisableHosted();
79 TriggerResolve(AuthFailure::AuthFailureNone());
82 void OnlineAttempt::OnClientLoginFailure(
83 const GoogleServiceAuthError& error) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86 weak_factory_.InvalidateWeakPtrs();
88 if (error.state() == GoogleServiceAuthError::REQUEST_CANCELED) {
91 // TODO(cmasone): add UMA tracking for this to see if we can remove it.
92 LOG(ERROR) << "Login attempt canceled!?!? Trying again.";
96 LOG(ERROR) << "Login attempt canceled again? Already retried...";
99 if (error.state() == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS &&
100 attempt_->is_first_time_user() &&
101 attempt_->hosted_policy() != GaiaAuthFetcher::HostedAccountsAllowed) {
102 // This was a first-time login, we already tried allowing HOSTED accounts
103 // and succeeded. That we've failed with INVALID_GAIA_CREDENTIALS now
104 // indicates that the account is HOSTED.
105 LOG(WARNING) << "Rejecting valid HOSTED account.";
106 TriggerResolve(AuthFailure::FromNetworkAuthFailure(
107 GoogleServiceAuthError(GoogleServiceAuthError::HOSTED_NOT_ALLOWED)));
111 if (error.state() == GoogleServiceAuthError::TWO_FACTOR) {
112 LOG(WARNING) << "Two factor authenticated. Sync will not work.";
113 TriggerResolve(AuthFailure::AuthFailureNone());
117 VLOG(2) << "ClientLogin attempt failed with " << error.state();
118 TriggerResolve(AuthFailure::FromNetworkAuthFailure(error));
121 void OnlineAttempt::TryClientLogin() {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
124 BrowserThread::PostDelayedTask(
125 BrowserThread::UI, FROM_HERE,
126 base::Bind(&OnlineAttempt::CancelClientLogin, weak_factory_.GetWeakPtr()),
127 base::TimeDelta::FromMilliseconds(kClientLoginTimeoutMs));
129 client_fetcher_->StartClientLogin(
130 attempt_->user_context.GetUserID(),
131 attempt_->user_context.GetKey()->GetSecret(),
132 GaiaConstants::kSyncService,
133 attempt_->login_token,
134 attempt_->login_captcha,
135 attempt_->hosted_policy());
138 bool OnlineAttempt::HasPendingFetch() {
139 return client_fetcher_->HasPendingFetch();
142 void OnlineAttempt::CancelRequest() {
143 weak_factory_.InvalidateWeakPtrs();
146 void OnlineAttempt::CancelClientLogin() {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
148 if (HasPendingFetch()) {
149 LOG(WARNING) << "Canceling ClientLogin attempt.";
152 TriggerResolve(AuthFailure(AuthFailure::LOGIN_TIMED_OUT));
156 void OnlineAttempt::TriggerResolve(const AuthFailure& outcome) {
157 attempt_->RecordOnlineLoginStatus(outcome);
158 client_fetcher_.reset(NULL);
159 resolver_->Resolve();
162 } // namespace chromeos