Skip to content

Commit

Permalink
Pass device_id and device_type to programmatic_auth endpoint when req…
Browse files Browse the repository at this point in the history
…uesting LST

I added StartCookieForOAuthLoginTokenExchangeWithDeviceId, it is not used anywhere.
I'll add code that uses it in the future changes.

BUG=382968
R=rogerta@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@277128 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pavely@chromium.org committed Jun 13, 2014
1 parent c2ef3eb commit 2558134
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
38 changes: 31 additions & 7 deletions google_apis/gaia/gaia_auth_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ const char GaiaAuthFetcher::kIssueAuthTokenFormat[] =
const char GaiaAuthFetcher::kClientLoginToOAuth2BodyFormat[] =
"scope=%s&client_id=%s";
// static
const char GaiaAuthFetcher::kClientLoginToOAuth2WithDeviceTypeBodyFormat[] =
"scope=%s&client_id=%s&device_type=chrome";
// static
const char GaiaAuthFetcher::kOAuth2CodeToTokenPairBodyFormat[] =
"scope=%s&"
"grant_type=authorization_code&"
Expand Down Expand Up @@ -154,6 +157,8 @@ const char GaiaAuthFetcher::kOAuthHeaderFormat[] = "Authorization: OAuth %s";
const char GaiaAuthFetcher::kOAuth2BearerHeaderFormat[] =
"Authorization: Bearer %s";
// static
const char GaiaAuthFetcher::kDeviceIdHeaderFormat[] = "X-Device-ID: %s";
// static
const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartSecure[] = "secure";
// static
const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartHttpOnly[] =
Expand Down Expand Up @@ -296,14 +301,20 @@ std::string GaiaAuthFetcher::MakeIssueAuthTokenBody(
}

