2
0

session.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Session Management
  6. ==================
  7. .. _`session-support`:
  8. Shared Sessions
  9. ===============
  10. It's possible to enable shared sessions whereby users already
  11. logged in to your website will also automatically be logged in on the XMPP server,
  12. Once a user is logged in, the session will be kept alive across page loads.
  13. There are a few ways to let your users be automatically authenticated to an
  14. XMPP server once they've logged in to your site.
  15. Option 1). Server-side authentication via BOSH prebinding
  16. ---------------------------------------------------------
  17. To **prebind** refers to a technique whereby your web application sets up an
  18. authenticated BOSH session with the XMPP server or a standalone `BOSH <https://xmpp.org/about-xmpp/technology-overview/bosh/>`_
  19. connection manager.
  20. Once authenticated, it receives RID and SID tokens which need to be passed
  21. on to Converse. Converse will then attach to that same session using
  22. those tokens.
  23. It's called "prebind" because you bind to the BOSH session beforehand, and then
  24. later in the page you just attach to that session again.
  25. The RID and SID tokens can be passed in manually when calling
  26. `converse.initialize`, but a more convenient way is to pass Converse a :ref:`prebind_url`
  27. which it will call when it needs the tokens. This way it will be able to
  28. automatically reconnect whenever the connection drops, by simply calling that
  29. URL again to fetch new tokens.
  30. Prebinding reduces network traffic and also speeds up the startup time for
  31. Converse. Additionally, because prebind works with tokens, it's not necessary
  32. for the XMPP client to know or store users' passwords.
  33. One potential drawback of using prebind is that in order to establish the
  34. authenticated BOSH session server-side, you'll need to access and pass on the XMPP
  35. credentials server-side, which, unless you're using tokens, means that you'll
  36. need to store XMPP passwords in cleartext.
  37. This is however not the case if you for example use LDAP or Active Directory as
  38. your authentication backend, since you could then configure your XMPP server to
  39. use that as well.
  40. To prebind you will require a BOSH-enabled XMPP server for Converse to connect to
  41. (see the :ref:`bosh-service-url` under :ref:`configuration-settings`)
  42. as well as a BOSH client in your web application (written for example in
  43. Python, Ruby or PHP) that will set up an authenticated BOSH session, which
  44. Converse can then attach to.
  45. .. note::
  46. A BOSH server acts as a bridge between HTTP, the protocol of the web, and
  47. XMPP, the instant messaging protocol.
  48. Converse can only communicate via HTTP (or websocket, in which case BOSH can't be used).
  49. It cannot open TCP sockets to communicate to an XMPP server directly.
  50. So the BOSH server acts as a middle man, translating our HTTP requests into XMPP stanzas and vice versa.
  51. Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe>`_
  52. about this and even provides an
  53. `example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach>`_
  54. to demonstrate it.
  55. When you authenticate to the XMPP server on your backend application (for
  56. example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
  57. The **Session ID (SID)** is a unique identifier for the current *session*. This
  58. number stays constant for the entire session.
  59. The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
  60. page load). Each page load is a new request which requires a new unique RID.
  61. The best way to achieve this is to simply increment the RID with each page
  62. load.
  63. You'll need to configure Converse with the :ref:`prebind` :ref:`prebind_url` settings.
  64. Please read the documentation on those settings for a fuller picture of what
  65. needs to be done.
  66. Example code for server-side prebinding
  67. ***************************************
  68. * PHP:
  69. See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php>`_ by
  70. Michael Weibel and the folks from Candy chat.
  71. * Python:
  72. See this `example Django application`_ by Jack Moffitt.
  73. Option 2). Delegated authentication, also called external authentication
  74. ------------------------------------------------------------------------
  75. Delegated authentication refers to the usecase where the XMPP server delegates
  76. authentication to some other service.
  77. This could be to LDAP or Active Directory (as shown in the diagram at the top
  78. of the page), or it could be to an OAuth provider, a SQL server to a specific
  79. website.
  80. The Prosody webserver has various user-contributed modules which delegate
  81. authentication to external services. They are listed in the `Prosody community modules
  82. page <https://modules.prosody.im/>`_. Other XMPP servers have similar plugin modules.
  83. If your web-application has access to the same credentials, it can send those
  84. credentials to Converse so that user's are automatically logged in when the
  85. page loads.
  86. This is can be done by setting :ref:`auto_login` to true and configuring the
  87. the :ref:`credentials_url` setting.
  88. Option 3). Temporary authentication tokens
  89. ------------------------------------------
  90. The first option has the drawback that your web-application needs to know the
  91. XMPP credentials of your users and that they need to be stored in the clear.
  92. The second option has that same drawback and it also needs to pass those
  93. credentials to Converse.
  94. To avoid these drawbacks, you can instead let your backend web application
  95. generate temporary authentication tokens which are then sent to the XMPP server
  96. which in turn delegates authentication to an external authentication provider
  97. (generally the same web-app that generated the tokens).
  98. This can be combined with prebind or with the :ref:`credentials_url` setting.
  99. Option 4). Cryptographically signed tokens
  100. ------------------------------------------
  101. A third potential option is to generate cryptographically signed tokens (e.g.
  102. HMAC tokens) which the XMPP server could authenticate by checking that they're
  103. signed with the right key and that they conform to some kind of pre-arranged
  104. format.
  105. In this case, you would also use the :ref:`credentials_url` setting, to specify a
  106. URL from which Converse should fetch the username and token.
  107. Keeping users logged-in across page reloads
  108. ===========================================
  109. If you've properly set up :ref:`shared session support <session-support>`, then
  110. your users will stay logged-in to the XMPP server upon page reloads.
  111. However, if users are logging in manually, then users might get logged out between requests.
  112. Credential Management API
  113. -------------------------
  114. Users with modern browsers which properly support the
  115. `Credential Management API <https://w3c.github.io/webappsec-credential-management>`_
  116. should be automatically logged-in across page reloads and therefore maintain
  117. their sessions.
  118. Using a cookie
  119. --------------
  120. The main reason why users can get logged-out between page reloads is because we
  121. don't (and can't) use cookies to maintain user sessions as is usually done with
  122. websites.
  123. This is because XMPP servers generally don't have support for logging in with a
  124. cookie. It would be theoretically possible to login with SASL-EXTERNAL and a
  125. cookie which the XMPP server looks up as part of the BOSH HTTP request or the
  126. websocket connection, but no XMPP servers currently support this out of the
  127. box.
  128. Prosody does have a plugin called `mod_auth_http_cookie <https://modules.prosody.im/mod_auth_http_cookie.html>`_
  129. which does the above. You'd have to `configure Converse.js to use SASL-EXTERNAL <https://opkode.com/blog/strophe_converse_sasl_external/>`_
  130. and then set up Prosody with that plugin. (Note, I haven't yet tested this setup personally).
  131. This is however not a cross-platform solution and won't work for hosters who
  132. want to support all or multple XMPP servers.
  133. Storing the password in localStorage
  134. ------------------------------------
  135. Since cookies are usually not an option, people have suggested storing the
  136. password in localStorage and logging in with it again when the user reloads the
  137. page.
  138. We've purposefully not put this functionality in Converse.js due to the
  139. security implications of storing plaintext passwords in localStorage.
  140. Storing the SASL SCRAM-SHA1 hash in IndexedDB
  141. ---------------------------------------------
  142. Another suggestion that's been suggested is to store the SCRAM-SHA1 computed
  143. ``clientKey`` in localStorage and to use that upon page reload to log the user in again.
  144. We might implement this feature in core Converse.js eventually.
  145. As always, contributions welcome!