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
|
// Copyright 2016 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/validating_authenticator.h"
#include <memory>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "remoting/protocol/authenticator.h"
#include "remoting/protocol/channel_authenticator.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
namespace remoting::protocol {
ValidatingAuthenticator::ValidatingAuthenticator(
const std::string& remote_jid,
const ValidationCallback& validation_callback,
std::unique_ptr<Authenticator> current_authenticator)
: remote_jid_(remote_jid),
validation_callback_(validation_callback),
current_authenticator_(std::move(current_authenticator)) {
DCHECK(!remote_jid_.empty());
DCHECK(validation_callback_);
DCHECK(current_authenticator_);
ChainStateChangeAfterAcceptedWithUnderlying(*current_authenticator_);
}
ValidatingAuthenticator::~ValidatingAuthenticator() = default;
CredentialsType ValidatingAuthenticator::credentials_type() const {
return current_authenticator_->credentials_type();
}
const Authenticator& ValidatingAuthenticator::implementing_authenticator()
const {
return current_authenticator_->implementing_authenticator();
}
Authenticator::State ValidatingAuthenticator::state() const {
return pending_auth_message_ ? MESSAGE_READY : state_;
}
bool ValidatingAuthenticator::started() const {
return current_authenticator_->started();
}
Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason()
const {
return rejection_reason_;
}
Authenticator::RejectionDetails ValidatingAuthenticator::rejection_details()
const {
return rejection_details_;
}
const std::string& ValidatingAuthenticator::GetAuthKey() const {
return current_authenticator_->GetAuthKey();
}
const SessionPolicies* ValidatingAuthenticator::GetSessionPolicies() const {
return current_authenticator_->GetSessionPolicies();
}
std::unique_ptr<ChannelAuthenticator>
ValidatingAuthenticator::CreateChannelAuthenticator() const {
return current_authenticator_->CreateChannelAuthenticator();
}
void ValidatingAuthenticator::ProcessMessage(
const jingle_xmpp::XmlElement* message,
base::OnceClosure resume_callback) {
DCHECK_EQ(state_, WAITING_MESSAGE);
state_ = PROCESSING_MESSAGE;
current_authenticator_->ProcessMessage(
message,
base::BindOnce(&ValidatingAuthenticator::UpdateState,
weak_factory_.GetWeakPtr(), std::move(resume_callback)));
}
std::unique_ptr<jingle_xmpp::XmlElement>
ValidatingAuthenticator::GetNextMessage() {
if (pending_auth_message_) {
DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE);
return std::move(pending_auth_message_);
}
std::unique_ptr<jingle_xmpp::XmlElement> result(
current_authenticator_->GetNextMessage());
state_ = current_authenticator_->state();
DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE);
return result;
}
void ValidatingAuthenticator::OnValidateComplete(base::OnceClosure callback,
Result validation_result) {
// Map |rejection_reason_| to a known reason, set |state_| to REJECTED and
// notify the listener of the connection error via the callback.
switch (validation_result) {
case Result::SUCCESS:
state_ = ACCEPTED;
std::move(callback).Run();
return;
case Result::ERROR_INVALID_CREDENTIALS:
rejection_reason_ = RejectionReason::INVALID_CREDENTIALS;
break;
case Result::ERROR_INVALID_ACCOUNT:
rejection_reason_ = RejectionReason::INVALID_ACCOUNT_ID;
break;
case Result::ERROR_TOO_MANY_CONNECTIONS:
rejection_reason_ = RejectionReason::TOO_MANY_CONNECTIONS;
break;
case Result::ERROR_REJECTED_BY_USER:
rejection_reason_ = RejectionReason::REJECTED_BY_USER;
break;
case Result::ERROR_UNAUTHORIZED_ACCOUNT:
rejection_reason_ = RejectionReason::UNAUTHORIZED_ACCOUNT;
break;
}
state_ = Authenticator::REJECTED;
rejection_details_ = RejectionDetails("Validation failed.");
// Clear the pending message so the signal strategy will generate a new
// SESSION_REJECT message in response to this state change.
pending_auth_message_.reset();
std::move(callback).Run();
}
void ValidatingAuthenticator::UpdateState(base::OnceClosure resume_callback) {
DCHECK_EQ(state_, PROCESSING_MESSAGE);
// Update our current state before running |resume_callback|.
state_ = current_authenticator_->state();
if (state_ == REJECTED) {
rejection_reason_ = current_authenticator_->rejection_reason();
rejection_details_ = current_authenticator_->rejection_details();
} else if (state_ == MESSAGE_READY) {
DCHECK(!pending_auth_message_);
pending_auth_message_ = current_authenticator_->GetNextMessage();
state_ = current_authenticator_->state();
}
if (state_ == ACCEPTED) {
state_ = PROCESSING_MESSAGE;
validation_callback_.Run(
remote_jid_,
base::BindOnce(&ValidatingAuthenticator::OnValidateComplete,
weak_factory_.GetWeakPtr(), std::move(resume_callback)));
} else {
std::move(resume_callback).Run();
}
}
void ValidatingAuthenticator::NotifyStateChangeAfterAccepted() {
state_ = current_authenticator_->state();
if (state_ == REJECTED) {
rejection_reason_ = current_authenticator_->rejection_reason();
rejection_details_ = current_authenticator_->rejection_details();
}
Authenticator::NotifyStateChangeAfterAccepted();
}
} // namespace remoting::protocol
|