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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* global $, APP*/
  17. var LoginDialog = require('./LoginDialog');
  18. var Moderator = require('../../xmpp/moderator');
  19. /* Initial "authentication required" dialog */
  20. var authDialog = null;
  21. /* Loop retry ID that wits for other user to create the room */
  22. var authRetryId = null;
  23. var authenticationWindow = null;
  24. var Authentication = {
  25. openAuthenticationDialog: function (roomName, intervalCallback, callback) {
  26. // This is the loop that will wait for the room to be created by
  27. // someone else. 'auth_required.moderator' will bring us back here.
  28. authRetryId = window.setTimeout(intervalCallback, 5000);
  29. // Show prompt only if it's not open
  30. if (authDialog !== null) {
  31. return;
  32. }
  33. // extract room name from 'room@muc.server.net'
  34. var room = roomName.substr(0, roomName.indexOf('@'));
  35. var title
  36. = APP.translation.generateTranslatonHTML("dialog.WaitingForHost");
  37. var msg
  38. = APP.translation.generateTranslatonHTML(
  39. "dialog.WaitForHostMsg", {room: room});
  40. var buttonTxt
  41. = APP.translation.generateTranslatonHTML("dialog.IamHost");
  42. var buttons = [];
  43. buttons.push({title: buttonTxt, value: "authNow"});
  44. authDialog = APP.UI.messageHandler.openDialog(
  45. title,
  46. msg,
  47. true,
  48. buttons,
  49. function (onSubmitEvent, submitValue) {
  50. // Do not close the dialog yet
  51. onSubmitEvent.preventDefault();
  52. // Open login popup
  53. if (submitValue === 'authNow') {
  54. callback();
  55. }
  56. }
  57. );
  58. },
  59. closeAuthenticationWindow: function () {
  60. if (authenticationWindow) {
  61. authenticationWindow.close();
  62. authenticationWindow = null;
  63. }
  64. },
  65. xmppAuthenticate: function () {
  66. var loginDialog = LoginDialog.show(
  67. function (connection, state) {
  68. if (!state) {
  69. // User cancelled
  70. loginDialog.close();
  71. return;
  72. } else if (state == APP.xmpp.Status.CONNECTED) {
  73. loginDialog.close();
  74. Authentication.stopInterval();
  75. Authentication.closeAuthenticationDialog();
  76. // Close the connection as anonymous one will be used
  77. // to create the conference. Session-id will authorize
  78. // the request.
  79. connection.disconnect();
  80. var roomName = APP.UI.generateRoomName();
  81. Moderator.allocateConferenceFocus(roomName, function () {
  82. // If it's not "on the fly" authentication now join
  83. // the conference room
  84. if (!APP.xmpp.getMUCJoined()) {
  85. APP.UI.checkForNicknameAndJoin();
  86. }
  87. });
  88. }
  89. }, true);
  90. },
  91. focusAuthenticationWindow: function () {
  92. // If auth window exists just bring it to the front
  93. if (authenticationWindow) {
  94. authenticationWindow.focus();
  95. return;
  96. }
  97. },
  98. closeAuthenticationDialog: function () {
  99. // Close authentication dialog if opened
  100. if (authDialog) {
  101. authDialog.close();
  102. authDialog = null;
  103. }
  104. },
  105. createAuthenticationWindow: function (callback, url) {
  106. authenticationWindow = APP.UI.messageHandler.openCenteredPopup(
  107. url, 910, 660,
  108. // On closed
  109. function () {
  110. // Close authentication dialog if opened
  111. Authentication.closeAuthenticationDialog();
  112. callback();
  113. authenticationWindow = null;
  114. });
  115. return authenticationWindow;
  116. },
  117. stopInterval: function () {
  118. // Clear retry interval, so that we don't call 'doJoinAfterFocus' twice
  119. if (authRetryId) {
  120. window.clearTimeout(authRetryId);
  121. authRetryId = null;
  122. }
  123. }
  124. };
  125. module.exports = Authentication;