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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* global JitsiMeetJS, APP */
  2. import LoginDialog from './LoginDialog';
  3. import UIEvents from '../../../service/UI/UIEvents';
  4. import UIUtil from '../util/UIUtil';
  5. import {openConnection} from '../../../connection';
  6. const ConferenceEvents = JitsiMeetJS.events.conference;
  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}).then(function (connection) {
  51. // open room
  52. let newRoom = connection.initJitsiConference(
  53. room.getName(), APP.conference._getConferenceOptions()
  54. );
  55. loginDialog.displayConnectionStatus(
  56. APP.translation.translateString('connection.FETCH_SESSION_ID')
  57. );
  58. newRoom.room.moderator.authenticate().then(function () {
  59. connection.disconnect();
  60. loginDialog.displayConnectionStatus(
  61. APP.translation.translateString('connection.GOT_SESSION_ID')
  62. );
  63. // authenticate conference on the fly
  64. room.join(lockPassword);
  65. loginDialog.close();
  66. }).catch(function (error, code) {
  67. connection.disconnect();
  68. console.error('Auth on the fly failed', error);
  69. let errorMsg = APP.translation.translateString(
  70. 'connection.GET_SESSION_ID_ERROR'
  71. );
  72. loginDialog.displayError(errorMsg + code);
  73. });
  74. }, function (err) {
  75. loginDialog.displayError(err);
  76. });
  77. }, function () { // user canceled
  78. loginDialog.close();
  79. });
  80. }
  81. /**
  82. * Authenticate for the conference.
  83. * Uses external service for auth if conference supports that.
  84. * @param {JitsiConference} room
  85. * @param {string} [lockPassword] password to use if the conference is locked
  86. */
  87. function authenticate (room, lockPassword) {
  88. if (room.isExternalAuthEnabled()) {
  89. doExternalAuth(room, lockPassword);
  90. } else {
  91. doXmppAuth(room, lockPassword);
  92. }
  93. }
  94. /**
  95. * De-authenticate local user.
  96. *
  97. * @param {JitsiConference} room
  98. * @param {string} [lockPassword] password to use if the conference is locked
  99. * @returns {Promise}
  100. */
  101. function logout (room) {
  102. return new Promise(function (resolve) {
  103. room.room.moderator.logout(resolve);
  104. }).then(function (url) {
  105. // de-authenticate conference on the fly
  106. if (room.isJoined()) {
  107. room.join();
  108. }
  109. return url;
  110. });
  111. }
  112. /**
  113. * Notify user that authentication is required to create the conference.
  114. * @param {JitsiConference} room
  115. * @param {string} [lockPassword] password to use if the conference is locked
  116. */
  117. function requireAuth(room, lockPassword) {
  118. if (authRequiredDialog) {
  119. return;
  120. }
  121. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  122. room.getName(), authenticate.bind(null, room, lockPassword)
  123. );
  124. }
  125. /**
  126. * Close auth-related dialogs if there are any.
  127. */
  128. function closeAuth() {
  129. if (externalAuthWindow) {
  130. externalAuthWindow.close();
  131. externalAuthWindow = null;
  132. }
  133. if (authRequiredDialog) {
  134. authRequiredDialog.close();
  135. authRequiredDialog = null;
  136. }
  137. }
  138. export default {
  139. authenticate,
  140. requireAuth,
  141. closeAuth,
  142. logout
  143. };