joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/protocol/validating_authenticator.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include "base/bind.h" |
| 12 | #include "base/callback.h" |
Hans Wennborg | 828a097 | 2020-04-27 18:10:09 | [diff] [blame] | 13 | #include "base/check_op.h" |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "remoting/protocol/authenticator.h" |
| 17 | #include "remoting/protocol/channel_authenticator.h" |
kjellander | f0e410b | 2017-01-04 14:45:01 | [diff] [blame] | 18 | #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 19 | |
| 20 | namespace remoting { |
| 21 | namespace protocol { |
| 22 | |
| 23 | ValidatingAuthenticator::ValidatingAuthenticator( |
| 24 | const std::string& remote_jid, |
Joe Downing | d281990 | 2020-04-27 23:38:46 | [diff] [blame] | 25 | const ValidationCallback& validation_callback, |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 26 | std::unique_ptr<Authenticator> current_authenticator) |
| 27 | : remote_jid_(remote_jid), |
Joe Downing | d281990 | 2020-04-27 23:38:46 | [diff] [blame] | 28 | validation_callback_(validation_callback), |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 29 | current_authenticator_(std::move(current_authenticator)) { |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 30 | DCHECK(!remote_jid_.empty()); |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 31 | DCHECK(validation_callback_); |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 32 | DCHECK(current_authenticator_); |
| 33 | } |
| 34 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 35 | ValidatingAuthenticator::~ValidatingAuthenticator() = default; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 36 | |
| 37 | Authenticator::State ValidatingAuthenticator::state() const { |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 38 | return pending_auth_message_ ? MESSAGE_READY : state_; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool ValidatingAuthenticator::started() const { |
| 42 | return current_authenticator_->started(); |
| 43 | } |
| 44 | |
| 45 | Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() |
| 46 | const { |
| 47 | return rejection_reason_; |
| 48 | } |
| 49 | |
| 50 | const std::string& ValidatingAuthenticator::GetAuthKey() const { |
| 51 | return current_authenticator_->GetAuthKey(); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<ChannelAuthenticator> |
| 55 | ValidatingAuthenticator::CreateChannelAuthenticator() const { |
| 56 | return current_authenticator_->CreateChannelAuthenticator(); |
| 57 | } |
| 58 | |
| 59 | void ValidatingAuthenticator::ProcessMessage( |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 60 | const jingle_xmpp::XmlElement* message, |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 61 | base::OnceClosure resume_callback) { |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 62 | DCHECK_EQ(state_, WAITING_MESSAGE); |
| 63 | state_ = PROCESSING_MESSAGE; |
| 64 | |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 65 | current_authenticator_->ProcessMessage( |
Daniel Cheng | 28a5ac48 | 2021-02-22 21:03:08 | [diff] [blame] | 66 | message, |
| 67 | base::BindOnce(&ValidatingAuthenticator::UpdateState, |
| 68 | weak_factory_.GetWeakPtr(), std::move(resume_callback))); |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 69 | } |
| 70 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 71 | std::unique_ptr<jingle_xmpp::XmlElement> ValidatingAuthenticator::GetNextMessage() { |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 72 | if (pending_auth_message_) { |
| 73 | DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
| 74 | return std::move(pending_auth_message_); |
| 75 | } |
| 76 | |
Mirko Bonadei | 80d1cea | 2019-01-18 22:22:17 | [diff] [blame] | 77 | std::unique_ptr<jingle_xmpp::XmlElement> result( |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 78 | current_authenticator_->GetNextMessage()); |
| 79 | state_ = current_authenticator_->state(); |
| 80 | DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
| 81 | |
| 82 | return result; |
| 83 | } |
| 84 | |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 85 | void ValidatingAuthenticator::OnValidateComplete(base::OnceClosure callback, |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 86 | Result validation_result) { |
| 87 | // Map |rejection_reason_| to a known reason, set |state_| to REJECTED and |
| 88 | // notify the listener of the connection error via the callback. |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 89 | switch (validation_result) { |
joedow | 5e393214 | 2016-09-03 00:30:34 | [diff] [blame] | 90 | case Result::SUCCESS: |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 91 | state_ = ACCEPTED; |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 92 | std::move(callback).Run(); |
joedow | 5e393214 | 2016-09-03 00:30:34 | [diff] [blame] | 93 | return; |
| 94 | |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 95 | case Result::ERROR_INVALID_CREDENTIALS: |
Lei Zhang | 8dea471 | 2022-07-22 16:28:25 | [diff] [blame] | 96 | rejection_reason_ = RejectionReason::INVALID_CREDENTIALS; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 97 | break; |
| 98 | |
| 99 | case Result::ERROR_INVALID_ACCOUNT: |
Lei Zhang | 8dea471 | 2022-07-22 16:28:25 | [diff] [blame] | 100 | rejection_reason_ = RejectionReason::INVALID_ACCOUNT_ID; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 101 | break; |
| 102 | |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 103 | case Result::ERROR_TOO_MANY_CONNECTIONS: |
Lei Zhang | 8dea471 | 2022-07-22 16:28:25 | [diff] [blame] | 104 | rejection_reason_ = RejectionReason::TOO_MANY_CONNECTIONS; |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 105 | break; |
| 106 | |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 107 | case Result::ERROR_REJECTED_BY_USER: |
Lei Zhang | 8dea471 | 2022-07-22 16:28:25 | [diff] [blame] | 108 | rejection_reason_ = RejectionReason::REJECTED_BY_USER; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 109 | break; |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 110 | } |
| 111 | |
joedow | 5e393214 | 2016-09-03 00:30:34 | [diff] [blame] | 112 | state_ = Authenticator::REJECTED; |
Joe Downing | c6696f2 | 2021-07-16 01:20:55 | [diff] [blame] | 113 | |
| 114 | // Clear the pending message so the signal strategy will generate a new |
| 115 | // SESSION_REJECT message in response to this state change. |
| 116 | pending_auth_message_.reset(); |
| 117 | |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 118 | std::move(callback).Run(); |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 119 | } |
| 120 | |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 121 | void ValidatingAuthenticator::UpdateState(base::OnceClosure resume_callback) { |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 122 | DCHECK_EQ(state_, PROCESSING_MESSAGE); |
| 123 | |
| 124 | // Update our current state before running |resume_callback|. |
| 125 | state_ = current_authenticator_->state(); |
| 126 | if (state_ == REJECTED) { |
| 127 | rejection_reason_ = current_authenticator_->rejection_reason(); |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 128 | } else if (state_ == MESSAGE_READY) { |
| 129 | DCHECK(!pending_auth_message_); |
| 130 | pending_auth_message_ = current_authenticator_->GetNextMessage(); |
| 131 | state_ = current_authenticator_->state(); |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 132 | } |
| 133 | |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 134 | if (state_ == ACCEPTED) { |
| 135 | state_ = PROCESSING_MESSAGE; |
Joe Downing | d281990 | 2020-04-27 23:38:46 | [diff] [blame] | 136 | validation_callback_.Run( |
| 137 | remote_jid_, |
| 138 | base::BindOnce(&ValidatingAuthenticator::OnValidateComplete, |
Daniel Cheng | 28a5ac48 | 2021-02-22 21:03:08 | [diff] [blame] | 139 | weak_factory_.GetWeakPtr(), std::move(resume_callback))); |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 140 | } else { |
Evan Stade | ce9372b | 2020-03-12 01:28:16 | [diff] [blame] | 141 | std::move(resume_callback).Run(); |
joedow | f38c5d7 | 2017-03-20 15:55:46 | [diff] [blame] | 142 | } |
joedow | 7dc4899 | 2016-08-31 19:13:38 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace protocol |
| 146 | } // namespace remoting |