You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

authenticateAndUpgradeRole.js 6.2KB

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