// static
std::string GaiaAuthFetcher::MakeGetAuthCodeBody() {
std::string GaiaAuthFetcher::MakeGetAuthCodeBody(bool include_device_type) {
std::string encoded_scope = net::EscapeUrlEncodedData(
GaiaConstants::kOAuth1LoginScope, true);
std::string encoded_client_id = net::EscapeUrlEncodedData(
GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true);
return base::StringPrintf(kClientLoginToOAuth2BodyFormat,
encoded_scope.c_str(),
encoded_client_id.c_str());
if (include_device_type) {
return base::StringPrintf(kClientLoginToOAuth2WithDeviceTypeBodyFormat,
encoded_scope.c_str(),
encoded_client_id.c_str());
} else {
return base::StringPrintf(kClientLoginToOAuth2BodyFormat,
encoded_scope.c_str(),
encoded_client_id.c_str());
}
}

// static
Expand Down Expand Up @@ -514,7 +525,7 @@ void GaiaAuthFetcher::StartLsoForOAuthLoginTokenExchange(
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";

DVLOG(1) << "Starting OAuth login token exchange with auth_token";
request_body_ = MakeGetAuthCodeBody();
request_body_ = MakeGetAuthCodeBody(false);
client_login_to_oauth2_gurl_ =
GaiaUrls::GetInstance()->client_login_to_oauth2_url();

Expand Down Expand Up @@ -545,10 +556,17 @@ void GaiaAuthFetcher::StartRevokeOAuth2Token(const std::string& auth_token) {

void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange(
const std::string& session_index) {
StartCookieForOAuthLoginTokenExchangeWithDeviceId(session_index,
std::string());
}

void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchangeWithDeviceId(
const std::string& session_index,
const std::string& device_id) {
DCHECK(!fetch_pending_) << "Tried to fetch two things at once!";

DVLOG(1) << "Starting OAuth login token fetch with cookie jar";
request_body_ = MakeGetAuthCodeBody();
request_body_ = MakeGetAuthCodeBody(!device_id.empty());

client_login_to_oauth2_gurl_ =
GaiaUrls::GetInstance()->client_login_to_oauth2_url();
Expand All @@ -557,9 +575,15 @@ void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange(
client_login_to_oauth2_gurl_.Resolve("?authuser=" + session_index);
}

std::string device_id_header;
if (!device_id.empty()) {
device_id_header =
base::StringPrintf(kDeviceIdHeaderFormat, device_id.c_str());
}

fetcher_.reset(CreateGaiaFetcher(getter_,
request_body_,
std::string(),
device_id_header,
client_login_to_oauth2_gurl_,
net::LOAD_NORMAL,
this));
Expand Down
19 changes: 18 additions & 1 deletion google_apis/gaia/gaia_auth_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate {
// called on the consumer on the original thread.
void StartCookieForOAuthLoginTokenExchange(const std::string& session_index);

// Start a request to exchange the cookies of a signed-in user session
// for an OAuthLogin-scoped oauth2 token. In the case of a session with
// multiple accounts signed in, |session_index| indicate the which of accounts
// within the session.
// Resulting refresh token is annotated on the server with |device_id|. Format
// of device_id on the server is at most 64 unicode characters.
//
// Either OnClientOAuthSuccess or OnClientOAuthFailure will be
// called on the consumer on the original thread.
void StartCookieForOAuthLoginTokenExchangeWithDeviceId(
const std::string& session_index,
const std::string& device_id);

// Start a request to exchange the authorization code for an OAuthLogin-scoped
// oauth2 token.
//
Expand Down Expand Up @@ -191,6 +204,9 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate {
static const char kIssueAuthTokenFormat[];
// The format of the POST body to get OAuth2 auth code from auth token.
static const char kClientLoginToOAuth2BodyFormat[];
// The format of the POST body to get OAuth2 auth code from auth token. This
// format is used for request annotated with device_id.
static const char kClientLoginToOAuth2WithDeviceTypeBodyFormat[];
// The format of the POST body to get OAuth2 token pair from auth code.
static const char kOAuth2CodeToTokenPairBodyFormat[];
// The format of the POST body to revoke an OAuth2 token.
Expand Down Expand Up @@ -229,6 +245,7 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate {
static const char kAuthHeaderFormat[];
static const char kOAuthHeaderFormat[];
static const char kOAuth2BearerHeaderFormat[];
static const char kDeviceIdHeaderFormat[];
static const char kClientLoginToOAuth2CookiePartSecure[];
static const char kClientLoginToOAuth2CookiePartHttpOnly[];
static const char kClientLoginToOAuth2CookiePartCodePrefix[];
Expand Down Expand Up @@ -314,7 +331,7 @@ class GaiaAuthFetcher : public net::URLFetcherDelegate {
const std::string& lsid,
const char* const service);
// Create body to get OAuth2 auth code.
static std::string MakeGetAuthCodeBody();
static std::string MakeGetAuthCodeBody(bool include_device_type);
// Given auth code, create body to get OAuth2 token pair.
static std::string MakeGetTokenPairBody(const std::string& auth_code);
// Given an OAuth2 token, create body to revoke the token.
Expand Down
19 changes: 19 additions & 0 deletions google_apis/gaia/gaia_auth_fetcher_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,25 @@ TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies) {
net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
EXPECT_TRUE(NULL != fetcher);
EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
EXPECT_FALSE(EndsWith(fetcher->upload_data(), "device_type=chrome", true));
}

TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenWithCookies_DeviceId) {
MockGaiaConsumer consumer;
net::TestURLFetcherFactory factory;
std::string expected_device_id("ABCDE-12345");
GaiaAuthFetcher auth(&consumer, std::string(), GetRequestContext());
auth.StartCookieForOAuthLoginTokenExchangeWithDeviceId("0",
expected_device_id);
net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
EXPECT_TRUE(NULL != fetcher);
EXPECT_EQ(net::LOAD_NORMAL, fetcher->GetLoadFlags());
EXPECT_TRUE(EndsWith(fetcher->upload_data(), "device_type=chrome", true));
net::HttpRequestHeaders extra_request_headers;
fetcher->GetExtraRequestHeaders(&extra_request_headers);
std::string device_id;
EXPECT_TRUE(extra_request_headers.GetHeader("X-Device-ID", &device_id));
EXPECT_EQ(device_id, expected_device_id);
}

TEST_F(GaiaAuthFetcherTest, OAuthLoginTokenClientLoginToOAuth2Failure) {
Expand Down

0 comments on commit 2558134

Please sign in to comment.