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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = APP.translation.generateTranslatonHTML("dialog.Stop");
  21. var msg = APP.translation.generateTranslatonHTML("dialog.AuthMsg",
  22. {room: room});
  23. var buttonTxt
  24. = APP.translation.generateTranslatonHTML("dialog.Authenticate");
  25. var buttons = [];
  26. buttons.push({title: buttonTxt, value: "authNow"});
  27. authDialog = APP.UI.messageHandler.openDialog(
  28. title,
  29. msg,
  30. true,
  31. buttons,
  32. function (onSubmitEvent, submitValue) {
  33. // Do not close the dialog yet
  34. onSubmitEvent.preventDefault();
  35. // Open login popup
  36. if (submitValue === 'authNow') {
  37. callback();
  38. }
  39. }
  40. );
  41. },
  42. closeAuthenticationWindow: function () {
  43. if (authenticationWindow) {
  44. authenticationWindow.close();
  45. authenticationWindow = null;
  46. }
  47. },
  48. xmppAuthenticate: function () {
  49. var loginDialog = LoginDialog.show(
  50. function (connection, state) {
  51. if (!state) {
  52. // User cancelled
  53. loginDialog.close();
  54. return;
  55. } else if (state == APP.xmpp.Status.CONNECTED) {
  56. loginDialog.close();
  57. Authentication.stopInterval();
  58. Authentication.closeAuthenticationDialog();
  59. // Close the connection as anonymous one will be used
  60. // to create the conference. Session-id will authorize
  61. // the request.
  62. connection.disconnect();
  63. var roomName = APP.UI.generateRoomName();
  64. Moderator.allocateConferenceFocus(roomName, function () {
  65. // If it's not "on the fly" authentication now join
  66. // the conference room
  67. if (!APP.xmpp.getMUCJoined()) {
  68. APP.UI.checkForNicknameAndJoin();
  69. }
  70. });
  71. }
  72. }, true);
  73. },
  74. focusAuthenticationWindow: function () {
  75. // If auth window exists just bring it to the front
  76. if (authenticationWindow) {
  77. authenticationWindow.focus();
  78. return;
  79. }
  80. },
  81. closeAuthenticationDialog: function () {
  82. // Close authentication dialog if opened
  83. if (authDialog) {
  84. authDialog.close();
  85. authDialog = null;
  86. }
  87. },
  88. createAuthenticationWindow: function (callback, url) {
  89. authenticationWindow = APP.UI.messageHandler.openCenteredPopup(
  90. url, 910, 660,
  91. // On closed
  92. function () {
  93. // Close authentication dialog if opened
  94. Authentication.closeAuthenticationDialog();
  95. callback();
  96. authenticationWindow = null;
  97. });
  98. return authenticationWindow;
  99. },
  100. stopInterval: function () {
  101. // Clear retry interval, so that we don't call 'doJoinAfterFocus' twice
  102. if (authRetryId) {
  103. window.clearTimeout(authRetryId);
  104. authRetryId = null;
  105. }
  106. }
  107. };
  108. module.exports = Authentication;