setup.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/setup.rst">Edit me on GitHub</a></div>
  3. .. _what-you-will-need:
  4. =====================
  5. Setup and integration
  6. =====================
  7. This page documents what you'll need to do to be able to connect Converse with
  8. your own XMPP server and to better integrate it into your website.
  9. At the very least you'll need Converse and an :ref:`XMPP server` with
  10. :ref:`websocket-section` or :ref:`BOSH-section` enabled. That's definitely
  11. enough to simply demo Converse or to do development work on it.
  12. For end-to-end encryption via OMEMO, you'll need to load `libsignal-protocol.js
  13. <https://github.com/signalapp/libsignal-protocol-javascript>`_ separately in
  14. your page. Take a look at the section on :ref:`libsignal <dependency-libsignal>` and the
  15. :ref:`security considerations around OMEMO <feature-omemo>`.
  16. If you want to more fully integrate it into a website
  17. then you'll likely need to set up more services and components.
  18. The diagram below shows a fairly common setup for a website or intranet:
  19. * Converse runs in the web-browser on the user's device.
  20. * It communicates with the XMPP server via BOSH or websocket which is usually
  21. reverse-proxied by a web-server in order to overcome cross-site scripting
  22. restrictions in the browser. For more info on that, read the section:
  23. `Overcoming cross-domain request restrictions`_
  24. * Optionally the XMPP server is configured to use a SQL database for storing
  25. archived chat messages.
  26. * Optionally there is a user directory such as Active Directory or LDAP, which
  27. the XMPP server is configured to use, so that users can log in with those
  28. credentials.
  29. * Usually (but optionally) there is a backend web application which hosts a
  30. website in which Converse appears.
  31. .. figure:: images/diagram.png
  32. :align: center
  33. :alt: A diagram of a possible setup, consisting of Converse, a web server, a backend web application, an XMPP server, a user directory such as LDAP and an XMPP server.
  34. This diagram shows the various services in a fairly common setup (image generated with `draw.io <https://draw.io>`_).
  35. ----------------------
  36. The various components
  37. ----------------------
  38. .. _`XMPP server`:
  39. An XMPP server
  40. ==============
  41. Converse uses `XMPP <http://xmpp.org/about-xmpp/>`_ as its
  42. messaging protocol, and therefore needs to connect to an XMPP/Jabber
  43. server (Jabber® is an older and more user-friendly synonym for XMPP).
  44. You can connect to public XMPP servers like ``conversejs.org`` but if you want to
  45. integrate Converse into your own website and to use your website's
  46. authentication sessions to log in users to the XMPP server (i.e. :ref:`session support <session-support>`)
  47. then you'll have to set up your own XMPP server.
  48. You can find a list of public XMPP servers/providers on `compliance.conversations.im <http://compliance.conversations.im/>`_
  49. and a list of servers that you can set up yourself on `xmpp.org <http://xmpp.org/xmpp-software/servers/>`_.
  50. .. _`BOSH-section`:
  51. BOSH
  52. ====
  53. Web-browsers do not allow the persistent, direct TCP socket connections used by
  54. desktop XMPP clients to communicate with XMPP servers.
  55. Instead, we have HTTP and websocket as available protocols.
  56. `BOSH`_ can be seen as XMPP-over-HTTP. In other words, it allows for XMPP
  57. stanzas to be sent over an HTTP connection.
  58. HTTP connections are stateless and usually shortlived.
  59. XMPP connections on the other hand are stateful and usually last much longer.
  60. So to enable a web application like Converse to communicate with an XMPP
  61. server, we need a proxy which acts as a bridge between these two protocols.
  62. This is the job of a BOSH connection manager. BOSH (Bidirectional-streams Over
  63. Synchronous HTTP) is a protocol for allowing XMPP communication over HTTP. The
  64. protocol is defined in `XEP-0206: XMPP Over BOSH <http://xmpp.org/extensions/xep-0206.html>`_.
  65. Popular XMPP servers such as `Ejabberd <http://www.ejabberd.im>`_,
  66. Prosody `(mod_bosh) <http://prosody.im/doc/setting_up_bosh>`_ and
  67. `OpenFire <http://www.igniterealtime.org/projects/openfire/>`_ all include
  68. their own BOSH connection managers (but you usually have to enable them in the
  69. configuration).
  70. However, if you intend to support multiple different servers (like
  71. https://conversejs.org does), then you'll need a standalone connection manager.
  72. For a standalone manager, see for example `Punjab <https://github.com/twonds/punjab>`_
  73. and `node-xmpp-bosh <https://github.com/dhruvbird/node-xmpp-bosh>`_.
  74. The demo on the `Converse homepage <https://conversejs.org>`_ uses a connection
  75. manager located at https://conversejs.org/http-bind.
  76. This connection manager is available for testing purposes only, please don't
  77. use it in production.
  78. Refer to the :ref:`bosh-service-url` configuration setting for information on
  79. how to configure Converse to connect to a BOSH URL.
  80. .. _`websocket-section`:
  81. Websocket
  82. =========
  83. Websockets provide an alternative means of connection to an XMPP server from
  84. your browser.
  85. Websockets provide long-lived, bidirectional connections which do not rely on
  86. HTTP. Therefore BOSH, which operates over HTTP, doesn't apply to websockets.
  87. `Prosody <http://prosody.im>`_ (from version 0.10) and `Ejabberd <http://www.ejabberd.im>`_ support websocket connections, as
  88. does the node-xmpp-bosh connection manager.
  89. Refer to the :ref:`websocket-url` configuration setting for information on how to
  90. configure Converse to connect to a websocket URL.
  91. The Webserver
  92. =============
  93. Lets say the domain under which you host Converse is *example.org:80*,
  94. but the domain of your connection manager or the domain of
  95. your HTTP file server (for `XEP-0363 HTTP File Upload <https://xmpp.org/extensions/xep-0363.html>`_)
  96. is at a different domain, either a different port like *example.org:5280* or a
  97. different name like *elsehwere.org*.
  98. In such a situation the same-origin security policy of the browser comes into force.
  99. For security purposes a browser does not by default allow a website to
  100. make certain types of requests to other domains.
  101. There are two ways in which you can solve this problem.
  102. .. _CORS:
  103. 1. Cross-Origin Resource Sharing (CORS)
  104. ---------------------------------------
  105. CORS is a technique for overcoming browser restrictions related to the
  106. `same-origin security policy <https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy>`_.
  107. CORS is enabled by adding an ``Access-Control-Allow-Origin`` header. Where this
  108. is configured depends on what webserver is used for your file upload server.
  109. 2. Reverse-proxy
  110. ----------------
  111. Another possible solution is to add a reverse proxy to a webserver such as Nginx or Apache to ensure that
  112. all services you use are hosted under the same domain name and port.
  113. Examples:
  114. *********
  115. Assuming your site is accessible on port ``80`` for the domain ``mysite.com``
  116. and your connection manager manager is running at ``someothersite.com/http-bind``.
  117. The *bosh_service_url* value you want to give Converse to overcome
  118. the cross-domain restriction is ``mysite.com/http-bind`` and not
  119. ``someothersite.com/http-bind``.
  120. Your ``nginx`` or ``apache`` configuration will look as follows:
  121. Nginx
  122. ~~~~~
  123. .. code-block:: nginx
  124. http {
  125. server {
  126. listen 80
  127. server_name mysite.com;
  128. location = / {
  129. root /path/to/converse.js/; # Properly set the path here
  130. index index.html;
  131. }
  132. location ~ ^/http-bind/ {
  133. proxy_pass http://someothersite.com;
  134. }
  135. # CORS
  136. location ~ .(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
  137. add_header Access-Control-Allow-Origin "*"; # Decide here whether you want to allow all or only a particular domain
  138. root /path/to/converse.js/; # Properly set the path here
  139. }
  140. }
  141. }
  142. Apache
  143. ~~~~~~
  144. .. code-block:: apache
  145. <VirtualHost *:80>
  146. ServerName mysite.com
  147. RewriteEngine On
  148. RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
  149. </VirtualHost>
  150. .. note::
  151. If you're getting XML parsing errors for your BOSH endpoint, for
  152. example::
  153. XML Parsing Error: mismatched tag. Expected: </hr>.
  154. Location: https://example.org/http-bind/
  155. Line Number 6, Column 3: bosh-anon:6:3
  156. Also ERROR: request id 12.2 error 504 happened
  157. Then your BOSH proxy is returning an HTML error page (for a 504 error in
  158. the above example).
  159. This might be because your webserver and BOSH proxy have the same timeout
  160. for BOSH requests. Because the webserver receives the request slightly earlier,
  161. it gives up a few microseconds before the XMPP server’s empty result and thus returns a
  162. 504 error page containing HTML to browser, which then gets parsed as if its
  163. XML.
  164. To fix this, make sure that the webserver's timeout is slightly higher.
  165. In Nginx you can do this by adding ``proxy_read_timeout 61;``;
  166. From Converse 4.0.0 onwards the default ``wait`` time is set to 59 seconds, to avoid
  167. this problem.
  168. .. _`session-support`:
  169. Single Session Support
  170. ======================
  171. It's possible to enable shared sessions whereby users already
  172. logged in to your website will also automatically be logged in on the XMPP server,
  173. Once a user is logged in, the session will be kept alive across page loads by
  174. way of the :ref:`keepalive` setting.
  175. There are a few ways to let your users be automatically authenticated to an
  176. XMPP server once they've logged in to your site.
  177. Option 1). Server-side authentication via BOSH prebinding
  178. ---------------------------------------------------------
  179. To **prebind** refers to a technique whereby your web application sets up an
  180. authenticated BOSH session with the XMPP server or a standalone `BOSH <http://xmpp.org/about-xmpp/technology-overview/bosh/>`_
  181. connection manager.
  182. Once authenticated, it receives RID and SID tokens which need to be passed
  183. on to Converse. Converse will then attach to that same session using
  184. those tokens.
  185. It's called "prebind" because you bind to the BOSH session beforehand, and then
  186. later in the page you just attach to that session again.
  187. The RID and SID tokens can be passed in manually when calling
  188. `converse.initialize`, but a more convenient way is to pass Converse a :ref:`prebind_url`
  189. which it will call when it needs the tokens. This way it will be able to
  190. automatically reconnect whenever the connection drops, by simply calling that
  191. URL again to fetch new tokens.
  192. Prebinding reduces network traffic and also speeds up the startup time for
  193. Converse. Additionally, because prebind works with tokens, it's not necessary
  194. for the XMPP client to know or store users' passwords.
  195. One potential drawback of using prebind is that in order to establish the
  196. authenticated BOSH session server-side, you'll need to access and pass on the XMPP
  197. credentials server-side, which, unless you're using tokens, means that you'll
  198. need to store XMPP passwords in cleartext.
  199. This is however not the case if you for example use LDAP or Active Directory as
  200. your authentication backend, since you could then configure your XMPP server to
  201. use that as well.
  202. To prebind you will require a BOSH-enabled XMPP server for Converse to connect to
  203. (see the :ref:`bosh-service-url` under :ref:`configuration-settings`)
  204. as well as a BOSH client in your web application (written for example in
  205. Python, Ruby or PHP) that will set up an authenticated BOSH session, which
  206. Converse can then attach to.
  207. .. note::
  208. A BOSH server acts as a bridge between HTTP, the protocol of the web, and
  209. XMPP, the instant messaging protocol.
  210. Converse can only communicate via HTTP (or websocket, in which case BOSH can't be used).
  211. It cannot open TCP sockets to communicate to an XMPP server directly.
  212. So the BOSH server acts as a middle man, translating our HTTP requests into XMPP stanzas and vice versa.
  213. Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe>`_
  214. about this and even provides an
  215. `example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach>`_
  216. to demonstrate it.
  217. When you authenticate to the XMPP server on your backend application (for
  218. example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
  219. The **Session ID (SID)** is a unique identifier for the current *session*. This
  220. number stays constant for the entire session.
  221. The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
  222. page load). Each page load is a new request which requires a new unique RID.
  223. The best way to achieve this is to simply increment the RID with each page
  224. load.
  225. You'll need to configure Converse with the ``prebind``, :ref:`keepalive` and
  226. :ref:`prebind_url` settings.
  227. Please read the documentation on those settings for a fuller picture of what
  228. needs to be done.
  229. Example code for server-side prebinding
  230. ***************************************
  231. * PHP:
  232. See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php>`_ by
  233. Michael Weibel and the folks from Candy chat.
  234. * Python:
  235. See this `example Django application`_ by Jack Moffitt.
  236. Option 2). Delegated authentication, also called external authentication
  237. ------------------------------------------------------------------------
  238. Delegated authentication refers to the usecase where the XMPP server delegates
  239. authentication to some other service.
  240. This could be to LDAP or Active Directory (as shown in the diagram at the top
  241. of the page), or it could be to an OAuth provider, a SQL server to a specific
  242. website.
  243. The Prosody webserver has various user-contributed modules which delegate
  244. authentication to external services. They are listed in the `Prosody community modules
  245. page <https://modules.prosody.im/>`_. Other XMPP servers have similar plugin modules.
  246. If your web-application has access to the same credentials, it can send those
  247. credentials to Converse so that user's are automatically logged in when the
  248. page loads.
  249. This is can be done by setting :ref:`auto_login` to true and configuring the
  250. the :ref:`credentials_url` setting.
  251. Option 3). Temporary authentication tokens
  252. ------------------------------------------
  253. The first option has the drawback that your web-application needs to know the
  254. XMPP credentials of your users and that they need to be stored in the clear.
  255. The second option has that same drawback and it also needs to pass those
  256. credentials to Converse.
  257. To avoid these drawbacks, you can instead let your backend web application
  258. generate temporary authentication tokens which are then sent to the XMPP server
  259. which in turn delegates authentication to an external authentication provider
  260. (generally the same web-app that generated the tokens).
  261. This can be combined with prebind or with the :ref:`credentials_url` setting.
  262. Option 4). Cryptographically signed tokens
  263. ------------------------------------------
  264. A third potential option is to generate cryptographically signed tokens (e.g.
  265. HMAC tokens) which the XMPP server could authenticate by checking that they're
  266. signed with the right key and that they conform to some kind of pre-arranged
  267. format.
  268. In this case, you would also use the :ref:`credentials_url` setting, to specify a
  269. URL from which Converse should fetch the username and token.