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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/drivefs/drivefs_auth.h"
#include "base/functional/bind.h"
#include "components/account_id/account_id.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
#include "google_apis/gaia/gaia_constants.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace drivefs {
namespace {
constexpr char kIdentityConsumerId[] = "drivefs";
} // namespace
DriveFsAuth::DriveFsAuth(const base::Clock* clock,
const base::FilePath& profile_path,
std::unique_ptr<base::OneShotTimer> timer,
Delegate* delegate)
: clock_(clock),
profile_path_(profile_path),
timer_(std::move(timer)),
delegate_(delegate) {}
DriveFsAuth::~DriveFsAuth() = default;
std::optional<std::string> DriveFsAuth::GetCachedAccessToken() {
const auto& token = GetOrResetCachedToken(true);
if (token.empty()) {
return std::nullopt;
}
return token;
}
void DriveFsAuth::GetAccessToken(bool use_cached,
AccessTokenCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (get_access_token_callback_) {
std::move(callback).Run(mojom::AccessTokenStatus::kTransientError,
mojom::AccessToken::New());
return;
}
const std::string& token = GetOrResetCachedToken(use_cached);
if (!token.empty()) {
std::move(callback).Run(mojom::AccessTokenStatus::kSuccess,
mojom::AccessToken::New(token, last_token_expiry_));
return;
}
signin::IdentityManager* identity_manager = delegate_->GetIdentityManager();
if (!identity_manager) {
std::move(callback).Run(mojom::AccessTokenStatus::kAuthError,
mojom::AccessToken::New());
return;
}
get_access_token_callback_ = std::move(callback);
// Timer is cancelled when it is destroyed, so use base::Unretained().
timer_->Start(
FROM_HERE, base::Seconds(30),
base::BindOnce(&DriveFsAuth::AuthTimeout, base::Unretained(this)));
std::set<std::string> scopes(
{GaiaConstants::kClientChannelOAuth2Scope,
GaiaConstants::kDriveOAuth2Scope,
GaiaConstants::kExperimentsAndConfigsOAuth2Scope});
access_token_fetcher_ =
std::make_unique<signin::PrimaryAccountAccessTokenFetcher>(
kIdentityConsumerId, identity_manager, scopes,
base::BindOnce(&DriveFsAuth::GotChromeAccessToken,
base::Unretained(this)),
signin::PrimaryAccountAccessTokenFetcher::Mode::kWaitUntilAvailable,
signin::ConsentLevel::kSignin);
}
void DriveFsAuth::GotChromeAccessToken(
GoogleServiceAuthError error,
signin::AccessTokenInfo access_token_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
timer_->Stop();
if (error.state() != GoogleServiceAuthError::NONE) {
std::move(get_access_token_callback_)
.Run(error.IsPersistentError()
? mojom::AccessTokenStatus::kAuthError
: mojom::AccessTokenStatus::kTransientError,
mojom::AccessToken::New());
return;
}
UpdateCachedToken(access_token_info.token, access_token_info.expiration_time);
std::move(get_access_token_callback_)
.Run(mojom::AccessTokenStatus::kSuccess,
mojom::AccessToken::New(access_token_info.token,
access_token_info.expiration_time));
}
const std::string& DriveFsAuth::GetOrResetCachedToken(bool use_cached) {
if (!use_cached || clock_->Now() >= last_token_expiry_) {
last_token_.clear();
}
return last_token_;
}
void DriveFsAuth::UpdateCachedToken(const std::string& token,
base::Time expiry) {
last_token_ = token;
last_token_expiry_ = expiry;
}
void DriveFsAuth::AuthTimeout() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
access_token_fetcher_.reset();
std::move(get_access_token_callback_)
.Run(mojom::AccessTokenStatus::kTransientError,
mojom::AccessToken::New());
}
} // namespace drivefs
|