OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/managed_mode/managed_user_refresh_token_fetcher.h" | 5 #include "chrome/browser/supervised_user/supervised_user_refresh_token_fetcher.h
" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "google_apis/gaia/gaia_constants.h" | 12 #include "google_apis/gaia/gaia_constants.h" |
13 #include "google_apis/gaia/gaia_oauth_client.h" | 13 #include "google_apis/gaia/gaia_oauth_client.h" |
14 #include "google_apis/gaia/gaia_urls.h" | 14 #include "google_apis/gaia/gaia_urls.h" |
15 #include "google_apis/gaia/google_service_auth_error.h" | 15 #include "google_apis/gaia/google_service_auth_error.h" |
(...skipping 22 matching lines...) Expand all Loading... |
38 "&scope=%s" | 38 "&scope=%s" |
39 "&response_type=code" | 39 "&response_type=code" |
40 "&profile_id=%s" | 40 "&profile_id=%s" |
41 "&device_name=%s"; | 41 "&device_name=%s"; |
42 | 42 |
43 static const char kAuthorizationHeaderFormat[] = | 43 static const char kAuthorizationHeaderFormat[] = |
44 "Authorization: Bearer %s"; | 44 "Authorization: Bearer %s"; |
45 | 45 |
46 static const char kCodeKey[] = "code"; | 46 static const char kCodeKey[] = "code"; |
47 | 47 |
48 class ManagedUserRefreshTokenFetcherImpl | 48 class SupervisedUserRefreshTokenFetcherImpl |
49 : public ManagedUserRefreshTokenFetcher, | 49 : public SupervisedUserRefreshTokenFetcher, |
50 public OAuth2TokenService::Consumer, | 50 public OAuth2TokenService::Consumer, |
51 public URLFetcherDelegate, | 51 public URLFetcherDelegate, |
52 public GaiaOAuthClient::Delegate { | 52 public GaiaOAuthClient::Delegate { |
53 public: | 53 public: |
54 ManagedUserRefreshTokenFetcherImpl(OAuth2TokenService* oauth2_token_service, | 54 SupervisedUserRefreshTokenFetcherImpl( |
55 const std::string& account_id, | 55 OAuth2TokenService* oauth2_token_service, |
56 URLRequestContextGetter* context); | 56 const std::string& account_id, |
57 virtual ~ManagedUserRefreshTokenFetcherImpl(); | 57 URLRequestContextGetter* context); |
| 58 virtual ~SupervisedUserRefreshTokenFetcherImpl(); |
58 | 59 |
59 // ManagedUserRefreshTokenFetcher implementation: | 60 // SupervisedUserRefreshTokenFetcher implementation: |
60 virtual void Start(const std::string& managed_user_id, | 61 virtual void Start(const std::string& supervised_user_id, |
61 const std::string& device_name, | 62 const std::string& device_name, |
62 const TokenCallback& callback) OVERRIDE; | 63 const TokenCallback& callback) OVERRIDE; |
63 | 64 |
64 protected: | 65 protected: |
65 // OAuth2TokenService::Consumer implementation: | 66 // OAuth2TokenService::Consumer implementation: |
66 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 67 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
67 const std::string& access_token, | 68 const std::string& access_token, |
68 const Time& expiration_time) OVERRIDE; | 69 const Time& expiration_time) OVERRIDE; |
69 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 70 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
70 const GoogleServiceAuthError& error) OVERRIDE; | 71 const GoogleServiceAuthError& error) OVERRIDE; |
(...skipping 16 matching lines...) Expand all Loading... |
87 void StartFetching(); | 88 void StartFetching(); |
88 | 89 |
89 void DispatchNetworkError(int error_code); | 90 void DispatchNetworkError(int error_code); |
90 void DispatchGoogleServiceAuthError(const GoogleServiceAuthError& error, | 91 void DispatchGoogleServiceAuthError(const GoogleServiceAuthError& error, |
91 const std::string& token); | 92 const std::string& token); |
92 OAuth2TokenService* oauth2_token_service_; | 93 OAuth2TokenService* oauth2_token_service_; |
93 std::string account_id_; | 94 std::string account_id_; |
94 URLRequestContextGetter* context_; | 95 URLRequestContextGetter* context_; |
95 | 96 |
96 std::string device_name_; | 97 std::string device_name_; |
97 std::string managed_user_id_; | 98 std::string supervised_user_id_; |
98 TokenCallback callback_; | 99 TokenCallback callback_; |
99 | 100 |
100 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | 101 scoped_ptr<OAuth2TokenService::Request> access_token_request_; |
101 std::string access_token_; | 102 std::string access_token_; |
102 bool access_token_expired_; | 103 bool access_token_expired_; |
103 scoped_ptr<URLFetcher> url_fetcher_; | 104 scoped_ptr<URLFetcher> url_fetcher_; |
104 scoped_ptr<GaiaOAuthClient> gaia_oauth_client_; | 105 scoped_ptr<GaiaOAuthClient> gaia_oauth_client_; |
105 }; | 106 }; |
106 | 107 |
107 ManagedUserRefreshTokenFetcherImpl::ManagedUserRefreshTokenFetcherImpl( | 108 SupervisedUserRefreshTokenFetcherImpl::SupervisedUserRefreshTokenFetcherImpl( |
108 OAuth2TokenService* oauth2_token_service, | 109 OAuth2TokenService* oauth2_token_service, |
109 const std::string& account_id, | 110 const std::string& account_id, |
110 URLRequestContextGetter* context) | 111 URLRequestContextGetter* context) |
111 : OAuth2TokenService::Consumer("managed_user"), | 112 : OAuth2TokenService::Consumer("supervised_user"), |
112 oauth2_token_service_(oauth2_token_service), | 113 oauth2_token_service_(oauth2_token_service), |
113 account_id_(account_id), | 114 account_id_(account_id), |
114 context_(context), | 115 context_(context), |
115 access_token_expired_(false) {} | 116 access_token_expired_(false) {} |
116 | 117 |
117 ManagedUserRefreshTokenFetcherImpl::~ManagedUserRefreshTokenFetcherImpl() {} | 118 SupervisedUserRefreshTokenFetcherImpl:: |
| 119 ~SupervisedUserRefreshTokenFetcherImpl() {} |
118 | 120 |
119 void ManagedUserRefreshTokenFetcherImpl::Start( | 121 void SupervisedUserRefreshTokenFetcherImpl::Start( |
120 const std::string& managed_user_id, | 122 const std::string& supervised_user_id, |
121 const std::string& device_name, | 123 const std::string& device_name, |
122 const TokenCallback& callback) { | 124 const TokenCallback& callback) { |
123 DCHECK(callback_.is_null()); | 125 DCHECK(callback_.is_null()); |
124 managed_user_id_ = managed_user_id; | 126 supervised_user_id_ = supervised_user_id; |
125 device_name_ = device_name; | 127 device_name_ = device_name; |
126 callback_ = callback; | 128 callback_ = callback; |
127 StartFetching(); | 129 StartFetching(); |
128 } | 130 } |
129 | 131 |
130 void ManagedUserRefreshTokenFetcherImpl::StartFetching() { | 132 void SupervisedUserRefreshTokenFetcherImpl::StartFetching() { |
131 OAuth2TokenService::ScopeSet scopes; | 133 OAuth2TokenService::ScopeSet scopes; |
132 scopes.insert(GaiaConstants::kOAuth1LoginScope); | 134 scopes.insert(GaiaConstants::kOAuth1LoginScope); |
133 access_token_request_ = oauth2_token_service_->StartRequest( | 135 access_token_request_ = oauth2_token_service_->StartRequest( |
134 account_id_, scopes, this); | 136 account_id_, scopes, this); |
135 } | 137 } |
136 | 138 |
137 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenSuccess( | 139 void SupervisedUserRefreshTokenFetcherImpl::OnGetTokenSuccess( |
138 const OAuth2TokenService::Request* request, | 140 const OAuth2TokenService::Request* request, |
139 const std::string& access_token, | 141 const std::string& access_token, |
140 const Time& expiration_time) { | 142 const Time& expiration_time) { |
141 DCHECK_EQ(access_token_request_.get(), request); | 143 DCHECK_EQ(access_token_request_.get(), request); |
142 access_token_ = access_token; | 144 access_token_ = access_token; |
143 | 145 |
144 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url()); | 146 GURL url(GaiaUrls::GetInstance()->oauth2_issue_token_url()); |
145 // GaiaOAuthClient uses id 0, so we use 1 to distinguish the requests in | 147 // GaiaOAuthClient uses id 0, so we use 1 to distinguish the requests in |
146 // unit tests. | 148 // unit tests. |
147 const int id = 1; | 149 const int id = 1; |
148 | 150 |
149 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); | 151 url_fetcher_.reset(URLFetcher::Create(id, url, URLFetcher::POST, this)); |
150 | 152 |
151 url_fetcher_->SetRequestContext(context_); | 153 url_fetcher_->SetRequestContext(context_); |
152 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 154 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
153 net::LOAD_DO_NOT_SAVE_COOKIES); | 155 net::LOAD_DO_NOT_SAVE_COOKIES); |
154 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 156 url_fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
155 url_fetcher_->AddExtraRequestHeader( | 157 url_fetcher_->AddExtraRequestHeader( |
156 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | 158 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); |
157 | 159 |
158 std::string body = base::StringPrintf( | 160 std::string body = base::StringPrintf( |
159 kIssueTokenBodyFormat, | 161 kIssueTokenBodyFormat, |
160 net::EscapeUrlEncodedData( | 162 net::EscapeUrlEncodedData( |
161 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true).c_str(), | 163 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true).c_str(), |
162 net::EscapeUrlEncodedData(kChromeSyncSupervisedOAuth2Scope, true).c_str(), | 164 net::EscapeUrlEncodedData(kChromeSyncSupervisedOAuth2Scope, true).c_str(), |
163 net::EscapeUrlEncodedData(managed_user_id_, true).c_str(), | 165 net::EscapeUrlEncodedData(supervised_user_id_, true).c_str(), |
164 net::EscapeUrlEncodedData(device_name_, true).c_str()); | 166 net::EscapeUrlEncodedData(device_name_, true).c_str()); |
165 url_fetcher_->SetUploadData("application/x-www-form-urlencoded", body); | 167 url_fetcher_->SetUploadData("application/x-www-form-urlencoded", body); |
166 | 168 |
167 url_fetcher_->Start(); | 169 url_fetcher_->Start(); |
168 } | 170 } |
169 | 171 |
170 void ManagedUserRefreshTokenFetcherImpl::OnGetTokenFailure( | 172 void SupervisedUserRefreshTokenFetcherImpl::OnGetTokenFailure( |
171 const OAuth2TokenService::Request* request, | 173 const OAuth2TokenService::Request* request, |
172 const GoogleServiceAuthError& error) { | 174 const GoogleServiceAuthError& error) { |
173 DCHECK_EQ(access_token_request_.get(), request); | 175 DCHECK_EQ(access_token_request_.get(), request); |
174 callback_.Run(error, std::string()); | 176 callback_.Run(error, std::string()); |
175 callback_.Reset(); | 177 callback_.Reset(); |
176 } | 178 } |
177 | 179 |
178 void ManagedUserRefreshTokenFetcherImpl::OnURLFetchComplete( | 180 void SupervisedUserRefreshTokenFetcherImpl::OnURLFetchComplete( |
179 const URLFetcher* source) { | 181 const URLFetcher* source) { |
180 const net::URLRequestStatus& status = source->GetStatus(); | 182 const net::URLRequestStatus& status = source->GetStatus(); |
181 if (!status.is_success()) { | 183 if (!status.is_success()) { |
182 DispatchNetworkError(status.error()); | 184 DispatchNetworkError(status.error()); |
183 return; | 185 return; |
184 } | 186 } |
185 | 187 |
186 int response_code = source->GetResponseCode(); | 188 int response_code = source->GetResponseCode(); |
187 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | 189 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { |
188 access_token_expired_ = true; | 190 access_token_expired_ = true; |
(...skipping 29 matching lines...) Expand all Loading... |
218 | 220 |
219 gaia::OAuthClientInfo client_info; | 221 gaia::OAuthClientInfo client_info; |
220 GaiaUrls* urls = GaiaUrls::GetInstance(); | 222 GaiaUrls* urls = GaiaUrls::GetInstance(); |
221 client_info.client_id = urls->oauth2_chrome_client_id(); | 223 client_info.client_id = urls->oauth2_chrome_client_id(); |
222 client_info.client_secret = urls->oauth2_chrome_client_secret(); | 224 client_info.client_secret = urls->oauth2_chrome_client_secret(); |
223 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(context_)); | 225 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(context_)); |
224 gaia_oauth_client_->GetTokensFromAuthCode(client_info, auth_code, kNumRetries, | 226 gaia_oauth_client_->GetTokensFromAuthCode(client_info, auth_code, kNumRetries, |
225 this); | 227 this); |
226 } | 228 } |
227 | 229 |
228 void ManagedUserRefreshTokenFetcherImpl::OnGetTokensResponse( | 230 void SupervisedUserRefreshTokenFetcherImpl::OnGetTokensResponse( |
229 const std::string& refresh_token, | 231 const std::string& refresh_token, |
230 const std::string& access_token, | 232 const std::string& access_token, |
231 int expires_in_seconds) { | 233 int expires_in_seconds) { |
232 // TODO(bauerb): It would be nice if we could pass the access token as well, | 234 // TODO(bauerb): It would be nice if we could pass the access token as well, |
233 // so we don't need to fetch another one immediately. | 235 // so we don't need to fetch another one immediately. |
234 DispatchGoogleServiceAuthError(GoogleServiceAuthError::AuthErrorNone(), | 236 DispatchGoogleServiceAuthError(GoogleServiceAuthError::AuthErrorNone(), |
235 refresh_token); | 237 refresh_token); |
236 } | 238 } |
237 | 239 |
238 void ManagedUserRefreshTokenFetcherImpl::OnRefreshTokenResponse( | 240 void SupervisedUserRefreshTokenFetcherImpl::OnRefreshTokenResponse( |
239 const std::string& access_token, | 241 const std::string& access_token, |
240 int expires_in_seconds) { | 242 int expires_in_seconds) { |
241 NOTREACHED(); | 243 NOTREACHED(); |
242 } | 244 } |
243 | 245 |
244 void ManagedUserRefreshTokenFetcherImpl::OnOAuthError() { | 246 void SupervisedUserRefreshTokenFetcherImpl::OnOAuthError() { |
245 DispatchGoogleServiceAuthError( | 247 DispatchGoogleServiceAuthError( |
246 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), | 248 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), |
247 std::string()); | 249 std::string()); |
248 } | 250 } |
249 | 251 |
250 void ManagedUserRefreshTokenFetcherImpl::OnNetworkError(int response_code) { | 252 void SupervisedUserRefreshTokenFetcherImpl::OnNetworkError(int response_code) { |
251 // TODO(bauerb): We should return the HTTP response code somehow. | 253 // TODO(bauerb): We should return the HTTP response code somehow. |
252 DLOG(WARNING) << "HTTP error " << response_code; | 254 DLOG(WARNING) << "HTTP error " << response_code; |
253 DispatchGoogleServiceAuthError( | 255 DispatchGoogleServiceAuthError( |
254 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), | 256 GoogleServiceAuthError(GoogleServiceAuthError::CONNECTION_FAILED), |
255 std::string()); | 257 std::string()); |
256 } | 258 } |
257 | 259 |
258 void ManagedUserRefreshTokenFetcherImpl::DispatchNetworkError(int error_code) { | 260 void SupervisedUserRefreshTokenFetcherImpl::DispatchNetworkError( |
| 261 int error_code) { |
259 DispatchGoogleServiceAuthError( | 262 DispatchGoogleServiceAuthError( |
260 GoogleServiceAuthError::FromConnectionError(error_code), std::string()); | 263 GoogleServiceAuthError::FromConnectionError(error_code), std::string()); |
261 } | 264 } |
262 | 265 |
263 void ManagedUserRefreshTokenFetcherImpl::DispatchGoogleServiceAuthError( | 266 void SupervisedUserRefreshTokenFetcherImpl::DispatchGoogleServiceAuthError( |
264 const GoogleServiceAuthError& error, | 267 const GoogleServiceAuthError& error, |
265 const std::string& token) { | 268 const std::string& token) { |
266 callback_.Run(error, token); | 269 callback_.Run(error, token); |
267 callback_.Reset(); | 270 callback_.Reset(); |
268 } | 271 } |
269 | 272 |
270 } // namespace | 273 } // namespace |
271 | 274 |
272 // static | 275 // static |
273 scoped_ptr<ManagedUserRefreshTokenFetcher> | 276 scoped_ptr<SupervisedUserRefreshTokenFetcher> |
274 ManagedUserRefreshTokenFetcher::Create(OAuth2TokenService* oauth2_token_service, | 277 SupervisedUserRefreshTokenFetcher::Create( |
275 const std::string& account_id, | 278 OAuth2TokenService* oauth2_token_service, |
276 URLRequestContextGetter* context) { | 279 const std::string& account_id, |
277 scoped_ptr<ManagedUserRefreshTokenFetcher> fetcher( | 280 URLRequestContextGetter* context) { |
278 new ManagedUserRefreshTokenFetcherImpl(oauth2_token_service, account_id, | 281 scoped_ptr<SupervisedUserRefreshTokenFetcher> fetcher( |
279 context)); | 282 new SupervisedUserRefreshTokenFetcherImpl(oauth2_token_service, |
| 283 account_id, |
| 284 context)); |
280 return fetcher.Pass(); | 285 return fetcher.Pass(); |
281 } | 286 } |
282 | 287 |
283 ManagedUserRefreshTokenFetcher::~ManagedUserRefreshTokenFetcher() {} | 288 SupervisedUserRefreshTokenFetcher::~SupervisedUserRefreshTokenFetcher() {} |
OLD | NEW |