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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 {string} [options.roomPassword] - The password to join the MUC with.
  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. // 2. Let the API client/consumer know as soon as the XMPP user has been
  62. // successfully logged in.
  63. onLoginSuccessful,
  64. // 3. Join the MUC.
  65. roomPassword
  66. }) {
  67. let canceled = false;
  68. let rejectPromise;
  69. let xmpp = new XMPP(this.connection.options);
  70. const process = new Promise((resolve, reject) => {
  71. // The process is represented by a Thenable with a cancel method. The
  72. // Thenable is implemented using Promise and the cancel using the
  73. // Promise's reject function.
  74. rejectPromise = reject;
  75. xmpp.addListener(
  76. CONNECTION_DISCONNECTED,
  77. () => {
  78. xmpp = undefined;
  79. });
  80. xmpp.addListener(
  81. CONNECTION_ESTABLISHED,
  82. () => {
  83. if (canceled) {
  84. return;
  85. }
  86. // Let the caller know that the XMPP login was successful.
  87. onLoginSuccessful && onLoginSuccessful();
  88. // Now authenticate with Jicofo and get a new session ID.
  89. const room
  90. = xmpp.createRoom(this.options.name, this.options.config);
  91. room.moderator.authenticate()
  92. .then(() => {
  93. xmpp && xmpp.disconnect();
  94. if (canceled) {
  95. return;
  96. }
  97. // At this point we should have the new session ID
  98. // stored in the settings. Jicofo will allow to join the
  99. // room.
  100. this.join(roomPassword);
  101. resolve();
  102. })
  103. .catch(({ error, message }) => {
  104. xmpp.disconnect();
  105. reject({
  106. authenticationError: error,
  107. message
  108. });
  109. });
  110. });
  111. xmpp.addListener(
  112. CONNECTION_FAILED,
  113. (connectionError, message, credentials) => {
  114. reject({
  115. connectionError,
  116. credentials,
  117. message
  118. });
  119. xmpp = undefined;
  120. });
  121. canceled || xmpp.connect(id, password);
  122. });
  123. /**
  124. * Cancels the process, if it's in progress, of authenticating and upgrading
  125. * the role of the local participant/user.
  126. *
  127. * @public
  128. * @returns {void}
  129. */
  130. process.cancel = () => {
  131. canceled = true;
  132. rejectPromise({});
  133. xmpp && xmpp.disconnect();
  134. };
  135. return process;
  136. }
  137. /* eslint-enable no-invalid-this */