Home | History | Annotate | Download | only in protocol
      1 // Copyright (c) 2012 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 #ifndef REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_
      6 #define REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "remoting/protocol/authentication_method.h"
     15 #include "remoting/protocol/authenticator.h"
     16 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
     17 
     18 namespace remoting {
     19 namespace protocol {
     20 
     21 // This class provides the common base for a meta-authenticator that allows
     22 // clients and hosts that support multiple authentication methods to negotiate a
     23 // method to use.
     24 //
     25 // The typical flow is:
     26 //  * Client sends a message to host with its supported methods.
     27 //      (clients may additionally pick a method and send its first message).
     28 //  * Host picks a method and sends its first message (if any).
     29 //      (if a message for that method was sent by the client, it is processed).
     30 //  * Client creates the authenticator selected by the host. If the method
     31 //      starts with a message from the host, it is processed.
     32 //  * Client and host exchange messages until the authentication is ACCEPTED or
     33 //      REJECTED.
     34 //
     35 // The details:
     36 //  * CreateAuthenticator() may be asynchronous (i.e. require user interaction
     37 //      to determine initial parameters, like PIN). This happens inside
     38 //      ProcessMessage, so to the outside this behaves like any asynchronous
     39 //      message processing. Internally, CreateAuthenticator() receives a
     40 //      callback, that will resume the authentication once the authenticator is
     41 //      created. If there is already a message to be processed by the new
     42 //      authenticator, this callback includes a call to the underlying
     43 //      ProcessMessage().
     44 //  * Some authentication methods may have a specific starting direction (e.g.
     45 //      host always sends the first message), while others are versatile (e.g.
     46 //      SPAKE, where either side can send the first message). When an
     47 //      authenticator is created, it is given a preferred initial state, which
     48 //      the authenticator may ignore.
     49 //  * If the new authenticator state doesn't match the preferred one,
     50 //      the NegotiatingAuthenticator deals with that, by sending an empty
     51 //      <authenticator> stanza if the method has no message to send, and
     52 //      ignoring such empty messages on the receiving end.
     53 //  * The client may optimistically pick a method on its first message (assuming
     54 //      it doesn't require user interaction to start). If the host doesn't
     55 //      support that method, it will just discard that message, and choose
     56 //      another method from the client's supported methods list.
     57 //  * The host never sends its own supported methods back to the client, so once
     58 //      the host picks a method from the client's list, it's final.
     59 //  * Any change in this class must maintain compatibility between any version
     60 //      mix of webapp, client plugin and host, for both Me2Me and IT2Me.
     61 class NegotiatingAuthenticatorBase : public Authenticator {
     62  public:
     63   virtual ~NegotiatingAuthenticatorBase();
     64 
     65   // Authenticator interface.
     66   virtual State state() const OVERRIDE;
     67   virtual RejectionReason rejection_reason() const OVERRIDE;
     68   virtual scoped_ptr<ChannelAuthenticator>
     69       CreateChannelAuthenticator() const OVERRIDE;
     70 
     71   // Calls |current_authenticator_| to process |message|, passing the supplied
     72   // |resume_callback|.
     73   void ProcessMessageInternal(const buzz::XmlElement* message,
     74                               const base::Closure& resume_callback);
     75 
     76   const AuthenticationMethod& current_method_for_testing() const {
     77     return current_method_;
     78   }
     79 
     80  protected:
     81   static const buzz::StaticQName kMethodAttributeQName;
     82   static const buzz::StaticQName kSupportedMethodsAttributeQName;
     83   static const char kSupportedMethodsSeparator;
     84 
     85   explicit NegotiatingAuthenticatorBase(Authenticator::State initial_state);
     86 
     87   void AddMethod(const AuthenticationMethod& method);
     88 
     89   // Updates |state_| to reflect the current underlying authenticator state.
     90   // |resume_callback| is called after the state is updated.
     91   void UpdateState(const base::Closure& resume_callback);
     92 
     93   // Gets the next message from |current_authenticator_|, if any, and fills in
     94   // the 'method' tag with |current_method_|.
     95   virtual scoped_ptr<buzz::XmlElement> GetNextMessageInternal();
     96 
     97   std::vector<AuthenticationMethod> methods_;
     98   AuthenticationMethod current_method_;
     99   scoped_ptr<Authenticator> current_authenticator_;
    100   State state_;
    101   RejectionReason rejection_reason_;
    102 
    103  private:
    104   DISALLOW_COPY_AND_ASSIGN(NegotiatingAuthenticatorBase);
    105 };
    106 
    107 }  // namespace protocol
    108 }  // namespace remoting
    109 
    110 #endif  // REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_
    111