OLD | NEW |
---|---|
(Empty) | |
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" | |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "remoting/protocol/authenticator.h" | |
18 #include "remoting/protocol/channel_authenticator.h" | |
19 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | |
20 | |
21 namespace remoting { | |
22 namespace protocol { | |
23 | |
24 ValidatingAuthenticator::ValidatingAuthenticator( | |
25 const std::string& remote_jid, | |
26 const ValidationCallback& validation_callback, | |
27 std::unique_ptr<Authenticator> current_authenticator) | |
28 : remote_jid_(remote_jid), | |
29 validation_callback_(validation_callback), | |
30 current_authenticator_(std::move(current_authenticator)), | |
31 weak_factory_(this) { | |
32 DCHECK(!remote_jid_.empty()); | |
33 DCHECK(!validation_callback_.is_null()); | |
34 DCHECK(current_authenticator_); | |
35 } | |
36 | |
37 ValidatingAuthenticator::~ValidatingAuthenticator() {} | |
38 | |
39 Authenticator::State ValidatingAuthenticator::state() const { | |
Jamie
2016/08/26 23:12:08
It seems we should be falling back on the underlyi
joedow
2016/08/29 23:43:22
I started off using a branching structure like thi
Jamie
2016/08/31 00:28:34
I'll make a couple of points in favour of my appro
joedow
2016/08/31 17:42:29
Discussed offline, I have tried this approach firs
| |
40 return state_; | |
41 } | |
42 | |
43 bool ValidatingAuthenticator::started() const { | |
44 return current_authenticator_->started(); | |
45 } | |
46 | |
47 Authenticator::RejectionReason ValidatingAuthenticator::rejection_reason() | |
48 const { | |
49 return rejection_reason_; | |
Jamie
2016/08/26 23:12:07
Similarly here, the underlying authenticator shoul
joedow
2016/08/29 23:43:22
Same as above, I think it is cleaner to just use |
| |
50 } | |
51 | |
52 const std::string& ValidatingAuthenticator::GetAuthKey() const { | |
53 DCHECK_EQ(state_, ACCEPTED); | |
Jamie
2016/08/26 23:12:08
I don't think you need this check; state() should
joedow
2016/08/29 23:43:22
I'm fine removing this as you are correct that the
| |
54 return current_authenticator_->GetAuthKey(); | |
55 } | |
56 | |
57 std::unique_ptr<ChannelAuthenticator> | |
58 ValidatingAuthenticator::CreateChannelAuthenticator() const { | |
59 DCHECK_EQ(state_, ACCEPTED); | |
60 return current_authenticator_->CreateChannelAuthenticator(); | |
61 } | |
62 | |
63 void ValidatingAuthenticator::ProcessMessage( | |
64 const buzz::XmlElement* message, | |
65 const base::Closure& resume_callback) { | |
66 DCHECK_EQ(state_, WAITING_MESSAGE); | |
67 state_ = PROCESSING_MESSAGE; | |
68 | |
69 if (first_message_received_) { | |
70 current_authenticator_->ProcessMessage( | |
71 message, base::Bind(&ValidatingAuthenticator::UpdateState, | |
72 weak_factory_.GetWeakPtr(), resume_callback)); | |
73 } else { | |
74 first_message_received_ = true; | |
75 validation_callback_.Run( | |
76 remote_jid_, base::Bind(&ValidatingAuthenticator::OnValidateComplete, | |
77 weak_factory_.GetWeakPtr(), | |
78 base::Owned(new buzz::XmlElement(*message)), | |
79 resume_callback)); | |
80 } | |
81 } | |
82 | |
83 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { | |
84 DCHECK_EQ(state_, MESSAGE_READY); | |
85 | |
86 std::unique_ptr<buzz::XmlElement> result; | |
87 if (current_authenticator_->state() == MESSAGE_READY) { | |
88 result = current_authenticator_->GetNextMessage(); | |
89 } else { | |
90 result = CreateEmptyAuthenticatorMessage(); | |
Jamie
2016/08/26 23:12:07
I don't think this code should be reachable; if th
joedow
2016/08/29 23:43:22
I think that's a reasonable assumption, I'll leave
| |
91 } | |
92 | |
93 state_ = current_authenticator_->state(); | |
94 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); | |
95 | |
96 return result; | |
97 } | |
98 | |
99 void ValidatingAuthenticator::OnValidateComplete( | |
100 const buzz::XmlElement* message, | |
101 const base::Closure& resume_callback, | |
102 Result validation_result) { | |
103 if (validation_result == Result::SUCCESS) { | |
104 current_authenticator_->ProcessMessage( | |
105 message, base::Bind(&ValidatingAuthenticator::UpdateState, | |
106 weak_factory_.GetWeakPtr(), resume_callback)); | |
107 return; | |
108 } | |
109 | |
110 // |validation_result| represents a rejected state so map the result to a | |
111 // rejection reason and call the callback to let the caller know the result. | |
112 state_ = Authenticator::REJECTED; | |
113 | |
114 switch (validation_result) { | |
115 case Result::ERROR_INVALID_CREDENTIALS: | |
116 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; | |
117 break; | |
118 | |
119 case Result::ERROR_INVALID_ACCOUNT: | |
120 rejection_reason_ = Authenticator::INVALID_ACCOUNT; | |
121 break; | |
122 | |
123 case Result::ERROR_REJECTED_BY_USER: | |
124 rejection_reason_ = Authenticator::REJECTED_BY_USER; | |
125 break; | |
126 | |
127 default: | |
128 // Log an error and use the default value for |rejection_result_|. | |
Jamie
2016/08/26 23:12:08
By defaulting to INVALID_CREDENTIALS here, aren't
joedow
2016/08/29 23:43:22
What should happen is that the person adding the n
Jamie
2016/08/31 00:28:34
Better still, if you fold the SUCCESS case into th
joedow
2016/08/31 17:42:29
Fixed the comment. It looks like the compiler doe
| |
129 NOTREACHED() << "Unknown validation result value: " | |
130 << static_cast<unsigned int>(validation_result); | |
131 } | |
132 | |
133 resume_callback.Run(); | |
134 } | |
135 | |
136 void ValidatingAuthenticator::UpdateState( | |
137 const base::Closure& resume_callback) { | |
138 DCHECK_EQ(state_, PROCESSING_MESSAGE); | |
139 | |
140 // After the underlying authenticator finishes processing the message, | |
141 // ValidatingAuthenticator must update its own state before running | |
142 // |resume_callback|. | |
143 state_ = current_authenticator_->state(); | |
144 | |
145 // Verify the new state represents a valid state transition. | |
146 DCHECK(state_ == MESSAGE_READY || state_ == ACCEPTED || state_ == REJECTED) | |
147 << "State: " << state_; | |
148 | |
149 if (state_ == REJECTED) { | |
150 rejection_reason_ = current_authenticator_->rejection_reason(); | |
151 } | |
152 | |
153 resume_callback.Run(); | |
154 } | |
155 | |
156 } // namespace protocol | |
157 } // namespace remoting | |
OLD | NEW |