Commit 408c7414 authored by tengs's avatar tengs Committed by Commit bot

Implement CryptAuthAccountTokenFetcher, which performs the fetching of the CryptAuth access token.

BUG=420316

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

Cr-Commit-Position: refs/heads/master@{#308207}
parent d7a1017b
......@@ -758,6 +758,7 @@
'proximity_auth/bluetooth_connection_finder_unittest.cc',
'proximity_auth/client_unittest.cc',
'proximity_auth/connection_unittest.cc',
'proximity_auth/cryptauth/cryptauth_account_token_fetcher_unittest.cc',
'proximity_auth/cryptauth/cryptauth_api_call_flow_unittest.cc',
'proximity_auth/cryptauth/cryptauth_client_unittest.cc',
'proximity_auth/proximity_auth_system_unittest.cc',
......@@ -773,6 +774,7 @@
'components.gyp:proximity_auth',
'components.gyp:cryptauth',
'../device/bluetooth/bluetooth.gyp:device_bluetooth_mocks',
'../google_apis/google_apis.gyp:google_apis_test_support',
'../third_party/protobuf/protobuf.gyp:protobuf_lite',
],
}],
......
......@@ -71,6 +71,8 @@
],
'sources': [
"proximity_auth/cryptauth/cryptauth_access_token_fetcher.h",
"proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc",
"proximity_auth/cryptauth/cryptauth_account_token_fetcher.h",
"proximity_auth/cryptauth/cryptauth_api_call_flow.cc",
"proximity_auth/cryptauth/cryptauth_api_call_flow.h",
"proximity_auth/cryptauth/cryptauth_client.cc",
......
......@@ -5,6 +5,8 @@
source_set("cryptauth") {
sources = [
"cryptauth_access_token_fetcher.h",
"cryptauth_account_token_fetcher.cc",
"cryptauth_account_token_fetcher.h",
"cryptauth_api_call_flow.cc",
"cryptauth_api_call_flow.h",
"cryptauth_client.cc",
......@@ -25,6 +27,7 @@ source_set("cryptauth") {
source_set("unit_tests") {
testonly = true
sources = [
"cryptauth_account_token_fetcher_unittest.cc",
"cryptauth_api_call_flow_unittest.cc",
"cryptauth_client_unittest.cc",
]
......@@ -32,6 +35,7 @@ source_set("unit_tests") {
deps = [
":cryptauth",
"//base/test:test_support",
"//google_apis:test_support",
"//net:test_support",
"//testing/gtest",
]
......
......@@ -5,10 +5,15 @@
#ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_ACCESS_TOKEN_FETCHER_H
#define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_ACCESS_TOKEN_FETCHER_H
#include <string>
#include "base/callback_forward.h"
namespace proximity_auth {
// Simple interface for fetching the OAuth2 access token that authorizes
// CryptAuth API calls.
// CryptAuth API calls. Do not reuse this after calling FetchAccessToken();
// instead, create a new instance.
class CryptAuthAccessTokenFetcher {
public:
virtual ~CryptAuthAccessTokenFetcher() {}
......
// Copyright 2014 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 "components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.h"
namespace proximity_auth {
namespace {
// Returns the set of OAuth2 scopes that CryptAuth uses.
OAuth2TokenService::ScopeSet GetScopes() {
OAuth2TokenService::ScopeSet scopes;
scopes.insert("https://www.googleapis.com/auth/cryptauth");
return scopes;
}
} // namespace
CryptAuthAccountTokenFetcher::CryptAuthAccountTokenFetcher(
OAuth2TokenService* token_service,
const std::string& account_id)
: OAuth2TokenService::Consumer("cryptauth_account_token_fetcher"),
token_service_(token_service),
account_id_(account_id),
fetch_started_(false) {
}
CryptAuthAccountTokenFetcher::~CryptAuthAccountTokenFetcher() {
}
void CryptAuthAccountTokenFetcher::FetchAccessToken(
const AccessTokenCallback& callback) {
if (fetch_started_) {
LOG(WARNING) << "Create an instance for each token fetched. Do not reuse.";
callback.Run(std::string());
return;
}
fetch_started_ = true;
callback_ = callback;
// This request will return a cached result if it is available, saving a
// network round trip every time we fetch the access token.
token_request_ = token_service_->StartRequest(account_id_, GetScopes(), this);
}
void CryptAuthAccountTokenFetcher::OnGetTokenSuccess(
const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) {
callback_.Run(access_token);
}
void CryptAuthAccountTokenFetcher::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
callback_.Run(std::string());
}
} // namespace proximity_auth
// Copyright 2014 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 COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_ACCOUNT_TOKEN_FETCHER_IMPL_H
#define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_ACCOUNT_TOKEN_FETCHER_IMPL_H
#include "base/callback.h"
#include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h"
#include "google_apis/gaia/oauth2_token_service.h"
namespace proximity_auth {
// Implementation of CryptAuthAccessTokenFetcher fetching an access token for a
// given account using the provided OAuth2TokenService.
class CryptAuthAccountTokenFetcher : public CryptAuthAccessTokenFetcher,
public OAuth2TokenService::Consumer {
public:
// |token_service| is not owned, and must outlive this object.
CryptAuthAccountTokenFetcher(OAuth2TokenService* token_service,
const std::string& account_id);
virtual ~CryptAuthAccountTokenFetcher();
// CryptAuthAccessTokenFetcher:
void FetchAccessToken(const AccessTokenCallback& callback) override;
private:
// OAuth2TokenService::Consumer:
void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) override;
void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override;
// System service that caches and fetches tokens for a given account.
// Not owned.
OAuth2TokenService* token_service_;
// The account id for whom to mint the token.
std::string account_id_;
// True if FetchAccessToken() has been called.
bool fetch_started_;
// Stores the request from |token_service_| to mint the token.
scoped_ptr<OAuth2TokenService::Request> token_request_;
// Callback to invoke when the token fetch succeeds or fails.
AccessTokenCallback callback_;
DISALLOW_COPY_AND_ASSIGN(CryptAuthAccountTokenFetcher);
};
} // namespace proximity_auth
#endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_ACCESS_TOKEN_FETCHER_IMPL_H
// Copyright 2014 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 "components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.h"
#include <string>
#include "base/bind.h"
#include "google_apis/gaia/fake_oauth2_token_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace proximity_auth {
namespace {
const char kAccountId[] = "account_id";
const char kAccessToken[] = "access_token";
const char kInvalidResult[] = "invalid_result";
// Callback that saves the fetched access token to the first argument.
void SaveAccessToken(std::string* out_token, const std::string& in_token) {
*out_token = in_token;
}
} // namespace
class ProximityAuthCryptAuthAccountTokenFetcherTest : public testing::Test {
protected:
ProximityAuthCryptAuthAccountTokenFetcherTest()
: fetcher_(&token_service_, kAccountId) {
token_service_.AddAccount(kAccountId);
}
virtual ~ProximityAuthCryptAuthAccountTokenFetcherTest() {}
FakeOAuth2TokenService token_service_;
CryptAuthAccountTokenFetcher fetcher_;
DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthAccountTokenFetcherTest);
};
TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchSuccess) {
std::string result;
fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result));
token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken,
base::Time::Max());
EXPECT_EQ(kAccessToken, result);
}
TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetchFailure) {
std::string result(kInvalidResult);
fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result));
token_service_.IssueErrorForAllPendingRequestsForAccount(
kAccountId,
GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
EXPECT_EQ(std::string(), result);
}
TEST_F(ProximityAuthCryptAuthAccountTokenFetcherTest, FetcherReuse) {
std::string result1;
fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result1));
{
std::string result2(kInvalidResult);
fetcher_.FetchAccessToken(base::Bind(SaveAccessToken, &result2));
EXPECT_EQ(std::string(), result2);
}
token_service_.IssueAllTokensForAccount(kAccountId, kAccessToken,
base::Time::Max());
EXPECT_EQ(kAccessToken, result1);
}
} // namespace proximity_auth
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