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 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "remoting/protocol/authenticator.h" | 17 #include "remoting/protocol/authenticator.h" |
18 #include "remoting/protocol/channel_authenticator.h" | 18 #include "remoting/protocol/channel_authenticator.h" |
19 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" | 19 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
20 | 20 |
21 namespace remoting { | 21 namespace remoting { |
22 namespace protocol { | 22 namespace protocol { |
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& incoming_callback, |
| 27 const ValidationCallback& accepted_callback, |
27 std::unique_ptr<Authenticator> current_authenticator) | 28 std::unique_ptr<Authenticator> current_authenticator) |
28 : remote_jid_(remote_jid), | 29 : remote_jid_(remote_jid), |
29 validation_callback_(validation_callback), | 30 incoming_callback_(incoming_callback), |
| 31 accepted_callback_(accepted_callback), |
30 current_authenticator_(std::move(current_authenticator)), | 32 current_authenticator_(std::move(current_authenticator)), |
31 weak_factory_(this) { | 33 weak_factory_(this) { |
32 DCHECK(!remote_jid_.empty()); | 34 DCHECK(!remote_jid_.empty()); |
33 DCHECK(!validation_callback_.is_null()); | 35 DCHECK(incoming_callback_); |
| 36 DCHECK(accepted_callback_); |
34 DCHECK(current_authenticator_); | 37 DCHECK(current_authenticator_); |
35 } | 38 } |
36 | 39 |
37 ValidatingAuthenticator::~ValidatingAuthenticator() {} | 40 ValidatingAuthenticator::~ValidatingAuthenticator() {} |
38 | 41 |
39 Authenticator::State ValidatingAuthenticator::state() const { | 42 Authenticator::State ValidatingAuthenticator::state() const { |
40 return state_; | 43 return pending_auth_message_ ? MESSAGE_READY : state_; |
41 } | 44 } |
42 | 45 |
43 bool ValidatingAuthenticator::started() const { | 46 bool ValidatingAuthenticator::started() const { |
44 return current_authenticator_->started(); | 47 return current_authenticator_->started(); |
45 } | 48 } |
46 | 49 |
47 Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() | 50 Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() |
48 const { | 51 const { |
49 return rejection_reason_; | 52 return rejection_reason_; |
50 } | 53 } |
(...skipping 12 matching lines...) Expand all Loading... |
63 const base::Closure& resume_callback) { | 66 const base::Closure& resume_callback) { |
64 DCHECK_EQ(state_, WAITING_MESSAGE); | 67 DCHECK_EQ(state_, WAITING_MESSAGE); |
65 state_ = PROCESSING_MESSAGE; | 68 state_ = PROCESSING_MESSAGE; |
66 | 69 |
67 if (first_message_received_) { | 70 if (first_message_received_) { |
68 current_authenticator_->ProcessMessage( | 71 current_authenticator_->ProcessMessage( |
69 message, base::Bind(&ValidatingAuthenticator::UpdateState, | 72 message, base::Bind(&ValidatingAuthenticator::UpdateState, |
70 weak_factory_.GetWeakPtr(), resume_callback)); | 73 weak_factory_.GetWeakPtr(), resume_callback)); |
71 } else { | 74 } else { |
72 first_message_received_ = true; | 75 first_message_received_ = true; |
73 validation_callback_.Run( | 76 incoming_callback_.Run( |
74 remote_jid_, base::Bind(&ValidatingAuthenticator::OnValidateComplete, | 77 remote_jid_, |
75 weak_factory_.GetWeakPtr(), | 78 base::Bind( |
76 base::Owned(new buzz::XmlElement(*message)), | 79 &ValidatingAuthenticator::OnValidateComplete, |
77 resume_callback)); | 80 weak_factory_.GetWeakPtr(), |
| 81 base::Bind(&Authenticator::ProcessMessage, |
| 82 base::Unretained(current_authenticator_.get()), |
| 83 base::Owned(new buzz::XmlElement(*message)), |
| 84 base::Bind(&ValidatingAuthenticator::UpdateState, |
| 85 weak_factory_.GetWeakPtr(), resume_callback)), |
| 86 resume_callback)); |
78 } | 87 } |
79 } | 88 } |
80 | 89 |
81 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { | 90 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { |
| 91 if (pending_auth_message_) { |
| 92 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
| 93 return std::move(pending_auth_message_); |
| 94 } |
| 95 |
82 std::unique_ptr<buzz::XmlElement> result( | 96 std::unique_ptr<buzz::XmlElement> result( |
83 current_authenticator_->GetNextMessage()); | 97 current_authenticator_->GetNextMessage()); |
84 state_ = current_authenticator_->state(); | 98 state_ = current_authenticator_->state(); |
85 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); | 99 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); |
86 | 100 |
87 return result; | 101 return result; |
88 } | 102 } |
89 | 103 |
90 void ValidatingAuthenticator::OnValidateComplete( | 104 void ValidatingAuthenticator::OnValidateComplete( |
91 const buzz::XmlElement* message, | 105 const base::Closure& success_callback, |
92 const base::Closure& resume_callback, | 106 const base::Closure& failure_callback, |
93 Result validation_result) { | 107 Result validation_result) { |
94 // Process the original message in the success case, otherwise map | 108 // Map |rejection_reason_| to a known reason, set |state_| to REJECTED and |
95 // |rejection_reason_| to a known reason, set |state_| to REJECTED and notify | 109 // notify the listener of the connection error via the callback. |
96 // the listener of the connection error via the callback. | |
97 switch (validation_result) { | 110 switch (validation_result) { |
98 case Result::SUCCESS: | 111 case Result::SUCCESS: |
99 current_authenticator_->ProcessMessage( | 112 success_callback.Run(); |
100 message, base::Bind(&ValidatingAuthenticator::UpdateState, | |
101 weak_factory_.GetWeakPtr(), resume_callback)); | |
102 return; | 113 return; |
103 | 114 |
104 case Result::ERROR_INVALID_CREDENTIALS: | 115 case Result::ERROR_INVALID_CREDENTIALS: |
105 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; | 116 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; |
106 break; | 117 break; |
107 | 118 |
108 case Result::ERROR_INVALID_ACCOUNT: | 119 case Result::ERROR_INVALID_ACCOUNT: |
109 rejection_reason_ = Authenticator::INVALID_ACCOUNT; | 120 rejection_reason_ = Authenticator::INVALID_ACCOUNT; |
110 break; | 121 break; |
111 | 122 |
| 123 case Result::ERROR_TOO_MANY_CONNECTIONS: |
| 124 rejection_reason_ = Authenticator::TOO_MANY_CONNECTIONS; |
| 125 break; |
| 126 |
112 case Result::ERROR_REJECTED_BY_USER: | 127 case Result::ERROR_REJECTED_BY_USER: |
113 rejection_reason_ = Authenticator::REJECTED_BY_USER; | 128 rejection_reason_ = Authenticator::REJECTED_BY_USER; |
114 break; | 129 break; |
115 } | 130 } |
116 | 131 |
117 state_ = Authenticator::REJECTED; | 132 state_ = Authenticator::REJECTED; |
118 resume_callback.Run(); | 133 failure_callback.Run(); |
119 } | 134 } |
120 | 135 |
121 void ValidatingAuthenticator::UpdateState( | 136 void ValidatingAuthenticator::UpdateState( |
122 const base::Closure& resume_callback) { | 137 const base::Closure& resume_callback) { |
123 DCHECK_EQ(state_, PROCESSING_MESSAGE); | 138 DCHECK_EQ(state_, PROCESSING_MESSAGE); |
124 | 139 |
125 // Update our current state before running |resume_callback|. | 140 // Update our current state before running |resume_callback|. |
126 state_ = current_authenticator_->state(); | 141 state_ = current_authenticator_->state(); |
127 if (state_ == REJECTED) { | 142 if (state_ == REJECTED) { |
128 rejection_reason_ = current_authenticator_->rejection_reason(); | 143 rejection_reason_ = current_authenticator_->rejection_reason(); |
| 144 } else if (state_ == MESSAGE_READY) { |
| 145 DCHECK(!pending_auth_message_); |
| 146 pending_auth_message_ = current_authenticator_->GetNextMessage(); |
| 147 state_ = current_authenticator_->state(); |
129 } | 148 } |
130 | 149 |
131 resume_callback.Run(); | 150 if (state_ == ACCEPTED) { |
| 151 accepted_callback_.Run( |
| 152 remote_jid_, base::Bind(&ValidatingAuthenticator::OnValidateComplete, |
| 153 weak_factory_.GetWeakPtr(), resume_callback, |
| 154 resume_callback)); |
| 155 } else { |
| 156 resume_callback.Run(); |
| 157 } |
132 } | 158 } |
133 | 159 |
134 } // namespace protocol | 160 } // namespace protocol |
135 } // namespace remoting | 161 } // namespace remoting |
OLD | NEW |