| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/net/engine_authentication_handler.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/callback_helpers.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "blimp/common/create_blimp_message.h" | |
| 15 #include "blimp/common/logging.h" | |
| 16 #include "blimp/common/proto/blimp_message.pb.h" | |
| 17 #include "blimp/common/protocol_version.h" | |
| 18 #include "blimp/net/blimp_connection.h" | |
| 19 #include "blimp/net/blimp_message_processor.h" | |
| 20 #include "blimp/net/blimp_transport.h" | |
| 21 #include "blimp/net/common.h" | |
| 22 #include "blimp/net/connection_error_observer.h" | |
| 23 #include "net/base/completion_callback.h" | |
| 24 #include "net/base/net_errors.h" | |
| 25 | |
| 26 namespace blimp { | |
| 27 | |
| 28 namespace { | |
| 29 // Expect Client to send the StartConnection within ten seconds of becoming | |
| 30 // connected. | |
| 31 const int kAuthTimeoutDurationInSeconds = 10; | |
| 32 | |
| 33 // Authenticates one connection. It deletes itself when | |
| 34 // * the connection is authenticated and passed to |connection_handler|. | |
| 35 // * the connection gets into an error state. | |
| 36 // * the auth message does not arrive within a reasonable time. | |
| 37 class Authenticator : public ConnectionErrorObserver, | |
| 38 public BlimpMessageProcessor { | |
| 39 public: | |
| 40 explicit Authenticator(std::unique_ptr<BlimpConnection> connection, | |
| 41 base::WeakPtr<ConnectionHandler> connection_handler, | |
| 42 const std::string& client_auth_token); | |
| 43 ~Authenticator() override; | |
| 44 | |
| 45 private: | |
| 46 // Processes authentication result and deletes |this|. | |
| 47 void OnConnectionAuthenticated(bool authenticated); | |
| 48 | |
| 49 // Handles timeout waiting for auth message, and deletes |this|. | |
| 50 void OnAuthenticationTimeout(); | |
| 51 | |
| 52 // ConnectionErrorObserver implementation. | |
| 53 void OnConnectionError(int error) override; | |
| 54 | |
| 55 // BlimpMessageProcessor implementation. | |
| 56 void ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
| 57 const net::CompletionCallback& callback) override; | |
| 58 | |
| 59 // The connection to be authenticated. | |
| 60 std::unique_ptr<BlimpConnection> connection_; | |
| 61 | |
| 62 // Handler to pass successfully authenticated connections to. | |
| 63 base::WeakPtr<ConnectionHandler> connection_handler_; | |
| 64 | |
| 65 // Used to authenticate incoming connection. | |
| 66 const std::string client_auth_token_; | |
| 67 | |
| 68 // A timer to fail authentication on timeout. | |
| 69 base::OneShotTimer timeout_timer_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(Authenticator); | |
| 72 }; | |
| 73 | |
| 74 Authenticator::Authenticator( | |
| 75 std::unique_ptr<BlimpConnection> connection, | |
| 76 base::WeakPtr<ConnectionHandler> connection_handler, | |
| 77 const std::string& client_auth_token) | |
| 78 : connection_(std::move(connection)), | |
| 79 connection_handler_(connection_handler), | |
| 80 client_auth_token_(client_auth_token) { | |
| 81 DVLOG(1) << "Authenticator object created."; | |
| 82 | |
| 83 // Observe for errors that might occur during the authentication phase. | |
| 84 connection_->AddConnectionErrorObserver(this); | |
| 85 connection_->SetIncomingMessageProcessor(this); | |
| 86 timeout_timer_.Start( | |
| 87 FROM_HERE, base::TimeDelta::FromSeconds(kAuthTimeoutDurationInSeconds), | |
| 88 this, &Authenticator::OnAuthenticationTimeout); | |
| 89 } | |
| 90 | |
| 91 Authenticator::~Authenticator() {} | |
| 92 | |
| 93 void Authenticator::OnConnectionAuthenticated(bool authenticated) { | |
| 94 DVLOG(1) << "OnConnectionAuthenticated result=" << authenticated; | |
| 95 | |
| 96 if (authenticated && connection_handler_) { | |
| 97 // Authentication is successful. Stop observing connection errors. | |
| 98 connection_->RemoveConnectionErrorObserver(this); | |
| 99 connection_handler_->HandleConnection(std::move(connection_)); | |
| 100 } | |
| 101 | |
| 102 delete this; | |
| 103 } | |
| 104 | |
| 105 void Authenticator::OnAuthenticationTimeout() { | |
| 106 DVLOG(1) << "Connection authentication timeout"; | |
| 107 OnConnectionAuthenticated(false); | |
| 108 } | |
| 109 | |
| 110 void Authenticator::OnConnectionError(int error) { | |
| 111 DVLOG(1) << "Connection error before authenticated " | |
| 112 << net::ErrorToString(error); | |
| 113 OnConnectionAuthenticated(false); | |
| 114 } | |
| 115 | |
| 116 void Authenticator::ProcessMessage(std::unique_ptr<BlimpMessage> message, | |
| 117 const net::CompletionCallback& callback) { | |
| 118 base::ScopedClosureRunner run_callback(base::Bind(callback, net::OK)); | |
| 119 | |
| 120 if (!message->has_protocol_control() || | |
| 121 !message->protocol_control().has_start_connection()) { | |
| 122 DVLOG(1) << "Expected PROTOCOL_CONTROL->START_CONNECTION, got " << *message; | |
| 123 OnConnectionAuthenticated(false); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 const StartConnectionMessage& start_connection = | |
| 128 message->protocol_control().start_connection(); | |
| 129 | |
| 130 // Verify that the protocol version is supported. | |
| 131 if (start_connection.protocol_version() != kProtocolVersion) { | |
| 132 DVLOG(1) << "Protocol version mismatch: " | |
| 133 << start_connection.protocol_version() << " vs " | |
| 134 << kProtocolVersion; | |
| 135 | |
| 136 // Inform the client of the mismatch before disconnecting it, so it can | |
| 137 // show the user an appropriate error. | |
| 138 connection_->GetOutgoingMessageProcessor()->ProcessMessage( | |
| 139 CreateEndConnectionMessage(EndConnectionMessage::PROTOCOL_MISMATCH), | |
| 140 net::CompletionCallback()); | |
| 141 | |
| 142 OnConnectionAuthenticated(false); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 // Verify that the authentication token matches. | |
| 147 bool token_match = client_auth_token_ == start_connection.client_auth_token(); | |
| 148 DVLOG(1) << "Authentication challenge received: " | |
| 149 << start_connection.client_auth_token() << ", and token " | |
| 150 << (token_match ? " matches" : " does not match"); | |
| 151 OnConnectionAuthenticated(token_match); | |
| 152 } | |
| 153 | |
| 154 } // namespace | |
| 155 | |
| 156 EngineAuthenticationHandler::EngineAuthenticationHandler( | |
| 157 ConnectionHandler* connection_handler, | |
| 158 const std::string& client_auth_token) | |
| 159 : connection_handler_weak_factory_(connection_handler), | |
| 160 client_auth_token_(client_auth_token) { | |
| 161 DCHECK(!client_auth_token_.empty()); | |
| 162 } | |
| 163 | |
| 164 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} | |
| 165 | |
| 166 void EngineAuthenticationHandler::HandleConnection( | |
| 167 std::unique_ptr<BlimpConnection> connection) { | |
| 168 // Authenticator manages its own lifetime. | |
| 169 new Authenticator(std::move(connection), | |
| 170 connection_handler_weak_factory_.GetWeakPtr(), | |
| 171 client_auth_token_); | |
| 172 } | |
| 173 | |
| 174 } // namespace blimp | |
| OLD | NEW |