| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/validating_authenticator.h" | 5 #include "remoting/protocol/validating_authenticator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 ValidatingAuthenticator::ValidatingAuthenticator( | 24 ValidatingAuthenticator::ValidatingAuthenticator( |
| 25 const std::string& remote_jid, | 25 const std::string& remote_jid, |
| 26 const ValidationCallback& validation_callback, | 26 const ValidationCallback& validation_callback, |
| 27 std::unique_ptr<Authenticator> current_authenticator) | 27 std::unique_ptr<Authenticator> current_authenticator) |
| 28 : remote_jid_(remote_jid), | 28 : remote_jid_(remote_jid), |
| 29 validation_callback_(validation_callback), | 29 validation_callback_(validation_callback), |
| 30 current_authenticator_(std::move(current_authenticator)), | 30 current_authenticator_(std::move(current_authenticator)), |
| 31 weak_factory_(this) { | 31 weak_factory_(this) { |
| 32 DCHECK(!remote_jid_.empty()); | 32 DCHECK(!remote_jid_.empty()); |
| 33 DCHECK(!validation_callback_.is_null()); | 33 DCHECK(validation_callback_); |
| 34 DCHECK(current_authenticator_); | 34 DCHECK(current_authenticator_); |
| 35 } | 35 } |
| 36 | 36 |
| 37 ValidatingAuthenticator::~ValidatingAuthenticator() {} | 37 ValidatingAuthenticator::~ValidatingAuthenticator() {} |
| 38 | 38 |
| 39 Authenticator::State ValidatingAuthenticator::state() const { | 39 Authenticator::State ValidatingAuthenticator::state() const { |
| 40 return state_; | 40 return pending_auth_message_ ? MESSAGE_READY : state_; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool ValidatingAuthenticator::started() const { | 43 bool ValidatingAuthenticator::started() const { |
| 44 return current_authenticator_->started(); | 44 return current_authenticator_->started(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() | 47 Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() |
| 48 const { | 48 const { |
| 49 return rejection_reason_; | 49 return rejection_reason_; |
| 50 } | 50 } |
| 51 | 51 |
| 52 const std::string& ValidatingAuthenticator::GetAuthKey() const { | 52 const std::string& ValidatingAuthenticator::GetAuthKey() const { |
| 53 return current_authenticator_->GetAuthKey(); | 53 return current_authenticator_->GetAuthKey(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 std::unique_ptr<ChannelAuthenticator> | 56 std::unique_ptr<ChannelAuthenticator> |
| 57 ValidatingAuthenticator::CreateChannelAuthenticator() const { | 57 ValidatingAuthenticator::CreateChannelAuthenticator() const { |
| 58 return current_authenticator_->CreateChannelAuthenticator(); | 58 return current_authenticator_->CreateChannelAuthenticator(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void ValidatingAuthenticator::ProcessMessage( | 61 void ValidatingAuthenticator::ProcessMessage( |
| 62 const buzz::XmlElement* message, | 62 const buzz::XmlElement* message, |
| 63 const base::Closure& resume_callback) { | 63 const base::Closure& resume_callback) { |
| 64 DCHECK_EQ(state_, WAITING_MESSAGE); | 64 DCHECK_EQ(state_, WAITING_MESSAGE); |
| 65 state_ = PROCESSING_MESSAGE; | 65 state_ = PROCESSING_MESSAGE; |
| 66 | 66 |
| 67 if (first_message_received_) { | 67 current_authenticator_->ProcessMessage( |
| 68 current_authenticator_->ProcessMessage( | 68 message, base::Bind(&ValidatingAuthenticator::UpdateState, |
| 69 message, base::Bind(&ValidatingAuthenticator::UpdateState, | 69 weak_factory_.GetWeakPtr(), resume_callback)); |
| 70 weak_factory_.GetWeakPtr(), resume_callback)); | |
| 71 } else { | |
| 72 first_message_received_ = true; | |
| 73 validation_callback_.Run( | |
| 74 remote_jid_, base::Bind(&ValidatingAuthenticator::OnValidateComplete, | |
| 75 weak_factory_.GetWeakPtr(), | |
| 76 base::Owned(new buzz::XmlElement(*message)), | |
| 77 resume_callback)); | |
| 78 } | |
| 79 } | 70 } |
| 80 | 71 |
| 81 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { | 72 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { |
| 73 if (pending_auth_message_) { |
| 74 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
| 75 return std::move(pending_auth_message_); |
| 76 } |
| 77 |
| 82 std::unique_ptr<buzz::XmlElement> result( | 78 std::unique_ptr<buzz::XmlElement> result( |
| 83 current_authenticator_->GetNextMessage()); | 79 current_authenticator_->GetNextMessage()); |
| 84 state_ = current_authenticator_->state(); | 80 state_ = current_authenticator_->state(); |
| 85 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); | 81 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
| 86 | 82 |
| 87 return result; | 83 return result; |
| 88 } | 84 } |
| 89 | 85 |
| 90 void ValidatingAuthenticator::OnValidateComplete( | 86 void ValidatingAuthenticator::OnValidateComplete(const base::Closure& callback, |
| 91 const buzz::XmlElement* message, | 87 Result validation_result) { |
| 92 const base::Closure& resume_callback, | 88 // Map |rejection_reason_| to a known reason, set |state_| to REJECTED and |
| 93 Result validation_result) { | 89 // notify the listener of the connection error via the callback. |
| 94 // Process the original message in the success case, otherwise map | |
| 95 // |rejection_reason_| to a known reason, set |state_| to REJECTED and notify | |
| 96 // the listener of the connection error via the callback. | |
| 97 switch (validation_result) { | 90 switch (validation_result) { |
| 98 case Result::SUCCESS: | 91 case Result::SUCCESS: |
| 99 current_authenticator_->ProcessMessage( | 92 state_ = ACCEPTED; |
| 100 message, base::Bind(&ValidatingAuthenticator::UpdateState, | 93 callback.Run(); |
| 101 weak_factory_.GetWeakPtr(), resume_callback)); | |
| 102 return; | 94 return; |
| 103 | 95 |
| 104 case Result::ERROR_INVALID_CREDENTIALS: | 96 case Result::ERROR_INVALID_CREDENTIALS: |
| 105 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; | 97 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; |
| 106 break; | 98 break; |
| 107 | 99 |
| 108 case Result::ERROR_INVALID_ACCOUNT: | 100 case Result::ERROR_INVALID_ACCOUNT: |
| 109 rejection_reason_ = Authenticator::INVALID_ACCOUNT; | 101 rejection_reason_ = Authenticator::INVALID_ACCOUNT; |
| 110 break; | 102 break; |
| 111 | 103 |
| 104 case Result::ERROR_TOO_MANY_CONNECTIONS: |
| 105 rejection_reason_ = Authenticator::TOO_MANY_CONNECTIONS; |
| 106 break; |
| 107 |
| 112 case Result::ERROR_REJECTED_BY_USER: | 108 case Result::ERROR_REJECTED_BY_USER: |
| 113 rejection_reason_ = Authenticator::REJECTED_BY_USER; | 109 rejection_reason_ = Authenticator::REJECTED_BY_USER; |
| 114 break; | 110 break; |
| 115 } | 111 } |
| 116 | 112 |
| 117 state_ = Authenticator::REJECTED; | 113 state_ = Authenticator::REJECTED; |
| 118 resume_callback.Run(); | 114 callback.Run(); |
| 119 } | 115 } |
| 120 | 116 |
| 121 void ValidatingAuthenticator::UpdateState( | 117 void ValidatingAuthenticator::UpdateState( |
| 122 const base::Closure& resume_callback) { | 118 const base::Closure& resume_callback) { |
| 123 DCHECK_EQ(state_, PROCESSING_MESSAGE); | 119 DCHECK_EQ(state_, PROCESSING_MESSAGE); |
| 124 | 120 |
| 125 // Update our current state before running |resume_callback|. | 121 // Update our current state before running |resume_callback|. |
| 126 state_ = current_authenticator_->state(); | 122 state_ = current_authenticator_->state(); |
| 127 if (state_ == REJECTED) { | 123 if (state_ == REJECTED) { |
| 128 rejection_reason_ = current_authenticator_->rejection_reason(); | 124 rejection_reason_ = current_authenticator_->rejection_reason(); |
| 125 } else if (state_ == MESSAGE_READY) { |
| 126 DCHECK(!pending_auth_message_); |
| 127 pending_auth_message_ = current_authenticator_->GetNextMessage(); |
| 128 state_ = current_authenticator_->state(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 resume_callback.Run(); | 131 if (state_ == ACCEPTED) { |
| 132 state_ = PROCESSING_MESSAGE; |
| 133 validation_callback_.Run( |
| 134 remote_jid_, base::Bind(&ValidatingAuthenticator::OnValidateComplete, |
| 135 weak_factory_.GetWeakPtr(), resume_callback)); |
| 136 } else { |
| 137 resume_callback.Run(); |
| 138 } |
| 132 } | 139 } |
| 133 | 140 |
| 134 } // namespace protocol | 141 } // namespace protocol |
| 135 } // namespace remoting | 142 } // namespace remoting |
| OLD | NEW |