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

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