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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/time/time.h"
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "remoting/test/session_authz_playground.h"
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string_view>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "remoting/base/certificate_helpers.h"
#include "remoting/base/oauth_token_getter.h"
#include "remoting/base/oauth_token_getter_impl.h"
#include "remoting/base/url_request_context_getter.h"
#include "remoting/host/host_config.h"
#include "remoting/proto/session_authz_service.h"
#include "remoting/test/cli_util.h"
namespace remoting {
namespace {
constexpr char kOAuthScope[] =
"https://www.googleapis.com/auth/chromoting.me2me.host";
void PrintErrorAndExit(const std::string& api_name, const HttpStatus& status) {
fprintf(stderr, "Failed to call API: %s, error code: %d\n", api_name.data(),
static_cast<int>(status.error_code()));
fprintf(stderr, "%s\n", status.error_message().data());
fprintf(stderr, "%s\n", status.response_body().data());
std::exit(1);
}
} // namespace
SessionAuthzPlayground::SessionAuthzPlayground() {
auto url_request_context_getter =
base::MakeRefCounted<URLRequestContextGetter>(
base::SingleThreadTaskRunner::GetCurrentDefault());
url_loader_factory_owner_ =
std::make_unique<network::TransitionalURLLoaderFactoryOwner>(
url_request_context_getter, /* is_trusted= */ true);
}
SessionAuthzPlayground::~SessionAuthzPlayground() = default;
void SessionAuthzPlayground::Start() {
auto* cmd_line = base::CommandLine::ForCurrentProcess();
base::CommandLine::StringVector args = cmd_line->GetArgs();
base::FilePath host_config_file_path;
if (args.size() == 1) {
host_config_file_path = base::FilePath(args[0]);
}
if (host_config_file_path.empty()) {
printf("Usage: %s <path-to-host-config-json>\n",
cmd_line->GetProgram().MaybeAsASCII().c_str());
std::exit(1);
}
service_client_ = std::make_unique<CorpSessionAuthzServiceClient>(
url_loader_factory_owner_->GetURLLoaderFactory(),
CreateClientCertStoreInstance(),
CreateOAuthTokenGetter(host_config_file_path),
/* support_id= */ std::string_view());
run_loop_ = std::make_unique<base::RunLoop>();
GenerateHostToken();
run_loop_->Run();
}
void SessionAuthzPlayground::GenerateHostToken() {
printf("Fetching host token...\n");
service_client_->GenerateHostToken(
base::BindOnce(
[](const HttpStatus& status,
std::unique_ptr<internal::GenerateHostTokenResponseStruct>
response) {
if (!status.ok()) {
PrintErrorAndExit("GenerateHostToken", status);
}
if (!response) {
fprintf(stderr, "No response was received.\n");
}
printf("Host token: %s\n", response->host_token.data());
printf("Session ID: %s\n", response->session_id.data());
return response->session_id;
})
.Then(base::BindPostTaskToCurrentDefault(
base::BindOnce(&SessionAuthzPlayground::VerifySessionToken,
base::Unretained(this)))));
}
void SessionAuthzPlayground::VerifySessionToken(const std::string& session_id) {
printf("Enter session token generated by the client: ");
std::string session_token = test::ReadString();
service_client_->VerifySessionToken(
session_token,
base::BindOnce(
[](const std::string& session_id, const HttpStatus& status,
std::unique_ptr<internal::VerifySessionTokenResponseStruct>
response) {
if (!status.ok()) {
PrintErrorAndExit("VerifySessionToken", status);
}
if (!response) {
fprintf(stderr, "No response was received.\n");
}
printf("Reauth token: %s\n", response->session_reauth_token.data());
printf("Reauth token expires in: %fs\n",
response->session_reauth_token_lifetime.InSecondsF());
printf("Session ID: %s\n", response->session_id.data());
printf("Shared secret: %s\n", response->shared_secret.data());
if (session_id != response->session_id) {
fprintf(stderr, "Unexpected session ID. Expected: %s\n",
session_id.data());
std::exit(1);
}
return response->session_reauth_token;
},
session_id)
.Then(base::BindPostTaskToCurrentDefault(
base::BindPostTaskToCurrentDefault(
base::BindOnce(&SessionAuthzPlayground::ReauthorizeHost,
base::Unretained(this), session_id)))));
}
void SessionAuthzPlayground::ReauthorizeHost(const std::string& session_id,
const std::string& reauth_token) {
printf("Reauthorize host? [y/N]: ");
bool shouldReauthorize = test::ReadYNBool();
if (!shouldReauthorize) {
run_loop_->Quit();
}
service_client_->ReauthorizeHost(
reauth_token, session_id, base::TimeTicks::Max(),
base::BindOnce([](const HttpStatus& status,
std::unique_ptr<internal::ReauthorizeHostResponseStruct>
response) {
if (!status.ok()) {
PrintErrorAndExit("ReauthorizeHost", status);
}
if (!response) {
fprintf(stderr, "No response was received.\n");
}
printf("Reauth token: %s\n", response->session_reauth_token.data());
printf("Reauth token expires in: %fs\n",
response->session_reauth_token_lifetime.InSecondsF());
return response->session_reauth_token;
})
.Then(base::BindPostTaskToCurrentDefault(
base::BindOnce(&SessionAuthzPlayground::ReauthorizeHost,
base::Unretained(this), session_id))));
}
std::unique_ptr<OAuthTokenGetter>
SessionAuthzPlayground::CreateOAuthTokenGetter(
const base::FilePath& host_config_file_path) {
auto host_config_dict = HostConfigFromJsonFile(host_config_file_path);
if (!host_config_dict.has_value()) {
fprintf(stderr, "Cannot read host config file: %s\n",
host_config_file_path.MaybeAsASCII().data());
std::exit(1);
}
const std::string* refresh_token =
host_config_dict->FindString(kOAuthRefreshTokenConfigPath);
if (!refresh_token) {
fprintf(stderr, "Cannot find OAuth refresh token from host config file.\n");
std::exit(1);
}
const std::string* service_account_email =
host_config_dict->FindString(kServiceAccountConfigPath);
if (!service_account_email) {
fprintf(stderr, "Cannot find service account email.\n");
std::exit(1);
}
auto oauth_credentials =
std::make_unique<OAuthTokenGetter::OAuthAuthorizationCredentials>(
*service_account_email, *refresh_token,
/* is_service_account= */ true,
std::vector<std::string>{kOAuthScope});
return std::make_unique<OAuthTokenGetterImpl>(
std::move(oauth_credentials),
url_loader_factory_owner_->GetURLLoaderFactory(), false);
}
} // namespace remoting
|