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

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