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/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 "", | 45 "", |
46 PrefRegistrySyncable::UNSYNCABLE_PREF); | 46 PrefRegistrySyncable::UNSYNCABLE_PREF); |
47 registry->RegisterStringPref(kOAuth1Secret, | 47 registry->RegisterStringPref(kOAuth1Secret, |
48 "", | 48 "", |
49 PrefRegistrySyncable::UNSYNCABLE_PREF); | 49 PrefRegistrySyncable::UNSYNCABLE_PREF); |
50 } | 50 } |
51 | 51 |
52 void OAuth2LoginManager::RestoreSession( | 52 void OAuth2LoginManager::RestoreSession( |
53 Profile* user_profile, | 53 Profile* user_profile, |
54 net::URLRequestContextGetter* auth_request_context, | 54 net::URLRequestContextGetter* auth_request_context, |
55 bool restore_from_auth_cookies) { | 55 SessionRestoreStrategy restore_strategy, |
| 56 const std::string& oauth2_refresh_token, |
| 57 const std::string& auth_code) { |
56 user_profile_ = user_profile; | 58 user_profile_ = user_profile; |
57 auth_request_context_ = auth_request_context; | 59 auth_request_context_ = auth_request_context; |
58 state_ = OAuthLoginManager::SESSION_RESTORE_IN_PROGRESS; | 60 state_ = OAuthLoginManager::SESSION_RESTORE_IN_PROGRESS; |
59 restore_from_auth_cookies_ = restore_from_auth_cookies; | 61 restore_strategy_ = restore_strategy; |
| 62 refresh_token_ = oauth2_refresh_token; |
| 63 auth_code_ = auth_code; |
60 | 64 |
61 // TODO(zelidrag): Remove eventually the next line in some future milestone. | 65 // TODO(zelidrag): Remove eventually the next line in some future milestone. |
62 RemoveLegacyTokens(); | 66 RemoveLegacyTokens(); |
63 | 67 |
64 // Reuse the access token fetched by the OAuth2PolicyFetcher, if it was | 68 // Reuse the access token fetched by the OAuth2PolicyFetcher, if it was |
65 // used to fetch policies before Profile creation. | 69 // used to fetch policies before Profile creation. |
66 if (oauth2_policy_fetcher_.get() && | 70 if (oauth2_policy_fetcher_.get() && |
67 oauth2_policy_fetcher_->has_oauth2_tokens()) { | 71 oauth2_policy_fetcher_->has_oauth2_tokens()) { |
68 VLOG(1) << "Resuming profile creation after fetching policy token"; | 72 VLOG(1) << "Resuming profile creation after fetching policy token"; |
69 // We already have tokens, no need to get them from the cookie jar again. | 73 // We already have tokens, no need to get them from the cookie jar again. |
70 restore_from_auth_cookies_ = false; | 74 if (restore_strategy_ == RESTORE_FROM_COOKIE_JAR) |
| 75 restore_strategy_ = RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN; |
| 76 |
71 StoreOAuth2Tokens(oauth2_policy_fetcher_->oauth2_tokens()); | 77 StoreOAuth2Tokens(oauth2_policy_fetcher_->oauth2_tokens()); |
72 } | 78 } |
73 | 79 |
74 ContinueSessionRestore(); | 80 ContinueSessionRestore(); |
75 } | 81 } |
76 | 82 |
77 void OAuth2LoginManager::ContinueSessionRestore() { | 83 void OAuth2LoginManager::ContinueSessionRestore() { |
78 if (restore_from_auth_cookies_) { | 84 if (restore_strategy_ == RESTORE_FROM_COOKIE_JAR || |
| 85 restore_strategy_ == RESTORE_FROM_AUTH_CODE) { |
79 FetchOAuth2Tokens(); | 86 FetchOAuth2Tokens(); |
80 return; | 87 return; |
81 } | 88 } |
82 | 89 |
83 // Save OAuth2 refresh token from the command line in forced | 90 // Save passed OAuth2 refresh token. |
84 // app mode. | 91 if (restore_strategy_ == RESTORE_FROM_PASSED_OAUTH2_REFRESH_TOKEN) { |
85 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 92 DCHECK(!refresh_token_.empty()); |
86 if (command_line->HasSwitch(switches::kForceAppMode) && | 93 restore_strategy_ = RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN; |
87 command_line->HasSwitch(switches::kAppId) && | |
88 command_line->HasSwitch(switches::kAppModeOAuth2Token) && | |
89 !command_line->GetSwitchValueASCII( | |
90 switches::kAppModeOAuth2Token).empty()) { | |
91 GaiaAuthConsumer::ClientOAuthResult oauth2_tokens; | 94 GaiaAuthConsumer::ClientOAuthResult oauth2_tokens; |
92 oauth2_tokens.refresh_token = | 95 oauth2_tokens.refresh_token = refresh_token_; |
93 command_line->GetSwitchValueASCII(switches::kAppModeOAuth2Token); | |
94 StoreOAuth2Tokens(oauth2_tokens); | 96 StoreOAuth2Tokens(oauth2_tokens); |
95 return; | 97 return; |
96 } | 98 } |
97 | 99 |
| 100 DCHECK(restore_strategy_ == RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN); |
98 LoadAndVerifyOAuth2Tokens(); | 101 LoadAndVerifyOAuth2Tokens(); |
99 } | 102 } |
100 | 103 |
101 void OAuth2LoginManager::RestorePolicyTokens( | 104 void OAuth2LoginManager::RestorePolicyTokens( |
102 net::URLRequestContextGetter* auth_request_context) { | 105 net::URLRequestContextGetter* auth_request_context) { |
103 oauth2_policy_fetcher_.reset( | 106 oauth2_policy_fetcher_.reset( |
104 new OAuth2PolicyFetcher(auth_request_context, | 107 new OAuth2PolicyFetcher(auth_request_context, |
105 g_browser_process->system_request_context())); | 108 g_browser_process->system_request_context())); |
106 oauth2_policy_fetcher_->Start(); | 109 oauth2_policy_fetcher_->Start(); |
107 } | 110 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // If we have no cookies, try to load saved OAuth2 token from TokenService. | 144 // If we have no cookies, try to load saved OAuth2 token from TokenService. |
142 TokenService* token_service = SetupTokenService(); | 145 TokenService* token_service = SetupTokenService(); |
143 token_service->Initialize(GaiaConstants::kChromeSource, user_profile_); | 146 token_service->Initialize(GaiaConstants::kChromeSource, user_profile_); |
144 token_service->LoadTokensFromDB(); | 147 token_service->LoadTokensFromDB(); |
145 } | 148 } |
146 | 149 |
147 void OAuth2LoginManager::FetchOAuth2Tokens() { | 150 void OAuth2LoginManager::FetchOAuth2Tokens() { |
148 DCHECK(auth_request_context_.get()); | 151 DCHECK(auth_request_context_.get()); |
149 // If we have authenticated cookie jar, get OAuth1 token first, then fetch | 152 // If we have authenticated cookie jar, get OAuth1 token first, then fetch |
150 // SID/LSID cookies through OAuthLogin call. | 153 // SID/LSID cookies through OAuthLogin call. |
151 oauth2_token_fetcher_.reset( | 154 if (restore_strategy_ == RESTORE_FROM_COOKIE_JAR) { |
152 new OAuth2TokenFetcher(this, auth_request_context_)); | 155 oauth2_token_fetcher_.reset( |
153 oauth2_token_fetcher_->Start(); | 156 new OAuth2TokenFetcher(this, auth_request_context_)); |
| 157 oauth2_token_fetcher_->StartExchangeFromCookies(); |
| 158 } else if (restore_strategy_ == RESTORE_FROM_AUTH_CODE) { |
| 159 DCHECK(!auth_code_.empty()); |
| 160 oauth2_token_fetcher_.reset( |
| 161 new OAuth2TokenFetcher(this, |
| 162 g_browser_process->system_request_context())); |
| 163 oauth2_token_fetcher_->StartExchangeFromAuthCode(auth_code_); |
| 164 } else { |
| 165 NOTREACHED(); |
| 166 } |
154 } | 167 } |
155 | 168 |
156 void OAuth2LoginManager::OnOAuth2TokensAvailable( | 169 void OAuth2LoginManager::OnOAuth2TokensAvailable( |
157 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { | 170 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) { |
158 LOG(INFO) << "OAuth2 tokens fetched"; | 171 LOG(INFO) << "OAuth2 tokens fetched"; |
159 StoreOAuth2Tokens(oauth2_tokens); | 172 StoreOAuth2Tokens(oauth2_tokens); |
160 } | 173 } |
161 | 174 |
162 void OAuth2LoginManager::OnOAuth2TokensFetchFailed() { | 175 void OAuth2LoginManager::OnOAuth2TokensFetchFailed() { |
163 LOG(ERROR) << "OAuth2 tokens fetch failed!"; | 176 LOG(ERROR) << "OAuth2 tokens fetch failed!"; |
(...skipping 12 matching lines...) Expand all Loading... |
176 const content::NotificationDetails& details) { | 189 const content::NotificationDetails& details) { |
177 TokenService* token_service = | 190 TokenService* token_service = |
178 TokenServiceFactory::GetForProfile(user_profile_); | 191 TokenServiceFactory::GetForProfile(user_profile_); |
179 switch (type) { | 192 switch (type) { |
180 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { | 193 case chrome::NOTIFICATION_TOKEN_LOADING_FINISHED: { |
181 refresh_token_ = token_service->GetOAuth2LoginRefreshToken(); | 194 refresh_token_ = token_service->GetOAuth2LoginRefreshToken(); |
182 RestoreSessionCookies(); | 195 RestoreSessionCookies(); |
183 break; | 196 break; |
184 } | 197 } |
185 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | 198 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { |
186 // This path should kick on only when we mint a new OAuth2 refresh | 199 // This path should not kick in if are loading OAuth2 refresh token |
187 // token for user cookies. Otherwise, wait for all tokens to load above. | 200 // from the TokenService. |
188 if (!restore_from_auth_cookies_) | 201 if (restore_strategy_ == RESTORE_FROM_SAVED_OAUTH2_REFRESH_TOKEN) |
189 return; | 202 return; |
190 | 203 |
191 TokenService::TokenAvailableDetails* token_details = | 204 TokenService::TokenAvailableDetails* token_details = |
192 content::Details<TokenService::TokenAvailableDetails>( | 205 content::Details<TokenService::TokenAvailableDetails>( |
193 details).ptr(); | 206 details).ptr(); |
194 if (token_details->service() == | 207 if (token_details->service() == |
195 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { | 208 GaiaConstants::kGaiaOAuth2LoginRefreshToken) { |
196 DCHECK(!login_verifier_.get()); | 209 DCHECK(!login_verifier_.get()); |
197 refresh_token_ = token_details->token(); | 210 refresh_token_ = token_details->token(); |
198 RestoreSessionCookies(); | 211 RestoreSessionCookies(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 } | 293 } |
281 | 294 |
282 void OAuth2LoginManager::StartTokenService( | 295 void OAuth2LoginManager::StartTokenService( |
283 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) { | 296 const GaiaAuthConsumer::ClientLoginResult& gaia_credentials) { |
284 TokenService* token_service = SetupTokenService(); | 297 TokenService* token_service = SetupTokenService(); |
285 token_service->UpdateCredentials(gaia_credentials); | 298 token_service->UpdateCredentials(gaia_credentials); |
286 CompleteAuthentication(); | 299 CompleteAuthentication(); |
287 } | 300 } |
288 | 301 |
289 } // namespace chromeos | 302 } // namespace chromeos |
OLD | NEW |