選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

authenticateAndUpgradeRole.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. onCreateResource,
  62. // 2. Let the API client/consumer know as soon as the XMPP user has been
  63. // successfully logged in.
  64. onLoginSuccessful,
  65. // 3. Join the MUC.
  66. roomPassword
  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.moderator.authenticate()
  96. .then(() => {
  97. xmpp && xmpp.disconnect();
  98. if (canceled) {
  99. return;
  100. }
  101. // At this point we should have the new session ID
  102. // stored in the settings. Jicofo will allow to join the
  103. // room.
  104. this.join(roomPassword);
  105. resolve();
  106. })
  107. .catch(({ error, message }) => {
  108. xmpp.disconnect();
  109. reject({
  110. authenticationError: error,
  111. message
  112. });
  113. });
  114. });
  115. xmpp.addListener(
  116. CONNECTION_FAILED,
  117. (connectionError, message, credentials) => {
  118. reject({
  119. connectionError,
  120. credentials,
  121. message
  122. });
  123. xmpp = undefined;
  124. });
  125. canceled || xmpp.connect(id, password);
  126. });
  127. /**
  128. * Cancels the process, if it's in progress, of authenticating and upgrading
  129. * the role of the local participant/user.
  130. *
  131. * @public
  132. * @returns {void}
  133. */
  134. process.cancel = () => {
  135. canceled = true;
  136. rejectPromise({});
  137. xmpp && xmpp.disconnect();
  138. };
  139. return process;
  140. }
  141. /* eslint-enable no-invalid-this */