Commit dffa06ff authored by rogerta@chromium.org's avatar rogerta@chromium.org

Remove unused code (gaia_oauth_fetcher).

The oauth2 apis are handled by google_apis\gaia\gaia_oauth_client.

BUG=None

Review URL: https://codereview.chromium.org/139153011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245632 0039d316-1c4b-4281-b951-d872f2087c98
parent 26a809b8
rickcam@chromium.org
sanjeevr@chromium.org
tim@chromium.org
zelidrag@chromium.org
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_CONSUMER_H_
#define CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_CONSUMER_H_
#include <string>
class GoogleServiceAuthError;
// An interface that defines the callbacks for objects to which
// GaiaOAuthFetcher can return data.
class GaiaOAuthConsumer {
public:
virtual ~GaiaOAuthConsumer() {}
virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) {}
virtual void OnGetOAuthTokenFailure(const GoogleServiceAuthError& error) {}
virtual void OnOAuthGetAccessTokenSuccess(const std::string& token,
const std::string& secret) {}
virtual void OnOAuthGetAccessTokenFailure(
const GoogleServiceAuthError& error) {}
virtual void OnOAuthWrapBridgeSuccess(const std::string& service_scope,
const std::string& token,
const std::string& expires_in) {}
virtual void OnOAuthWrapBridgeFailure(const std::string& service_scope,
const GoogleServiceAuthError& error) {}
virtual void OnUserInfoSuccess(const std::string& email) {}
virtual void OnUserInfoFailure(const GoogleServiceAuthError& error) {}
virtual void OnOAuthLoginSuccess(const std::string& sid,
const std::string& lsid,
const std::string& auth) {}
virtual void OnOAuthLoginFailure(const GoogleServiceAuthError& error) {}
virtual void OnOAuthRevokeTokenSuccess() {}
virtual void OnOAuthRevokeTokenFailure(const GoogleServiceAuthError& error) {}
};
#endif // CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_CONSUMER_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/gaia/gaia_oauth_fetcher.h"
#include <string>
#include <utility>
#include <vector>
#include "base/json/json_reader.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/net/gaia/gaia_oauth_consumer.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/oauth_request_signer.h"
#include "grit/chromium_strings.h"
#include "net/base/load_flags.h"
#include "net/cookies/parsed_cookie.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_status.h"
#include "ui/base/l10n/l10n_util.h"
static const char kOAuthTokenCookie[] = "oauth_token";
GaiaOAuthFetcher::GaiaOAuthFetcher(GaiaOAuthConsumer* consumer,
net::URLRequestContextGetter* getter,
const std::string& service_scope)
: consumer_(consumer),
getter_(getter),
service_scope_(service_scope),
fetch_pending_(false),
auto_fetch_limit_(USER_INFO) {}
GaiaOAuthFetcher::~GaiaOAuthFetcher() {}
bool GaiaOAuthFetcher::HasPendingFetch() const {
return fetch_pending_;
}
void GaiaOAuthFetcher::CancelRequest() {
fetcher_.reset();
fetch_pending_ = false;
}
// static
net::URLFetcher* GaiaOAuthFetcher::CreateGaiaFetcher(
net::URLRequestContextGetter* getter,
const GURL& gaia_gurl,
const std::string& body,
const std::string& headers,
bool send_cookies,
net::URLFetcherDelegate* delegate) {
bool empty_body = body.empty();
net::URLFetcher* result = net::URLFetcher::Create(
0, gaia_gurl,
empty_body ? net::URLFetcher::GET : net::URLFetcher::POST,
delegate);
result->SetRequestContext(getter);
// Fetchers are sometimes cancelled because a network change was detected,
// especially at startup and after sign-in on ChromeOS. Retrying once should
// be enough in those cases; let the fetcher retry up to 3 times just in case.
// http://crbug.com/163710
result->SetAutomaticallyRetryOnNetworkChanges(3);
// The Gaia/OAuth token exchange requests do not require any cookie-based
// identification as part of requests. We suppress sending any cookies to
// maintain a separation between the user's browsing and Chrome's internal
// services. Where such mixing is desired (prelogin, autologin
// or chromeos login), it will be done explicitly.
if (!send_cookies)
result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES);
if (!empty_body)
result->SetUploadData("application/x-www-form-urlencoded", body);
if (!headers.empty())
result->SetExtraRequestHeaders(headers);
return result;
}
// static
GURL GaiaOAuthFetcher::MakeGetOAuthTokenUrl(
const std::string& oauth1_login_scope,
const std::string& product_name) {
return GaiaUrls::GetInstance()->get_oauth_token_url().Resolve(
"?scope=" + oauth1_login_scope +
"&xoauth_display_name=" +
OAuthRequestSigner::Encode(product_name));
}
// static
std::string GaiaOAuthFetcher::MakeOAuthLoginBody(
const char* source,
const char* service,
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret) {
OAuthRequestSigner::Parameters parameters;
parameters["service"] = service;
parameters["source"] = source;
std::string signed_request;
bool is_signed = OAuthRequestSigner::SignURL(
GaiaUrls::GetInstance()->oauth1_login_url(),
parameters,
OAuthRequestSigner::HMAC_SHA1_SIGNATURE,
OAuthRequestSigner::POST_METHOD,
"anonymous", // oauth_consumer_key
"anonymous", // consumer secret
oauth1_access_token, // oauth_token
oauth1_access_token_secret, // token secret
&signed_request);
DCHECK(is_signed);
return signed_request;
}
// static
std::string GaiaOAuthFetcher::MakeOAuthGetAccessTokenBody(
const std::string& oauth1_request_token) {
OAuthRequestSigner::Parameters empty_parameters;
std::string signed_request;
bool is_signed = OAuthRequestSigner::SignURL(
GaiaUrls::GetInstance()->oauth_get_access_token_url(),
empty_parameters,
OAuthRequestSigner::HMAC_SHA1_SIGNATURE,
OAuthRequestSigner::POST_METHOD,
"anonymous", // oauth_consumer_key
"anonymous", // consumer secret
oauth1_request_token, // oauth_token
"", // token secret
&signed_request);
DCHECK(is_signed);
return signed_request;
}
// static
std::string GaiaOAuthFetcher::MakeOAuthWrapBridgeBody(
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret,
const std::string& wrap_token_duration,
const std::string& oauth2_scope) {
OAuthRequestSigner::Parameters parameters;
parameters["wrap_token_duration"] = wrap_token_duration;
parameters["wrap_scope"] = oauth2_scope;
std::string signed_request;
bool is_signed = OAuthRequestSigner::SignURL(
GaiaUrls::GetInstance()->oauth_wrap_bridge_url(),
parameters,
OAuthRequestSigner::HMAC_SHA1_SIGNATURE,
OAuthRequestSigner::POST_METHOD,
"anonymous", // oauth_consumer_key
"anonymous", // consumer secret
oauth1_access_token, // oauth_token
oauth1_access_token_secret, // token secret
&signed_request);
DCHECK(is_signed);
return signed_request;
}
// Helper method that extracts tokens from a successful reply.
// static
void GaiaOAuthFetcher::ParseOAuthLoginResponse(
const std::string& data,
std::string* sid,
std::string* lsid,
std::string* auth) {
using std::vector;
using std::pair;
using std::string;
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == "SID") {
*sid = i->second;
} else if (i->first == "LSID") {
*lsid = i->second;
} else if (i->first == "Auth") {
*auth = i->second;
}
}
}
// Helper method that extracts tokens from a successful reply.
// static
void GaiaOAuthFetcher::ParseOAuthGetAccessTokenResponse(
const std::string& data,
std::string* token,
std::string* secret) {
using std::vector;
using std::pair;
using std::string;
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '&', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == "oauth_token") {
std::string decoded;
if (OAuthRequestSigner::Decode(i->second, &decoded))
token->assign(decoded);
} else if (i->first == "oauth_token_secret") {
std::string decoded;
if (OAuthRequestSigner::Decode(i->second, &decoded))
secret->assign(decoded);
}
}
}
// Helper method that extracts tokens from a successful reply.
// static
void GaiaOAuthFetcher::ParseOAuthWrapBridgeResponse(const std::string& data,
std::string* token,
std::string* expires_in) {
using std::vector;
using std::pair;
using std::string;
vector<pair<string, string> > tokens;
base::SplitStringIntoKeyValuePairs(data, '=', '&', &tokens);
for (vector<pair<string, string> >::iterator i = tokens.begin();
i != tokens.end(); ++i) {
if (i->first == "wrap_access_token") {
std::string decoded;
if (OAuthRequestSigner::Decode(i->second, &decoded))
token->assign(decoded);
} else if (i->first == "wrap_access_token_expires_in") {
std::string decoded;
if (OAuthRequestSigner::Decode(i->second, &decoded))
expires_in->assign(decoded);
}
}
}
// Helper method that extracts tokens from a successful reply.
// static
void GaiaOAuthFetcher::ParseUserInfoResponse(const std::string& data,
std::string* email_result) {
scoped_ptr<base::Value> value(base::JSONReader::Read(data));
if (value->GetType() == base::Value::TYPE_DICTIONARY) {
base::Value* email_value;
base::DictionaryValue* dict =
static_cast<base::DictionaryValue*>(value.get());
if (dict->Get("email", &email_value)) {
if (email_value->GetType() == base::Value::TYPE_STRING) {
email_value->GetAsString(email_result);
}
}
}
}
void GaiaOAuthFetcher::StartOAuthLogin(
const char* source,
const char* service,
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH1_LOGIN;
// Must outlive fetcher_.
request_body_ = MakeOAuthLoginBody(source, service, oauth1_access_token,
oauth1_access_token_secret);
request_headers_ = "";
GURL url(GaiaUrls::GetInstance()->oauth1_login_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartGetOAuthTokenRequest() {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH1_REQUEST_TOKEN;
// Must outlive fetcher_.
request_body_ = "";
request_headers_ = "";
fetcher_.reset(CreateGaiaFetcher(getter_,
MakeGetOAuthTokenUrl(GaiaUrls::GetInstance()->oauth1_login_scope(),
l10n_util::GetStringUTF8(IDS_PRODUCT_NAME)),
std::string(),
std::string(),
true, // send_cookies
this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartOAuthGetAccessToken(
const std::string& oauth1_request_token) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH1_ALL_ACCESS_TOKEN;
// Must outlive fetcher_.
request_body_ = MakeOAuthGetAccessTokenBody(oauth1_request_token);
request_headers_ = "";
GURL url(GaiaUrls::GetInstance()->oauth_get_access_token_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartOAuthWrapBridge(
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret,
const std::string& wrap_token_duration,
const std::string& service_scope) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH2_SERVICE_ACCESS_TOKEN;
VLOG(1) << "Starting OAuthWrapBridge for: " << service_scope;
std::string combined_scope = service_scope + " " +
GaiaUrls::GetInstance()->oauth_wrap_bridge_user_info_scope();
service_scope_ = service_scope;
// Must outlive fetcher_.
request_body_ = MakeOAuthWrapBridgeBody(
oauth1_access_token,
oauth1_access_token_secret,
wrap_token_duration,
combined_scope);
request_headers_ = "";
GURL url(GaiaUrls::GetInstance()->oauth_wrap_bridge_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartUserInfo(const std::string& oauth2_access_token) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = USER_INFO;
// Must outlive fetcher_.
request_body_ = "";
request_headers_ = "Authorization: OAuth " + oauth2_access_token;
GURL url(GaiaUrls::GetInstance()->oauth_user_info_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartOAuthRevokeAccessToken(const std::string& token,
const std::string& secret) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH2_REVOKE_TOKEN;
// Must outlive fetcher_.
request_body_ = "";
OAuthRequestSigner::Parameters empty_parameters;
std::string auth_header;
bool is_signed = OAuthRequestSigner::SignAuthHeader(
GaiaUrls::GetInstance()->oauth_revoke_token_url(),
empty_parameters,
OAuthRequestSigner::HMAC_SHA1_SIGNATURE,
OAuthRequestSigner::GET_METHOD,
"anonymous",
"anonymous",
token,
secret,
&auth_header);
DCHECK(is_signed);
request_headers_ = "Authorization: " + auth_header;
GURL url(GaiaUrls::GetInstance()->oauth_revoke_token_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
void GaiaOAuthFetcher::StartOAuthRevokeWrapToken(const std::string& token) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";
request_type_ = OAUTH2_REVOKE_TOKEN;
// Must outlive fetcher_.
request_body_ = "";
request_headers_ = "Authorization: Bearer " + token;
GURL url(GaiaUrls::GetInstance()->oauth_revoke_token_url());
fetcher_.reset(CreateGaiaFetcher(getter_, url, request_body_,
request_headers_, false, this));
fetch_pending_ = true;
fetcher_->Start();
}
// static
GoogleServiceAuthError GaiaOAuthFetcher::GenerateAuthError(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (!status.is_success()) {
if (status.status() == net::URLRequestStatus::CANCELED) {
return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
} else {
LOG(WARNING) << "Could not reach Google Accounts servers: errno "
<< status.error();
return GoogleServiceAuthError::FromConnectionError(status.error());
}
} else {
LOG(WARNING) << "Unrecognized response from Google Accounts servers "
<< "code " << response_code << " data " << data;
return GoogleServiceAuthError(
GoogleServiceAuthError::SERVICE_UNAVAILABLE);
}
NOTREACHED();
return GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE);
}
void GaiaOAuthFetcher::OnGetOAuthTokenUrlFetched(
const net::ResponseCookies& cookies,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
for (net::ResponseCookies::const_iterator iter = cookies.begin();
iter != cookies.end(); ++iter) {
net::ParsedCookie cookie(*iter);
if (cookie.Name() == kOAuthTokenCookie) {
std::string token = cookie.Value();
consumer_->OnGetOAuthTokenSuccess(token);
if (ShouldAutoFetch(OAUTH1_ALL_ACCESS_TOKEN))
StartOAuthGetAccessToken(token);
return;
}
}
}
consumer_->OnGetOAuthTokenFailure(
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE));
}
void GaiaOAuthFetcher::OnOAuthLoginFetched(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
std::string sid;
std::string lsid;
std::string auth;
ParseOAuthLoginResponse(data, &sid, &lsid, &auth);
if (!sid.empty() && !lsid.empty() && !auth.empty()) {
consumer_->OnOAuthLoginSuccess(sid, lsid, auth);
return;
}
}
// OAuthLogin returns error messages that are identical to ClientLogin,
// so we use GaiaAuthFetcher::GenerateAuthError to parse the response
// instead.
consumer_->OnOAuthLoginFailure(
GaiaAuthFetcher::GenerateOAuthLoginError(data, status));
}
void GaiaOAuthFetcher::OnOAuthGetAccessTokenFetched(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
VLOG(1) << "OAuth1 access token fetched.";
std::string secret;
std::string token;
ParseOAuthGetAccessTokenResponse(data, &token, &secret);
if (!token.empty() && !secret.empty()) {
consumer_->OnOAuthGetAccessTokenSuccess(token, secret);
if (ShouldAutoFetch(OAUTH2_SERVICE_ACCESS_TOKEN))
StartOAuthWrapBridge(
token, secret, GaiaConstants::kGaiaOAuthDuration, service_scope_);
return;
}
}
consumer_->OnOAuthGetAccessTokenFailure(GenerateAuthError(data, status,
response_code));
}
void GaiaOAuthFetcher::OnOAuthWrapBridgeFetched(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
VLOG(1) << "OAuth2 access token fetched.";
std::string token;
std::string expires_in;
ParseOAuthWrapBridgeResponse(data, &token, &expires_in);
if (!token.empty() && !expires_in.empty()) {
consumer_->OnOAuthWrapBridgeSuccess(service_scope_, token, expires_in);
if (ShouldAutoFetch(USER_INFO))
StartUserInfo(token);
return;
}
}
consumer_->OnOAuthWrapBridgeFailure(service_scope_,
GenerateAuthError(data, status,
response_code));
}
void GaiaOAuthFetcher::OnOAuthRevokeTokenFetched(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
consumer_->OnOAuthRevokeTokenSuccess();
} else {
LOG(ERROR) << "Token revocation failure " << response_code << ": " << data;
consumer_->OnOAuthRevokeTokenFailure(GenerateAuthError(data, status,
response_code));
}
}
void GaiaOAuthFetcher::OnUserInfoFetched(
const std::string& data,
const net::URLRequestStatus& status,
int response_code) {
if (status.is_success() && response_code == net::HTTP_OK) {
std::string email;
ParseUserInfoResponse(data, &email);
if (!email.empty()) {
VLOG(1) << "GAIA user info fetched for " << email << ".";
consumer_->OnUserInfoSuccess(email);
return;
}
}
consumer_->OnUserInfoFailure(GenerateAuthError(data, status,
response_code));
}
void GaiaOAuthFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
// Keep |fetcher_| around to avoid invalidating its |status| (accessed below).
scoped_ptr<net::URLFetcher> current_fetcher(fetcher_.release());
fetch_pending_ = false;
std::string data;
source->GetResponseAsString(&data);
net::URLRequestStatus status = source->GetStatus();
int response_code = source->GetResponseCode();
switch (request_type_) {
case OAUTH1_LOGIN:
OnOAuthLoginFetched(data, status, response_code);
break;
case OAUTH1_REQUEST_TOKEN:
OnGetOAuthTokenUrlFetched(source->GetCookies(), status, response_code);
break;
case OAUTH1_ALL_ACCESS_TOKEN:
OnOAuthGetAccessTokenFetched(data, status, response_code);
break;
case OAUTH2_SERVICE_ACCESS_TOKEN:
OnOAuthWrapBridgeFetched(data, status, response_code);
break;
case USER_INFO:
OnUserInfoFetched(data, status, response_code);
break;
case OAUTH2_REVOKE_TOKEN:
OnOAuthRevokeTokenFetched(data, status, response_code);
break;
}
}
bool GaiaOAuthFetcher::ShouldAutoFetch(RequestType fetch_step) {
return fetch_step <= auto_fetch_limit_;
}
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_
#define CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"
class GaiaOAuthConsumer;
namespace net {
class URLFetcher;
class URLRequestContextGetter;
class URLRequestStatus;
typedef std::vector<std::string> ResponseCookies;
}
// Authenticate a user using Gaia's OAuth1 and OAuth2 support.
//
// Users of this class typically desire an OAuth2 Access token scoped for a
// specific service. This will typically start with either an interactive
// login, using StartOAuthLogin, or with a long-lived OAuth1 all-scope
// token obtained through a previous login or other means, using
// StartOAuthGetAccessToken. In fact, one can start with any of these
// routines:
// StartOAuthLogin()
// StartGetOAuthTokenRequest()
// StartOAuthGetAccessToken()
// StartOAuthWrapBridge()
// StartUserInfo()
// with the expectation that each of these calls the next Start* routine in
// the sequence, except for StartUserInfo as it's the last one.
//
// This class can handle one request at a time, and all calls through an
// instance should be serialized.
class GaiaOAuthFetcher : public net::URLFetcherDelegate {
public:
// Defines steps of OAuth process performed by this class.
typedef enum {
OAUTH1_LOGIN,
OAUTH1_REQUEST_TOKEN,
OAUTH1_ALL_ACCESS_TOKEN,
OAUTH2_SERVICE_ACCESS_TOKEN,
USER_INFO,
OAUTH2_REVOKE_TOKEN,
} RequestType;
GaiaOAuthFetcher(GaiaOAuthConsumer* consumer,
net::URLRequestContextGetter* getter,
const std::string& service_scope);
virtual ~GaiaOAuthFetcher();
// Sets the mask of which OAuth fetch steps should be automatically kicked
// of upon successful completition of the previous steps. By default,
// this class will chain all steps in OAuth proccess.
void SetAutoFetchLimit(RequestType limit) { auto_fetch_limit_ = limit; }
// Non-UI version of the method above. Initiates Gaia OAuth request token
// retrieval.
void StartGetOAuthTokenRequest();
// Performs account login based on OAuth1 access token and its secret.
void StartOAuthLogin(const char* source,
const char* service,
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret);
// Obtains an OAuth1 access token and secret
//
// oauth1_request_token is from GetOAuthToken's result.
virtual void StartOAuthGetAccessToken(
const std::string& oauth1_request_token);
// Obtains an OAuth2 access token using Gaia's OAuth1-to-OAuth2 bridge.
//
// oauth1_access_token and oauth1_access_token_secret are from
// OAuthGetAccessToken's result.
//
// wrap_token_duration is typically one hour,
// which is also the max -- you can only decrease it.
//
// service_scope will be used as a service name. For example, Chromium Sync
// uses https://www.googleapis.com/auth/chromesync for its OAuth2 service
// scope.
virtual void StartOAuthWrapBridge(
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret,
const std::string& wrap_token_duration,
const std::string& service_scope);
// Obtains user information related to an OAuth2 access token
//
// oauth2_access_token is from OAuthWrapBridge's result.
virtual void StartUserInfo(const std::string& oauth2_access_token);
// Starts a request for revoking the given OAuth access token (as requested by
// StartOAuthGetAccessToken).
virtual void StartOAuthRevokeAccessToken(const std::string& token,
const std::string& secret);
// Starts a request for revoking the given OAuth Bearer token (as requested by
// StartOAuthWrapBridge).
virtual void StartOAuthRevokeWrapToken(const std::string& token);
// Implementation of net::URLFetcherDelegate
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
// StartGetOAuthToken (or other Start* routine) been called, but results
// are not back yet.
virtual bool HasPendingFetch() const;
// Stop any URL fetches in progress.
virtual void CancelRequest();
protected:
// Stores the type of the current request in flight.
RequestType request_type_;
private:
// Process the results of a GetOAuthToken fetch for non-UI driven path.
virtual void OnGetOAuthTokenUrlFetched(const net::ResponseCookies& cookies,
const net::URLRequestStatus& status,
int response_code);
// Process the results of a OAuthLogin fetch.
virtual void OnOAuthLoginFetched(const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Process the results of a OAuthGetAccessToken fetch.
virtual void OnOAuthGetAccessTokenFetched(const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Process the results of a OAuthWrapBridge fetch.
virtual void OnOAuthWrapBridgeFetched(const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Process the results of a token revocation fetch.
virtual void OnOAuthRevokeTokenFetched(const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Process the results of a userinfo fetch.
virtual void OnUserInfoFetched(const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Tokenize the results of a OAuthLogin fetch.
static void ParseOAuthLoginResponse(const std::string& data,
std::string* sid,
std::string* lsid,
std::string* auth);
// Tokenize the results of a OAuthGetAccessToken fetch.
static void ParseOAuthGetAccessTokenResponse(const std::string& data,
std::string* token,
std::string* secret);
// Tokenize the results of a OAuthWrapBridge fetch.
static void ParseOAuthWrapBridgeResponse(const std::string& data,
std::string* token,
std::string* expires_in);
// Tokenize the results of a userinfo fetch.
static void ParseUserInfoResponse(const std::string& data,
std::string* email);
// From a URLFetcher result, generate an appropriate error.
static GoogleServiceAuthError GenerateAuthError(
const std::string& data,
const net::URLRequestStatus& status,
int response_code);
// Given parameters, create a OAuth v1 request URL.
static GURL MakeGetOAuthTokenUrl(const std::string& oauth1_login_scope,
const std::string& product_name);
// Given parameters, create a OAuthGetAccessToken request body.
static std::string MakeOAuthGetAccessTokenBody(
const std::string& oauth1_request_token);
// Given parameters, create a OAuthLogin request body.
static std::string MakeOAuthLoginBody(
const char* source,
const char* service,
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret);
// Given parameters, create a OAuthWrapBridge request body.
static std::string MakeOAuthWrapBridgeBody(
const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret,
const std::string& wrap_token_duration,
const std::string& oauth2_service_scope);
// Create a fetcher useable for making any Gaia OAuth request.
static net::URLFetcher* CreateGaiaFetcher(
net::URLRequestContextGetter* getter,
const GURL& gaia_gurl_,
const std::string& body,
const std::string& headers,
bool send_cookies,
net::URLFetcherDelegate* delegate);
bool ShouldAutoFetch(RequestType fetch_step);
// These fields are common to GaiaOAuthFetcher, same every request
GaiaOAuthConsumer* const consumer_;
net::URLRequestContextGetter* const getter_;
// While a fetch is going on:
scoped_ptr<net::URLFetcher> fetcher_;
std::string request_body_;
std::string request_headers_;
std::string service_scope_;
bool fetch_pending_;
RequestType auto_fetch_limit_;
DISALLOW_COPY_AND_ASSIGN(GaiaOAuthFetcher);
};
#endif // CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A complete set of unit tests for GaiaOAuthFetcher.
// Originally ported from GaiaAuthFetcher tests.
#include <string>
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/net/gaia/gaia_oauth_consumer.h"
#include "chrome/browser/net/gaia/gaia_oauth_fetcher.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
class MockGaiaOAuthConsumer : public GaiaOAuthConsumer {
public:
MockGaiaOAuthConsumer() {}
~MockGaiaOAuthConsumer() {}
MOCK_METHOD1(OnGetOAuthTokenSuccess, void(const std::string& oauth_token));
MOCK_METHOD1(OnGetOAuthTokenFailure,
void(const GoogleServiceAuthError& error));
MOCK_METHOD2(OnOAuthGetAccessTokenSuccess, void(const std::string& token,
const std::string& secret));
MOCK_METHOD1(OnOAuthGetAccessTokenFailure,
void(const GoogleServiceAuthError& error));
MOCK_METHOD3(OnOAuthWrapBridgeSuccess,
void(const std::string& service_scope,
const std::string& token,
const std::string& expires_in));
MOCK_METHOD2(OnOAuthWrapBridgeFailure,
void(const std::string& service_scope,
const GoogleServiceAuthError& error));
MOCK_METHOD1(OnUserInfoSuccess, void(const std::string& email));
MOCK_METHOD1(OnUserInfoFailure, void(const GoogleServiceAuthError& error));
MOCK_METHOD0(OnOAuthRevokeTokenSuccess, void());
MOCK_METHOD1(OnOAuthRevokeTokenFailure,
void(const GoogleServiceAuthError& error));
};
class MockGaiaOAuthFetcher : public GaiaOAuthFetcher {
public:
MockGaiaOAuthFetcher(GaiaOAuthConsumer* consumer,
net::URLRequestContextGetter* getter,
const std::string& service_scope)
: GaiaOAuthFetcher(
consumer, getter, service_scope) {}
~MockGaiaOAuthFetcher() {}
void set_request_type(RequestType type) {
request_type_ = type;
}
MOCK_METHOD1(StartOAuthGetAccessToken,
void(const std::string& oauth1_request_token));
MOCK_METHOD4(StartOAuthWrapBridge,
void(const std::string& oauth1_access_token,
const std::string& oauth1_access_token_secret,
const std::string& wrap_token_duration,
const std::string& oauth2_scope));
MOCK_METHOD1(StartUserInfo, void(const std::string& oauth2_access_token));
};
#if 0 // Suppressing for now
TEST(GaiaOAuthFetcherTest, GetOAuthToken) {
const std::string oauth_token = "4/OAuth1-Request_Token-1234567";
base::Time creation = base::Time::Now();
base::Time expiration = base::Time::Time();
scoped_ptr<net::CanonicalCookie> canonical_cookie;
canonical_cookie.reset(
new net::CanonicalCookie(
GURL("http://www.google.com/"), // url
"oauth_token", // name
oauth_token, // value
"www.google.com", // domain
"/accounts/o8/GetOAuthToken", // path
"", // mac_key
"", // mac_algorithm
creation, // creation
expiration, // expiration
creation, // last_access
true, // secure
true, // httponly
false)); // has_expires
scoped_ptr<ChromeCookieDetails::ChromeCookieDetails> cookie_details;
cookie_details.reset(
new ChromeCookieDetails::ChromeCookieDetails(
canonical_cookie.get(),
false,
net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT));
MockGaiaOAuthConsumer consumer;
EXPECT_CALL(consumer, OnGetOAuthTokenSuccess(oauth_token)).Times(1);
TestingProfile profile;
MockGaiaOAuthFetcher oauth_fetcher(&consumer,
profile.GetRequestContext(),
std::string());
EXPECT_CALL(oauth_fetcher, StartOAuthGetAccessToken(oauth_token)).Times(1);
}
#endif // 0 // Suppressing for now
class GaiaOAuthFetcherTest : public testing::Test {
private:
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(GaiaOAuthFetcherTest, OAuthGetAccessToken) {
const std::string oauth_token =
"1/OAuth1-Access_Token-1234567890abcdefghijklm";
const std::string oauth_token_secret = "Dont_tell_the_secret-123";
const std::string data("oauth_token="
"1%2FOAuth1-Access_Token-1234567890abcdefghijklm"
"&oauth_token_secret=Dont_tell_the_secret-123");
MockGaiaOAuthConsumer consumer;
EXPECT_CALL(consumer,
OnOAuthGetAccessTokenSuccess(oauth_token,
oauth_token_secret)).Times(1);
TestingProfile profile;
MockGaiaOAuthFetcher oauth_fetcher(&consumer,
profile.GetRequestContext(),
"service_scope-JnG18MEE");
oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN);
EXPECT_CALL(oauth_fetcher,
StartOAuthWrapBridge(oauth_token,
oauth_token_secret,
"3600",
"service_scope-JnG18MEE")).Times(1);
net::ResponseCookies cookies;
net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
GURL url(GaiaUrls::GetInstance()->oauth_get_access_token_url());
net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
test_fetcher.set_url(url);
test_fetcher.set_status(status);
test_fetcher.set_response_code(net::HTTP_OK);
test_fetcher.set_cookies(cookies);
test_fetcher.SetResponseString(data);
oauth_fetcher.OnURLFetchComplete(&test_fetcher);
}
TEST_F(GaiaOAuthFetcherTest, OAuthWrapBridge) {
const std::string wrap_token =
"1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
const std::string expires_in = "3600";
const std::string data("wrap_access_token="
"1%2FOAuth2-Access_Token-nopqrstuvwxyz1234567890"
"&wrap_access_token_expires_in=3600");
MockGaiaOAuthConsumer consumer;
EXPECT_CALL(consumer,
OnOAuthWrapBridgeSuccess("service_scope-0fL85iOi",
wrap_token,
expires_in)).Times(1);
TestingProfile profile;
MockGaiaOAuthFetcher oauth_fetcher(&consumer,
profile.GetRequestContext(),
"service_scope-0fL85iOi");
oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);
EXPECT_CALL(oauth_fetcher, StartUserInfo(wrap_token)).Times(1);
net::ResponseCookies cookies;
net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
GURL url(GaiaUrls::GetInstance()->oauth_wrap_bridge_url());
net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
test_fetcher.set_url(url);
test_fetcher.set_status(status);
test_fetcher.set_response_code(net::HTTP_OK);
test_fetcher.set_cookies(cookies);
test_fetcher.SetResponseString(data);
oauth_fetcher.OnURLFetchComplete(&test_fetcher);
}
TEST_F(GaiaOAuthFetcherTest, UserInfo) {
const std::string email_address = "someone@somewhere.net";
const std::string wrap_token =
"1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
const std::string expires_in = "3600";
const std::string data("{\n \"email\": \"someone@somewhere.net\",\n"
" \"verified_email\": true\n}\n");
MockGaiaOAuthConsumer consumer;
EXPECT_CALL(consumer,
OnUserInfoSuccess(email_address)).Times(1);
TestingProfile profile;
MockGaiaOAuthFetcher oauth_fetcher(&consumer,
profile.GetRequestContext(),
"service_scope-Nrj4LmgU");
oauth_fetcher.set_request_type(GaiaOAuthFetcher::USER_INFO);
net::ResponseCookies cookies;
net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
GURL url(GaiaUrls::GetInstance()->oauth_user_info_url());
net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
test_fetcher.set_url(url);
test_fetcher.set_status(status);
test_fetcher.set_response_code(net::HTTP_OK);
test_fetcher.set_cookies(cookies);
test_fetcher.SetResponseString(data);
oauth_fetcher.OnURLFetchComplete(&test_fetcher);
}
TEST_F(GaiaOAuthFetcherTest, OAuthRevokeToken) {
const std::string token = "1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
MockGaiaOAuthConsumer consumer;
EXPECT_CALL(consumer,
OnOAuthRevokeTokenSuccess()).Times(1);
TestingProfile profile;
MockGaiaOAuthFetcher oauth_fetcher(&consumer,
profile.GetRequestContext(),
"service_scope-Nrj4LmgU");
oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH2_REVOKE_TOKEN);
net::ResponseCookies cookies;
net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
GURL url(GaiaUrls::GetInstance()->oauth_revoke_token_url());
net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
test_fetcher.set_url(url);
test_fetcher.set_status(status);
test_fetcher.set_response_code(net::HTTP_OK);
test_fetcher.set_cookies(cookies);
oauth_fetcher.OnURLFetchComplete(&test_fetcher);
}
...@@ -1253,9 +1253,6 @@ ...@@ -1253,9 +1253,6 @@
'browser/net/evicted_domain_cookie_counter.h', 'browser/net/evicted_domain_cookie_counter.h',
'browser/net/firefox_proxy_settings.cc', 'browser/net/firefox_proxy_settings.cc',
'browser/net/firefox_proxy_settings.h', 'browser/net/firefox_proxy_settings.h',
'browser/net/gaia/gaia_oauth_consumer.h',
'browser/net/gaia/gaia_oauth_fetcher.cc',
'browser/net/gaia/gaia_oauth_fetcher.h',
'browser/net/http_pipelining_compatibility_client.cc', 'browser/net/http_pipelining_compatibility_client.cc',
'browser/net/http_pipelining_compatibility_client.h', 'browser/net/http_pipelining_compatibility_client.h',
'browser/net/http_server_properties_manager.cc', 'browser/net/http_server_properties_manager.cc',
...@@ -2917,9 +2914,6 @@ ...@@ -2917,9 +2914,6 @@
'browser/extensions/api/terminal/terminal_extension_helper.h', 'browser/extensions/api/terminal/terminal_extension_helper.h',
'browser/extensions/api/terminal/terminal_private_api.cc', 'browser/extensions/api/terminal/terminal_private_api.cc',
'browser/extensions/api/terminal/terminal_private_api.h', 'browser/extensions/api/terminal/terminal_private_api.h',
'browser/net/gaia/gaia_oauth_consumer.h',
'browser/net/gaia/gaia_oauth_fetcher.cc',
'browser/net/gaia/gaia_oauth_fetcher.h',
'browser/renderer_host/offline_resource_throttle.cc', 'browser/renderer_host/offline_resource_throttle.cc',
'browser/renderer_host/offline_resource_throttle.h', 'browser/renderer_host/offline_resource_throttle.h',
'browser/renderer_host/pepper/pepper_platform_verification_message_filter.cc', 'browser/renderer_host/pepper/pepper_platform_verification_message_filter.cc',
...@@ -3193,7 +3187,6 @@ ...@@ -3193,7 +3187,6 @@
'browser/sync/sync_global_error.h', 'browser/sync/sync_global_error.h',
'browser/download/download_crx_util.cc', 'browser/download/download_crx_util.cc',
'browser/net/gaia/gaia_oauth_fetcher.cc',
'browser/policy/cloud/user_policy_signin_service.cc', 'browser/policy/cloud/user_policy_signin_service.cc',
'browser/policy/cloud/user_policy_signin_service.h', 'browser/policy/cloud/user_policy_signin_service.h',
'browser/sessions/persistent_tab_restore_service.cc', 'browser/sessions/persistent_tab_restore_service.cc',
......
...@@ -1052,7 +1052,6 @@ ...@@ -1052,7 +1052,6 @@
'browser/net/dns_probe_service_unittest.cc', 'browser/net/dns_probe_service_unittest.cc',
'browser/net/evicted_domain_cookie_counter_unittest.cc', 'browser/net/evicted_domain_cookie_counter_unittest.cc',
'browser/net/firefox_proxy_settings_unittest.cc', 'browser/net/firefox_proxy_settings_unittest.cc',
'browser/net/gaia/gaia_oauth_fetcher_unittest.cc',
'browser/net/http_pipelining_compatibility_client_unittest.cc', 'browser/net/http_pipelining_compatibility_client_unittest.cc',
'browser/net/http_server_properties_manager_unittest.cc', 'browser/net/http_server_properties_manager_unittest.cc',
'browser/net/net_error_tab_helper_unittest.cc', 'browser/net/net_error_tab_helper_unittest.cc',
...@@ -2313,7 +2312,6 @@ ...@@ -2313,7 +2312,6 @@
], ],
'sources!': [ 'sources!': [
'browser/extensions/api/enterprise_platform_keys_private/enterprise_platform_keys_private_api_unittest.cc', 'browser/extensions/api/enterprise_platform_keys_private/enterprise_platform_keys_private_api_unittest.cc',
'browser/net/gaia/gaia_oauth_fetcher_unittest.cc',
'browser/notifications/login_state_notification_blocker_chromeos_unittest.cc', 'browser/notifications/login_state_notification_blocker_chromeos_unittest.cc',
'browser/extensions/api/log_private/syslog_parser_unittest.cc', 'browser/extensions/api/log_private/syslog_parser_unittest.cc',
], ],
...@@ -2542,7 +2540,6 @@ ...@@ -2542,7 +2540,6 @@
'browser/download/download_shelf_unittest.cc', 'browser/download/download_shelf_unittest.cc',
'browser/extensions/extension_message_bubble_controller_unittest.cc', 'browser/extensions/extension_message_bubble_controller_unittest.cc',
'browser/storage_monitor/media_storage_util_unittest.cc', 'browser/storage_monitor/media_storage_util_unittest.cc',
'browser/net/gaia/gaia_oauth_fetcher_unittest.cc',
'browser/policy/policy_path_parser_unittest.cc', 'browser/policy/policy_path_parser_unittest.cc',
'browser/profiles/off_the_record_profile_impl_unittest.cc', 'browser/profiles/off_the_record_profile_impl_unittest.cc',
'browser/profiles/profile_list_desktop_unittest.cc', 'browser/profiles/profile_list_desktop_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment