modified lib-jitsi-meet dev repo
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.5KB

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.onLoginSuccessful] - Callback called when logging
  41. * into the XMPP server was successful. The next step will be to obtain a new
  42. * session ID from Jicofo and join the MUC using it which will effectively
  43. * upgrade the user's role to moderator.
  44. * @returns {Object} A <tt>thenable</tt> which (1) settles when the process of
  45. * authenticating and upgrading the role of the specified XMPP user finishes and
  46. * (2) has a <tt>cancel</tt> method that allows the caller to interrupt the
  47. * process. If the process finishes successfully, the session ID has been stored
  48. * in the settings and the <tt>thenable</tt> is resolved. If the process
  49. * finishes with failure, the <tt>thenable</tt> is rejected with reason of type
  50. * {@link UpgradeRoleError} which will have either <tt>connectionError</tt> or
  51. * <tt>authenticationError</tt> property set depending on which of the steps has
  52. * failed. If <tt>cancel</tt> is called before the process finishes, then the
  53. * thenable will be rejected with an empty object (i.e. no error property will
  54. * be set on the rejection reason).
  55. */
  56. export default function authenticateAndUpgradeRole({
  57. // 1. Log the specified XMPP user in.
  58. id,
  59. password,
  60. onCreateResource,
  61. // 2. Let the API client/consumer know as soon as the XMPP user has been
  62. // successfully logged in.
  63. onLoginSuccessful
  64. }) {
  65. let canceled = false;
  66. let rejectPromise;
  67. let xmpp = new XMPP(this.connection.options);
  68. const process = new Promise((resolve, reject) => {
  69. // The process is represented by a Thenable with a cancel method. The
  70. // Thenable is implemented using Promise and the cancel using the
  71. // Promise's reject function.
  72. rejectPromise = reject;
  73. xmpp.addListener(
  74. CONNECTION_DISCONNECTED,
  75. () => {
  76. xmpp = undefined;
  77. });
  78. xmpp.addListener(
  79. CONNECTION_ESTABLISHED,
  80. () => {
  81. if (canceled) {
  82. return;
  83. }
  84. // Let the caller know that the XMPP login was successful.
  85. onLoginSuccessful && onLoginSuccessful();
  86. // Now authenticate with Jicofo and get a new session ID.
  87. const room = xmpp.createRoom(
  88. this.options.name,
  89. this.options.config,
  90. onCreateResource
  91. );
  92. room.moderator.authenticate()
  93. .then(() => {
  94. xmpp && xmpp.disconnect();
  95. if (canceled) {
  96. return;
  97. }
  98. // At this point we should have the new session ID
  99. // stored in the settings. Send a new conference IQ.
  100. this.room.moderator.allocateConferenceFocus();
  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 */