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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. if (room.isJoined()) {
  64. // just reallocate focus if already joined
  65. room.room.moderator.allocateConferenceFocus();
  66. } else {
  67. // or join
  68. room.join(lockPassword);
  69. }
  70. loginDialog.close();
  71. }).catch(function (error, code) {
  72. connection.disconnect();
  73. console.error('Auth on the fly failed', error);
  74. let errorMsg = APP.translation.translateString(
  75. 'connection.GET_SESSION_ID_ERROR'
  76. );
  77. loginDialog.displayError(errorMsg + code);
  78. });
  79. }, function (err) {
  80. loginDialog.displayError(err);
  81. });
  82. }, function () { // user canceled
  83. loginDialog.close();
  84. });
  85. }
  86. /**
  87. * Authenticate for the conference.
  88. * Uses external service for auth if conference supports that.
  89. * @param {JitsiConference} room
  90. * @param {string} [lockPassword] password to use if the conference is locked
  91. */
  92. function authenticate (room, lockPassword) {
  93. if (room.isExternalAuthEnabled()) {
  94. doExternalAuth(room, lockPassword);
  95. } else {
  96. doXmppAuth(room, lockPassword);
  97. }
  98. }
  99. /**
  100. * Notify user that authentication is required to create the conference.
  101. * @param {JitsiConference} room
  102. * @param {string} [lockPassword] password to use if the conference is locked
  103. */
  104. function requireAuth(room, lockPassword) {
  105. if (authRequiredDialog) {
  106. return;
  107. }
  108. authRequiredDialog = LoginDialog.showAuthRequiredDialog(
  109. room.getName(), authenticate.bind(null, room, lockPassword)
  110. );
  111. }
  112. /**
  113. * Close auth-related dialogs if there are any.
  114. */
  115. function closeAuth() {
  116. if (externalAuthWindow) {
  117. externalAuthWindow.close();
  118. externalAuthWindow = null;
  119. }
  120. if (authRequiredDialog) {
  121. authRequiredDialog.close();
  122. authRequiredDialog = null;
  123. }
  124. }
  125. export default {
  126. authenticate,
  127. requireAuth,
  128. closeAuth
  129. };