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.

AuthHandler.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* global APP, config, JitsiMeetJS, Promise */
  2. import LoginDialog from './LoginDialog';
  3. import UIUtil from '../util/UIUtil';
  4. import {openConnection} from '../../../connection';
  5. const ConferenceEvents = JitsiMeetJS.events.conference;
  6. const ConnectionErrors = JitsiMeetJS.errors.connection;
  7. let externalAuthWindow;
  8. let authRequiredDialog;
  9. /**
  10. * Authenticate using external service or just focus
  11. * external auth window if there is one already.
  12. *
  13. * @param {JitsiConference} room
  14. * @param {string} [lockPassword] password to use if the conference is locked
  15. */
  16. function doExternalAuth (room, lockPassword) {
  17. if (externalAuthWindow) {
  18. externalAuthWindow.focus();
  19. return;
  20. }
  21. if (room.isJoined()) {
  22. room.getExternalAuthUrl(true).then(function (url) {
  23. externalAuthWindow = LoginDialog.showExternalAuthDialog(
  24. url,
  25. function () {
  26. externalAuthWindow = null;
  27. room.join(lockPassword);
  28. }
  29. );
  30. });
  31. } else {
  32. // If conference has not been started yet
  33. // then redirect to login page
  34. room.getExternalAuthUrl().then(UIUtil.redirect);
  35. }
  36. }
  37. /**
  38. * Authenticate on the server.
  39. * @param {JitsiConference} room
  40. * @param {string} [lockPassword] password to use if the conference is locked
  41. */
  42. function doXmppAuth (room, lockPassword) {
  43. let loginDialog = LoginDialog.showAuthDialog(function (id, password) {
  44. // auth "on the fly":
  45. // 1. open new connection with proper id and password
  46. // 2. connect to the room
  47. // (this will store sessionId in the localStorage)
  48. // 3. close new connection
  49. // 4. reallocate focus in current room
  50. openConnection({id, password, roomName: room.getName()}).then(
  51. function (connection) {
  52. // open room
  53. let newRoom = connection.initJitsiConference(
  54. room.getName(), APP.conference._getConferenceOptions()
  55. );
  56. loginDialog.displayConnectionStatus(
  57. APP.translation.translateString('connection.FETCH_SESSION_ID')
  58. );
  59. newRoom.room.moderator.authenticate().then(function () {
  60. connection.disconnect();
  61. loginDialog.displayConnectionStatus(
  62. APP.translation.translateString('connection.GOT_SESSION_ID')
  63. );
  64. // authenticate conference on the fly
  65. room.join(lockPassword);
  66. loginDialog.close();
  67. }).catch(function (error, code) {
  68. connection.disconnect();
  69. console.error('Auth on the fly failed', error);
  70. let errorMsg = APP.translation.translateString(
  71. 'connection.GET_SESSION_ID_ERROR'
  72. );
  73. loginDialog.displayError(errorMsg + code);
  74. });
  75. }, function (err) {
  76. loginDialog.displayError(err);
  77. });
  78. }, function () { // user canceled
  79. loginDialog.close();
  80. });
  81. }
  82. /**
  83. * Authenticate for the conference.
  84. * Uses external service for auth if conference supports that.
  85. * @param {JitsiConference} room
  86. * @param {string} [lockPassword] password to use if the conference is locked
  87. */
  88. function authenticate (room, lockPassword) {
  89. if (room.isExternalAuthEnabled()) {
  90. doExternalAuth(room, lockPassword);
  91. } else {
  92. doXmppAuth(room, lockPassword);
  93. }
  94. }
  95. /**
  96. * De-authenticate local user.
  97. *
  98. * @param {JitsiConference} room
  99. * @param {string} [lockPassword] password to use if the conference is locked
  100. * @returns {Promise}
  101. */
  102. function logout (room) {
  103. return new Promise(function (resolve) {
  104. room.room.moderator.logout(resolve);
  105. }).then(function (url) {
  106. // de-authenticate conference on the fly
  107. if (room.isJoined()) {
  108. room.join();
  109. }
  110. return url;
  111. });
  112. }
  113. /**
  114. * Notify user that authentication is required to create the conference.
  115. * @param {JitsiConference} room
  116. * @param {string} [lockPassword] password to use if the conference is locked
  117. */
  118. function requireAuth(room, lockPassword) {
  119. if (authRequiredDialog) {
  120. return;
  121. }
  122. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  123. room.getName(), authenticate.bind(null, room, lockPassword)
  124. );
  125. }
  126. /**
  127. * Close auth-related dialogs if there are any.
  128. */
  129. function closeAuth() {
  130. if (externalAuthWindow) {
  131. externalAuthWindow.close();
  132. externalAuthWindow = null;
  133. }
  134. if (authRequiredDialog) {
  135. authRequiredDialog.close();
  136. authRequiredDialog = null;
  137. }
  138. }
  139. function showXmppPasswordPrompt(roomName, connect) {
  140. return new Promise(function (resolve, reject) {
  141. let authDialog = LoginDialog.showAuthDialog(
  142. function (id, password) {
  143. connect(id, password, roomName).then(function (connection) {
  144. authDialog.close();
  145. resolve(connection);
  146. }, function (err) {
  147. if (err === ConnectionErrors.PASSWORD_REQUIRED) {
  148. authDialog.displayError(err);
  149. } else {
  150. authDialog.close();
  151. reject(err);
  152. }
  153. });
  154. }
  155. );
  156. });
  157. }
  158. /**
  159. * Show Authentication Dialog and try to connect with new credentials.
  160. * If failed to connect because of PASSWORD_REQUIRED error
  161. * then ask for password again.
  162. * @param {string} [roomName] name of the conference room
  163. * @param {function(id, password, roomName)} [connect] function that returns
  164. * a Promise which resolves with JitsiConnection or fails with one of
  165. * ConnectionErrors.
  166. * @returns {Promise<JitsiConnection>}
  167. */
  168. function requestAuth(roomName, connect) {
  169. return showXmppPasswordPrompt(roomName, connect);
  170. }
  171. export default {
  172. authenticate,
  173. requireAuth,
  174. requestAuth,
  175. closeAuth,
  176. logout
  177. };