Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AuthHandler.js 4.1KB

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