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 { | |
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_; | |
50 } | |
51 | |
52 const std::string& ValidatingAuthenticator::GetAuthKey() const { | |
53 return current_authenticator_->GetAuthKey(); | |
54 } | |
55 | |
56 std::unique_ptr<ChannelAuthenticator> | |
57 ValidatingAuthenticator::CreateChannelAuthenticator() const { | |
58 return current_authenticator_->CreateChannelAuthenticator(); | |
59 } | |
60 | |
61 void ValidatingAuthenticator::ProcessMessage( | |
62 const buzz::XmlElement* message, | |
63 const base::Closure& resume_callback) { | |
64 DCHECK_EQ(state_, WAITING_MESSAGE); | |
65 state_ = PROCESSING_MESSAGE; | |
66 | |
67 if (first_message_received_) { | |
68 current_authenticator_->ProcessMessage( | |
69 message, base::Bind(&ValidatingAuthenticator::UpdateState, | |
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 } | |
80 | |
81 std::unique_ptr<buzz::XmlElement> ValidatingAuthenticator::GetNextMessage() { | |
82 std::unique_ptr<buzz::XmlElement> result( | |
83 current_authenticator_->GetNextMessage()); | |
84 state_ = current_authenticator_->state(); | |
85 DCHECK(state_ == ACCEPTED || state_ == WAITING_MESSAGE); | |
86 | |
87 return result; | |
88 } | |
89 | |
90 void ValidatingAuthenticator::OnValidateComplete( | |
91 const buzz::XmlElement* message, | |
92 const base::Closure& resume_callback, | |
93 Result validation_result) { | |
94 if (validation_result == Result::SUCCESS) { | |
95 current_authenticator_->ProcessMessage( | |
96 message, base::Bind(&ValidatingAuthenticator::UpdateState, | |
97 weak_factory_.GetWeakPtr(), resume_callback)); | |
98 return; | |
99 } | |
100 | |
101 // |validation_result| represents a rejected state so map the result to a | |
102 // rejection reason and call the callback to let the caller know the result. | |
103 state_ = Authenticator::REJECTED; | |
104 | |
105 switch (validation_result) { | |
106 case Result::ERROR_INVALID_CREDENTIALS: | |
107 rejection_reason_ = Authenticator::INVALID_CREDENTIALS; | |
108 break; | |
109 | |
110 case Result::ERROR_INVALID_ACCOUNT: | |
111 rejection_reason_ = Authenticator::INVALID_ACCOUNT; | |
112 break; | |
113 | |
114 case Result::ERROR_REJECTED_BY_USER: | |
115 rejection_reason_ = Authenticator::REJECTED_BY_USER; | |
116 break; | |
117 | |
118 default: | |
Sergey Ulanov
2016/08/31 23:41:24
It's best to avoid default case for enum switch st
| |
119 // Log an error and fail to prevent logging a misleading error value. | |
120 CHECK(false) << "Unknown validation result value: " | |
Sergey Ulanov
2016/08/31 23:41:24
I think NOTREACHED() would be appropriate here - t
| |
121 << static_cast<unsigned int>(validation_result); | |
122 } | |
123 | |
124 resume_callback.Run(); | |
125 } | |
126 | |
127 void ValidatingAuthenticator::UpdateState( | |
128 const base::Closure& resume_callback) { | |
129 DCHECK_EQ(state_, PROCESSING_MESSAGE); | |
130 | |
131 // Update our current state before running |resume_callback|. | |
132 state_ = current_authenticator_->state(); | |
133 if (state_ == REJECTED) { | |
134 rejection_reason_ = current_authenticator_->rejection_reason(); | |
135 } | |
136 | |
137 resume_callback.Run(); | |
138 } | |
139 | |
140 } // namespace protocol | |
141 } // namespace remoting | |
OLD | NEW |