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.

Authentication.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* global $, APP*/
  2. /* jshint -W101 */
  3. import messageHandler from '../util/MessageHandler';
  4. var LoginDialog = require('./LoginDialog');
  5. var Moderator = require('../../xmpp/moderator');
  6. /* Initial "authentication required" dialog */
  7. var authDialog = null;
  8. /* Loop retry ID that wits for other user to create the room */
  9. var authRetryId = null;
  10. var authenticationWindow = null;
  11. var Authentication = {
  12. authenticate () {
  13. Authentication.focusAuthenticationWindow();
  14. if (!APP.xmpp.isExternalAuthEnabled()) {
  15. Authentication.xmppAuthenticate();
  16. return;
  17. }
  18. // Get authentication URL
  19. if (!APP.xmpp.isMUCJoined()) {
  20. APP.xmpp.getLoginUrl(APP.conference.roomName, function (url) {
  21. // If conference has not been started yet - redirect to login page
  22. window.location.href = url;
  23. });
  24. } else {
  25. APP.xmpp.getPopupLoginUrl(APP.conference.roomName, function (url) {
  26. // Otherwise - open popup with authentication URL
  27. var authenticationWindow = Authentication.createAuthenticationWindow(
  28. function () {
  29. // On popup closed - retry room allocation
  30. APP.xmpp.allocateConferenceFocus(
  31. APP.conference.roomName,
  32. function () { console.info("AUTH DONE"); }
  33. );
  34. }, url);
  35. if (!authenticationWindow) {
  36. messageHandler.openMessageDialog(null, "dialog.popupError");
  37. }
  38. });
  39. }
  40. },
  41. openAuthenticationDialog (roomName, intervalCallback) {
  42. // This is the loop that will wait for the room to be created by
  43. // someone else. 'auth_required.moderator' will bring us back here.
  44. authRetryId = window.setTimeout(intervalCallback, 5000);
  45. // Show prompt only if it's not open
  46. if (authDialog !== null) {
  47. return;
  48. }
  49. // extract room name from 'room@muc.server.net'
  50. var room = roomName.substr(0, roomName.indexOf('@'));
  51. var title
  52. = APP.translation.generateTranslationHTML("dialog.WaitingForHost");
  53. var msg
  54. = APP.translation.generateTranslationHTML(
  55. "dialog.WaitForHostMsg", {room: room});
  56. var buttonTxt
  57. = APP.translation.generateTranslationHTML("dialog.IamHost");
  58. var buttons = [];
  59. buttons.push({title: buttonTxt, value: "authNow"});
  60. authDialog = messageHandler.openDialog(
  61. title,
  62. msg,
  63. true,
  64. buttons,
  65. function (onSubmitEvent, submitValue) {
  66. // Do not close the dialog yet
  67. onSubmitEvent.preventDefault();
  68. // Open login popup
  69. if (submitValue === 'authNow') {
  70. Authentication.authenticate();
  71. }
  72. }
  73. );
  74. },
  75. closeAuthenticationWindow () {
  76. if (authenticationWindow) {
  77. authenticationWindow.close();
  78. authenticationWindow = null;
  79. }
  80. },
  81. xmppAuthenticate () {
  82. var loginDialog = LoginDialog.show(
  83. function (connection, state) {
  84. if (!state) {
  85. // User cancelled
  86. loginDialog.close();
  87. return;
  88. } else if (state == APP.xmpp.Status.CONNECTED) {
  89. loginDialog.close();
  90. Authentication.stopInterval();
  91. Authentication.closeAuthenticationDialog();
  92. // Close the connection as anonymous one will be used
  93. // to create the conference. Session-id will authorize
  94. // the request.
  95. connection.disconnect();
  96. var roomName = APP.conference.roomName;
  97. Moderator.allocateConferenceFocus(roomName, function () {
  98. // If it's not "on the fly" authentication now join
  99. // the conference room
  100. if (!APP.xmpp.isMUCJoined()) {
  101. APP.UI.checkForNicknameAndJoin();
  102. }
  103. });
  104. }
  105. }, true);
  106. },
  107. focusAuthenticationWindow () {
  108. // If auth window exists just bring it to the front
  109. if (authenticationWindow) {
  110. authenticationWindow.focus();
  111. return;
  112. }
  113. },
  114. closeAuthenticationDialog () {
  115. // Close authentication dialog if opened
  116. if (authDialog) {
  117. authDialog.close();
  118. authDialog = null;
  119. }
  120. },
  121. createAuthenticationWindow (callback, url) {
  122. authenticationWindow = messageHandler.openCenteredPopup(
  123. url, 910, 660,
  124. // On closed
  125. function () {
  126. // Close authentication dialog if opened
  127. Authentication.closeAuthenticationDialog();
  128. callback();
  129. authenticationWindow = null;
  130. });
  131. return authenticationWindow;
  132. },
  133. stopInterval () {
  134. // Clear retry interval, so that we don't call 'doJoinAfterFocus' twice
  135. if (authRetryId) {
  136. window.clearTimeout(authRetryId);
  137. authRetryId = null;
  138. }
  139. }
  140. };
  141. module.exports = Authentication;