| 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->has_protocol_control() && | 116 base::ScopedClosureRunner run_callback(base::Bind(callback, net::OK)); |
| 115 message->protocol_control().has_start_connection()) { | 117 |
| 116 bool token_match = | 118 if (!message->has_protocol_control() || |
| 117 client_token_ == | 119 !message->protocol_control().has_start_connection()) { |
| 118 message->protocol_control().start_connection().client_token(); | 120 DVLOG(1) << "Expected PROTOCOL_CONTROL->START_CONNECTION, got " << *message; |
| 119 DVLOG(1) << "Authentication challenge received: " | |
| 120 << message->protocol_control().start_connection().client_token() | |
| 121 << ", and token " | |
| 122 << (token_match ? " matches" : " does not match"); | |
| 123 OnConnectionAuthenticated(token_match); | |
| 124 } else { | |
| 125 DVLOG(1) << "Expected START_CONNECTION message, got " << *message | |
| 126 << " instead."; | |
| 127 OnConnectionAuthenticated(false); | 121 OnConnectionAuthenticated(false); |
| 122 return; |
| 128 } | 123 } |
| 129 | 124 |
| 130 callback.Run(net::OK); | 125 const StartConnectionMessage& start_connection = |
| 126 message->protocol_control().start_connection(); |
| 127 |
| 128 // Verify that the protocol version is supported. |
| 129 if (start_connection.protocol_version() != GetProtocolVersion()) { |
| 130 DVLOG(1) << "Protocol version mismatch: " |
| 131 << start_connection.protocol_version() << " vs " |
| 132 << GetProtocolVersion(); |
| 133 |
| 134 // Inform the client of the mismatch before disconnecting it, so it can |
| 135 // show the user an appropriate error. |
| 136 connection_->GetOutgoingMessageProcessor()->ProcessMessage( |
| 137 CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH), |
| 138 net::CompletionCallback()); |
| 139 |
| 140 OnConnectionAuthenticated(false); |
| 141 return; |
| 142 } |
| 143 |
| 144 // Verify that the authentication token matches. |
| 145 bool token_match = client_token_ == start_connection.client_token(); |
| 146 DVLOG(1) << "Authentication challenge received: " |
| 147 << start_connection.client_token() << ", and token " |
| 148 << (token_match ? " matches" : " does not match"); |
| 149 OnConnectionAuthenticated(token_match); |
| 131 } | 150 } |
| 132 | 151 |
| 133 } // namespace | 152 } // namespace |
| 134 | 153 |
| 135 EngineAuthenticationHandler::EngineAuthenticationHandler( | 154 EngineAuthenticationHandler::EngineAuthenticationHandler( |
| 136 ConnectionHandler* connection_handler, | 155 ConnectionHandler* connection_handler, |
| 137 const std::string& client_token) | 156 const std::string& client_token) |
| 138 : connection_handler_weak_factory_(connection_handler), | 157 : connection_handler_weak_factory_(connection_handler), |
| 139 client_token_(client_token) { | 158 client_token_(client_token) { |
| 140 DCHECK(!client_token_.empty()); | 159 DCHECK(!client_token_.empty()); |
| 141 } | 160 } |
| 142 | 161 |
| 143 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} | 162 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} |
| 144 | 163 |
| 145 void EngineAuthenticationHandler::HandleConnection( | 164 void EngineAuthenticationHandler::HandleConnection( |
| 146 std::unique_ptr<BlimpConnection> connection) { | 165 std::unique_ptr<BlimpConnection> connection) { |
| 147 // Authenticator manages its own lifetime. | 166 // Authenticator manages its own lifetime. |
| 148 new Authenticator(std::move(connection), | 167 new Authenticator(std::move(connection), |
| 149 connection_handler_weak_factory_.GetWeakPtr(), | 168 connection_handler_weak_factory_.GetWeakPtr(), |
| 150 client_token_); | 169 client_token_); |
| 151 } | 170 } |
| 152 | 171 |
| 153 } // namespace blimp | 172 } // namespace blimp |
| OLD | NEW |