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
|
// Copyright 2025 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/glic/host/auth_controller.h"
#include "base/command_line.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "chrome/browser/glic/host/glic_cookie_synchronizer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_ui_util.h"
#include "chrome/common/chrome_switches.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/base/signin_metrics.h"
namespace glic {
namespace {
// TODO(crbug.com/391378260): Once the web client can request to sync auth
// reliably, we should not need any timeout.
base::TimeDelta kCookieSyncRepeatTime = base::Minutes(5);
bool IsAutomationEnabled() {
auto* command_line = base::CommandLine::ForCurrentProcess();
return command_line->HasSwitch(::switches::kGlicAutomation);
}
} // namespace
AuthController::AuthController(Profile* profile,
signin::IdentityManager* identity_manager,
bool use_for_fre)
: profile_(profile),
identity_manager_(identity_manager),
cookie_synchronizer_(
std::make_unique<GlicCookieSynchronizer>(profile,
identity_manager,
use_for_fre)),
observation_(this) {
observation_.Observe(identity_manager_);
}
AuthController::~AuthController() = default;
bool AuthController::CheckAuthBeforeShowSync(base::OnceClosure after_signin) {
if (IsAutomationEnabled()) {
return true;
}
switch (GetTokenState()) {
case TokenState::kRequiresSignIn:
ShowReauthForAccount(std::move(after_signin));
return false;
case TokenState::kUnknownError:
case TokenState::kOk:
default:
return true;
}
}
void AuthController::CheckAuthBeforeLoad(
base::OnceCallback<void(mojom::PrepareForClientResult)> callback) {
// If automation is enabled skip auth check.
if (IsAutomationEnabled()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback),
mojom::PrepareForClientResult::kSuccess));
return;
}
if (GetTokenState() == TokenState::kRequiresSignIn) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback),
mojom::PrepareForClientResult::kRequiresSignIn));
return;
}
cookie_synchronizer_->CopyCookiesToWebviewStoragePartition(
base::BindOnce(&AuthController::CookieSyncBeforeLoadDone, GetWeakPtr(),
std::move(callback)));
}
AuthController::TokenState AuthController::GetTokenState() const {
// If automation is enabled skip auth check.
if (IsAutomationEnabled()) {
return TokenState::kOk;
}
CoreAccountId account_id =
identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin);
// If the user is signed-out, Glic shouldn't be running. Return an error
// to avoid crashing if sign-out happens while Glic is loading.
if (account_id.empty()) {
return TokenState::kUnknownError;
}
if (identity_manager_->HasAccountWithRefreshTokenInPersistentErrorState(
account_id)) {
return TokenState::kRequiresSignIn;
}
return TokenState::kOk;
}
void AuthController::OnPrimaryAccountChanged(
const signin::PrimaryAccountChangeEvent& event_details) {
switch (event_details.GetEventTypeFor(signin::ConsentLevel::kSignin)) {
case signin::PrimaryAccountChangeEvent::Type::kSet:
last_cookie_sync_time_ = std::nullopt;
break;
// Ignore until primary account is set.
case signin::PrimaryAccountChangeEvent::Type::kNone:
case signin::PrimaryAccountChangeEvent::Type::kCleared:
break;
}
}
void AuthController::ForceSyncCookies(base::OnceCallback<void(bool)> callback) {
last_cookie_sync_time_ = std::nullopt;
SyncCookiesIfRequired(std::move(callback));
}
void AuthController::OnErrorStateOfRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info,
const GoogleServiceAuthError& error,
signin_metrics::SourceForRefreshTokenOperation token_operation_source) {
if (identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin) !=
account_info.account_id) {
return;
}
last_cookie_sync_time_ = std::nullopt;
if (after_signin_callback_ &&
after_signin_callback_expiration_time_ > base::TimeTicks::Now()) {
if (GetTokenState() == TokenState::kOk) {
std::move(after_signin_callback_).Run();
}
}
}
void AuthController::OnRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info) {
if (identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin) !=
account_info.account_id) {
return;
}
last_cookie_sync_time_ = std::nullopt;
}
void AuthController::SyncCookiesIfRequired(
base::OnceCallback<void(bool)> callback) {
// If cookies were synced successfully and recently, don't do it again.
if (last_cookie_sync_time_ &&
base::TimeTicks::Now() - *last_cookie_sync_time_ <
kCookieSyncRepeatTime) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true));
return;
}
last_cookie_sync_time_ = std::nullopt;
cookie_synchronizer_->CopyCookiesToWebviewStoragePartition(base::BindOnce(
&AuthController::CookieSyncDone, GetWeakPtr(), std::move(callback)));
}
void AuthController::CookieSyncDone(base::OnceCallback<void(bool)> callback,
bool sync_success) {
if (sync_success) {
last_cookie_sync_time_ = base::TimeTicks::Now();
}
std::move(callback).Run(sync_success);
}
void AuthController::ShowReauthForAccount(base::OnceClosure after_signin) {
after_signin_callback_ = std::move(after_signin);
// TODO(crbug.com/396500584): Check what timeout is appropriate.
after_signin_callback_expiration_time_ =
base::TimeTicks::Now() + base::Minutes(5);
CoreAccountInfo primary_account_info =
identity_manager_->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
signin_ui_util::ShowReauthForAccount(
profile_, primary_account_info.email,
signin_metrics::AccessPoint::kGlicLaunchButton);
}
void AuthController::OnGlicWindowOpened() {
after_signin_callback_.Reset();
}
bool AuthController::RequiresSignIn() const {
return GetTokenState() == TokenState::kRequiresSignIn;
}
void AuthController::CookieSyncBeforeLoadDone(
base::OnceCallback<void(mojom::PrepareForClientResult)> callback,
bool sync_success) {
if (sync_success) {
last_cookie_sync_time_ = base::TimeTicks::Now();
std::move(callback).Run(mojom::PrepareForClientResult::kSuccess);
return;
}
std::move(callback).Run(GetTokenState() == TokenState::kRequiresSignIn
? mojom::PrepareForClientResult::kRequiresSignIn
: mojom::PrepareForClientResult::kUnknownError);
}
void AuthController::SetCookieSynchronizerForTesting(
std::unique_ptr<GlicCookieSynchronizer> synchronizer) {
cookie_synchronizer_ = std::move(synchronizer);
}
} // namespace glic
|