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
|
// 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 "remoting/protocol/session_authz_authenticator.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "remoting/base/http_status.h"
#include "remoting/base/protobuf_http_request_config.h"
#include "remoting/base/session_authz_service_client.h"
#include "remoting/proto/session_authz_service.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/credentials_type.h"
#include "remoting/protocol/session_authz_reauthorizer.h"
namespace remoting::protocol {
namespace {
Authenticator::RejectionReason ToRejectionReason(
HttpStatus::Code status_code,
Authenticator::RejectionReason permission_denied_reason) {
switch (status_code) {
case HttpStatus::Code::PERMISSION_DENIED:
return permission_denied_reason;
case HttpStatus::Code::UNAUTHENTICATED:
return Authenticator::RejectionReason::INVALID_CREDENTIALS;
case HttpStatus::Code::RESOURCE_EXHAUSTED:
return Authenticator::RejectionReason::TOO_MANY_CONNECTIONS;
case HttpStatus::Code::NETWORK_ERROR:
return Authenticator::RejectionReason::NETWORK_FAILURE;
default:
return Authenticator::RejectionReason::UNEXPECTED_ERROR;
}
}
} // namespace
SessionAuthzAuthenticator::SessionAuthzAuthenticator(
CredentialsType credentials_type,
std::unique_ptr<SessionAuthzServiceClient> service_client,
const CreateBaseAuthenticatorCallback& create_base_authenticator_callback)
: credentials_type_(credentials_type),
service_client_(std::move(service_client)),
create_base_authenticator_callback_(create_base_authenticator_callback) {
DCHECK(credentials_type == CredentialsType::CLOUD_SESSION_AUTHZ ||
credentials_type == CredentialsType::CORP_SESSION_AUTHZ);
}
SessionAuthzAuthenticator::~SessionAuthzAuthenticator() = default;
void SessionAuthzAuthenticator::Start(base::OnceClosure resume_callback) {
GenerateHostToken(std::move(resume_callback));
}
CredentialsType SessionAuthzAuthenticator::credentials_type() const {
return credentials_type_;
}
const Authenticator& SessionAuthzAuthenticator::implementing_authenticator()
const {
return *this;
}
Authenticator::State SessionAuthzAuthenticator::state() const {
switch (session_authz_state_) {
case SessionAuthzState::NOT_STARTED:
case SessionAuthzState::WAITING_FOR_SESSION_TOKEN:
return WAITING_MESSAGE;
case SessionAuthzState::GENERATING_HOST_TOKEN:
case SessionAuthzState::VERIFYING_SESSION_TOKEN:
return PROCESSING_MESSAGE;
case SessionAuthzState::READY_TO_SEND_HOST_TOKEN:
return MESSAGE_READY;
case SessionAuthzState::SHARED_SECRET_FETCHED:
return underlying_->state();
case SessionAuthzState::FAILED:
return REJECTED;
}
}
bool SessionAuthzAuthenticator::started() const {
return session_authz_state_ != SessionAuthzState::NOT_STARTED;
}
Authenticator::RejectionReason SessionAuthzAuthenticator::rejection_reason()
const {
DCHECK_EQ(state(), REJECTED);
if (session_authz_state_ == SessionAuthzState::FAILED) {
return session_authz_rejection_reason_;
}
return underlying_->rejection_reason();
}
Authenticator::RejectionDetails SessionAuthzAuthenticator::rejection_details()
const {
DCHECK_EQ(state(), REJECTED);
if (session_authz_state_ == SessionAuthzState::FAILED) {
return rejection_details_;
}
return underlying_->rejection_details();
}
void SessionAuthzAuthenticator::ProcessMessage(
const jingle_xmpp::XmlElement* message,
base::OnceClosure resume_callback) {
DCHECK_EQ(state(), WAITING_MESSAGE);
switch (session_authz_state_) {
case SessionAuthzState::WAITING_FOR_SESSION_TOKEN:
VerifySessionToken(*message, std::move(resume_callback));
break;
case SessionAuthzState::SHARED_SECRET_FETCHED:
DCHECK_EQ(underlying_->state(), WAITING_MESSAGE);
underlying_->ProcessMessage(message, std::move(resume_callback));
StartReauthorizerIfNecessary();
break;
default:
NOTREACHED() << "Unexpected SessionAuthz state: "
<< static_cast<int>(session_authz_state_);
}
}
std::unique_ptr<jingle_xmpp::XmlElement>
SessionAuthzAuthenticator::GetNextMessage() {
DCHECK_EQ(state(), MESSAGE_READY);
std::unique_ptr<jingle_xmpp::XmlElement> message;
if (underlying_ && underlying_->state() == MESSAGE_READY) {
message = underlying_->GetNextMessage();
StartReauthorizerIfNecessary();
} else {
message = CreateEmptyAuthenticatorMessage();
}
if (session_authz_state_ == SessionAuthzState::READY_TO_SEND_HOST_TOKEN) {
AddHostTokenElement(message.get());
}
return message;
}
const std::string& SessionAuthzAuthenticator::GetAuthKey() const {
DCHECK_EQ(state(), ACCEPTED);
return underlying_->GetAuthKey();
}
const SessionPolicies* SessionAuthzAuthenticator::GetSessionPolicies() const {
DCHECK_EQ(state(), ACCEPTED);
return session_policies_.has_value() ? &session_policies_.value() : nullptr;
}
std::unique_ptr<ChannelAuthenticator>
SessionAuthzAuthenticator::CreateChannelAuthenticator() const {
DCHECK_EQ(state(), ACCEPTED);
return underlying_->CreateChannelAuthenticator();
}
void SessionAuthzAuthenticator::SetReauthorizerForTesting(
std::unique_ptr<SessionAuthzReauthorizer> reauthorizer) {
reauthorizer_ = std::move(reauthorizer);
}
void SessionAuthzAuthenticator::SetSessionIdForTesting(
std::string_view session_id) {
session_id_ = session_id;
}
void SessionAuthzAuthenticator::SetHostTokenForTesting(
std::string_view host_token) {
host_token_ = host_token;
}
void SessionAuthzAuthenticator::GenerateHostToken(
base::OnceClosure resume_callback) {
session_authz_state_ = SessionAuthzState::GENERATING_HOST_TOKEN;
// Safe to use Unretained() for requests made to |service_client_|, since
// this class owns |service_client_|, which cancels requests once it gets
// deleted.
service_client_->GenerateHostToken(
base::BindOnce(&SessionAuthzAuthenticator::OnHostTokenGenerated,
base::Unretained(this), std::move(resume_callback)));
}
void SessionAuthzAuthenticator::OnHostTokenGenerated(
base::OnceClosure resume_callback,
const HttpStatus& status,
std::unique_ptr<internal::GenerateHostTokenResponseStruct> response) {
if (!status.ok()) {
HandleSessionAuthzError("GenerateHostToken", status);
std::move(resume_callback).Run();
return;
}
session_id_ = response->session_id;
host_token_ = response->host_token;
session_authz_state_ = SessionAuthzState::READY_TO_SEND_HOST_TOKEN;
std::move(resume_callback).Run();
}
void SessionAuthzAuthenticator::AddHostTokenElement(
jingle_xmpp::XmlElement* message) {
DCHECK_EQ(session_authz_state_, SessionAuthzState::READY_TO_SEND_HOST_TOKEN);
DCHECK(!host_token_.empty());
jingle_xmpp::XmlElement* host_token_element =
new jingle_xmpp::XmlElement(kHostTokenTag);
host_token_element->SetBodyText(host_token_);
message->AddElement(host_token_element);
session_authz_state_ = SessionAuthzState::WAITING_FOR_SESSION_TOKEN;
}
void SessionAuthzAuthenticator::VerifySessionToken(
const jingle_xmpp::XmlElement& message,
base::OnceClosure resume_callback) {
session_authz_state_ = SessionAuthzState::VERIFYING_SESSION_TOKEN;
std::string session_token = message.TextNamed(kSessionTokenTag);
service_client_->VerifySessionToken(
session_token,
base::BindOnce(&SessionAuthzAuthenticator::OnVerifiedSessionToken,
base::Unretained(this), message,
std::move(resume_callback)));
}
void SessionAuthzAuthenticator::OnVerifiedSessionToken(
const jingle_xmpp::XmlElement& message,
base::OnceClosure resume_callback,
const HttpStatus& status,
std::unique_ptr<internal::VerifySessionTokenResponseStruct> response) {
if (!status.ok()) {
HandleSessionAuthzError("VerifySessionToken", status);
std::move(resume_callback).Run();
return;
}
if (response->session_id != session_id_) {
session_authz_state_ = SessionAuthzState::FAILED;
session_authz_rejection_reason_ = RejectionReason::INVALID_ACCOUNT_ID;
rejection_details_ = RejectionDetails(base::StringPrintf(
"Session token verification failed. Expected session ID: %s, actual: "
"%s",
session_id_, response->session_id));
std::move(resume_callback).Run();
return;
}
session_authz_state_ = SessionAuthzState::SHARED_SECRET_FETCHED;
// The other side already started the SPAKE authentication.
underlying_ = create_base_authenticator_callback_.Run(response->shared_secret,
WAITING_MESSAGE);
session_policies_ = std::move(response->session_policies);
verify_token_response_ = std::move(response);
underlying_->ProcessMessage(&message, std::move(resume_callback));
StartReauthorizerIfNecessary();
}
void SessionAuthzAuthenticator::HandleSessionAuthzError(
const std::string_view& action_name,
const HttpStatus& status) {
DCHECK(!status.ok());
rejection_details_ = RejectionDetails(base::StringPrintf(
"SessionAuthz %s error, code: %d, message: %s", action_name,
static_cast<int>(status.error_code()), status.error_message()));
session_authz_state_ = SessionAuthzState::FAILED;
session_authz_rejection_reason_ = ToRejectionReason(
status.error_code(), RejectionReason::AUTHZ_POLICY_CHECK_FAILED);
}
void SessionAuthzAuthenticator::StartReauthorizerIfNecessary() {
if (reauthorizer_) {
return;
}
if (!underlying_ || underlying_->state() != ACCEPTED) {
return;
}
DCHECK(verify_token_response_);
reauthorizer_ = std::make_unique<SessionAuthzReauthorizer>(
service_client_.get(), verify_token_response_->session_id,
verify_token_response_->session_reauth_token,
verify_token_response_->session_reauth_token_lifetime,
base::BindOnce(&SessionAuthzAuthenticator::OnReauthorizationFailed,
base::Unretained(this)));
reauthorizer_->Start();
verify_token_response_.reset();
}
void SessionAuthzAuthenticator::OnReauthorizationFailed(
HttpStatus::Code error_code,
const Authenticator::RejectionDetails& details) {
session_authz_state_ = SessionAuthzState::FAILED;
session_authz_rejection_reason_ = ToRejectionReason(
error_code, RejectionReason::REAUTHZ_POLICY_CHECK_FAILED);
rejection_details_ = details;
reauthorizer_.reset();
NotifyStateChangeAfterAccepted();
}
} // namespace remoting::protocol
|