Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / test / access_token_fetcher.cc
blob5d23a102b4701d198cea8c300794f40002c6d112
1 // Copyright 2015 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 "remoting/test/access_token_fetcher.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "google_apis/gaia/gaia_constants.h"
16 #include "google_apis/google_api_keys.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "remoting/base/url_request_context_getter.h"
20 namespace {
21 const int kMaxGetTokensRetries = 3;
22 const char kOauthRedirectUrl[] =
23 "https://chromoting-oauth.talkgadget."
24 "google.com/talkgadget/oauth/chrome-remote-desktop/dev";
25 } // namespace
27 namespace remoting {
28 namespace test {
30 AccessTokenFetcher::AccessTokenFetcher() {
31 oauth_client_info_ = {
32 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING),
33 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING),
34 kOauthRedirectUrl};
37 AccessTokenFetcher::~AccessTokenFetcher() {
40 void AccessTokenFetcher::GetAccessTokenFromAuthCode(
41 const std::string& auth_code,
42 const AccessTokenCallback& callback) {
43 DCHECK(!auth_code.empty());
44 DCHECK(!callback.is_null());
45 DCHECK(access_token_callback_.is_null());
47 VLOG(2) << "Calling GetTokensFromAuthCode to exchange auth_code for token";
49 access_token_.clear();
50 refresh_token_.clear();
51 access_token_callback_ = callback;
53 // Create a new GaiaOAuthClient for each request to GAIA.
54 CreateNewGaiaOAuthClientInstance();
55 auth_client_->GetTokensFromAuthCode(
56 oauth_client_info_, auth_code, kMaxGetTokensRetries,
57 this); // GaiaOAuthClient::Delegate* delegate
60 void AccessTokenFetcher::GetAccessTokenFromRefreshToken(
61 const std::string& refresh_token,
62 const AccessTokenCallback& callback) {
63 DCHECK(!refresh_token.empty());
64 DCHECK(!callback.is_null());
65 DCHECK(access_token_callback_.is_null());
67 VLOG(2) << "Calling RefreshToken to generate a new access token";
69 access_token_.clear();
70 refresh_token_ = refresh_token;
71 access_token_callback_ = callback;
73 // Create a new GaiaOAuthClient for each request to GAIA.
74 CreateNewGaiaOAuthClientInstance();
75 auth_client_->RefreshToken(oauth_client_info_, refresh_token_,
76 std::vector<std::string>(), // scopes
77 kMaxGetTokensRetries,
78 this); // GaiaOAuthClient::Delegate* delegate
81 void AccessTokenFetcher::CreateNewGaiaOAuthClientInstance() {
82 scoped_refptr<remoting::URLRequestContextGetter> request_context_getter;
83 request_context_getter = new remoting::URLRequestContextGetter(
84 base::ThreadTaskRunnerHandle::Get(), // network_runner
85 base::ThreadTaskRunnerHandle::Get()); // file_runner
87 auth_client_.reset(new gaia::GaiaOAuthClient(request_context_getter.get()));
90 void AccessTokenFetcher::OnGetTokensResponse(const std::string& refresh_token,
91 const std::string& access_token,
92 int expires_in_seconds) {
93 VLOG(1) << "AccessTokenFetcher::OnGetTokensResponse() Called";
94 VLOG(1) << "--refresh_token: " << refresh_token;
95 VLOG(1) << "--access_token: " << access_token;
96 VLOG(1) << "--expires_in_seconds: " << expires_in_seconds;
98 refresh_token_ = refresh_token;
99 access_token_ = access_token;
101 ValidateAccessToken();
104 void AccessTokenFetcher::OnRefreshTokenResponse(const std::string& access_token,
105 int expires_in_seconds) {
106 VLOG(1) << "AccessTokenFetcher::OnRefreshTokenResponse() Called";
107 VLOG(1) << "--access_token: " << access_token;
108 VLOG(1) << "--expires_in_seconds: " << expires_in_seconds;
110 access_token_ = access_token;
112 ValidateAccessToken();
115 void AccessTokenFetcher::OnGetUserEmailResponse(const std::string& user_email) {
116 // This callback should not be called as we do not request the user's email.
117 NOTREACHED();
120 void AccessTokenFetcher::OnGetUserIdResponse(const std::string& user_id) {
121 // This callback should not be called as we do not request the user's id.
122 NOTREACHED();
125 void AccessTokenFetcher::OnGetUserInfoResponse(
126 scoped_ptr<base::DictionaryValue> user_info) {
127 // This callback should not be called as we do not request user info.
128 NOTREACHED();
131 void AccessTokenFetcher::OnGetTokenInfoResponse(
132 scoped_ptr<base::DictionaryValue> token_info) {
133 VLOG(1) << "AccessTokenFetcher::OnGetTokenInfoResponse() Called";
135 std::string error_string;
136 std::string error_description;
138 // Check to see if the token_info we received had any errors,
139 // otherwise we will assume that it is valid for our purposes.
140 if (token_info->HasKey("error")) {
141 token_info->GetString("error", &error_string);
142 token_info->GetString("error_description", &error_description);
144 LOG(ERROR) << "OnGetTokenInfoResponse returned an error. "
145 << "error: " << error_string << ", "
146 << "description: " << error_description;
147 access_token_.clear();
148 refresh_token_.clear();
149 } else {
150 VLOG(1) << "Access Token has been validated";
153 base::ResetAndReturn(&access_token_callback_)
154 .Run(access_token_, refresh_token_);
157 void AccessTokenFetcher::OnOAuthError() {
158 LOG(ERROR) << "AccessTokenFetcher::OnOAuthError() Called";
160 access_token_.clear();
161 refresh_token_.clear();
163 base::ResetAndReturn(&access_token_callback_)
164 .Run(access_token_, refresh_token_);
167 void AccessTokenFetcher::OnNetworkError(int response_code) {
168 LOG(ERROR) << "AccessTokenFetcher::OnNetworkError() Called";
169 LOG(ERROR) << "response code: " << response_code;
171 access_token_.clear();
172 refresh_token_.clear();
174 base::ResetAndReturn(&access_token_callback_)
175 .Run(access_token_, refresh_token_);
178 void AccessTokenFetcher::ValidateAccessToken() {
179 VLOG(2) << "Calling GetTokenInfo to validate access token";
181 // Create a new GaiaOAuthClient for each request to GAIA.
182 CreateNewGaiaOAuthClientInstance();
183 auth_client_->GetTokenInfo(access_token_, kMaxGetTokensRetries,
184 this); // GaiaOAuthClient::Delegate* delegate
187 } // namespace test
188 } // namespace remoting