Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

authenticateAndUpgradeRole.js 5.2KB

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