This flag is obsolete, since a duplicate of it already exists as:
[chromium-blink-merge.git] / remoting / signaling / xmpp_login_handler.h
blob10af045e3f2f48adcf7e51790f52b2e823f8daca
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.
5 #ifndef REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_
6 #define REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "remoting/signaling/signal_strategy.h"
13 // Undefine SendMessage and ERROR defined in Windows headers.
14 #ifdef SendMessage
15 #undef SendMessage
16 #endif
18 #ifdef ERROR
19 #undef ERROR
20 #endif
22 namespace remoting {
24 class XmppStreamParser;
26 // XmppLoginHandler handles authentication handshake for XmppSignalStrategy. It
27 // receives incoming data using onDataReceived(), calls Delegate::SendMessage()
28 // to send outgoing messages and calls Delegate::OnHandshakeDone() after
29 // authentication is finished successfully or Delegate::OnError() on error.
31 // See RFC3920 for description of XMPP and authentication handshake.
32 class XmppLoginHandler {
33 public:
34 class Delegate {
35 public:
36 Delegate() {}
38 virtual void SendMessage(const std::string& message) = 0;
39 virtual void StartTls() = 0;
40 virtual void OnHandshakeDone(const std::string& jid,
41 scoped_ptr<XmppStreamParser> parser) = 0;
42 virtual void OnLoginHandlerError(SignalStrategy::Error error) = 0;
44 protected:
45 virtual ~Delegate() {}
48 enum class TlsMode {
49 NO_TLS,
50 WITH_HANDSHAKE,
51 WITHOUT_HANDSHAKE,
54 XmppLoginHandler(const std::string& server,
55 const std::string& username,
56 const std::string& auth_token,
57 TlsMode tls_mode,
58 Delegate* delegate);
59 ~XmppLoginHandler();
61 void Start();
62 void OnDataReceived(const std::string& data);
63 void OnTlsStarted();
65 private:
66 // States the handshake goes through. States are iterated from INIT to DONE
67 // sequentially, except for ERROR state which may be accepted at any point.
69 // Following messages are sent/received in each state:
70 // INIT
71 // client -> server: Stream header
72 // client -> server: <starttls>
73 // WAIT_STREAM_HEADER
74 // client <- server: Stream header with list of supported features which
75 // should include starttls.
76 // WAIT_STARTTLS_RESPONSE
77 // client <- server: <proceed>
78 // STARTING_TLS
79 // TLS handshake
80 // client -> server: Stream header
81 // client -> server: <auth> message with the OAuth2 token.
82 // WAIT_STREAM_HEADER_AFTER_TLS
83 // client <- server: Stream header with list of supported authentication
84 // methods which is expected to include X-OAUTH2
85 // WAIT_AUTH_RESULT
86 // client <- server: <success> or <failure>
87 // client -> server: Stream header
88 // client -> server: <bind>
89 // client -> server: <iq><session/></iq> to start the session
90 // WAIT_STREAM_HEADER_AFTER_AUTH
91 // client <- server: Stream header with list of features that should
92 // include <bind>.
93 // WAIT_BIND_RESULT
94 // client <- server: <bind> result with JID.
95 // WAIT_SESSION_IQ_RESULT
96 // client <- server: result for <iq><session/></iq>
97 // DONE
98 enum class State {
99 INIT,
100 WAIT_STREAM_HEADER,
101 WAIT_STARTTLS_RESPONSE,
102 STARTING_TLS,
103 WAIT_STREAM_HEADER_AFTER_TLS,
104 WAIT_AUTH_RESULT,
105 WAIT_STREAM_HEADER_AFTER_AUTH,
106 WAIT_BIND_RESULT,
107 WAIT_SESSION_IQ_RESULT,
108 DONE,
109 ERROR,
112 // Callbacks for XmppStreamParser.
113 void OnStanza(scoped_ptr<buzz::XmlElement> stanza);
114 void OnParserError();
116 // Starts authentication handshake in WAIT_STREAM_HEADER_AFTER_TLS state.
117 void StartAuthHandshake();
119 // Helper used to send stream header.
120 void StartStream(const std::string& first_message);
122 // Report the |error| to the delegate and changes |state_| to ERROR,
123 void OnError(SignalStrategy::Error error);
125 std::string server_;
126 std::string username_;
127 std::string auth_token_;
128 TlsMode tls_mode_;
129 Delegate* delegate_;
131 State state_;
133 std::string jid_;
135 scoped_ptr<XmppStreamParser> stream_parser_;
137 DISALLOW_COPY_AND_ASSIGN(XmppLoginHandler);
140 } // namespace remoting
142 #endif // REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_