1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/device_identity/device_oauth2_token_service.h"
#include <stdint.h>
#include <memory>
#include <set>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "build/build_config.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "google_apis/gaia/gaia_oauth_client.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/oauth2_access_token_manager_test_util.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kRobotEmail[] = "service_acct@system.gserviceaccount.com";
const char kWrongRobotEmail[] = "WRONG_service_acct@system.gserviceaccount.com";
} // namespace
class MockDeviceOAuth2TokenStore : public DeviceOAuth2TokenStore {
public:
MockDeviceOAuth2TokenStore() = default;
~MockDeviceOAuth2TokenStore() override = default;
// DeviceOAuth2TokenStore:
void Init(InitCallback callback) override {
pending_init_callback_ = std::move(callback);
}
CoreAccountId GetAccountId() const override { return account_id_; }
std::string GetRefreshToken() const override { return refresh_token_; }
void SetAndSaveRefreshToken(const std::string& refresh_token,
StatusCallback result_callback) override {
refresh_token_ = refresh_token;
pending_status_callback_ = std::move(result_callback);
}
void PrepareTrustedAccountId(TrustedAccountIdCallback callback) override {
pending_trusted_account_id_callback_ = std::move(callback);
TriggerTrustedAccountIdCallback(true);
}
#if !BUILDFLAG(IS_CHROMEOS)
void SetAccountEmail(const std::string& account_email) override {
account_id_ = CoreAccountId::FromRobotEmail(account_email);
}
#endif
// Mock-specific functions:
void SetRefreshTokenForTesting(const std::string& token) {
refresh_token_ = token;
}
void SetAccountIdForTesting(CoreAccountId account_id) {
account_id_ = account_id;
}
void TriggerInitCallback(bool success, bool validation_required) {
std::move(pending_init_callback_).Run(success, validation_required);
}
void TriggerStatusCallback(bool success) {
std::move(pending_status_callback_).Run(success);
}
void TriggerTrustedAccountIdCallback(bool account_present) {
std::move(pending_trusted_account_id_callback_).Run(account_present);
}
private:
CoreAccountId account_id_;
std::string refresh_token_;
InitCallback pending_init_callback_;
StatusCallback pending_status_callback_;
TrustedAccountIdCallback pending_trusted_account_id_callback_;
};
class DeviceOAuth2TokenServiceTest : public testing::Test {
public:
DeviceOAuth2TokenServiceTest()
: scoped_testing_local_state_(TestingBrowserProcess::GetGlobal()) {}
// Most tests just want a noop crypto impl with a dummy refresh token value in
// Local State (if the value is an empty string, it will be ignored).
void SetUpDefaultValues() {
CreateService();
token_store_->SetRefreshTokenForTesting("device_refresh_token_4_test");
SetRobotAccountId(kRobotEmail);
AssertConsumerTokensAndErrors(0, 0);
token_store_->TriggerInitCallback(true, true);
}
void SetRobotAccountId(const std::string& robot_email) {
token_store_->SetAccountIdForTesting(
CoreAccountId::FromRobotEmail(robot_email));
}
std::unique_ptr<OAuth2AccessTokenManager::Request> StartTokenRequest() {
return oauth2_service_->StartAccessTokenRequest(std::set<std::string>(),
&consumer_);
}
void SetUp() override {}
void TearDown() override {
oauth2_service_.reset();
base::ThreadPoolInstance::Get()->FlushForTesting();
base::RunLoop().RunUntilIdle();
}
MockDeviceOAuth2TokenStore* token_store() { return token_store_; }
void CreateService() {
auto store = std::make_unique<MockDeviceOAuth2TokenStore>();
token_store_ = store.get();
oauth2_service_.reset(new DeviceOAuth2TokenService(
test_url_loader_factory_.GetSafeWeakWrapper(), std::move(store)));
oauth2_service_->max_refresh_token_validation_retries_ = 0;
oauth2_service_->GetAccessTokenManager()
->set_max_authorization_token_fetch_retries_for_testing(0);
}
std::string GetValidTokenInfoResponse(const std::string& email) {
return "{ \"email\": \"" + email +
"\","
" \"user_id\": \"1234567890\" }";
}
std::string GetInvalidScopeResponse(const std::string& scope) {
return "{ \"error\": \"invalid_scope\", "
"\"error_description\": \"Some requested scopes were invalid. "
"{invalid\\u003d[" +
scope +
"}\", "
"\"error_uri\": "
"\"https://developers.google.com/identity/protocols/oauth2\""
"}";
}
bool RefreshTokenIsAvailable() {
return oauth2_service_->RefreshTokenIsAvailable();
}
std::string GetRefreshToken() {
if (!RefreshTokenIsAvailable())
return std::string();
return oauth2_service_->GetRefreshToken();
}
// A utility method to return fake URL results, for testing the refresh token
// validation logic. For a successful validation attempt, this method will be
// called three times for the steps listed below.
//
// Step 1a: fetch the access token for the tokeninfo API.
// Step 1b: call the tokeninfo API.
// Step 2: Fetch the access token for the requested scope
// (in this case, cloudprint).
void ReturnOAuthUrlFetchResults(const std::string& url,
net::HttpStatusCode response_code,
const std::string& response_string);
// Generates URL fetch replies with the specified results for requests
// generated by the token service.
void PerformURLFetchesWithResults(
net::HttpStatusCode tokeninfo_access_token_status,
const std::string& tokeninfo_access_token_response,
net::HttpStatusCode tokeninfo_fetch_status,
const std::string& tokeninfo_fetch_response,
net::HttpStatusCode service_access_token_status,
const std::string& service_access_token_response);
// Generates URL fetch replies for the success path.
void PerformURLFetches();
void AssertConsumerTokensAndErrors(int num_tokens, int num_errors);
protected:
// This is here because DeviceOAuth2TokenService's destructor is private;
// base::DefaultDeleter therefore doesn't work. However, the test class is
// declared friend in DeviceOAuth2TokenService, so this deleter works.
struct TokenServiceDeleter {
inline void operator()(DeviceOAuth2TokenService* ptr) const { delete ptr; }
};
content::BrowserTaskEnvironment task_environment_;
ScopedTestingLocalState scoped_testing_local_state_;
network::TestURLLoaderFactory test_url_loader_factory_;
std::unique_ptr<DeviceOAuth2TokenService, TokenServiceDeleter>
oauth2_service_;
TestingOAuth2AccessTokenManagerConsumer consumer_;
raw_ptr<MockDeviceOAuth2TokenStore, DanglingUntriaged> token_store_;
};
void DeviceOAuth2TokenServiceTest::ReturnOAuthUrlFetchResults(
const std::string& url,
net::HttpStatusCode response_code,
const std::string& response_string) {
if (test_url_loader_factory_.IsPending(url)) {
test_url_loader_factory_.SimulateResponseForPendingRequest(
GURL(url), network::URLLoaderCompletionStatus(net::OK),
network::CreateURLResponseHead(response_code), response_string);
}
}
void DeviceOAuth2TokenServiceTest::PerformURLFetchesWithResults(
net::HttpStatusCode tokeninfo_access_token_status,
const std::string& tokeninfo_access_token_response,
net::HttpStatusCode tokeninfo_fetch_status,
const std::string& tokeninfo_fetch_response,
net::HttpStatusCode service_access_token_status,
const std::string& service_access_token_response) {
ReturnOAuthUrlFetchResults(GaiaUrls::GetInstance()->oauth2_token_url().spec(),
tokeninfo_access_token_status,
tokeninfo_access_token_response);
ReturnOAuthUrlFetchResults(
GaiaUrls::GetInstance()->oauth2_token_info_url().spec(),
tokeninfo_fetch_status, tokeninfo_fetch_response);
ReturnOAuthUrlFetchResults(GaiaUrls::GetInstance()->oauth2_token_url().spec(),
service_access_token_status,
service_access_token_response);
}
void DeviceOAuth2TokenServiceTest::PerformURLFetches() {
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, GetValidTokenInfoResponse(kRobotEmail), net::HTTP_OK,
GetValidTokenResponse("scoped_access_token", 3600));
}
void DeviceOAuth2TokenServiceTest::AssertConsumerTokensAndErrors(
int num_tokens,
int num_errors) {
EXPECT_EQ(num_tokens, consumer_.number_of_successful_tokens_);
EXPECT_EQ(num_errors, consumer_.number_of_errors_);
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Success) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetches();
AssertConsumerTokensAndErrors(1, 0);
EXPECT_EQ("scoped_access_token", consumer_.last_token_);
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_SuccessAsyncLoad) {
CreateService();
token_store()->SetRefreshTokenForTesting("device_refresh_token_4_test");
SetRobotAccountId(kRobotEmail);
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetches();
AssertConsumerTokensAndErrors(0, 0);
token_store()->TriggerInitCallback(true, true);
base::RunLoop().RunUntilIdle();
PerformURLFetches();
AssertConsumerTokensAndErrors(1, 0);
EXPECT_EQ("scoped_access_token", consumer_.last_token_);
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Cancel) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
request.reset();
PerformURLFetches();
// Test succeeds if this line is reached without a crash.
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_InitFailure) {
CreateService();
token_store()->SetRefreshTokenForTesting("device_refresh_token_4_test");
SetRobotAccountId(kRobotEmail);
token_store()->TriggerInitCallback(false, true);
EXPECT_FALSE(RefreshTokenIsAvailable());
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
base::RunLoop().RunUntilIdle();
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_TokenInfoAccessTokenHttpError) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(net::HTTP_UNAUTHORIZED, "", net::HTTP_OK,
GetValidTokenInfoResponse(kRobotEmail),
net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_TokenInfoAccessTokenInvalidResponse) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(net::HTTP_OK, "invalid response", net::HTTP_OK,
GetValidTokenInfoResponse(kRobotEmail),
net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_InvalidScope) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, GetValidTokenInfoResponse(kRobotEmail),
net::HTTP_BAD_REQUEST, GetInvalidScopeResponse("test_scope"));
AssertConsumerTokensAndErrors(0, 1);
EXPECT_EQ(consumer_.last_error_.state(),
GoogleServiceAuthError::SCOPE_LIMITED_UNRECOVERABLE_ERROR);
EXPECT_EQ(consumer_.last_error_.GetScopeLimitedUnrecoverableErrorReason(),
GoogleServiceAuthError::ScopeLimitedUnrecoverableErrorReason::
kInvalidScope);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_TokenInfoApiCallHttpError) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_INTERNAL_SERVER_ERROR, "", net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_TokenInfoApiCallInvalidResponse) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, "invalid response", net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_CloudPrintAccessTokenHttpError) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, GetValidTokenInfoResponse(kRobotEmail),
net::HTTP_BAD_REQUEST, "");
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest,
RefreshTokenValidation_Failure_CloudPrintAccessTokenInvalidResponse) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, GetValidTokenInfoResponse(kRobotEmail), net::HTTP_OK,
"invalid request");
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Failure_BadOwner) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
SetRobotAccountId(kWrongRobotEmail);
PerformURLFetchesWithResults(
net::HTTP_OK, GetValidTokenResponse("tokeninfo_access_token", 3600),
net::HTTP_OK, GetValidTokenInfoResponse(kRobotEmail), net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
}
TEST_F(DeviceOAuth2TokenServiceTest, RefreshTokenValidation_Retry) {
SetUpDefaultValues();
std::unique_ptr<OAuth2AccessTokenManager::Request> request =
StartTokenRequest();
PerformURLFetchesWithResults(
net::HTTP_INTERNAL_SERVER_ERROR, "", net::HTTP_OK,
GetValidTokenInfoResponse(kRobotEmail), net::HTTP_OK,
GetValidTokenResponse("ignored", 3600));
AssertConsumerTokensAndErrors(0, 1);
// Retry should succeed.
request = StartTokenRequest();
PerformURLFetches();
AssertConsumerTokensAndErrors(1, 1);
}
|