| 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 "base/callback_helpers.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/timer/timer.h" |
| 10 #include "blimp/common/proto/blimp_message.pb.h" |
| 11 #include "blimp/net/blimp_connection.h" |
| 12 #include "blimp/net/blimp_message_processor.h" |
| 13 #include "blimp/net/blimp_transport.h" |
| 14 #include "blimp/net/connection_error_observer.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_errors.h" |
| 17 |
| 18 namespace blimp { |
| 19 |
| 20 namespace { |
| 21 // Expect Client to send the StartConnection within ten seconds of becoming |
| 22 // connected. |
| 23 const int kAuthTimeoutDurationInSeconds = 10; |
| 24 |
| 25 // Authenticates one connection. It deletes itself when |
| 26 // * the connection is authenticated and passed to |connection_handler|. |
| 27 // * the connection gets into an error state. |
| 28 // * the auth message does not arrive within a reasonable time. |
| 29 class Authenticator : public ConnectionErrorObserver, |
| 30 public BlimpMessageProcessor { |
| 31 public: |
| 32 explicit Authenticator(scoped_ptr<BlimpConnection> connection, |
| 33 base::WeakPtr<ConnectionHandler> connection_handler); |
| 34 ~Authenticator() override; |
| 35 |
| 36 private: |
| 37 // Processes authentication result and deletes |this|. |
| 38 void OnConnectionAuthenticated(bool authenticated); |
| 39 |
| 40 // Handles timeout waiting for auth message, and deletes |this|. |
| 41 void OnAuthenticationTimeout(); |
| 42 |
| 43 // ConnectionErrorObserver implementation. |
| 44 void OnConnectionError(int error) override; |
| 45 |
| 46 // BlimpMessageProcessor implementation. |
| 47 void ProcessMessage(scoped_ptr<BlimpMessage> message, |
| 48 const net::CompletionCallback& callback) override; |
| 49 |
| 50 // The connection to be authenticated. |
| 51 scoped_ptr<BlimpConnection> connection_; |
| 52 |
| 53 // Handler to pass successfully authenticated connections to. |
| 54 base::WeakPtr<ConnectionHandler> connection_handler_; |
| 55 |
| 56 // A timer to fail authentication on timeout. |
| 57 base::OneShotTimer timeout_timer_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(Authenticator); |
| 60 }; |
| 61 |
| 62 Authenticator::Authenticator( |
| 63 scoped_ptr<BlimpConnection> connection, |
| 64 base::WeakPtr<ConnectionHandler> connection_handler) |
| 65 : connection_(std::move(connection)), |
| 66 connection_handler_(connection_handler) { |
| 67 connection_->SetConnectionErrorObserver(this); |
| 68 connection_->SetIncomingMessageProcessor(this); |
| 69 timeout_timer_.Start( |
| 70 FROM_HERE, base::TimeDelta::FromSeconds(kAuthTimeoutDurationInSeconds), |
| 71 this, &Authenticator::OnAuthenticationTimeout); |
| 72 } |
| 73 |
| 74 Authenticator::~Authenticator() {} |
| 75 |
| 76 void Authenticator::OnConnectionAuthenticated(bool authenticated) { |
| 77 connection_->SetIncomingMessageProcessor(nullptr); |
| 78 connection_->SetConnectionErrorObserver(nullptr); |
| 79 |
| 80 if (authenticated && connection_handler_) { |
| 81 connection_handler_->HandleConnection(std::move(connection_)); |
| 82 } |
| 83 |
| 84 delete this; |
| 85 } |
| 86 |
| 87 void Authenticator::OnAuthenticationTimeout() { |
| 88 DVLOG(1) << "Connection authentication timeout"; |
| 89 OnConnectionAuthenticated(false); |
| 90 } |
| 91 |
| 92 void Authenticator::OnConnectionError(int error) { |
| 93 DVLOG(1) << "Connection error before authenticated " |
| 94 << net::ErrorToString(error); |
| 95 OnConnectionAuthenticated(false); |
| 96 } |
| 97 |
| 98 void Authenticator::ProcessMessage(scoped_ptr<BlimpMessage> message, |
| 99 const net::CompletionCallback& callback) { |
| 100 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) { |
| 101 // TODO(haibinlu): check client token. |
| 102 OnConnectionAuthenticated(true); |
| 103 } else { |
| 104 DVLOG(1) << "The first message is not START_CONNECTION"; |
| 105 OnConnectionAuthenticated(false); |
| 106 } |
| 107 |
| 108 callback.Run(net::OK); |
| 109 } |
| 110 |
| 111 } // namespace |
| 112 |
| 113 EngineAuthenticationHandler::EngineAuthenticationHandler( |
| 114 ConnectionHandler* connection_handler) |
| 115 : connection_handler_weak_factory_(connection_handler) {} |
| 116 |
| 117 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} |
| 118 |
| 119 void EngineAuthenticationHandler::HandleConnection( |
| 120 scoped_ptr<BlimpConnection> connection) { |
| 121 // Authenticator manages its own lifetime. |
| 122 new Authenticator(std::move(connection), |
| 123 connection_handler_weak_factory_.GetWeakPtr()); |
| 124 } |
| 125 |
| 126 } // namespace blimp |
| OLD | NEW |