OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/sync/signin_manager_oauth.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/net/gaia/authentication_service.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
| 14 #include "chrome/common/net/gaia/gaia_constants.h" |
| 15 #include "chrome/common/net/gaia/authentication_fetcher_oauth.h" |
| 16 #include "chrome/common/pref_names.h" |
| 17 #include "content/common/notification_service.h" |
| 18 |
| 19 const char kGetInfoEmailKey[] = "email"; |
| 20 |
| 21 // static |
| 22 const char SigninManagerOAuth::kSigninManagerVariantName[] = "OAuth 2.0"; |
| 23 |
| 24 SigninManagerOAuth::SigninManagerOAuth() |
| 25 : profile_(NULL), had_two_factor_error_(false) {} |
| 26 |
| 27 SigninManagerOAuth::~SigninManagerOAuth() {} |
| 28 |
| 29 // static |
| 30 void SigninManagerOAuth::RegisterUserPrefs(PrefService* user_prefs) { |
| 31 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, ""); |
| 32 } |
| 33 |
| 34 // virtual |
| 35 void SigninManagerOAuth::Initialize(Profile* profile) { |
| 36 profile_ = profile; |
| 37 username_ = profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); |
| 38 profile_->GetAuthenticationService()->Initialize(GaiaConstants::kChromeSource, |
| 39 profile_); |
| 40 if (!username_.empty()) { |
| 41 profile_->GetAuthenticationService()->LoadTokensFromDB(); |
| 42 } |
| 43 } |
| 44 |
| 45 // If a username already exists, the user is logged in. |
| 46 const std::string& SigninManagerOAuth::GetUsername() { |
| 47 return username_; |
| 48 } |
| 49 |
| 50 void SigninManagerOAuth::SetUsername(const std::string& username) { |
| 51 username_ = username; |
| 52 } |
| 53 |
| 54 // Users must always sign out before they sign in again. |
| 55 void SigninManagerOAuth::StartSignIn(const std::string& username, |
| 56 const std::string& password, |
| 57 const std::string& login_token, |
| 58 const std::string& login_captcha) { |
| 59 DCHECK(username_.empty()); |
| 60 #if !defined(OS_CHROMEOS) |
| 61 // The Sign out should clear the token service credentials. |
| 62 // Note: In CHROMEOS we might have valid credentials but still need to |
| 63 // set up 2-factor authentication. |
| 64 DCHECK(!profile_->GetAuthenticationService()->AreCredentialsValid()); |
| 65 #endif |
| 66 username_.assign(username); |
| 67 password_.assign(password); |
| 68 |
| 69 fetcher_.reset(new AuthenticationFetcherOAuth(this, |
| 70 GaiaConstants::kChromeSource, |
| 71 profile_->GetRequestContext())); |
| 72 fetcher_->StartAuthentication( |
| 73 username, password, "", login_token, login_captcha, |
| 74 AuthenticationFetcher::HostedAccountsNotAllowed); |
| 75 } |
| 76 |
| 77 void SigninManagerOAuth::ProvideSecondFactorAccessCode( |
| 78 const std::string& access_code) { |
| 79 DCHECK(!username_.empty() && !password_.empty() && |
| 80 static_cast<AuthenticationConsumerOAuth::AuthenticationResult*>( |
| 81 last_result_.get())->data.empty()); |
| 82 |
| 83 fetcher_.reset(new AuthenticationFetcherOAuth(this, |
| 84 GaiaConstants::kChromeSource, |
| 85 profile_->GetRequestContext())); |
| 86 fetcher_->StartAuthentication( |
| 87 username_, access_code, "", std::string(), std::string(), |
| 88 AuthenticationFetcherOAuth::HostedAccountsNotAllowed); |
| 89 } |
| 90 |
| 91 void SigninManagerOAuth::SignOut() { |
| 92 if (!profile_) |
| 93 return; |
| 94 |
| 95 fetcher_.reset(); |
| 96 last_result_.reset(new AuthenticationResult()); |
| 97 username_.clear(); |
| 98 password_.clear(); |
| 99 had_two_factor_error_ = false; |
| 100 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); |
| 101 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 102 profile_->GetAuthenticationService()->ResetCredentialsInMemory(); |
| 103 profile_->GetAuthenticationService()->EraseTokensFromDB(); |
| 104 } |
| 105 |
| 106 void SigninManagerOAuth::OnAuthenticationSuccess( |
| 107 AuthenticationResult* result) { |
| 108 last_result_.reset(result); |
| 109 // Make a request for the canonical email address. |
| 110 fetcher_->StartGetUserInfo( |
| 111 static_cast<AuthenticationConsumerOAuth::AuthenticationResult*>( |
| 112 result)->lsid, |
| 113 kGetInfoEmailKey); |
| 114 } |
| 115 |
| 116 void SigninManagerOAuth::OnGetUserInfoSuccess(const std::string& key, |
| 117 const std::string& value) { |
| 118 DCHECK(key == kGetInfoEmailKey); |
| 119 |
| 120 username_ = value; |
| 121 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); |
| 122 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 123 |
| 124 GoogleServiceSigninSuccessDetails details(username_, password_); |
| 125 NotificationService::current()->Notify( |
| 126 NotificationType::GOOGLE_SIGNIN_SUCCESSFUL, |
| 127 Source<Profile>(profile_), |
| 128 Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 129 |
| 130 password_.clear(); // Don't need it anymore. |
| 131 |
| 132 profile_->GetAuthenticationService()->UpdateCredentials(last_result_.get()); |
| 133 DCHECK(profile_->GetAuthenticationService()->AreCredentialsValid()); |
| 134 profile_->GetAuthenticationService()->StartFetchingTokens(); |
| 135 } |
| 136 |
| 137 void SigninManagerOAuth::OnGetUserInfoKeyNotFound(const std::string& key) { |
| 138 DCHECK(key == kGetInfoEmailKey); |
| 139 LOG(ERROR) << "Account is not associated with a valid email address. " |
| 140 << "Login failed."; |
| 141 OnAuthenticationFailure(GoogleServiceAuthError( |
| 142 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 143 } |
| 144 |
| 145 void SigninManagerOAuth::OnGetUserInfoFailure( |
| 146 const GoogleServiceAuthError& error) { |
| 147 LOG(ERROR) << "Unable to retreive the canonical email address. Login failed."; |
| 148 OnAuthenticationFailure(error); |
| 149 } |
| 150 |
| 151 void SigninManagerOAuth::OnAuthenticationFailure( |
| 152 const GoogleServiceAuthError& error) { |
| 153 NotificationService::current()->Notify( |
| 154 NotificationType::GOOGLE_SIGNIN_FAILED, |
| 155 Source<Profile>(profile_), |
| 156 Details<const GoogleServiceAuthError>(&error)); |
| 157 |
| 158 // We don't sign-out if the password was valid and we're just dealing with |
| 159 // a second factor error, and we don't sign out if we're dealing with |
| 160 // an invalid access code (again, because the password was valid). |
| 161 bool invalid_gaia = error.state() == |
| 162 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS; |
| 163 if (error.state() == GoogleServiceAuthError::TWO_FACTOR || |
| 164 (had_two_factor_error_ && invalid_gaia)) { |
| 165 had_two_factor_error_ = true; |
| 166 return; |
| 167 } |
| 168 |
| 169 SignOut(); |
| 170 } |
OLD | NEW |