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 <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "remoting/protocol/authentication_method.h"
15#include "remoting/protocol/authenticator.h"
16#include "remoting/protocol/negotiating_authenticator_base.h"
17#include "remoting/protocol/third_party_client_authenticator.h"
18
19namespace remoting {
20namespace protocol {
21
22// Client-side implementation of NegotiatingAuthenticatorBase.
23// See comments in negotiating_authenticator_base.h for a general explanation.
24class NegotiatingClientAuthenticator : public NegotiatingAuthenticatorBase {
25 public:
26  // TODO(jamiewalch): Pass ClientConfig instead of separate parameters.
27  NegotiatingClientAuthenticator(
28      const std::string& client_pairing_id,
29      const std::string& shared_secret,
30      const std::string& authentication_tag,
31      const FetchSecretCallback& fetch_secret_callback,
32      scoped_ptr<ThirdPartyClientAuthenticator::TokenFetcher> token_fetcher_,
33      const std::vector<AuthenticationMethod>& methods);
34
35  virtual ~NegotiatingClientAuthenticator();
36
37  // Overriden from Authenticator.
38  virtual void ProcessMessage(const buzz::XmlElement* message,
39                              const base::Closure& resume_callback) OVERRIDE;
40  virtual scoped_ptr<buzz::XmlElement> GetNextMessage() OVERRIDE;
41
42 private:
43  // (Asynchronously) creates an authenticator, and stores it in
44  // |current_authenticator_|. Authenticators that can be started in either
45  // state will be created in |preferred_initial_state|.
46  // |resume_callback| is called after |current_authenticator_| is set.
47  void CreateAuthenticatorForCurrentMethod(
48      Authenticator::State preferred_initial_state,
49      const base::Closure& resume_callback);
50
51  // If possible, create a preferred authenticator ready to send an
52  // initial message optimistically to the host. The host is free to
53  // ignore the client's preferred authenticator and initial message
54  // and to instead reply with an alternative method. See the comments
55  // in negotiating_authenticator_base.h for more details.
56  //
57  // Sets |current_authenticator_| and |current_method_| iff the client
58  // has a preferred authenticator that can optimistically send an initial
59  // message.
60  void CreatePreferredAuthenticator();
61
62  // Creates a V2Authenticator in state |initial_state| with the given
63  // |shared_secret|, then runs |resume_callback|.
64  void CreateV2AuthenticatorWithSecret(
65      Authenticator::State initial_state,
66      const base::Closure& resume_callback,
67      const std::string& shared_secret);
68
69  // Used for pairing authenticators
70  std::string client_pairing_id_;
71  std::string shared_secret_;
72
73  // Used for all authenticators.
74  std::string authentication_tag_;
75
76  // Used for shared secret authenticators.
77  FetchSecretCallback fetch_secret_callback_;
78
79  // Used for third party authenticators.
80  scoped_ptr<ThirdPartyClientAuthenticator::TokenFetcher> token_fetcher_;
81
82  // Internal NegotiatingClientAuthenticator data.
83  bool method_set_by_host_;
84  base::WeakPtrFactory<NegotiatingClientAuthenticator> weak_factory_;
85
86  DISALLOW_COPY_AND_ASSIGN(NegotiatingClientAuthenticator);
87};
88
89}  // namespace protocol
90}  // namespace remoting
91
92#endif  // REMOTING_PROTOCOL_NEGOTIATING_CLIENT_AUTHENTICATOR_H_
93