OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" | 5 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/chromeos/login/user_manager.h" | 10 #include "chrome/browser/chromeos/login/user_manager.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 OAuth2LoginManager::~OAuth2LoginManager() { | 29 OAuth2LoginManager::~OAuth2LoginManager() { |
30 } | 30 } |
31 | 31 |
32 void OAuth2LoginManager::RestoreSession( | 32 void OAuth2LoginManager::RestoreSession( |
33 Profile* user_profile, | 33 Profile* user_profile, |
34 net::URLRequestContextGetter* auth_request_context, | 34 net::URLRequestContextGetter* auth_request_context, |
35 bool restore_from_auth_cookies) { | 35 bool restore_from_auth_cookies) { |
36 user_profile_ = user_profile; | 36 user_profile_ = user_profile; |
37 auth_request_context_ = auth_request_context; | 37 auth_request_context_ = auth_request_context; |
38 state_ = OAuthLoginManager::SESSION_RESTORE_IN_PROGRESS; | 38 state_ = OAuthLoginManager::SESSION_RESTORE_IN_PROGRESS; |
| 39 restore_from_auth_cookies_ = restore_from_auth_cookies; |
39 | 40 |
40 // TODO(zelidrag): Remove eventually the next line in some future milestone. | 41 // TODO(zelidrag): Remove eventually the next line in some future milestone. |
41 RemoveLegacyTokens(); | 42 RemoveLegacyTokens(); |
42 | 43 |
43 // Reuse the access token fetched by the OAuth2PolicyFetcher, if it was | 44 // Reuse the access token fetched by the OAuth2PolicyFetcher, if it was |
44 // used to fetch policies before Profile creation. | 45 // used to fetch policies before Profile creation. |
45 if (oauth2_policy_fetcher_.get() && | 46 if (oauth2_policy_fetcher_.get() && |
46 oauth2_policy_fetcher_->has_oauth2_tokens()) { | 47 oauth2_policy_fetcher_->has_oauth2_tokens()) { |
47 VLOG(1) << "Resuming profile creation after fetching policy token"; | 48 VLOG(1) << "Resuming profile creation after fetching policy token"; |
48 refresh_token_ = oauth2_policy_fetcher_->oauth2_tokens().refresh_token; | 49 // We already have tokens, no need to get them from the cookie jar again. |
49 restore_from_auth_cookies = false; | 50 restore_from_auth_cookies_ = false; |
| 51 StoreOAuth2Tokens(oauth2_policy_fetcher_->oauth2_tokens()); |
50 } | 52 } |
51 restore_from_auth_cookies_ = restore_from_auth_cookies; | 53 |
52 ContinueSessionRestore(); | 54 ContinueSessionRestore(); |
53 } | 55 } |
54 | 56 |
55 void OAuth2LoginManager::ContinueSessionRestore() { | 57 void OAuth2LoginManager::ContinueSessionRestore() { |
56 if (restore_from_auth_cookies_) { | 58 if (restore_from_auth_cookies_) { |
57 FetchOAuth2Tokens(); | 59 FetchOAuth2Tokens(); |
58 return; | 60 return; |
59 } | 61 } |
60 | 62 |
61 // Did we already fetch the refresh token (either policy or db)? | |
62 if (!refresh_token_.empty()) { | |
63 // TODO(zelidrag): Figure out where to stick that refresh_token_ into. | |
64 // We probalby need bit more than that. | |
65 } | |
66 LoadAndVerifyOAuth2Tokens(); | 63 LoadAndVerifyOAuth2Tokens(); |
67 } | 64 } |
68 | 65 |
69 void OAuth2LoginManager::RestorePolicyTokens( | 66 void OAuth2LoginManager::RestorePolicyTokens( |
70 net::URLRequestContextGetter* auth_request_context) { | 67 net::URLRequestContextGetter* auth_request_context) { |
71 oauth2_policy_fetcher_.reset( | 68 oauth2_policy_fetcher_.reset( |
72 new OAuth2PolicyFetcher(auth_request_context, | 69 new OAuth2PolicyFetcher(auth_request_context, |
73 g_browser_process->system_request_context())); | 70 g_browser_process->system_request_context())); |
74 oauth2_policy_fetcher_->Start(); | 71 oauth2_policy_fetcher_->Start(); |
75 } | 72 } |
(...skipping 24 matching lines...) Expand all Loading... |
100 PrefServiceSyncable::UNSYNCABLE_PREF); | 97 PrefServiceSyncable::UNSYNCABLE_PREF); |
101 prefs->RegisterStringPref(prefs::kOAuth1Secret, | 98 prefs->RegisterStringPref(prefs::kOAuth1Secret, |
102 "", | 99 "", |
103 PrefServiceSyncable::UNSYNCABLE_PREF); | 100 PrefServiceSyncable::UNSYNCABLE_PREF); |
104 prefs->ClearPref(prefs::kOAuth1Token); | 101 prefs->ClearPref(prefs::kOAuth1Token); |
105 prefs->ClearPref(prefs::kOAuth1Secret); | 102 prefs->ClearPref(prefs::kOAuth1Secret); |
106 prefs->UnregisterPreference(prefs::kOAuth1Token); | 103 prefs->UnregisterPreference(prefs::kOAuth1Token); |
107 prefs->UnregisterPreference(prefs::kOAuth1Secret); | 104 prefs->UnregisterPreference(prefs::kOAuth1Secret); |
108 } | 105 } |
109 | 106 |
| 107 void OAuth2LoginManager::StoreOAuth2Tokens( |
| 108 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
| 109 TokenService* token_service = |
| 110 TokenServiceFactory::GetForProfile(user_profile_); |
| 111 token_service->UpdateCredentialsWithOAuth2(oauth2_tokens); |
| 112 } |
| 113 |
110 void OAuth2LoginManager::LoadAndVerifyOAuth2Tokens() { | 114 void OAuth2LoginManager::LoadAndVerifyOAuth2Tokens() { |
111 // If we have no cookies, try to load saved OAuth2 token from TokenService. | 115 // If we have no cookies, try to load saved OAuth2 token from TokenService. |
112 TokenService* token_service = SetupTokenService(); | 116 TokenService* token_service = SetupTokenService(); |
113 token_service->Initialize(GaiaConstants::kChromeSource, user_profile_); | 117 token_service->Initialize(GaiaConstants::kChromeSource, user_profile_); |
114 token_service->LoadTokensFromDB(); | 118 token_service->LoadTokensFromDB(); |
115 } | 119 } |
116 | 120 |
117 void OAuth2LoginManager::FetchOAuth2Tokens() { | 121 void OAuth2LoginManager::FetchOAuth2Tokens() { |
118 DCHECK(auth_request_context_.get()); | 122 DCHECK(auth_request_context_.get()); |
119 // If we have authenticated cookie jar, get OAuth1 token first, then fetch | 123 // If we have authenticated cookie jar, get OAuth1 token first, then fetch |
120 // SID/LSID cookies through OAuthLogin call. | 124 // SID/LSID cookies through OAuthLogin call. |
121 oauth2_token_fetcher_.reset( | 125 oauth2_token_fetcher_.reset( |
122 new OAuth2TokenFetcher(this, auth_request_context_)); | 126 new OAuth2TokenFetcher(this, auth_request_context_)); |
123 oauth2_token_fetcher_->Start(); | 127 oauth2_token_fetcher_->Start(); |
124 } | 128 } |
125 | 129 |
126 void OAuth2LoginManager::OnOAuth2TokensAvailable( | 130 void OAuth2LoginManager::OnOAuth2TokensAvailable( |
127 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { | 131 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
128 LOG(INFO) << "OAuth2 tokens fetched"; | 132 LOG(INFO) << "OAuth2 tokens fetched"; |
129 TokenService* token_service = SetupTokenService(); | 133 StoreOAuth2Tokens(oauth2_tokens); |
130 token_service->UpdateCredentialsWithOAuth2(oauth2_tokens); | |
131 } | 134 } |
132 | 135 |
133 void OAuth2LoginManager::OnOAuth2TokensFetchFailed() { | 136 void OAuth2LoginManager::OnOAuth2TokensFetchFailed() { |
134 LOG(ERROR) << "OAuth2 tokens fetch failed!"; | 137 LOG(ERROR) << "OAuth2 tokens fetch failed!"; |
135 state_ = OAuthLoginManager::SESSION_RESTORE_DONE; | 138 state_ = OAuthLoginManager::SESSION_RESTORE_DONE; |
136 UserManager::Get()->SaveUserOAuthStatus( | 139 UserManager::Get()->SaveUserOAuthStatus( |
137 UserManager::Get()->GetLoggedInUser()->email(), | 140 UserManager::Get()->GetLoggedInUser()->email(), |
138 User::OAUTH2_TOKEN_STATUS_INVALID); | 141 User::OAUTH2_TOKEN_STATUS_INVALID); |
139 UMA_HISTOGRAM_ENUMERATION("OAuth2Login.SessionRestore", | 142 UMA_HISTOGRAM_ENUMERATION("OAuth2Login.SessionRestore", |
140 SESSION_RESTORE_TOKEN_FETCH_FAILED, | 143 SESSION_RESTORE_TOKEN_FETCH_FAILED, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 253 } |
251 | 254 |
252 void OAuth2LoginManager::StartTokenService( | 255 void OAuth2LoginManager::StartTokenService( |
253 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) { | 256 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) { |
254 TokenService* token_service = SetupTokenService(); | 257 TokenService* token_service = SetupTokenService(); |
255 token_service->UpdateCredentials(gaia_credentials); | 258 token_service->UpdateCredentials(gaia_credentials); |
256 CompleteAuthentication(); | 259 CompleteAuthentication(); |
257 } | 260 } |
258 | 261 |
259 } // namespace chromeos | 262 } // namespace chromeos |
OLD | NEW |