Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

authenticateAndUpgradeRole.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. CONNECTION_DISCONNECTED,
  3. CONNECTION_ESTABLISHED,
  4. CONNECTION_FAILED
  5. } from './JitsiConnectionEvents';
  6. import XMPP from './modules/xmpp/xmpp';
  7. /**
  8. * @typedef {Object} UpgradeRoleError
  9. *
  10. * @property {JitsiConnectionErrors} [connectionError] - One of
  11. * {@link JitsiConnectionErrors} which occurred when trying to connect to the
  12. * XMPP server.
  13. * @property {String} [authenticationError] - One of XMPP error conditions
  14. * returned by Jicofo on authentication attempt. See
  15. * {@link https://xmpp.org/rfcs/rfc3920.html#streams-error}.
  16. * @property {String} [message] - More details about the error.
  17. * @property {Object} [credentials] - The credentials that failed the
  18. * authentication.
  19. * @property {String} [credentials.jid] - The XMPP ID part of the credentials
  20. * that failed the authentication.
  21. * @property {string} [credentials.password] - The password part of the
  22. * credentials that failed the authentication.
  23. *
  24. * NOTE If neither one of the errors is present, then the operation has been
  25. * canceled.
  26. */
  27. /* eslint-disable no-invalid-this */
  28. /**
  29. * Connects to the XMPP server using the specified credentials and contacts
  30. * Jicofo in order to obtain a session ID (which is then stored in the local
  31. * storage). The user's role of the parent conference will be upgraded to
  32. * moderator (by Jicofo). It's also used to join the conference when starting
  33. * from anonymous domain and only authenticated users are allowed to create new
  34. * rooms.
  35. *
  36. * @param {Object} options
  37. * @param {string} options.id - XMPP user's ID to log in. For example,
  38. * user@xmpp-server.com.
  39. * @param {string} options.password - XMPP user's password to log in with.
  40. * @param {Function} [options.onCreateResource]
  41. * @param {Function} [options.onLoginSuccessful] - Callback called when logging
  42. * into the XMPP server was successful. The next step will be to obtain a new
  43. * session ID from Jicofo and join the MUC using it which will effectively
  44. * upgrade the user's role to moderator.
  45. * @returns {Object} A <tt>thenable</tt> which (1) settles when the process of
  46. * authenticating and upgrading the role of the specified XMPP user finishes and
  47. * (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
  48. * process. If the process finishes successfully, the session ID has been stored
  49. * in the settings and the <tt>thenable</tt> is resolved. If the process
  50. * finishes with failure, the <tt>thenable</tt> is rejected with reason of type
  51. * {@link UpgradeRoleError} which will have either <tt>connectionError</tt> or
  52. * <tt>authenticationError</tt> property set depending on which of the steps has
  53. * failed. If <tt>cancel</tt> is called before the process finishes, then the
  54. * thenable will be rejected with an empty object (i.e. no error property will
  55. * be set on the rejection reason).
  56. */
  57. export default function authenticateAndUpgradeRole({
  58. // 1. Log the specified XMPP user in.
  59. id,
  60. password,
  61. onCreateResource,
  62. // 2. Let the API client/consumer know as soon as the XMPP user has been
  63. // successfully logged in.
  64. onLoginSuccessful
  65. }) {
  66. let canceled = false;
  67. let rejectPromise;
  68. let xmpp = new XMPP(this.connection.options);
  69. const process = new Promise((resolve, reject) => {
  70. // The process is represented by a Thenable with a cancel method. The
  71. // Thenable is implemented using Promise and the cancel using the
  72. // Promise's reject function.
  73. rejectPromise = reject;
  74. xmpp.addListener(
  75. CONNECTION_DISCONNECTED,
  76. () => {
  77. xmpp = undefined;
  78. });
  79. xmpp.addListener(
  80. CONNECTION_ESTABLISHED,
  81. () => {
  82. if (canceled) {
  83. return;
  84. }
  85. // Let the caller know that the XMPP login was successful.
  86. onLoginSuccessful && onLoginSuccessful();
  87. // Now authenticate with Jicofo and get a new session ID.
  88. const room = xmpp.createRoom(
  89. this.options.name,
  90. this.options.config,
  91. onCreateResource
  92. );
  93. room.xmpp.moderator.authenticate(room.roomjid)
  94. .then(() => {
  95. xmpp && xmpp.disconnect();
  96. if (canceled) {
  97. return;
  98. }
  99. // we execute this logic in JitsiConference where we bind the current conference as `this`
  100. // At this point we should have the new session ID
  101. // stored in the settings. Send a new conference IQ.
  102. this.room.xmpp.moderator.sendConferenceRequest(this.room.roomjid).finally(resolve);
  103. })
  104. .catch(({ error, message }) => {
  105. xmpp.disconnect();
  106. reject({
  107. authenticationError: error,
  108. message
  109. });
  110. });
  111. });
  112. xmpp.addListener(
  113. CONNECTION_FAILED,
  114. (connectionError, message, credentials) => {
  115. reject({
  116. connectionError,
  117. credentials,
  118. message
  119. });
  120. xmpp = undefined;
  121. });
  122. canceled || xmpp.connect(id, password);
  123. });
  124. /**
  125. * Cancels the process, if it's in progress, of authenticating and upgrading
  126. * the role of the local participant/user.
  127. *
  128. * @public
  129. * @returns {void}
  130. */
  131. process.cancel = () => {
  132. canceled = true;
  133. rejectPromise({});
  134. xmpp && xmpp.disconnect();
  135. };
  136. return process;
  137. }
  138. /* eslint-enable no-invalid-this */