Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / remoting / protocol / negotiating_authenticator_base.h
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/webrtc/libjingle/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 bool started() const OVERRIDE;
68   virtual RejectionReason rejection_reason() const OVERRIDE;
69   virtual scoped_ptr<ChannelAuthenticator>
70       CreateChannelAuthenticator() const OVERRIDE;
71
72   // Calls |current_authenticator_| to process |message|, passing the supplied
73   // |resume_callback|.
74   void ProcessMessageInternal(const buzz::XmlElement* message,
75                               const base::Closure& resume_callback);
76
77   const AuthenticationMethod& current_method_for_testing() const {
78     return current_method_;
79   }
80
81  protected:
82   static const buzz::StaticQName kMethodAttributeQName;
83   static const buzz::StaticQName kSupportedMethodsAttributeQName;
84   static const char kSupportedMethodsSeparator;
85
86   explicit NegotiatingAuthenticatorBase(Authenticator::State initial_state);
87
88   void AddMethod(const AuthenticationMethod& method);
89
90   // Updates |state_| to reflect the current underlying authenticator state.
91   // |resume_callback| is called after the state is updated.
92   void UpdateState(const base::Closure& resume_callback);
93
94   // Gets the next message from |current_authenticator_|, if any, and fills in
95   // the 'method' tag with |current_method_|.
96   virtual scoped_ptr<buzz::XmlElement> GetNextMessageInternal();
97
98   std::vector<AuthenticationMethod> methods_;
99   AuthenticationMethod current_method_;
100   scoped_ptr<Authenticator> current_authenticator_;
101   State state_;
102   RejectionReason rejection_reason_;
103
104  private:
105   DISALLOW_COPY_AND_ASSIGN(NegotiatingAuthenticatorBase);
106 };
107
108 }  // namespace protocol
109 }  // namespace remoting
110
111 #endif  // REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_BASE_H_