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
|
// Copyright 2022 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/ash/printing/oauth2/authorization_zones_manager.h"
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/notreached.h"
#include "chrome/browser/ash/printing/oauth2/authorization_zone.h"
#include "chrome/browser/ash/printing/oauth2/client_ids_database.h"
#include "chrome/browser/ash/printing/oauth2/log_entry.h"
#include "chrome/browser/ash/printing/oauth2/profile_auth_servers_sync_bridge.h"
#include "chrome/browser/ash/printing/oauth2/status_code.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/data_type_store_service_factory.h"
#include "chromeos/printing/uri.h"
#include "components/device_event_log/device_event_log.h"
#include "components/sync/model/data_type_local_change_processor.h"
#include "components/sync/model/data_type_store.h"
#include "components/sync/model/data_type_store_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
namespace ash::printing::oauth2 {
namespace {
// Logs results to device-log and calls `callback` with parameters `status` and
// `data`.
void LogAndCall(StatusCallback callback,
std::string_view method,
const GURL& auth_server,
const chromeos::Uri& ipp_endpoint,
StatusCode status,
std::string data) {
if (status == StatusCode::kOK || status == StatusCode::kAuthorizationNeeded) {
PRINTER_LOG(EVENT) << LogEntry((status == StatusCode::kOK) ? "" : data,
method, auth_server, status, ipp_endpoint);
} else {
PRINTER_LOG(ERROR) << LogEntry(data, method, auth_server, status,
ipp_endpoint);
}
std::move(callback).Run(status, std::move(data));
}
void AddLoggingToCallback(StatusCallback& callback,
std::string_view method,
const GURL& auth_server,
const chromeos::Uri& ipp_endpoint = chromeos::Uri()) {
// Wrap the `callback` with the function LogAndCall() defined above.
auto new_call = base::BindOnce(&LogAndCall, std::move(callback), method,
auth_server, ipp_endpoint);
callback = std::move(new_call);
}
class AuthorizationZonesManagerImpl
: public AuthorizationZonesManager,
private ProfileAuthServersSyncBridge::Observer {
public:
explicit AuthorizationZonesManagerImpl(Profile* profile)
: client_ids_database_(ClientIdsDatabase::Create()),
sync_bridge_(ProfileAuthServersSyncBridge::Create(
this,
DataTypeStoreServiceFactory::GetForProfile(profile)
->GetStoreFactory())),
url_loader_factory_(profile->GetURLLoaderFactory()),
auth_zone_creator_(base::BindRepeating(AuthorizationZone::Create,
url_loader_factory_)) {}
// Constructor for testing.
AuthorizationZonesManagerImpl(
Profile* profile,
CreateAuthZoneCallback auth_zone_creator,
std::unique_ptr<ClientIdsDatabase> client_ids_database,
std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
syncer::OnceDataTypeStoreFactory store_factory)
: client_ids_database_(std::move(client_ids_database)),
sync_bridge_(ProfileAuthServersSyncBridge::CreateForTesting(
this,
std::move(change_processor),
std::move(store_factory))),
url_loader_factory_(profile->GetURLLoaderFactory()),
auth_zone_creator_(std::move(auth_zone_creator)) {}
StatusCode SaveAuthorizationServerAsTrusted(
const GURL& auth_server) override {
if (!auth_server.is_valid() || !auth_server.SchemeIs("https") ||
!auth_server.has_host() || auth_server.has_username() ||
auth_server.has_query() || auth_server.has_ref()) {
PRINTER_LOG(USER) << LogEntry("", __func__, auth_server,
StatusCode::kInvalidURL);
return StatusCode::kInvalidURL;
}
std::unique_ptr<AuthorizationZone> auth_zone =
auth_zone_creator_.Run(auth_server, client_ids_database_.get());
if (sync_bridge_->IsInitialized()) {
if (!base::Contains(servers_, auth_server)) {
servers_.emplace(auth_server, std::move(auth_zone));
sync_bridge_->AddAuthorizationServer(auth_server);
}
} else {
if (!base::Contains(waiting_servers_, auth_server)) {
waiting_servers_[auth_server].server = std::move(auth_zone);
}
}
PRINTER_LOG(USER) << LogEntry("", __func__, auth_server, StatusCode::kOK);
return StatusCode::kOK;
}
void InitAuthorization(const GURL& auth_server,
const std::string& scope,
StatusCallback callback) override {
PRINTER_LOG(USER) << LogEntry("scope=" + scope, __func__, auth_server);
AddLoggingToCallback(callback, __func__, auth_server);
AuthorizationZone* zone = GetAuthorizationZone(auth_server);
if (!zone) {
auto it = waiting_servers_.find(auth_server);
if (it == waiting_servers_.end()) {
std::move(callback).Run(StatusCode::kUntrustedAuthorizationServer, "");
} else {
it->second.init_calls.emplace_back(
InitAuthorizationCall{scope, std::move(callback)});
}
return;
}
zone->InitAuthorization(scope, std::move(callback));
}
void FinishAuthorization(const GURL& auth_server,
const GURL& redirect_url,
StatusCallback callback) override {
PRINTER_LOG(USER) << LogEntry("", __func__, auth_server);
AddLoggingToCallback(callback, __func__, auth_server);
AuthorizationZone* zone = GetAuthorizationZone(auth_server);
if (!zone) {
const StatusCode code = base::Contains(waiting_servers_, auth_server)
? StatusCode::kAuthorizationNeeded
: StatusCode::kUntrustedAuthorizationServer;
std::move(callback).Run(code, "");
return;
}
zone->FinishAuthorization(redirect_url, std::move(callback));
}
void GetEndpointAccessToken(const GURL& auth_server,
const chromeos::Uri& ipp_endpoint,
const std::string& scope,
StatusCallback callback) override {
PRINTER_LOG(USER) << LogEntry("scope=" + scope, __func__, auth_server,
std::nullopt, ipp_endpoint);
AddLoggingToCallback(callback, __func__, auth_server, ipp_endpoint);
AuthorizationZone* zone = GetAuthorizationZone(auth_server);
if (!zone) {
const StatusCode code = base::Contains(waiting_servers_, auth_server)
? StatusCode::kAuthorizationNeeded
: StatusCode::kUntrustedAuthorizationServer;
std::move(callback).Run(code, "");
return;
}
zone->GetEndpointAccessToken(ipp_endpoint, scope, std::move(callback));
}
void MarkEndpointAccessTokenAsExpired(
const GURL& auth_server,
const chromeos::Uri& ipp_endpoint,
const std::string& endpoint_access_token) override {
AuthorizationZone* zone = GetAuthorizationZone(auth_server);
PRINTER_LOG(EVENT) << LogEntry(
"", __func__, auth_server,
zone ? StatusCode::kOK : StatusCode::kUntrustedAuthorizationServer,
ipp_endpoint);
if (zone) {
zone->MarkEndpointAccessTokenAsExpired(ipp_endpoint,
endpoint_access_token);
}
}
private:
struct InitAuthorizationCall {
std::string scope;
StatusCallback callback;
};
struct WaitingServer {
std::unique_ptr<AuthorizationZone> server;
std::vector<InitAuthorizationCall> init_calls;
};
// Returns a pointer to the corresponding element in `servers_` or nullptr if
// `auth_server` is untrusted.
AuthorizationZone* GetAuthorizationZone(const GURL& auth_server) {
auto it_server = servers_.find(auth_server);
if (it_server == servers_.end()) {
return nullptr;
}
return it_server->second.get();
}
syncer::DataTypeSyncBridge* GetDataTypeSyncBridge() override {
return sync_bridge_.get();
}
void OnProfileAuthorizationServersInitialized() override {
for (auto& [url, ws] : waiting_servers_) {
auto [it, created] = servers_.emplace(url, std::move(ws.server));
if (created) {
sync_bridge_->AddAuthorizationServer(url);
}
for (InitAuthorizationCall& iac : ws.init_calls) {
it->second->InitAuthorization(iac.scope, std::move(iac.callback));
}
}
waiting_servers_.clear();
}
void OnProfileAuthorizationServersUpdate(std::set<GURL> added,
std::set<GURL> deleted) override {
for (const GURL& url : deleted) {
auto it = servers_.find(url);
if (it == servers_.end()) {
continue;
}
// First, we have to remove the AuthorizationZone from `servers_` to make
// sure it is not accessed in any callbacks returned by
// MarkAuthorizationZoneAsUntrusted().
std::unique_ptr<AuthorizationZone> auth_zone = std::move(it->second);
servers_.erase(it);
auth_zone->MarkAuthorizationZoneAsUntrusted();
}
for (const GURL& url : added) {
if (!base::Contains(servers_, url)) {
servers_.emplace(
url, auth_zone_creator_.Run(url, client_ids_database_.get()));
}
}
}
// Must live longer than all instances of AuthorizationZone
// (`waiting_servers_` and `servers_`).
std::unique_ptr<ClientIdsDatabase> client_ids_database_;
std::map<GURL, WaitingServer> waiting_servers_;
std::unique_ptr<ProfileAuthServersSyncBridge> sync_bridge_;
std::map<GURL, std::unique_ptr<AuthorizationZone>> servers_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
CreateAuthZoneCallback auth_zone_creator_;
};
} // namespace
std::unique_ptr<AuthorizationZonesManager> AuthorizationZonesManager::Create(
Profile* profile) {
DCHECK(profile);
return std::make_unique<AuthorizationZonesManagerImpl>(profile);
}
std::unique_ptr<AuthorizationZonesManager>
AuthorizationZonesManager::CreateForTesting(
Profile* profile,
CreateAuthZoneCallback auth_zone_creator,
std::unique_ptr<ClientIdsDatabase> client_ids_database,
std::unique_ptr<syncer::DataTypeLocalChangeProcessor> change_processor,
syncer::OnceDataTypeStoreFactory store_factory) {
DCHECK(profile);
return std::make_unique<AuthorizationZonesManagerImpl>(
profile, std::move(auth_zone_creator), std::move(client_ids_database),
std::move(change_processor), std::move(store_factory));
}
AuthorizationZonesManager::~AuthorizationZonesManager() = default;
AuthorizationZonesManager::AuthorizationZonesManager() = default;
} // namespace ash::printing::oauth2
|