OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/net/engine_authentication_handler.h" | 5 #include "blimp/net/engine_authentication_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 #include "blimp/common/create_blimp_message.h" | |
12 #include "blimp/common/logging.h" | 13 #include "blimp/common/logging.h" |
13 #include "blimp/common/proto/blimp_message.pb.h" | 14 #include "blimp/common/proto/blimp_message.pb.h" |
15 #include "blimp/common/version_info.h" | |
14 #include "blimp/net/blimp_connection.h" | 16 #include "blimp/net/blimp_connection.h" |
15 #include "blimp/net/blimp_message_processor.h" | 17 #include "blimp/net/blimp_message_processor.h" |
16 #include "blimp/net/blimp_transport.h" | 18 #include "blimp/net/blimp_transport.h" |
17 #include "blimp/net/common.h" | 19 #include "blimp/net/common.h" |
18 #include "blimp/net/connection_error_observer.h" | 20 #include "blimp/net/connection_error_observer.h" |
19 #include "net/base/completion_callback.h" | 21 #include "net/base/completion_callback.h" |
20 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
21 | 23 |
22 namespace blimp { | 24 namespace blimp { |
23 | 25 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 } | 106 } |
105 | 107 |
106 void Authenticator::OnConnectionError(int error) { | 108 void Authenticator::OnConnectionError(int error) { |
107 DVLOG(1) << "Connection error before authenticated " | 109 DVLOG(1) << "Connection error before authenticated " |
108 << net::ErrorToString(error); | 110 << net::ErrorToString(error); |
109 OnConnectionAuthenticated(false); | 111 OnConnectionAuthenticated(false); |
110 } | 112 } |
111 | 113 |
112 void Authenticator::ProcessMessage(std::unique_ptr<BlimpMessage> message, | 114 void Authenticator::ProcessMessage(std::unique_ptr<BlimpMessage> message, |
113 const net::CompletionCallback& callback) { | 115 const net::CompletionCallback& callback) { |
114 if (message->type() == BlimpMessage::PROTOCOL_CONTROL && | 116 base::ScopedClosureRunner run_callback(base::Bind(callback, net::OK)); |
115 message->protocol_control().type() == | 117 |
116 ProtocolControlMessage::START_CONNECTION) { | 118 if ((message->type() != BlimpMessage::PROTOCOL_CONTROL) || |
117 bool token_match = | 119 (message->protocol_control().type() != |
118 client_token_ == | 120 ProtocolControlMessage::START_CONNECTION)) { |
119 message->protocol_control().start_connection().client_token(); | 121 DVLOG(1) << "Expected PROTOCOL_CONTROL->START_CONNECTION, got " << *message; |
120 DVLOG(1) << "Authentication challenge received: " | |
121 << message->protocol_control().start_connection().client_token() | |
122 << ", and token " | |
123 << (token_match ? " matches" : " does not match"); | |
124 OnConnectionAuthenticated(token_match); | |
125 } else { | |
126 DVLOG(1) << "Expected START_CONNECTION message, got " << *message | |
127 << " instead."; | |
128 OnConnectionAuthenticated(false); | 122 OnConnectionAuthenticated(false); |
123 return; | |
129 } | 124 } |
130 | 125 |
131 callback.Run(net::OK); | 126 const StartConnectionMessage& start_connection = |
127 message->protocol_control().start_connection(); | |
128 | |
129 // Verify that the protocol version is supported. | |
130 if (start_connection.protocol_version() != GetProtocolVersion()) { | |
131 DVLOG(1) << "Protocol version mismatch: " | |
Kevin M
2016/04/22 21:12:54
Could be good to VLOG(), since this error might be
| |
132 << start_connection.protocol_version() << " vs " | |
133 << GetProtocolVersion(); | |
134 | |
135 // Inform the client of the mismatch before disconnecting it, so it can | |
136 // show the user an appropriate error. | |
137 connection_->GetOutgoingMessageProcessor()->ProcessMessage( | |
138 CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH), | |
139 net::CompletionCallback()); | |
140 | |
141 OnConnectionAuthenticated(false); | |
142 return; | |
143 } | |
144 | |
145 // Verify that the authentication token matches. | |
146 bool token_match = client_token_ == start_connection.client_token(); | |
147 DVLOG(1) << "Authentication challenge received: " | |
148 << start_connection.client_token() << ", and token " | |
149 << (token_match ? " matches" : " does not match"); | |
150 OnConnectionAuthenticated(token_match); | |
132 } | 151 } |
133 | 152 |
134 } // namespace | 153 } // namespace |
135 | 154 |
136 EngineAuthenticationHandler::EngineAuthenticationHandler( | 155 EngineAuthenticationHandler::EngineAuthenticationHandler( |
137 ConnectionHandler* connection_handler, | 156 ConnectionHandler* connection_handler, |
138 const std::string& client_token) | 157 const std::string& client_token) |
139 : connection_handler_weak_factory_(connection_handler), | 158 : connection_handler_weak_factory_(connection_handler), |
140 client_token_(client_token) { | 159 client_token_(client_token) { |
141 DCHECK(!client_token_.empty()); | 160 DCHECK(!client_token_.empty()); |
142 } | 161 } |
143 | 162 |
144 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} | 163 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} |
145 | 164 |
146 void EngineAuthenticationHandler::HandleConnection( | 165 void EngineAuthenticationHandler::HandleConnection( |
147 std::unique_ptr<BlimpConnection> connection) { | 166 std::unique_ptr<BlimpConnection> connection) { |
148 // Authenticator manages its own lifetime. | 167 // Authenticator manages its own lifetime. |
149 new Authenticator(std::move(connection), | 168 new Authenticator(std::move(connection), |
150 connection_handler_weak_factory_.GetWeakPtr(), | 169 connection_handler_weak_factory_.GetWeakPtr(), |
151 client_token_); | 170 client_token_); |
152 } | 171 } |
153 | 172 |
154 } // namespace blimp | 173 } // namespace blimp |
OLD | NEW |