1 // Copyright 2013 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_CLIENT_AUTHENTICATOR_H_
6 #define REMOTING_PROTOCOL_NEGOTIATING_CLIENT_AUTHENTICATOR_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "remoting/protocol/authenticator.h"
15 #include "remoting/protocol/client_authentication_config.h"
16 #include "remoting/protocol/negotiating_authenticator_base.h"
17 #include "remoting/protocol/third_party_client_authenticator.h"
18 
19 namespace remoting {
20 namespace protocol {
21 
22 // Client-side implementation of NegotiatingAuthenticatorBase.
23 // See comments in negotiating_authenticator_base.h for a general explanation.
24 class NegotiatingClientAuthenticator : public NegotiatingAuthenticatorBase {
25  public:
26   explicit NegotiatingClientAuthenticator(
27       const std::string& local_id,
28       const std::string& remote_id,
29       const ClientAuthenticationConfig& config);
30   ~NegotiatingClientAuthenticator() override;
31 
32   // NegotiatingAuthenticatorBase:
33   void ProcessMessage(const jingle_xmpp::XmlElement* message,
34                       base::OnceClosure resume_callback) override;
35   std::unique_ptr<jingle_xmpp::XmlElement> GetNextMessage() override;
36 
37  private:
38   // (Asynchronously) creates an authenticator, and stores it in
39   // |current_authenticator_|. Authenticators that can be started in either
40   // state will be created in |preferred_initial_state|.
41   // |resume_callback| is called after |current_authenticator_| is set.
42   void CreateAuthenticatorForCurrentMethod(
43       Authenticator::State preferred_initial_state,
44       base::OnceClosure resume_callback);
45 
46   // If possible, create a preferred authenticator ready to send an
47   // initial message optimistically to the host. The host is free to
48   // ignore the client's preferred authenticator and initial message
49   // and to instead reply with an alternative method. See the comments
50   // in negotiating_authenticator_base.h for more details.
51   //
52   // Sets |current_authenticator_| and |current_method_| iff the client
53   // has a preferred authenticator that can optimistically send an initial
54   // message.
55   void CreatePreferredAuthenticator();
56 
57   // Creates a shared-secret authenticator in state |initial_state| with the
58   // given |shared_secret|, then runs |resume_callback|.
59   void CreateSharedSecretAuthenticator(Authenticator::State initial_state,
60                                        base::OnceClosure resume_callback,
61                                        const std::string& shared_secret);
62 
63   bool is_paired();
64 
65   std::string local_id_;
66   std::string remote_id_;
67 
68   ClientAuthenticationConfig config_;
69 
70   // Internal NegotiatingClientAuthenticator data.
71   bool method_set_by_host_ = false;
72   base::WeakPtrFactory<NegotiatingClientAuthenticator> weak_factory_{this};
73 
74   DISALLOW_COPY_AND_ASSIGN(NegotiatingClientAuthenticator);
75 };
76 
77 }  // namespace protocol
78 }  // namespace remoting
79 
80 #endif  // REMOTING_PROTOCOL_NEGOTIATING_CLIENT_AUTHENTICATOR_H_
81